feat: import Sea vHost single-server testing release

This commit is contained in:
canglan committed 2026-10-30 08:39:10 +08:00
commit 248badd33e
441 files changed
+63221

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
package httpapi
import (
"net/http/httptest"
"testing"
"time"
)
func TestSameOriginRequiresMatchingSchemeAndHost(t *testing.T) {
request := httptest.NewRequest("POST", "http://console.example/api/v1/auth/login", nil)
request.RemoteAddr = "127.0.0.1:42000"
request.Header.Set("X-Forwarded-Proto", "https")
tests := []struct {
name string
origin string
fetchSite string
wantAllowed bool
}{
{name: "matching", origin: "https://console.example", wantAllowed: true},
{name: "matching trailing slash", origin: "https://console.example/", wantAllowed: true},
{name: "missing origin", wantAllowed: true},
{name: "wrong scheme", origin: "http://console.example", wantAllowed: false},
{name: "wrong host", origin: "https://attacker.example", wantAllowed: false},
{name: "origin path", origin: "https://console.example/path", wantAllowed: false},
{name: "null origin", origin: "null", wantAllowed: false},
{name: "cross site fetch", origin: "https://console.example", fetchSite: "cross-site", wantAllowed: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
request.Header.Set("Origin", test.origin)
request.Header.Set("Sec-Fetch-Site", test.fetchSite)
if got := sameOrigin(request); got != test.wantAllowed {
t.Fatalf("sameOrigin() = %v, want %v", got, test.wantAllowed)
}
})
}
}
func TestForwardedHeadersAreTrustedOnlyFromLoopback(t *testing.T) {
request := httptest.NewRequest("GET", "http://console.example/", nil)
request.Header.Set("X-Forwarded-Proto", "https")
request.Header.Set("X-Real-IP", "203.0.113.77")
request.RemoteAddr = "198.51.100.8:44000"
if got := requestScheme(request); got != "http" {
t.Fatalf("untrusted request scheme = %q", got)
}
if got := requestClientIP(request); got != "198.51.100.8" {
t.Fatalf("untrusted client IP = %q", got)
}
request.RemoteAddr = "[::1]:44000"
if got := requestScheme(request); got != "https" {
t.Fatalf("trusted request scheme = %q", got)
}
if got := requestClientIP(request); got != "203.0.113.77" {
t.Fatalf("trusted client IP = %q", got)
}
}
func TestLoginLimiterBlocksExpiresAndResets(t *testing.T) {
limiter := newLoginLimiter()
now := time.Date(2026, time.October, 14, 8, 0, 0, 0, time.UTC)
for attempt := 0; attempt < 2; attempt++ {
if allowed, _ := limiter.allow("identifier:user@example.test", now, 2); !allowed {
t.Fatalf("attempt %d was unexpectedly blocked", attempt+1)
}
}
if allowed, retryAfter := limiter.allow("identifier:user@example.test", now, 2); allowed || retryAfter != loginWindow {
t.Fatalf("blocked attempt = (%v, %v), want (false, %v)", allowed, retryAfter, loginWindow)
}
if allowed, _ := limiter.allow("identifier:user@example.test", now.Add(loginWindow), 2); !allowed {
t.Fatal("attempt was not allowed after the window expired")
}
limiter.reset("identifier:user@example.test")
if allowed, _ := limiter.allow("identifier:user@example.test", now, 2); !allowed {
t.Fatal("attempt was not allowed after reset")
}
}
func TestLoginLimiterBoundsTrackedKeys(t *testing.T) {
limiter := newLoginLimiter()
now := time.Date(2026, time.October, 14, 8, 0, 0, 0, time.UTC)
for index := 0; index < loginLimiterMaxKeys+100; index++ {
limiter.allow(string(rune(index+1)), now, 1)
}
if got := len(limiter.attempts); got > loginLimiterMaxKeys {
t.Fatalf("tracked keys = %d, want at most %d", got, loginLimiterMaxKeys)
}
}