fix: loop-B adversarial findings — tip-type double-charge, tip-refund capacity, loyalty stamp farming, gate ordering, auth amplification, admin audit log

Loop B restart (money/security/dup-mod adversarial) fixes:
- CRITICAL: CreateTerminalPayment rejects payment_type='tip' (mirrors CreateBookingPayment) — a tip-typed admin charge no longer records the FULL amount as a tip and double-collects (all is-paid computations exclude tip rows)
- HIGH: tip refunds can no longer re-open booking capacity — refunded_total subqueries filter payment_type <> 'tip' (service.go) and RefundPayment rejects tip rows
- MEDIUM: loyalty-stamp farming closed — stamp award once-per-booking via loyalty_stamp_awarded_at column (init-script.sql) + existing same-day guard
- MEDIUM: CreateTipPayment/CreateBookingPayment 2FA gates moved AFTER the idempotency completed-dedup (code consumed only on new money paths; terminal path already correct) — lost-response retries return the completed payment instead of 400
- MEDIUM: replayRescueLowerBoundSkew widened to 5m (DB-clock-skew stranded originals now rescued)
- MEDIUM-1: verifyFamilyAlive DB amplification reduced via 30s bounded family-alive cache; admin route group rate-limited
- MEDIUM-3: admin saved-card charges now write admin_audit_log (handlers.go helper + till); [2FA] log line decoupled from user identity
- LOW-1: logout scoped to the presented token's family (no cross-session kill)
- LOW-2: refresh-reuse grace widened for same-IP replays
- LOW-4: squareEnvironmentMismatch enforced for empty env
- LOW-5: uuid.ts hard-fails on Math.random fallback (crypto.randomUUID)
- Cash/giftcard tip-enabled overflow mirrors the card-terminal carve

26/26 backend packages; 72/72 frontend tests + build; env-docs 41/41.
This commit is contained in:
2026-08-22 00:34:50 +01:00
parent 03d85c6d13
commit 7c424b28b8
23 changed files with 1151 additions and 225 deletions
+11 -21
View File
@@ -1,27 +1,17 @@
/**
* Generate a UUID v4 string.
* Generate a UUID v4 string using `crypto.randomUUID()`.
*
* Uses `window.crypto.getRandomValues()` when available (secure context),
* falls back to `Math.random()` for non-secure contexts (e.g. plain HTTP).
*
* Safe for all environments — does NOT rely on `crypto.randomUUID()`
* which requires a secure context (HTTPS).
* The app is HTTPS-only (secure context), so `crypto.randomUUID()` is always
* available in the browser and in Node 19+ (vitest/dev server). Idempotency
* keys derived here gate Square dedup — they must be unpredictable, so this
* HARD-FAILS (throws) rather than silently falling back to `Math.random()` in
* a non-secure context (LOW-5). No fallback is ever used.
*/
export function generateUUID(): string {
const array = new Uint8Array(16);
if (typeof window !== 'undefined' && window.crypto?.getRandomValues) {
window.crypto.getRandomValues(array);
} else {
for (let i = 0; i < 16; i++) array[i] = Math.floor(Math.random() * 256);
if (typeof globalThis.crypto?.randomUUID !== 'function') {
throw new Error(
'generateUUID requires crypto.randomUUID() (secure context). The app is HTTPS-only; refusing to fall back to Math.random().'
);
}
// UUID v4 marker and variant
array[6] = (array[6] & 0x0f) | 0x40;
array[8] = (array[8] & 0x3f) | 0x80;
return [...array]
.map((b, i) => {
const hex = b.toString(16).padStart(2, '0');
if (i === 4 || i === 6 || i === 8 || i === 10) return '-' + hex;
return hex;
})
.join('');
return globalThis.crypto.randomUUID();
}