Files
cairnobs/api/localauth/ratelimit_test.go
T
jcoffey-dev 4b5dae5879 Add local login, agent extra log paths, IPv4/IPv6 metrics; remediate security audit findings
This is a large squashed commit covering two batches of prior uncommitted
work plus a full security-audit remediation pass, kept together because
go.mod/go.sum and several shared files (main.go, handler.go) were touched
by both and splitting risked non-building intermediate commits.

Features (built earlier, previously uncommitted):
- Local username/password login for single-tenant deployments with no
  SSO configured (api/localauth, alerting/internal/sessioncheck,
  sentryctl users, web/src/routes/login, metadata migrations 0040/0041).
- Remotely-editable additional log file paths for agents, on top of
  their existing primary source (api/agents, agent/sentry-agent
  extra-file-path diffing, web agent config UI).
- IPv4/IPv6 addresses reported alongside other host system metrics.

Security audit remediation (this pass, all live-verified in production):
- Critical: block ClickHouse SSRF table functions (url/remote/file/s3/...)
  in the raw-SQL query escape hatch.
- High: deny sensitive paths and require Admin to add agent
  extra_file_paths (Editor could previously point an agent at /etc/shadow
  or an SSH key); alerting webhook targets now validate against
  internal/metadata/loopback addresses, both at creation and send time;
  alerting's session middleware now enforces an Editor+ floor on
  mutating requests instead of "any authenticated session"; bumped
  goxmldsig to close a SAML signature-verification bypass (GO-2026-4753).
- Medium: per-IP login rate limiting; security response headers
  (HSTS/CSP/nosniff/X-Frame-Options/Referrer-Policy/Permissions-Policy)
  on web/nginx.conf; a DevCredentialWarnings check in every Go service's
  config loader, logging loudly at startup if a deployment is still on
  docker-compose.yml's literal dev-only credentials; dependency bumps
  (golang.org/x/text, grpc, x/net, quick-xml, h2) across every affected
  Go module and both Rust crates, including a previously-uncovered x/net
  vulnerability in deploy/operator; a new security-scan.yml CI workflow
  running cargo-deny/govulncheck/npm-audit, mirroring the existing
  license-compliance.yml matrix shape.
- Low: removed sentryctl's plaintext --password flag (shell
  history/`ps` exposure) in favor of stdin and a --password-stdin flag
  for reset-password's optional specific-password path; a dummy bcrypt
  comparison closes a login response-time username-enumeration
  side-channel.
2026-08-18 23:53:20 -07:00

85 lines
2.4 KiB
Go

package localauth
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/sentry/sentry/api/authz"
)
func TestLoginLimiterAllowsUpToMax(t *testing.T) {
l := newLoginLimiter(3, time.Minute)
for i := 0; i < 3; i++ {
if !l.allow("1.2.3.4") {
t.Fatalf("attempt %d: want allowed", i+1)
}
}
if l.allow("1.2.3.4") {
t.Fatal("4th attempt within the window: want denied")
}
}
func TestLoginLimiterIsPerKey(t *testing.T) {
l := newLoginLimiter(1, time.Minute)
if !l.allow("1.2.3.4") {
t.Fatal("first attempt from 1.2.3.4: want allowed")
}
if !l.allow("5.6.7.8") {
t.Fatal("a different IP must have its own budget")
}
if l.allow("1.2.3.4") {
t.Fatal("second attempt from 1.2.3.4: want denied")
}
}
func TestLoginLimiterResetsAfterWindow(t *testing.T) {
l := newLoginLimiter(1, 10*time.Millisecond)
if !l.allow("1.2.3.4") {
t.Fatal("first attempt: want allowed")
}
if l.allow("1.2.3.4") {
t.Fatal("second attempt within the window: want denied")
}
time.Sleep(20 * time.Millisecond)
if !l.allow("1.2.3.4") {
t.Fatal("attempt after the window elapsed: want allowed")
}
}
func TestClientIPPrefersForwardedFor(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/auth/login", nil)
r.RemoteAddr = "10.0.0.1:5555"
r.Header.Set("X-Forwarded-For", "203.0.113.9, 10.0.0.1")
if got := clientIP(r); got != "203.0.113.9" {
t.Errorf("clientIP() = %q, want %q", got, "203.0.113.9")
}
}
func TestClientIPFallsBackToRemoteAddr(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/auth/login", nil)
r.RemoteAddr = "198.51.100.7:5555"
if got := clientIP(r); got != "198.51.100.7" {
t.Errorf("clientIP() = %q, want %q", got, "198.51.100.7")
}
}
// TestHandleLoginRateLimited is the regression test for the
// security-audit finding that POST /auth/login had no rate limiting at
// all -- repeated attempts from the same client must eventually get a
// 429, not another 401.
func TestHandleLoginRateLimited(t *testing.T) {
fs := newFakeStore()
mustCreateUser(t, fs, "alice", "hunter22", authz.RoleEditor)
_, mux := newTestHandler(t, fs)
var last *httptest.ResponseRecorder
for i := 0; i < loginRateLimitMax+1; i++ {
last = doRequest(t, mux, http.MethodPost, "/auth/login", `{"username":"alice","password":"wrong-password"}`, nil)
}
if last.Code != http.StatusTooManyRequests {
t.Fatalf("status after exceeding the limit = %d, want 429", last.Code)
}
}