Files
Crussell/backend/mw/ratelimit_shared.go
T
popertots 78e6d00dc5 fix: payments review rounds — money-safety, GDPR, security, gift-card cancel, modal stacking
Money-safety:
- Deterministic till idempotency fallback (Square-charging only); cash/on_the_house keep unique keys; £250 till gift-card cap; 45-char key validation
- Gift-card admin caps £250/tx + £5,000/day; user buy £500/day; BuyGiftCard allowlist unchanged
- CancelGiftCard: CCR 2013 14-day right with partial-spend refund of the unspent balance (spend verified via payments.gift_card_id); atomic vs redeem/transfer; refunds stay pending until reversal commits; admin cancel surface (AdminCancelGiftCard)
- Sweep: cancelled-booking charges failed+notified instead of silently completed; source-override replay uses live square_source_id; legacy square-less refund sweep; snapshot refresh on pending reuse
- Refund lock consolidation; recordTerminalPaymentTx shared recorder; structured Square error codes; terminal checkout CustomerID

GDPR / security:
- Notes retained as de-identified medical/safety record at erasure (single field treated as health data; rest of record wiped, no re-identification map) + comments updated per UK GDPR/Art 9/Equality Act 2010
- square_request_snapshot PII scrubbed on all erasure paths; delete_guest_user FK unlinks; verification codes + dispute reasons handled; idle/stale-guest erasure deletes Square cards/customers + CardDAV/R2
- Durable square-erasure outbox job (retry-square-erasures); 2FA dev/prod build split, pepper fail-closed, no prod code-in-log; prod 2FA delivery fail-loud without a channel
- Webhook unknown-type family split (non-money acked, money retried); untracked dispute notifications; rate-limit CF/X-Real-IP trust gating; nginx CSP nonce + api_limit

Frontend:
- Dynamic z-index stack (ui/dialog/zindex.ts) claimed in open order via data-state observer; re-claims on every reopen; removes stale !z-* overrides — nested modals (booking→user→booking) always paint newest-on-top (browser-verified 3-level + reopen)
- Mobile: iOS zoom fixes, bottom-sheet dialogs, 44px touch targets, inputmode decimal, dvh
- Gift-card buy/cancel UI, admin £250 + daily limits, cancellation/privacy/terms policy accuracy

S3:
- Connect() creates buckets before probing; in-memory fallback only on genuine unreachability; health reports degraded; stale S3_PUBLIC_URL documented (host-specific)

Tests/docs:
- 2263 test functions; all 22 backend packages green; round8/9/10 regression suites; NextEditWindowTime removes wall-clock flake; docs reconciled (notes retention, gift-card partial-use, modal T15 future work)
2026-08-22 00:34:50 +01:00

139 lines
4.3 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"
"os"
"strconv"
"sync"
"time"
)
// 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 }
// 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()