| index 879763d09..7c10f67d7 100644 |
| --- a/internal/gate/gate_test.go |
| +++ b/internal/gate/gate_test.go |
| @@ -74,14 +74,14 @@ func TestLimiterTenPerMinute(t *testing.T) { |
| } |
| } |
| |
| -func testGate(hash string) *Gate { |
| +func testGate() *Gate { |
| g := New() |
| g.now = func() time.Time { return time.Unix(1_800_000_000, 0) } |
| return g |
| } |
| |
| func TestAdmitNoCookieIsTheLoginPage(t *testing.T) { |
| - g := testGate("") |
| + g := testGate() |
| h, _ := Hash("correct horse") |
| for _, method := range []string{http.MethodGet, http.MethodPost, http.MethodHead} { |
| rec := httptest.NewRecorder() |
| @@ -128,7 +128,7 @@ func postLogin(g *Gate, host, hash, password, next string) *httptest.ResponseRec |
| } |
| |
| func TestAdmitRightPasswordSetsCookieAndRedirects(t *testing.T) { |
| - g := testGate("") |
| + g := testGate() |
| h, _ := Hash("correct horse") |
| rec := postLogin(g, "site.example", h, "correct horse", "/docs?x=1") |
| if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/docs?x=1" { |
| @@ -162,7 +162,7 @@ func TestAdmitRightPasswordSetsCookieAndRedirects(t *testing.T) { |
| } |
| |
| func TestAdmitBadNextBecomesRoot(t *testing.T) { |
| - g := testGate("") |
| + g := testGate() |
| h, _ := Hash("correct horse") |
| for _, next := range []string{"//evil.example/", "https://evil.example/", "docs", ""} { |
| rec := postLogin(g, "site.example", h, "correct horse", next) |
| @@ -173,7 +173,7 @@ func TestAdmitBadNextBecomesRoot(t *testing.T) { |
| } |
| |
| func TestAdmitWrongPasswordThenLimit(t *testing.T) { |
| - g := testGate("") |
| + g := testGate() |
| h, _ := Hash("correct horse") |
| compares := 0 |
| g.compare = func(hash, password string) bool { compares++; return false } |
| @@ -196,7 +196,7 @@ func TestAdmitWrongPasswordThenLimit(t *testing.T) { |
| } |
| |
| func TestLoginPathOnGetIsJustAGatedRequest(t *testing.T) { |
| - g := testGate("") |
| + g := testGate() |
| h, _ := Hash("correct horse") |
| rec := httptest.NewRecorder() |
| r := httptest.NewRequest(http.MethodGet, "https://site.example"+LoginPath, nil) |
| @@ -208,7 +208,19 @@ func TestLoginPathOnGetIsJustAGatedRequest(t *testing.T) { |
| // safeNext is the whole open-redirect surface, so pin it directly rather |
| // than only through the three shapes the POST test happens to send. |
| func TestSafeNextIsAPathOnThisHostOrRoot(t *testing.T) { |
| - for _, bad := range []string{"//evil.example/", `/\evil.example/`, `/\/evil.example/`, "https://evil.example/", "javascript:alert(1)", `\\evil.example\`, "", "docs", "?x=1", "#frag"} { |
| + bad := []string{ |
| + "//evil.example/", `/\evil.example/`, `/\/evil.example/`, |
| + "https://evil.example/", "javascript:alert(1)", `\\evil.example\`, |
| + "", "docs", "?x=1", "#frag", |
| + // Control bytes: net/http rewrites only CR and LF, and a browser |
| + // strips the rest before resolving, so a tab here would land as |
| + // "//evil.example/". |
| + "/\t/evil.example/", "/\r\n/evil.example/", "/\x00/evil.example/", |
| + "/\x7f/evil.example/", "/\x1f/evil.example/", |
| + // The login path itself: the app behind the curtain does not serve it. |
| + LoginPath, LoginPath + "?x=1", LoginPath + "#f", |
| + } |
| + for _, bad := range bad { |
| if got := safeNext(bad); got != "/" { |
| t.Errorf("safeNext(%q) = %q, want /", bad, got) |
| } |
| @@ -250,10 +262,12 @@ func TestStripCookieDropsTheHeaderWhenTheGateCookieIsTheOnlyOne(t *testing.T) { |
| } |
| |
| // A body over maxFormBody fails to parse; that must cost no bcrypt and no |
| -// limiter token, so a flood of junk cannot lock a tenant out of their own |
| -// site. |
| +// limiter token. The bucket is keyed on the sender's own address, so a junk |
| +// flood only ever starves the flooder — except behind the backhaul fold, |
| +// where a sibling edge's address stands in for every visitor through it, |
| +// and there this is what keeps a flood from reaching them. |
| func TestOversizedFormBodyIsRefusedWithoutSpendingAnAttempt(t *testing.T) { |
| - g := testGate("") |
| + g := testGate() |
| h, _ := Hash("correct horse") |
| compares := 0 |
| g.compare = func(hash, password string) bool { compares++; return true } |
| @@ -272,3 +286,114 @@ func TestOversizedFormBodyIsRefusedWithoutSpendingAnAttempt(t *testing.T) { |
| t.Fatal("an unparseable body spent an attempt") |
| } |
| } |
| + |
| +// Tenant hosts share a wildcard apex, so a sibling host — or an XSS on one |
| +// — can plant a second carlos_gate scoped to .apex, and the browser sends |
| +// it first. Taking only the first cookie would lock the visitor out of a |
| +// site they hold a good cookie for, with no way to clear it. |
| +func TestAdmitAcceptsAnyValidGateCookieNotJustTheFirst(t *testing.T) { |
| + g := testGate() |
| + h, _ := Hash("correct horse") |
| + good := Mint("site.example", h, g.now().Add(Lifetime)) |
| + rec := httptest.NewRecorder() |
| + r := httptest.NewRequest(http.MethodGet, "https://site.example/", nil) |
| + r.Header.Set("Cookie", CookieName+"=v1.9999999999.junk; theme=dark; "+CookieName+"="+good) |
| + if !g.Admit(rec, r, "site.example", h) { |
| + t.Fatal("a planted decoy cookie locked the visitor out") |
| + } |
| + got := r.Header.Get("Cookie") |
| + if strings.Contains(got, CookieName) { |
| + t.Fatalf("upstream Cookie = %q, want every gate cookie stripped", got) |
| + } |
| + if got != "theme=dark" { |
| + t.Fatalf("upstream Cookie = %q, want the other cookies untouched", got) |
| + } |
| +} |
| + |
| +func postLoginFrom(g *Gate, reqHost, host, hash, password, next, origin string) *httptest.ResponseRecorder { |
| + rec := httptest.NewRecorder() |
| + form := url.Values{"password": {password}, "next": {next}} |
| + r := httptest.NewRequest(http.MethodPost, "https://"+reqHost+LoginPath, strings.NewReader(form.Encode())) |
| + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + if origin != "" { |
| + r.Header.Set("Origin", origin) |
| + } |
| + r.RemoteAddr = "203.0.113.9:4242" |
| + g.Admit(rec, r, host, hash) |
| + return rec |
| +} |
| + |
| +// The login POST needs no cookie, so SameSite does not cover it: without an |
| +// Origin check a hidden form on an attacker's page could auto-submit eleven |
| +// guesses and park a visitor at 429. |
| +func TestLoginRefusesACrossOriginPostWithoutSpendingAnAttempt(t *testing.T) { |
| + g := testGate() |
| + h, _ := Hash("correct horse") |
| + compares := 0 |
| + g.compare = func(hash, password string) bool { compares++; return true } |
| + |
| + for _, origin := range []string{"https://evil.example", "null", "https://evil.example:443", "http://site.example.evil.example"} { |
| + rec := postLoginFrom(g, "site.example", "site.example", h, "correct horse", "/docs", origin) |
| + if rec.Code != http.StatusForbidden { |
| + t.Fatalf("Origin %q -> %d, want 403", origin, rec.Code) |
| + } |
| + if strings.Contains(rec.Body.String(), msgWrong) || strings.Contains(rec.Body.String(), msgTooMany) { |
| + t.Fatalf("Origin %q: the 403 page judged the password: %s", origin, rec.Body.String()) |
| + } |
| + if !strings.Contains(rec.Body.String(), "Password required") { |
| + t.Fatalf("Origin %q: the 403 is not the login page: %s", origin, rec.Body.String()) |
| + } |
| + } |
| + if compares != 0 { |
| + t.Fatalf("bcrypt compared %d times on cross-origin posts, want 0", compares) |
| + } |
| + // The bucket is untouched: a visitor who was targeted can still make |
| + // all ten of their own attempts. |
| + for i := 0; i < 10; i++ { |
| + if rec := postLogin(g, "site.example", h, "correct horse", "/"); rec.Code == http.StatusTooManyRequests { |
| + t.Fatalf("attempt %d was rate-limited — the cross-origin posts drained the bucket", i+1) |
| + } |
| + } |
| +} |
| + |
| +func TestLoginAllowsSameOriginAndAbsentOrigin(t *testing.T) { |
| + g := testGate() |
| + h, _ := Hash("correct horse") |
| + cases := []struct{ name, reqHost, origin string }{ |
| + {"same origin", "site.example", "https://site.example"}, |
| + {"absent origin (curl)", "site.example", ""}, |
| + {"origin and host both carry the port", "site.example:8443", "https://site.example:8443"}, |
| + {"port on one side only", "site.example:8443", "https://site.example"}, |
| + {"case-insensitive host", "site.example", "https://SITE.example"}, |
| + // An alias posts from its own name, so the comparison is against |
| + // r.Host and not the route's primary hostname. |
| + {"alias posts from the alias", "alias.example", "https://alias.example"}, |
| + } |
| + for _, c := range cases { |
| + rec := postLoginFrom(g, c.reqHost, "site.example", h, "correct horse", "/docs", c.origin) |
| + if rec.Code != http.StatusSeeOther { |
| + t.Errorf("%s: %d, want 303", c.name, rec.Code) |
| + } |
| + } |
| +} |
| + |
| +func TestSuccessfulLoginIsNoStoreAndNoindex(t *testing.T) { |
| + g := testGate() |
| + h, _ := Hash("correct horse") |
| + rec := postLogin(g, "site.example", h, "correct horse", "/docs") |
| + if cc := rec.Header().Get("Cache-Control"); cc != "no-store" { |
| + t.Errorf("Cache-Control = %q", cc) |
| + } |
| + if rt := rec.Header().Get("X-Robots-Tag"); rt != "noindex, nofollow" { |
| + t.Errorf("X-Robots-Tag = %q", rt) |
| + } |
| +} |
| + |
| +func TestCookieExpiryEncodingIsCanonical(t *testing.T) { |
| + now := time.Unix(1_800_000_000, 0) |
| + v := Mint("site.example", "$2a$10$hash", now.Add(Lifetime)) |
| + parts := strings.Split(v, ".") |
| + if Verify(parts[0]+".+"+parts[1]+"."+parts[2], "site.example", "$2a$10$hash", now) { |
| + t.Fatal(`a "+"-signed expiry was accepted`) |
| + } |
| +} |