Files
Crussell/backend/mw/ratelimit_shared.go
T
popertots fe88f2084d fix: review-loop B — adversarial findings (sweep auto-refund, admin clamp, 2FA real challenge, opaque refresh tokens, gated client IP, GBP pence)
Loop B aggressive adversarial round (3 attack agents) + fix + secondary + verification:
- CRITICAL: sweep replay auto-refunds provably-created-later duplicate charges (gated on parseable CreatedAt); 22h legitimate-retry window == 22h sweep cutoff (no dead zone)
- HIGH: admin Take Payment clamps to remaining obligation (cash/giftcard/saved-card/terminal); no unintended tip from overflow; campaign credit against remaining
- HIGH: /api/services/eligible-for/{id} requires auth + owner-or-admin (DOB/age + patch-test health-data leak closed)
- HIGH: opaque refresh-token rotation (login/refresh return {token, jti, refreshToken}; refresh REQUIRES opaque token; single-use rotation; logout revokes; access token rejected at refresh)
- HIGH: saved-card charges require a REAL 2FA verification code (B6/B10) — backend gate on all 8 charge paths + shared TwoFactorCodeInput frontend component on all 7 surfaces; 2FA gate is no longer setup-flag-only
- MEDIUM: ungated CF-Connecting-IP in reserve/admin_reserve gated via exported mw.ClientIP; 2FA limiter keyed on userID alone (no header-rotation bypass); ChangePassword actually revokes JTI + refresh tokens; 2FA setup mint cooldown + persistent failed-attempt counter; campaign redemption race surfaces campaign_fully_redeemed
- Terminal saved-card VAT applied (was under-collected); age-guard reconcile failures notify; isWeakJWTSecret entropy gate; gift-card redeem per-card counter + per-user limiter; webhook signature key startup validation
- NEW internal/twofa package (single source of truth breaking the payments<->user import cycle); consolidation of duplicate 2FA hash/verify
- Frontend: refresh-token storage + rotation, TwoFactorCodeInput component, amountPaidPence in admin modal, B5/B6/B10 contract wiring; 70 frontend tests
- Tests: loop_b_fixes_test.go, internal/twofa tests, updated auth/services/profile/twofa/mw tests

All 26 backend packages pass (incl. internal/twofa); frontend 70/70 + build clean; env-docs 41/41.
2026-08-22 00:34:50 +01:00

174 lines
5.7 KiB
Go

