diff options
| author | Adam Mathes <adam@adammathes.com> | 2026-02-14 09:17:56 -0800 |
|---|---|---|
| committer | Adam Mathes <adam@adammathes.com> | 2026-02-14 09:17:56 -0800 |
| commit | cac85dc06b519d9bd6db4d017d501dffbbd8bac4 (patch) | |
| tree | dc8024e501c0fbda6b9d28622ff2553475044487 /internal/safehttp/safehttp_test.go | |
| parent | ca1418fc0135d52a009ab218d6e24187fb355a3c (diff) | |
| download | neko-cac85dc06b519d9bd6db4d017d501dffbbd8bac4.tar.gz neko-cac85dc06b519d9bd6db4d017d501dffbbd8bac4.tar.bz2 neko-cac85dc06b519d9bd6db4d017d501dffbbd8bac4.zip | |
security: mitigate SSRF in image proxy and feed fetcher (fixing NK-0ca7nq)
Diffstat (limited to 'internal/safehttp/safehttp_test.go')
| -rw-r--r-- | internal/safehttp/safehttp_test.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/safehttp/safehttp_test.go b/internal/safehttp/safehttp_test.go new file mode 100644 index 0000000..b2636da --- /dev/null +++ b/internal/safehttp/safehttp_test.go @@ -0,0 +1,53 @@ +package safehttp + +import ( + "net" + "testing" + "time" +) + +func TestSafeClient(t *testing.T) { + client := NewSafeClient(2 * time.Second) + + // Localhost should fail + t.Log("Testing localhost...") + _, err := client.Get("http://127.0.0.1:8080") + if err == nil { + t.Error("Expected error for localhost request, got nil") + } else { + t.Logf("Got expected error: %v", err) + } + + // Private IP should fail + t.Log("Testing private IP...") + _, err = client.Get("http://10.0.0.1") + if err == nil { + t.Error("Expected error for private IP request, got nil") + } else { + t.Logf("Got expected error: %v", err) + } +} + +func TestIsPrivateIP(t *testing.T) { + tests := []struct { + ip string + expected bool + }{ + {"127.0.0.1", true}, + {"10.0.0.1", true}, + {"172.16.0.1", true}, + {"192.168.1.1", true}, + {"169.254.1.1", true}, + {"8.8.8.8", false}, + {"1.1.1.1", false}, + {"::1", true}, + {"fe80::1", true}, + {"fc00::1", true}, + } + + for _, tc := range tests { + if res := isPrivateIP(net.ParseIP(tc.ip)); res != tc.expected { + t.Errorf("isPrivateIP(%s) = %v, want %v", tc.ip, res, tc.expected) + } + } +} |
