Files
otche-backend/internal/netpolicy/policy.go
T

115 lines
3.8 KiB
Go

package netpolicy
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/netip"
"strings"
)
// NonPublic is the canonical IPv4 exclusion list for public-only mode.
// Callers must treat this exported slice as immutable.
var NonPublic = []string{
"0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8",
"169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.0.2.0/24",
"192.88.99.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24",
"203.0.113.0/24", "224.0.0.0/4", "240.0.0.0/4",
}
// NeverAllowed is the canonical IPv4 list rejected even when explicitly named.
// Operator-protected infrastructure is additionally denied by the host gateway.
// Callers must treat this exported slice as immutable.
var NeverAllowed = []string{
"0.0.0.0/8", "127.0.0.0/8", "169.254.0.0/16", "192.0.0.0/24",
"192.0.2.0/24", "192.88.99.0/24", "198.51.100.0/24", "203.0.113.0/24",
"224.0.0.0/4", "240.0.0.0/4",
}
var nonPublicPrefixes = parsePrefixes(NonPublic)
var neverAllowedPrefixes = parsePrefixes(NeverAllowed)
func parsePrefixes(entries []string) []netip.Prefix {
prefixes := make([]netip.Prefix, len(entries))
for i, entry := range entries {
prefixes[i] = netip.MustParsePrefix(entry)
}
return prefixes
}
// Normalize validates a destination policy and returns canonical IPv4 prefixes
// in first-occurrence order. The sole /0 entry is a public-only mode flag, never
// an accept-all route; explicitly allowed private/lab prefixes remain separate.
// Limits apply to the submitted strings before whitespace trimming or deduplication.
func Normalize(mode string, entries []string) ([]string, error) {
if mode != "online" && mode != "offline" {
return nil, errors.New("internet must be online/offline")
}
if mode == "offline" {
if len(entries) != 0 {
return nil, errors.New("allowed_cidrs must be empty when internet is offline")
}
return nil, nil
}
if len(entries) == 0 || len(entries) > 128 {
return nil, errors.New("online allowed_cidrs requires 1 to 128 IPv4 addresses or CIDRs")
}
bytes := 0
for _, entry := range entries {
if len(entry) > 8192-bytes {
return nil, errors.New("allowed_cidrs exceeds 8192 UTF-8 bytes")
}
bytes += len(entry)
}
normalized := make([]string, 0, len(entries))
seen := make(map[string]bool, len(entries))
for i, entry := range entries {
entry = strings.TrimSpace(entry)
var prefix netip.Prefix
if strings.Contains(entry, "/") {
var err error
prefix, err = netip.ParsePrefix(entry)
if err != nil || !prefix.Addr().Is4() {
return nil, fmt.Errorf("allowed_cidrs entry %d must be an IPv4 address or CIDR", i+1)
}
prefix = prefix.Masked()
} else {
address, err := netip.ParseAddr(entry)
if err != nil || !address.Is4() {
return nil, fmt.Errorf("allowed_cidrs entry %d must be an IPv4 address or CIDR", i+1)
}
bits := 32
if address.IsUnspecified() {
bits = 0
}
prefix = netip.PrefixFrom(address, bits)
}
if prefix.Bits() != 0 {
for _, denied := range neverAllowedPrefixes {
if prefix.Overlaps(denied) {
return nil, fmt.Errorf("allowed_cidrs entry %d overlaps never-allowed range %s", i+1, denied)
}
}
for _, private := range nonPublicPrefixes {
if prefix.Overlaps(private) && prefix.Bits() < private.Bits() {
return nil, fmt.Errorf("allowed_cidrs entry %d mixes public and private ranges; use separate explicit CIDRs", i+1)
}
}
}
canonical := prefix.String()
if !seen[canonical] {
normalized = append(normalized, canonical)
seen[canonical] = true
}
}
return normalized, nil
}
// Digest hashes normalized entries in order, separated and terminated by LF.
// Call Normalize first; Digest does not validate or reinterpret its input.
func Digest(entries []string) string {
sum := sha256.Sum256([]byte(strings.Join(entries, "\n") + "\n"))
return hex.EncodeToString(sum[:])
}