// Shared types, registration, and cleanup for RateLimiter + ProgressiveRateLimiter.
// Used by both production (!dev) and dev (dev) builds — keep tag-free.
package mw
import (
"context"
"crussell/clock"
"net"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/go-chi/chi/v5/middleware"
)
// trustProxyHeaders gates clientIP()'s use of the proxy-set client-IP headers:
// CF-Connecting-IP in clientIP(), and (via TrustProxyHeaders) the X-Real-IP
// ClientIPFromHeader middleware registered in main.go. It is read once at
// startup from the TRUST_PROXY_HEADERS env var and defaults to false.
//
// nginx (nginx/conf.d/default.conf) resolves the real client IP itself with
// the real_ip module (set_real_ip_from <Cloudflare ranges> +
// real_ip_header CF-Connecting-IP), then overwrites both X-Real-IP and
// CF-Connecting-IP with the validated $remote_addr — so behind nginx those
// headers are the authoritative, unspoofable per-client key. Set
// TRUST_PROXY_HEADERS=true for ANY deployment where a trusted proxy (nginx
// and/or the Cloudflare edge) sits between clients and this backend and
// overwrites these headers itself. It MUST stay false when the backend is
// origin-exposed: a client talking directly to the backend could otherwise
// rotate X-Real-IP and/or CF-Connecting-IP to bypass per-IP rate limiting.
var trustProxyHeaders = func() bool {
v, ok := os.LookupEnv("TRUST_PROXY_HEADERS")
if !ok {
return false
}
b, err := strconv.ParseBool(v)
return err == nil && b
}()
// TrustProxyHeaders reports whether proxy-set client-IP headers (X-Real-IP,
// CF-Connecting-IP) are honored by the rate limiter. main.go uses it to gate
// middleware.ClientIPFromHeader("X-Real-IP") on the same flag, so an
// origin-exposed backend never registers a middleware that would let a client
// forge its own rate-limit key. Both the header trust in ClientIP and the
// middleware registration read this single source of truth.
func TrustProxyHeaders() bool { return trustProxyHeaders }
// ClientIP derives the per-client IP for security-sensitive handlers that need
// a client-address key (reservation ipHash, per-IP audit trails) using the
// SAME gated resolution as the rate limiter. Priority:
//
// 1. CF-Connecting-IP header — honored ONLY when TRUST_PROXY_HEADERS=true
// (see trustProxyHeaders). A trusted edge (Cloudflare, or nginx whose
// real_ip module validated it against the set_real_ip_from ranges) has
// already overwritten it with the real client IP, so it is unspoofable
// there. Ignored by default because an origin-exposed backend must never
// trust a client-controlled value (B7).
// 2. middleware.GetClientIP(r.Context()) — the X-Real-IP value nginx sets
// from $remote_addr, captured by middleware.ClientIPFromHeader("X-Real-IP")
// in main.go. That middleware is registered only when
// TRUST_PROXY_HEADERS=true, so it too is trusted solely behind a proxy.
// 3. net.SplitHostPort(r.RemoteAddr) / r.RemoteAddr fallback — the actual
// TCP peer; the only key source usable when the backend is origin-exposed.
func ClientIP(r *http.Request) string {
if trustProxyHeaders {
if ip := r.Header.Get("CF-Connecting-IP"); ip != "" {
return ip
}
}
if ip := middleware.GetClientIP(r.Context()); ip != "" {
return ip
}
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err == nil && ip != "" {
return ip
}
return r.RemoteAddr
}
// RateLimiter implements a simple in-memory rate limiter
type RateLimiter struct {
requests map[string][]time.Time
mu sync.RWMutex
limit int
window time.Duration
}
// ProgressiveRateLimiter implements per-IP rate limiting with increasing backoff.
// Designed for bot-spam prevention across accounts (not account-specific lockout).
type ProgressiveRateLimiter struct {
requests map[string]*ipProgressiveState
mu sync.RWMutex
}
type ipProgressiveState struct {
// Timestamps of all requests within the tracking window
timestamps []time.Time
}
var (
registeredLimiters []*RateLimiter
registeredLimitersMu sync.Mutex
)
func registerLimiter(rl *RateLimiter) {
registeredLimitersMu.Lock()
registeredLimiters = append(registeredLimiters, rl)
registeredLimitersMu.Unlock()
}
// CleanupAllRateLimiters runs Cleanup on every registered RateLimiter.
// Called by the centralised jobs scheduler.
func CleanupAllRateLimiters(ctx context.Context) (int, error) {
registeredLimitersMu.Lock()
limiters := make([]*RateLimiter, len(registeredLimiters))
copy(limiters, registeredLimiters)
registeredLimitersMu.Unlock()
for _, l := range limiters {
l.Cleanup()
}
return 0, nil
}
// CleanupProgressiveRateLimiter runs Cleanup on the global progressive rate limiter.
// Called by the centralised jobs scheduler.
func CleanupProgressiveRateLimiter(ctx context.Context) (int, error) {
globalProgressiveLimiter.Cleanup()
return 0, nil
}
// Cleanup removes expired entries from the rate limiter map.
func (rl *RateLimiter) Cleanup() {
rl.mu.Lock()
defer rl.mu.Unlock()
now := clock.Now()
for key, times := range rl.requests {
var valid []time.Time
for _, t := range times {
if now.Sub(t) < rl.window {
valid = append(valid, t)
}
}
if len(valid) == 0 {
delete(rl.requests, key)
} else {
rl.requests[key] = valid
}
}
}
// Cleanup removes expired entries from the progressive rate limiter map.
func (prl *ProgressiveRateLimiter) Cleanup() {
prl.mu.Lock()
defer prl.mu.Unlock()
cutoff := clock.Now().Add(-60 * time.Second)
for ip, state := range prl.requests {
var valid []time.Time
for _, t := range state.timestamps {
if t.After(cutoff) {
valid = append(valid, t)
}
}
if len(valid) == 0 {
delete(prl.requests, ip)
} else {
state.timestamps = valid
}
}
}
var globalProgressiveLimiter = NewProgressiveRateLimiter()