Check Client IPs Against a CIDR Allowlist

Build an IP allowlist from CIDR prefixes and check IPv4 or IPv6 addresses without allocating or trusting raw strings.

GoSecurity Boundaries

Code

gosnippet
60 lines
package main

import (
	"fmt"
	"net/netip"
)

type Allowlist struct {
	prefixes []netip.Prefix
}

func NewAllowlist(cidrs []string) (*Allowlist, error) {
	list := &Allowlist{}
	for _, cidr := range cidrs {
		prefix, err := netip.ParsePrefix(cidr)
		if err != nil {
			return nil, fmt.Errorf("parse %q: %w", cidr, err)
		}
		list.prefixes = append(list.prefixes, prefix.Masked())
	}
	return list, nil
}

func (a *Allowlist) Allows(raw string) bool {
	address, err := netip.ParseAddr(raw)
	if err != nil {
		return false
	}
	for _, prefix := range a.prefixes {
		if prefix.Contains(address) {
			return true
		}
	}
	return false
}

func main() {
	list, err := NewAllowlist([]string{"10.0.0.0/8", "192.168.1.0/24", "2001:db8::/32"})
	if err != nil {
		panic(err)
	}
	cases := map[string]bool{
		"10.2.3.4":      true,
		"192.168.1.200": true,
		"192.168.2.1":   false,
		"2001:db8::1":   true,
		"203.0.113.9":   false,
	}
	for address, want := range cases {
		got := list.Allows(address)
		fmt.Printf("%-15s allowed=%v\n", address, got)
		if got != want {
			panic("unexpected result for " + address)
		}
	}
	if _, err := NewAllowlist([]string{"10.0.0.0/33"}); err == nil {
		panic("invalid prefix should fail")
	}
}

Inside the snippet

Check Client IPs Against a CIDR Allowlist keeps everything in the functions NewAllowlist, Allows and main.

The one repetition in this snippet is the loop for _, cidr := range cidrs. Failures are values here: 3 sites check err and decide instead of letting one propagate.

What to watch for

  • Nothing third-party is pulled in; the snippet is dependency-free.
  • Error values are returned, not thrown — every call site must inspect err before using the other return values.

Putting it to use

The entry shape of the file is func NewAllowlist(cidrs []string) (*Allowlist, error)

The demo flow inside main is NewAllowlist and Allows — run the file to see it end to end.

The core lines of the flow

return nil, fmt.Errorf("parse %q: %w", cidr, err)
func (a *Allowlist) Allows(raw string) bool {
address, err := netip.ParseAddr(raw)
if err != nil {
return false
}
for _, prefix := range a.prefixes {
if prefix.Contains(address) {
return true
}
}
return false
}
for address, want := range cases {
got := list.Allows(address)
fmt.Printf("%-15s allowed=%v\n", address, got)
if got != want {
panic("unexpected result for " + address)
}
}

Interactive Tools for This Topic

FAQ

How does the code signal failure?
Through `error` values — the snippet checks `err != nil` at 3 sites, and the entry flow turns failures into `panic(err)`.
How do you run Check Client IPs Against a CIDR Allowlist?
The runtime alone is enough: execute the file as a complete program.