security

package
v0.8.41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const MaxRRLEntries = 1_000_000

MaxRRLEntries caps the per-(prefix, qname, response_type) entries map. RRL is a remote-attacker-reachable surface: each entry is keyed on the requester's source-IP /prefix, the query name (up to 255 bytes), and a response-type tag. A UDP-source-spoofing attacker sending queries for distinct names from distinct spoofed sources can grow this map without bound — between the 5-minute cleanup ticks, the entries map can reach gigabytes of resident memory before any eviction happens. 1 M is the same ceiling used on RateLimiter.clients (v0.7.65) and AdminServer.clientQueryNum (v0.7.66) — well above any legitimate workload and bounded under the OOM threshold. When the cap fires the oldest entry by `lastTime` is evicted to make room. RRL semantics survive eviction: the spoofed attacker already gets a fresh per-key budget on every distinct (prefix, qname, responseType) tuple by design, so eviction-then-recreate preserves the rate-limit contract.

View Source
const MaxRateLimiterClients = 1_000_000

MaxRateLimiterClients caps the number of distinct client IPs the per-IP rate limiter will track at once. Before v0.7.65 the map grew without bound: an attacker spoofing UDP source IPs can send DNS queries from millions of distinct apparent sources from a single host, and each unique IP lazily creates a tokenBucket entry. The cleanup tick runs every 5 minutes (StartCleanup) and only evicts entries idle for longer than the cleanup window — between ticks the map can grow into resolver RAM. 1M tracked clients is far above any realistic legitimate population (even a busy ISP-side resolver sees tens of thousands of concurrent distinct clients) and small enough that the worst-case footprint stays bounded to a few hundred MB. When the cap is reached, Allow evicts the OLDEST (least recently used) bucket to make room for the new client. Eviction does not degrade security: an attacker who can already spoof unlimited distinct source IPs already gets fresh per-IP budget on every request; the cap only closes the memory growth, not the per-IP isolation.

Variables

This section is empty.

Functions

func FilterPrivateAddresses

func FilterPrivateAddresses(answers []dns.ResourceRecord) []dns.ResourceRecord

FilterPrivateAddresses removes A/AAAA records that contain private, loopback, link-local, multicast, broadcast, CGNAT, TEST-NET, IPv4-mapped IPv6, NAT64, 6to4, documentation, or otherwise non-publicly-routable addresses. Prevents DNS rebinding attacks where an external domain resolves to an internal IP.

func InZone

func InZone(name string, zone string) bool

InZone checks if name is at or below zone.

func IsReservedIP added in v0.6.2

func IsReservedIP(ip net.IP) bool

IsReservedIP reports whether ip falls in any non-publicly-routable range recognized by FilterPrivateAddresses. Exposed for use elsewhere in the codebase (e.g. ACL checks, metrics dimensions).

func SanitizeBailiwick

func SanitizeBailiwick(msg *dns.Message, zone string)

SanitizeBailiwick removes records from a DNS response that are outside the authority of the responding nameserver's zone.

Out-of-bailiwick glue (e.g. a `.com` server returning glue for `ns.evil.org` to back an example.com delegation) is dropped: an out-of-zone responder has no authority to bind a hostname to an address. Such records must be re-resolved through the proper chain of authority. RFC 8499 §7 / RFC 1034 §4.2.1.

When zone is empty (the priming/root case), the filter is permissive: the root server is allowed to publish glue for any TLD nameserver, since that is exactly its job.

func SipHash24 added in v0.6.0

func SipHash24(key [16]byte, msg []byte) uint64

SipHash24 computes a SipHash-2-4 MAC for the given message using a 128-bit key. Used by RFC 9018 for DNS server cookie generation.

Types

type ACL

type ACL struct {
	// contains filtered or unexported fields
}

ACL implements access control list based on CIDR ranges, with optional per-zone overrides.

func NewACL

func NewACL(allow, deny []string) (*ACL, error)

NewACL creates an ACL from allow and deny CIDR lists.

func (*ACL) AddZoneACL

func (acl *ACL) AddZoneACL(cfg ZoneACLConfig) error

AddZoneACL adds a per-zone access control rule. Zone-specific rules are checked before global rules when using CheckWithZone.

func (*ACL) Check

func (acl *ACL) Check(ipStr string) bool

Check returns true if the IP is allowed by the global ACL rules.

func (*ACL) CheckWithZone

func (acl *ACL) CheckWithZone(ipStr string, qname string) bool

CheckWithZone returns true if the IP is allowed for queries to the given qname. It first looks for a matching zone-specific rule. If one is found, only that zone's allow/deny lists are evaluated. If no zone matches, the global rules are used.

type RRL

type RRL struct {
	// contains filtered or unexported fields
}

RRL implements Response Rate Limiting to prevent DNS amplification attacks.

func NewRRL

func NewRRL(responsesPerSecond float64, slipRatio, ipv4Prefix, ipv6Prefix int) *RRL

NewRRL creates a new Response Rate Limiter.

func (*RRL) AllowResponse

func (r *RRL) AllowResponse(sourceIP string, qname string, responseType string) RRLAction

AllowResponse checks if a response should be allowed, dropped, or slipped.

func (*RRL) StartCleanup

func (r *RRL) StartCleanup(ctx interface{ Done() <-chan struct{} })

StartCleanup removes stale RRL entries periodically.

type RRLAction

type RRLAction int

RRLAction represents the action to take for a rate-limited response.

const (
	RRLAllow RRLAction = iota
	RRLDrop
	RRLSlip // send TC=1 truncated response
)

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter implements per-IP token bucket rate limiting.

func NewRateLimiter

func NewRateLimiter(rate float64, burst int) *RateLimiter

NewRateLimiter creates a new per-IP rate limiter.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(clientIP string) bool

Allow checks if a request from clientIP should be allowed.

func (*RateLimiter) StartCleanup

func (rl *RateLimiter) StartCleanup(ctx context.Context)

StartCleanup removes idle clients periodically.

type ZoneACL

type ZoneACL struct {
	Zone      string
	AllowNets []*net.IPNet
	DenyNets  []*net.IPNet
}

ZoneACL defines access control rules scoped to a specific DNS zone. When a query matches a zone, these rules are evaluated instead of the global allow/deny lists.

type ZoneACLConfig

type ZoneACLConfig struct {
	Zone  string
	Allow []string
	Deny  []string
}

ZoneACLConfig is the configuration input for a per-zone ACL rule.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL