- internal/{address,identity,protocol,tce,transport,verify} -> pkg/ so
external Go projects can import the verified core; invariant tests
updated for the new paths
- Config.TrustProxy: key rate limiting by X-Forwarded-For when the relay
sits behind a reverse proxy (off by default, header never trusted
otherwise)
- examples/service + examples/approve: complete passwordless login round
trip (mint request -> wallet approves -> local verify), run live in CI
- docs/SERVICE-GUIDE.md: the integration recipe
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAllowUnderLimit(t *testing.T) {
|
|
l := newIPLimiter(3, time.Minute)
|
|
for i := 0; i < 3; i++ {
|
|
if !l.allow("1.2.3.4") {
|
|
t.Fatalf("request %d: expected allow", i)
|
|
}
|
|
}
|
|
if l.allow("1.2.3.4") {
|
|
t.Fatal("expected deny once limit reached")
|
|
}
|
|
}
|
|
|
|
func TestDistinctIPsIndependent(t *testing.T) {
|
|
l := newIPLimiter(1, time.Minute)
|
|
if !l.allow("10.0.0.1") {
|
|
t.Fatal("ip1 first request should be allowed")
|
|
}
|
|
if l.allow("10.0.0.1") {
|
|
t.Fatal("ip1 second request should be denied")
|
|
}
|
|
if !l.allow("10.0.0.2") {
|
|
t.Fatal("ip2 should have its own budget")
|
|
}
|
|
}
|
|
|
|
func TestWindowReset(t *testing.T) {
|
|
l := newIPLimiter(2, 20*time.Millisecond)
|
|
if !l.allow("9.9.9.9") || !l.allow("9.9.9.9") {
|
|
t.Fatal("first two requests should be allowed")
|
|
}
|
|
if l.allow("9.9.9.9") {
|
|
t.Fatal("third request within window should be denied")
|
|
}
|
|
time.Sleep(30 * time.Millisecond)
|
|
if !l.allow("9.9.9.9") {
|
|
t.Fatal("request after window expiry should be allowed again")
|
|
}
|
|
}
|
|
|
|
func TestClientIP(t *testing.T) {
|
|
withPort, _ := http.NewRequest(http.MethodGet, "/", nil)
|
|
withPort.RemoteAddr = "192.168.1.5:54321"
|
|
if got := clientIP(withPort, false); got != "192.168.1.5" {
|
|
t.Fatalf("expected 192.168.1.5, got %q", got)
|
|
}
|
|
|
|
noPort, _ := http.NewRequest(http.MethodGet, "/", nil)
|
|
noPort.RemoteAddr = "10.0.0.1"
|
|
if got := clientIP(noPort, false); got != "10.0.0.1" {
|
|
t.Fatalf("expected 10.0.0.1, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestClientIPTrustsForwardedFor(t *testing.T) {
|
|
r := httptest.NewRequest("POST", "/v1/objects", nil)
|
|
r.RemoteAddr = "10.0.0.9:55555"
|
|
r.Header.Set("X-Forwarded-For", "203.0.113.7, 10.0.0.1")
|
|
if got := clientIP(r, true); got != "203.0.113.7" {
|
|
t.Fatalf("trusted proxy: got %q, want 203.0.113.7", got)
|
|
}
|
|
// Without the knob the header must be ignored (spoofable).
|
|
if got := clientIP(r, false); got != "10.0.0.9" {
|
|
t.Fatalf("untrusted: got %q, want 10.0.0.9", got)
|
|
}
|
|
}
|