Files
Crussell/nginx/conf.d/default.conf
T
popertots a8d54f1e2a Fix review findings: aggregated-refund/saved-card/legacy-refund idempotency keys, structured Square error classification, CSP for Square SDK
Money-safety idempotency fixes (external review bugs 1-3):
- processChargeGroup: aggregated refund key now hashes the sorted pending-row
  set (chargeID-square-agg-<sha256 suffix>) so a changed group can never mark
  a new row completed against an old smaller refund; >45-char chargeIDs use a
  hashed prefix instead of verbatim truncation (which would collide charges on
  Square's global key dedup). Same-set crash-retry keeps Square's dedup.
- CreateTerminalPayment saved_card: two-tier idempotency key — client-supplied
  per-attempt UUID preferred (distinct identical charges no longer collapse),
  deterministic booking+type+amount+card fallback for no-key retry safety.
  PaymentModal sends a per-charge UUID cleared after success.
- ensureRefundKey: legacy NULL-key manual refunds persist a generated key to
  the row BEFORE the Square call (race-safe AND idempotency_key IS NULL guard),
  so a lost-response retry reuses the key and never double-refunds. Wired into
  resumeManualPendingRefund and the sweep's manual-retry loop.

Classification + money-safety hardening:
- till.go/sweep.go: structured square.ErrorCode/IsNotFound are authoritative
  when present; message-substring matching only for non-structured errors
  (dev mock, client-side status errors). Fixes fragile string-matching driving
  sweep retries and gift-card clawbacks.
- SaveCardForUser: ON CONFLICT (user_id, square_card_id) DO NOTHING + re-select
  (was a latent UNIQUE-violation 500 on save-card retry).
- CreateBookingPayment: partial payments re-validated against remaining balance
  inside the advisory lock (closes concurrent-overpayment race).
- InvalidateSquareCustomerCache on GDPR erasure paths (account.go,
  time-blockers.go stale-guest anonymization).
- GetUserGiftCardBalanceAdmin: in-handler admin check (defense-in-depth).
- getCheckoutHTTP: warn on multi-payment checkouts instead of dropping
  payments[1:].
- Cash/giftcard terminal branch: removed dead idempotency SELECT, "tip-" ->
  "till-" prefix.
- UserPaymentModal: removed vestigial polling state; proper interval cleanup.
- account/+page.svelte: gift-card redeem dialog links /terms.
- nginx CSP: allow *.squarecdn.com and js.squareup.com so the Square Web
  Payments SDK + card iframe can tokenize behind the proxy.

Tests: +8 regression tests covering changed-set refund keys, legacy NULL-key
single-refund, saved-card client-key dedup/no-dedup, concurrent partials, and
cache invalidation. Full suite + race detector clean via run-tests.sh lockfile.
2026-08-22 00:34:49 +01:00

111 lines
4.3 KiB
Plaintext

# Define cache for API responses
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=api_cache:10m max_size=100m inactive=60m use_temp_path=off;
# Rate limiting (per IP) — must be at http level, not inside server block
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=20r/m;
limit_req_zone $binary_remote_addr zone=dav_limit:10m rate=100r/m;
server {
listen 80;
listen 443 ssl http2;
server_name _;
# TLS certs (you'll mount them into /etc/nginx/certs)
ssl_certificate /etc/nginx/certs/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/privkey.pem;
# Security headers
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
# Square Web Payments SDK: script from *.squarecdn.com, card-entry iframe
# from js.squareup.com (frame-src; without it the payment form cannot
# tokenize behind this proxy). connect-src allows the SDK's own network calls.
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://*.squarecdn.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://*.squareup.com https://*.squarecdn.com; frame-src https://js.squareup.com https://*.squareup.com; frame-ancestors 'none';" always;
# Serve static frontend
root /usr/share/nginx/html;
index index.html;
# Cache immutable assets aggressively
location ~ ^/_app/immutable/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# Normal frontend routes (SPA fallback)
location / {
try_files $uri /index.html;
}
# Proxy API requests to backend
location /api/ {
limit_req zone=api_limit burst=5 nodelay;
proxy_pass http://backend:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Optional lightweight caching for API responses
proxy_cache api_cache;
proxy_cache_valid 200 1m;
proxy_cache_valid any 10s;
}
# SabreDAV - CardDAV and CalDAV
location /dav/ {
limit_req zone=dav_limit burst=20 nodelay;
# Important: rewrite to remove /dav prefix for PHP processing
rewrite ^/dav/(.*)$ /server.php/$1 break;
# Pass to PHP-FPM in sabredav container
fastcgi_pass sabredav:9000;
fastcgi_index server.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/dav/server.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param REQUEST_URI $request_uri;
# Required for DAV
fastcgi_param HTTPS $https if_not_empty;
fastcgi_read_timeout 300;
fastcgi_buffering off;
# Disable caching for DAV
proxy_cache off;
add_header Cache-Control "no-store, no-cache, must-revalidate";
# Allow DAV methods
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '$http_origin';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PROPFIND, PROPPATCH, REPORT, MKCOL, MOVE, COPY';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-None-Match,If-Modified-Since,Cache-Control,Content-Type,Range,Depth,Authorization,If-Match,Destination,Overwrite,Lock-Token,Timeout';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
# Remove security headers that interfere with DAV
add_header X-Content-Type-Options "" always;
add_header X-Frame-Options "" always;
add_header X-XSS-Protection "" always;
}
# Legacy CardDAV endpoint (backward compatibility)
location /carddav/ {
return 301 $scheme://$host/dav/addressbooks$request_uri;
}
# Legacy CalDAV endpoint (backward compatibility)
location /caldav/ {
return 301 $scheme://$host/dav/calendars$request_uri;
}
}