gunk-dev/the-valley

scanner_test.go

source4cbb5ec9c5dbmain's published tip history
5.8 KiB
1package scan
2
3import (
4 "io"
5 "strings"
6 "testing"
7)
8
9func TestCleanContent(t *testing.T) {
10 input := `{"type":"assistant","message":{"content":[{"type":"text","text":"Hello world"}]}}
11{"type":"result","total_cost_usd":1.5}`
12 findings := CheckSensitivity(strings.NewReader(input))
13 if len(findings) != 0 {
14 t.Errorf("clean content produced %d findings, want 0", len(findings))
15 }
16}
17
18func TestPrivateIPs(t *testing.T) {
19 tests := []struct {
20 name string
21 input string
22 want bool
23 }{
24 {"10.x", "connecting to 10.0.1.5 on port 22", true},
25 {"172.16.x", "host 172.16.0.1 is up", true},
26 {"172.31.x", "addr: 172.31.255.255", true},
27 {"192.168.x", "gateway 192.168.9.1", true},
28 {"public IP", "server at 8.8.8.8", false},
29 {"172.15 not private", "host 172.15.0.1", false},
30 {"172.32 not private", "host 172.32.0.1", false},
31 {"version number", "v10.0.0.0 release", true}, // looks like IP — acceptable false positive
32 }
33
34 for _, tt := range tests {
35 t.Run(tt.name, func(t *testing.T) {
36 findings := CheckSensitivity(strings.NewReader(tt.input))
37 found := hasCategory(findings, "private IP addresses")
38 if found != tt.want {
39 t.Errorf("input %q: found=%v, want=%v", tt.input, found, tt.want)
40 }
41 })
42 }
43}
44
45func TestSSHKeys(t *testing.T) {
46 tests := []struct {
47 name string
48 input string
49 want bool
50 }{
51 {"openssh", "-----BEGIN OPENSSH PRIVATE KEY-----", true},
52 {"rsa", "-----BEGIN RSA PRIVATE KEY-----", true},
53 {"ec", "-----BEGIN EC PRIVATE KEY-----", true},
54 {"dsa", "-----BEGIN DSA PRIVATE KEY-----", true},
55 {"public key", "-----BEGIN PUBLIC KEY-----", false},
56 }
57
58 for _, tt := range tests {
59 t.Run(tt.name, func(t *testing.T) {
60 findings := CheckSensitivity(strings.NewReader(tt.input))
61 found := hasCategory(findings, "SSH private key material")
62 if found != tt.want {
63 t.Errorf("input %q: found=%v, want=%v", tt.input, found, tt.want)
64 }
65 })
66 }
67}
68
69func TestCredentialPatterns(t *testing.T) {
70 tests := []struct {
71 name string
72 input string
73 want bool
74 }{
75 {"password=", "password=hunter2", true},
76 {"PASSWORD:", "PASSWORD: secret", true},
77 {"token=", "token=abc123", true},
78 {"secret:", "secret: mysecret", true},
79 {"api_key=", "api_key=xyz", true},
80 {"apikey:", "apikey: foo", true},
81 {"api-key=", "api-key=bar", true},
82 {"no match", "the password was reset by admin", false},
83 {"word boundary", "secretive planning", false},
84 }
85
86 for _, tt := range tests {
87 t.Run(tt.name, func(t *testing.T) {
88 findings := CheckSensitivity(strings.NewReader(tt.input))
89 found := hasCategory(findings, "credential patterns (password/token/secret/api_key)")
90 if found != tt.want {
91 t.Errorf("input %q: found=%v, want=%v", tt.input, found, tt.want)
92 }
93 })
94 }
95}
96
97func TestAgeSecrets(t *testing.T) {
98 tests := []struct {
99 name string
100 input string
101 want bool
102 }{
103 {"age ref", "secrets/wifi.age: network-key-here", true},
104 {"no content", "secrets/wifi.age", false},
105 {"age word", "the age of reason", false},
106 }
107
108 for _, tt := range tests {
109 t.Run(tt.name, func(t *testing.T) {
110 findings := CheckSensitivity(strings.NewReader(tt.input))
111 found := hasCategory(findings, ".age secret file references")
112 if found != tt.want {
113 t.Errorf("input %q: found=%v, want=%v", tt.input, found, tt.want)
114 }
115 })
116 }
117}
118
119func TestMultipleFindings(t *testing.T) {
120 input := `connecting to 10.0.1.5
121-----BEGIN RSA PRIVATE KEY-----
122password=hunter2
123secrets/wifi.age: key`
124 findings := CheckSensitivity(strings.NewReader(input))
125 if len(findings) != 4 {
126 t.Errorf("got %d findings, want 4", len(findings))
127 for _, f := range findings {
128 t.Logf(" found: %s", f.Category)
129 }
130 }
131}
132
133func TestDeduplication(t *testing.T) {
134 input := `host 10.0.0.1
135host 10.0.0.2
136host 192.168.9.1`
137 findings := CheckSensitivity(strings.NewReader(input))
138 count := 0
139 for _, f := range findings {
140 if f.Category == "private IP addresses" {
141 count++
142 }
143 }
144 if count != 1 {
145 t.Errorf("got %d 'private IP' findings, want 1 (deduped)", count)
146 }
147}
148
149func hasCategory(findings []Finding, category string) bool {
150 for _, f := range findings {
151 if f.Category == category {
152 return true
153 }
154 }
155 return false
156}
157
158func TestRemoteHost(t *testing.T) {
159 for in, want := range map[string]string{
160 "git@10.0.2.1:the-valley.git": "10.0.2.1",
161 "10.0.2.1:repo.git": "10.0.2.1",
162 "ssh://git@10.0.2.1/repo.git": "10.0.2.1",
163 "ssh://git@10.0.2.1:2222/repo.git": "10.0.2.1",
164 "https://user:pw@192.168.9.5/o/r.git": "192.168.9.5",
165 "git@github.com:acme/widget.git": "github.com",
166 "/tmp/origin.git": "",
167 "./rel:thing": "",
168 } {
169 if got := RemoteHost(in); got != want {
170 t.Errorf("RemoteHost(%q) = %q, want %q", in, got, want)
171 }
172 }
173}
174
175func TestAllowedHostIsExactMatch(t *testing.T) {
176 allow := []string{"10.0.2.1"}
177 flags := func(s string) bool {
178 return len(CheckSensitivityAllowing(strings.NewReader(s), allow)) > 0
179 }
180 if flags("To 10.0.2.1:the-valley.git") {
181 t.Error("own remote flagged")
182 }
183 if !flags("To 10.0.2.2:the-valley.git") {
184 t.Error("other private IP not flagged")
185 }
186 if !flags("10.0.2.1 and also 10.0.2.2 here") {
187 t.Error("other private IP on the same line not flagged")
188 }
189 if !flags("To 10.0.2.11:x.git") {
190 t.Error("10.0.2.11 treated as 10.0.2.1")
191 }
192 if !flags("To 10.0.2.1:x.git\npassword: hunter2") {
193 t.Error("other patterns must still flag")
194 }
195}
196
197func TestLongLineFailsClosed(t *testing.T) {
198 in := strings.Repeat("a", 2<<20) + "\npassword = hunter2\n"
199 if len(CheckSensitivity(strings.NewReader(in))) == 0 {
200 t.Error("secret after a >1MiB line not flagged")
201 }
202 if len(CheckSensitivity(strings.NewReader(strings.Repeat("a", 2<<20)))) != 0 {
203 t.Error("a long clean line should scan clean")
204 }
205}
206
207type errReader struct{}
208
209func (errReader) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF }
210
211func TestReadErrorFailsClosed(t *testing.T) {
212 if len(CheckSensitivity(errReader{})) == 0 {
213 t.Error("read error reported clean")
214 }
215}