-
Notifications
You must be signed in to change notification settings - Fork 2
enh(security): Improve whitelisting and threshold applications #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
537bc7a
fix(security): Never auto-block whitelisted sources
nfebe 1b7d145
feat(config): Apply detection threshold changes without a restart
nfebe 3fa870d
refactor(security): Cache the whitelist in memory
nfebe 978346f
test(e2e): Simulate external client IPs in realtime capture tests
nfebe 666b36c
fix(security): Only trust forwarded client IPs from configured proxies
nfebe a375f3d
feat(config): Reload nginx when forwarded-proxy trust settings change
nfebe 19b06dd
test(e2e): Fail closed on IPv6 ranges in trusted-proxy check
nfebe 3d5b51b
fix(security): Sanitize trusted-proxy entries before injecting into lua
nfebe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/flatrun/agent/pkg/config" | ||
| ) | ||
|
|
||
| func TestRuntimeConfigKeysAdvertisesTrustKeys(t *testing.T) { | ||
| server := &Server{config: &config.Config{}} | ||
| keys := server.runtimeConfigKeys() | ||
|
|
||
| for _, key := range []string{"security.trusted_proxies", "security.trust_cf_header"} { | ||
| if !keys[key] { | ||
| t.Errorf("expected %q to be advertised as a runtime config key", key) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestTrustKeyApplierNoOpWhenSecurityDisabled(t *testing.T) { | ||
| server := &Server{config: &config.Config{}} | ||
| server.config.Security.Enabled = false | ||
|
|
||
| apply := server.runtimeAppliers()["security.trusted_proxies"] | ||
| if apply == nil { | ||
| t.Fatal("expected an applier for security.trusted_proxies") | ||
| } | ||
| if err := apply(server); err != nil { | ||
| t.Fatalf("applier should be a no-op when security is disabled, got: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package security | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func newTestManager(t *testing.T) *Manager { | ||
| t.Helper() | ||
| m, err := NewManager(t.TempDir()) | ||
| if err != nil { | ||
| t.Fatalf("NewManager: %v", err) | ||
| } | ||
| t.Cleanup(func() { m.Close() }) | ||
| return m | ||
| } | ||
|
|
||
| func ingestAuthFailures(t *testing.T, m *Manager, ip, path string, n int) *IngestResult { | ||
| t.Helper() | ||
| var last *IngestResult | ||
| for i := 0; i < n; i++ { | ||
| var err error | ||
| last, err = m.IngestEvent(&IngestEvent{ | ||
| SourceIP: ip, | ||
| RequestPath: path, | ||
| RequestMethod: "GET", | ||
| StatusCode: 401, | ||
| UserAgent: "Mozilla/5.0", | ||
| }, time.Hour) | ||
| if err != nil { | ||
| t.Fatalf("IngestEvent: %v", err) | ||
| } | ||
| } | ||
| return last | ||
| } | ||
|
|
||
| func TestIngestEventAutoBlocksOnRepeatedAuthFailures(t *testing.T) { | ||
| m := newTestManager(t) | ||
|
|
||
| result := ingestAuthFailures(t, m, "203.0.113.10", "/api/v1/stats", 5) | ||
|
|
||
| if !result.AutoBlocked { | ||
| t.Fatal("expected IP to be auto-blocked after repeated auth failures") | ||
| } | ||
| blocked, err := m.IsIPBlocked("203.0.113.10") | ||
| if err != nil { | ||
| t.Fatalf("IsIPBlocked: %v", err) | ||
| } | ||
| if !blocked { | ||
| t.Fatal("expected IP to be in blocked list") | ||
| } | ||
| } | ||
|
|
||
| func TestIngestEventSkipsWhitelistedIP(t *testing.T) { | ||
| m := newTestManager(t) | ||
|
|
||
| if _, err := m.AddWhitelistEntry("203.0.113.7", "ip", "test"); err != nil { | ||
| t.Fatalf("AddWhitelistEntry: %v", err) | ||
| } | ||
|
|
||
| result := ingestAuthFailures(t, m, "203.0.113.7", "/api/v1/stats", 20) | ||
|
|
||
| if result.Event != nil { | ||
| t.Fatal("expected no event for whitelisted IP") | ||
| } | ||
| if result.AutoBlocked { | ||
| t.Fatal("expected whitelisted IP to never be auto-blocked") | ||
| } | ||
| blocked, err := m.IsIPBlocked("203.0.113.7") | ||
| if err != nil { | ||
| t.Fatalf("IsIPBlocked: %v", err) | ||
| } | ||
| if blocked { | ||
| t.Fatal("whitelisted IP must not be blocked") | ||
| } | ||
| } | ||
|
|
||
| func TestIngestEventSkipsIPInWhitelistedCIDR(t *testing.T) { | ||
| m := newTestManager(t) | ||
|
|
||
| if _, err := m.AddWhitelistEntry("198.51.100.0/24", "cidr", "test range"); err != nil { | ||
| t.Fatalf("AddWhitelistEntry: %v", err) | ||
| } | ||
|
|
||
| result := ingestAuthFailures(t, m, "198.51.100.20", "/api/v1/stats", 20) | ||
|
|
||
| if result.Event != nil || result.AutoBlocked { | ||
| t.Fatal("expected IP inside whitelisted CIDR to be skipped") | ||
| } | ||
| } | ||
|
|
||
| func TestIngestEventSkipsSeededPrivateNetworks(t *testing.T) { | ||
| m := newTestManager(t) | ||
|
|
||
| for _, ip := range []string{"127.0.0.1", "10.1.2.3", "172.18.0.5", "192.168.1.50"} { | ||
| result := ingestAuthFailures(t, m, ip, "/api/v1/stats", 20) | ||
| if result.Event != nil || result.AutoBlocked { | ||
| t.Fatalf("expected default-whitelisted IP %s to be skipped", ip) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestIngestEventSkipsWhitelistedPathPrefix(t *testing.T) { | ||
| m := newTestManager(t) | ||
|
|
||
| result, err := m.IngestEvent(&IngestEvent{ | ||
| SourceIP: "203.0.113.99", | ||
| RequestPath: "/api/health", | ||
| RequestMethod: "GET", | ||
| StatusCode: 500, | ||
| UserAgent: "Mozilla/5.0", | ||
| }, time.Hour) | ||
| if err != nil { | ||
| t.Fatalf("IngestEvent: %v", err) | ||
| } | ||
|
|
||
| if result.Event != nil || result.AutoBlocked { | ||
| t.Fatal("expected request to whitelisted path to be skipped") | ||
| } | ||
| } | ||
|
|
||
| func TestWhitelistCacheInvalidatedOnMutation(t *testing.T) { | ||
| m := newTestManager(t) | ||
|
|
||
| got, err := m.IsRequestWhitelisted("203.0.113.40", "/api/v1/stats") | ||
| if err != nil { | ||
| t.Fatalf("IsRequestWhitelisted: %v", err) | ||
| } | ||
| if got { | ||
| t.Fatal("IP unexpectedly whitelisted before adding entry") | ||
| } | ||
|
|
||
| id, err := m.AddWhitelistEntry("203.0.113.40", "ip", "test") | ||
| if err != nil { | ||
| t.Fatalf("AddWhitelistEntry: %v", err) | ||
| } | ||
| got, err = m.IsRequestWhitelisted("203.0.113.40", "/api/v1/stats") | ||
| if err != nil { | ||
| t.Fatalf("IsRequestWhitelisted: %v", err) | ||
| } | ||
| if !got { | ||
| t.Fatal("expected entry added after cache build to be honored") | ||
| } | ||
|
|
||
| if err := m.RemoveWhitelistEntry(id); err != nil { | ||
| t.Fatalf("RemoveWhitelistEntry: %v", err) | ||
| } | ||
| got, err = m.IsRequestWhitelisted("203.0.113.40", "/api/v1/stats") | ||
| if err != nil { | ||
| t.Fatalf("IsRequestWhitelisted: %v", err) | ||
| } | ||
| if got { | ||
| t.Fatal("expected removed entry to stop matching") | ||
| } | ||
| } | ||
|
|
||
| func TestIsRequestWhitelisted(t *testing.T) { | ||
| m := newTestManager(t) | ||
|
|
||
| if _, err := m.AddWhitelistEntry("2001:db8::/32", "cidr", "test v6"); err != nil { | ||
| t.Fatalf("AddWhitelistEntry: %v", err) | ||
| } | ||
|
|
||
| cases := []struct { | ||
| ip string | ||
| path string | ||
| want bool | ||
| }{ | ||
| {"127.0.0.1", "/anything", true}, | ||
| {"10.255.0.1", "/anything", true}, | ||
| {"2001:db8::1", "/anything", true}, | ||
| {"203.0.113.5", "/api/_internal/blocked-ips", true}, | ||
| {"203.0.113.5", "/wp-login.php", false}, | ||
| {"not-an-ip", "/wp-login.php", false}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| got, err := m.IsRequestWhitelisted(tc.ip, tc.path) | ||
| if err != nil { | ||
| t.Fatalf("IsRequestWhitelisted(%s, %s): %v", tc.ip, tc.path, err) | ||
| } | ||
| if got != tc.want { | ||
| t.Errorf("IsRequestWhitelisted(%s, %s) = %v, want %v", tc.ip, tc.path, got, tc.want) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performing the whitelist check before the block check is correct for performance (whitelists are usually smaller). However, ensure that
event.SourceIPis already normalized (e.g. trimmed) before this call to avoid bypasses using whitespace.