Repository navigation
Expand file tree
/
Copy pathallowlist_test.go
More file actions
163 lines (147 loc) · 4.85 KB
/
Copy pathallowlist_test.go
File metadata and controls
163 lines (147 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"net"
"os"
"path/filepath"
"testing"
)
func TestCanonicalHost(t *testing.T) {
cases := map[string]string{
"GHE.Corp": "ghe.corp",
"ghe.corp.": "ghe.corp",
" ghe.CORP ": "ghe.corp",
"Example.COM.": "example.com",
"zeropath.com": "zeropath.com",
}
for in, want := range cases {
if got := canonicalHost(in); got != want {
t.Errorf("canonicalHost(%q) = %q, want %q", in, got, want)
}
}
}
// newTestAllowlist builds an Allowlist with only the local half populated,
// bypassing the YAML file for the pure intersection-logic tests.
func newTestAllowlist(local []Target) *Allowlist {
return &Allowlist{localTargets: toKeySet(local)}
}
func TestEffectiveAllowlistRequiresBothSets(t *testing.T) {
a := newTestAllowlist([]Target{
{Host: "ghe.corp", Port: 443},
{Host: "gitlab.internal", Port: 443},
})
// Default-deny: nothing is allowed until the gateway pushes an allowlist.
if a.IsHostPortAllowed("ghe.corp", 443) {
t.Fatal("expected default-deny before the gateway pushes an allowlist")
}
// Gateway pushes a set that only partially overlaps the local file.
a.SetGatewayTargets(7, []Target{
{Host: "ghe.corp", Port: 443},
{Host: "jira.internal", Port: 443},
})
if !a.IsHostPortAllowed("ghe.corp", 443) {
t.Error("ghe.corp:443 is in both sets and must be allowed")
}
if a.IsHostPortAllowed("gitlab.internal", 443) {
t.Error("gitlab.internal:443 is local-only and must be denied")
}
if a.IsHostPortAllowed("jira.internal", 443) {
t.Error("jira.internal:443 is gateway-only and must be denied")
}
if a.IsHostPortAllowed("ghe.corp", 8443) {
t.Error("ghe.corp:8443 has a non-allowed port and must be denied")
}
if a.IsHostPortAllowed("unknown.corp", 443) {
t.Error("unknown.corp:443 is in neither set and must be denied")
}
}
func TestEffectiveAllowlistCaseAndDotInsensitive(t *testing.T) {
a := newTestAllowlist([]Target{{Host: "GHE.Corp", Port: 443}})
a.SetGatewayTargets(1, []Target{{Host: "ghe.corp.", Port: 443}})
if !a.IsHostPortAllowed("Ghe.CORP", 443) {
t.Error("host match must be case-insensitive and trailing-dot-insensitive")
}
}
func TestIPDenyReasonDenied(t *testing.T) {
denied := []string{
"127.0.0.1", // loopback
"::1", // loopback (v6)
"0.0.0.0", // unspecified
"224.0.0.1", // multicast
"169.254.1.1", // link-local
"169.254.169.254", // IMDS (also link-local)
"fd00:ec2::254", // AWS IMDS over IPv6 (explicit metadata entry)
"100.64.0.1", // CGNAT
"100.127.255.254", // CGNAT (upper edge)
}
for _, s := range denied {
ip := net.ParseIP(s)
if ip == nil {
t.Fatalf("test bug: could not parse %q", s)
}
if reason := ipDenyReason(ip); reason == "" {
t.Errorf("ipDenyReason(%s) = allowed, want denied", s)
}
}
}
func TestIPDenyReasonAllowed(t *testing.T) {
// RFC1918 hosts are the whole point of the broker (on-prem targets), and a
// routable public IP is fine too. None of these should be denied.
allowed := []string{
"10.0.0.5", // RFC1918
"172.16.4.9", // RFC1918
"192.168.1.10", // RFC1918
"203.0.113.7", // public (TEST-NET-3)
}
for _, s := range allowed {
ip := net.ParseIP(s)
if ip == nil {
t.Fatalf("test bug: could not parse %q", s)
}
if reason := ipDenyReason(ip); reason != "" {
t.Errorf("ipDenyReason(%s) = denied (%s), want allowed", s, reason)
}
}
}
func TestLoadAllowlistFileYAML(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "allowlist.yaml")
content := "" +
"version: 3\n" +
"default: deny\n" +
"targets:\n" +
" - host: GHE.corp\n" +
" port: 443\n" +
" - host: gitlab.internal\n" +
" port: 443\n"
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write temp allowlist: %v", err)
}
a, err := LoadAllowlistFile(path)
if err != nil {
t.Fatalf("LoadAllowlistFile: %v", err)
}
// Gateway pushes only ghe.corp -> intersection allows only ghe.corp.
a.SetGatewayTargets(3, []Target{{Host: "ghe.corp", Port: 443}})
if !a.IsHostPortAllowed("ghe.corp", 443) {
t.Error("ghe.corp:443 should be allowed (local file + gateway, case-insensitive)")
}
if a.IsHostPortAllowed("gitlab.internal", 443) {
t.Error("gitlab.internal:443 was not gateway-pushed and should be denied")
}
}
func TestLoadAllowlistFileRejectsNonDenyDefault(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "allowlist.yaml")
content := "version: 1\ndefault: allow\ntargets: []\n"
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write temp allowlist: %v", err)
}
if _, err := LoadAllowlistFile(path); err == nil {
t.Error("expected LoadAllowlistFile to reject default: allow, got nil error")
}
}
func TestLoadAllowlistFileMissing(t *testing.T) {
if _, err := LoadAllowlistFile(filepath.Join(t.TempDir(), "does-not-exist.yaml")); err == nil {
t.Error("expected error for missing allowlist file, got nil")
}
}