| 1 | package netguard |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "net/netip" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func ap(s string) netip.AddrPort { return netip.MustParseAddrPort(s) } |
| 13 | |
| 14 | |
| 15 | func TestDefault(t *testing.T) { |
| 16 | p := &Policy{} |
| 17 | for dst, want := range map[string]bool{ |
| 18 | "1.1.1.1:443": true, |
| 19 | "[2606:4700::1111]:443": true, |
| 20 | "127.0.0.1:5432": false, |
| 21 | "[::1]:5432": false, |
| 22 | "[::ffff:127.0.0.1]:5432": false, |
| 23 | "192.168.1.1:80": false, |
| 24 | "10.1.2.3:22": false, |
| 25 | "172.16.0.1:80": false, |
| 26 | "100.100.100.100:53": false, |
| 27 | "169.254.169.254:80": false, |
| 28 | "[fd00::1]:80": false, |
| 29 | "[fe80::1]:80": false, |
| 30 | "224.0.0.251:5353": false, |
| 31 | "0.0.0.0:80": false, |
| 32 | "255.255.255.255:67": false, |
| 33 | "[::]:80": false, |
| 34 | } { |
| 35 | if got := p.Allows("tcp", ap(dst)); got != want { |
| 36 | t.Errorf("%s: %v", dst, got) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | func TestReserved(t *testing.T) { |
| 44 | p := &Policy{} |
| 45 | for dst, want := range map[string]bool{ |
| 46 | "192.0.0.8:80": false, |
| 47 | "192.0.2.1:80": false, |
| 48 | "198.18.0.1:80": false, |
| 49 | "198.19.255.255:80": false, |
| 50 | "198.20.0.1:80": true, |
| 51 | "198.51.100.1:80": false, |
| 52 | "203.0.113.1:80": false, |
| 53 | "240.0.0.1:80": false, |
| 54 | "255.255.255.254:80": false, |
| 55 | "239.255.255.250:1900": false, |
| 56 | "100.64.0.1:80": false, |
| 57 | "100.127.255.255:80": false, |
| 58 | "100.128.0.1:80": true, |
| 59 | "169.254.1.1:80": false, |
| 60 | "[fec0::1]:80": false, |
| 61 | "[ff02::1]:80": false, |
| 62 | "[::127.0.0.1]:80": false, |
| 63 | "[::ffff:10.0.0.1]:80": false, |
| 64 | "[::ffff:8.8.8.8]:80": true, |
| 65 | "[64:ff9b::7f00:1]:80": false, |
| 66 | "[64:ff9b::c0a8:101]:80": false, |
| 67 | "[64:ff9b::808:808]:80": true, |
| 68 | "[64:ff9b:1::a00:1]:80": false, |
| 69 | "[2002:7f00:1::1]:80": false, |
| 70 | "[2002:a00:1::1]:80": false, |
| 71 | "[2002:808:808::1]:80": true, |
| 72 | "[2001:0:4136:e378:8000:63bf:f7f7:f7f7]:80": true, |
| 73 | "[2001:0:4136:e378:8000:63bf:80ff:fffe]:80": false, |
| 74 | "[2001:0:7f00:1:8000:63bf:f7f7:f7f7]:80": false, |
| 75 | "[2001:0:4136:e378:8000:63bf:f5ff:fffe]:80": false, |
| 76 | "[2001:db8::1]:80": false, |
| 77 | "[100::1]:80": false, |
| 78 | "[2606:4700::1111]:443": true, |
| 79 | } { |
| 80 | if got := p.Allows("tcp", ap(dst)); got != want { |
| 81 | t.Errorf("%s: %v", dst, got) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if p.Allows("tcp", netip.AddrPortFrom(netip.MustParseAddr("fe80::1%en0"), 80)) { |
| 86 | t.Error("zoned link-local") |
| 87 | } |
| 88 | |
| 89 | q := &Policy{Allow: []Rule{{Net: netip.MustParsePrefix("198.18.0.0/15")}}} |
| 90 | if !q.Allows("tcp", ap("198.18.0.5:80")) { |
| 91 | t.Error("allowed benchmarking range") |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | func TestNetwork(t *testing.T) { |
| 97 | p := &Policy{} |
| 98 | for network, want := range map[string]bool{"tcp": true, "udp": true, "icmp": false, "": false, "tcp4": false} { |
| 99 | if got := p.Allows(network, ap("1.1.1.1:443")); got != want { |
| 100 | t.Errorf("%q: %v", network, got) |
| 101 | } |
| 102 | } |
| 103 | if !(&Policy{Open: true}).Allows("icmp", ap("127.0.0.1:1")) { |
| 104 | t.Error("open") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | func TestAllow(t *testing.T) { |
| 110 | dir := t.TempDir() |
| 111 | os.WriteFile(filepath.Join(dir, FileName), []byte("# ollama on this machine\nallow localhost:11434\nallow 192.168.1.20\nallow 10.0.0.0/8 # lab\n"), 0o644) |
| 112 | p, err := Load(dir) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | for dst, want := range map[string]bool{ |
| 117 | "127.0.0.1:11434": true, |
| 118 | "[::1]:11434": true, |
| 119 | "127.0.0.1:5432": false, |
| 120 | "192.168.1.20:22": true, |
| 121 | "192.168.1.21:22": false, |
| 122 | "10.200.0.1:443": true, |
| 123 | "8.8.8.8:53": true, |
| 124 | } { |
| 125 | if got := p.Allows("tcp", ap(dst)); got != want { |
| 126 | t.Errorf("%s: %v", dst, got) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | func TestForms(t *testing.T) { |
| 134 | dir := t.TempDir() |
| 135 | os.WriteFile(filepath.Join(dir, FileName), []byte("allow localhost\nallow [fd00::5]:80\ndomain *.github.com\n"), 0o644) |
| 136 | p, err := Load(dir) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | for dst, want := range map[string]bool{ |
| 141 | "127.0.0.1:1": true, |
| 142 | "[::1]:65535": true, |
| 143 | "[fd00::5]:80": true, |
| 144 | "[fd00::5]:81": false, |
| 145 | "192.168.1.20:22": false, |
| 146 | } { |
| 147 | if got := p.Allows("udp", ap(dst)); got != want { |
| 148 | t.Errorf("%s: %v", dst, got) |
| 149 | } |
| 150 | } |
| 151 | if !p.Name("github.com") || !p.Name("api.github.com") || p.Name("example.com") { |
| 152 | t.Error("*.github.com") |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestLoadErrors(t *testing.T) { |
| 157 | for _, line := range []string{"allow", "allow nowhere", "allow 1.2.3.4:0", "deny 1.2.3.4", "domain a/b", "allow 1.2.3.4 5"} { |
| 158 | dir := t.TempDir() |
| 159 | os.WriteFile(filepath.Join(dir, FileName), []byte(line+"\n"), 0o644) |
| 160 | if _, err := Load(dir); err == nil || !strings.Contains(err.Error(), "line 1") { |
| 161 | t.Errorf("%q: %v", line, err) |
| 162 | } |
| 163 | } |
| 164 | p, err := Load(t.TempDir()) |
| 165 | if err != nil || p.Open || len(p.Allow) != 0 { |
| 166 | t.Errorf("no file: %+v %v", p, err) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | func TestDomains(t *testing.T) { |
| 172 | p := &Policy{Domains: []string{"github.com", "npmjs.org"}} |
| 173 | for name, want := range map[string]bool{"github.com": true, "api.github.com.": true, "GitHub.com": true, "evilgithub.com": false, "example.com": false, "registry.npmjs.org": true} { |
| 174 | if p.Name(name) != want { |
| 175 | t.Errorf("Name(%q) != %v", name, want) |
| 176 | } |
| 177 | } |
| 178 | if p.Allows("tcp", ap("140.82.112.3:443")) { |
| 179 | t.Error("an address no allowed name gave") |
| 180 | } |
| 181 | p.Answered("github.com", []net.IP{net.ParseIP("140.82.112.3")}) |
| 182 | p.Answered("example.com", []net.IP{net.ParseIP("93.184.216.34")}) |
| 183 | if !p.Allows("tcp", ap("140.82.112.3:443")) || p.Allows("tcp", ap("93.184.216.34:443")) { |
| 184 | t.Error("answered addresses") |
| 185 | } |
| 186 | |
| 187 | p.Answered("github.com", []net.IP{net.ParseIP("127.0.0.1")}) |
| 188 | if p.Allows("tcp", ap("127.0.0.1:443")) { |
| 189 | t.Error("loopback through an allowed name") |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func TestOpen(t *testing.T) { |
| 194 | t.Setenv("AGENT_VM_UNSAFE_OPEN_NETWORK", "1") |
| 195 | p, _ := Load(t.TempDir()) |
| 196 | if !p.Allows("tcp", ap("127.0.0.1:5432")) { |
| 197 | t.Error("open") |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | func TestOwnAddress(t *testing.T) { |
| 203 | addrs, _ := net.InterfaceAddrs() |
| 204 | p := &Policy{} |
| 205 | for _, a := range addrs { |
| 206 | if n, ok := a.(*net.IPNet); ok { |
| 207 | ip, _ := netip.AddrFromSlice(n.IP) |
| 208 | if p.Allows("tcp", netip.AddrPortFrom(ip.Unmap(), 80)) { |
| 209 | t.Errorf("own address %s reachable", ip) |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |