PSR 2017 reg 100 makes SCA mandatory and non-waivable for customer-initiated stored-credential charges; a merchant-side 2FA check cannot legally substitute for it (authorising a token-less charge via 2FA leaves the MERCHANT liable for ECI 7 / SLI 210 chargebacks and reg 77(6) compensation regardless of consent). - payments/twofa.go: the homegrown 2FA fallback for token-less saved-card charges is REMOVED ENTIRELY. requireTwoFactorForCardAccess is now SCA-only: a non-empty Square verification_token (charge surfaces, token forwarded to Square) skips the gate; anything else is refused 402 verification_required. enforceSCAFallbackConsent is a compile-compatible no-op (fallback never runs). - New requireTwoFactorForCardAccessWithTokenValidation distinguishes surfaces where the token IS forwarded to Square (charge — Square validates it) from card-SAVE surfaces (token client-asserted, never forwarded: a non-empty token must NOT skip the save gate, auth-F1). - SCA tokenize-result wire contract (C1): a saved card charged with a fresh one-time tokenize-result sends the token as the charge SOURCE (new_card_token -> source_id) alongside saved_card_id, never a separate verification_token. resolveChargeSource resolves the saved-card branch FIRST (customer from the card row, token as source) so combined token+card requests are SCA-clean. - C6 consent fields (consent_version / consent_accepted) added to the booking/ tip/till/gift-card charge requests, enforced server-side before any fallback charge could reach Square and recorded on the 2fa_fallback_charge audit row; logVerificationTokenProvenance traces minted tokens to their charge. - user 2FA issuance gate refactored into pure build-agnostic functions (twoFAPepperConfigured / twoFADeliveryChannelConfigured / twoFAEnsureIssueAllowedStrict) shared with the payments re-issue path and exercised directly by the test,dev suite; TWO_FACTOR_FALLBACK switch and .env.example entry removed; startup posture notes updated. - Test coverage: fail-closed 2FA production gates (pepper/delivery), token validation on save vs charge surfaces, completion idempotency, idempotency key determinism, refund-policy 72h/24h epsilon boundaries, VAT parity.
98 lines
5.2 KiB
Go
98 lines
5.2 KiB
Go
//go:build !dev && !test
|
|
|
|
package user
|
|
|
|
// Production builds (neither the `dev` nor the `test` tag) must never persist
|
|
// an unsalted digest and must never write a 2FA code in plaintext by default:
|
|
// the plaintext [2FA] log delivery and the TWO_FACTOR_PEPPER fallback exist
|
|
// only in dev/test builds (twofa_dev.go). Here code issuance fails closed on
|
|
// BOTH missing configuration pieces:
|
|
//
|
|
// - a missing TWO_FACTOR_PEPPER (an unsalted digest in the 1M code space
|
|
// would be offline-brute-forceable from a log/DB leak), mirroring how
|
|
// main.go refuses to start without a strong JWT_SECRET_KEY; and
|
|
// - a missing delivery channel. The email/SMS transport is not wired yet
|
|
// (P6), so the ONLY production channel is the operator's explicit opt-in
|
|
// to the insecure log-delivery mode (TWO_FACTOR_ALLOW_LOG_DELIVERY=true).
|
|
// Without it, issuing a code would silently dead-end setup — the user
|
|
// could never receive the code and the enforced saved-card-payments gate
|
|
// would lock them out with no way forward. Issuance is refused and the
|
|
// handlers surface errTwoFADeliveryUnavailable ("2FA requires an email or
|
|
// SMS delivery channel; contact the salon").
|
|
//
|
|
// The plaintext code is therefore never written to the server log unless the
|
|
// operator explicitly opted into log delivery and accepted its risk.
|
|
|
|
import (
|
|
"crussell/internal/twofa"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
// twoFAAllowLogDeliveryEnv (the TWO_FACTOR_ALLOW_LOG_DELIVERY opt-in) and
|
|
// errTwoFAPepperRequired are defined in twofa.go — shared by the pure issuance
|
|
// gate (twoFAEnsureIssueAllowedStrict), which the test,dev suite exercises
|
|
// directly, and this production build.
|
|
|
|
// init registers the production pepper reader into the shared verification
|
|
// core (crussell/internal/twofa): raw env read, no fallback — code issuance
|
|
// fails closed via twoFAEnsureIssueAllowed, so no pending code is ever
|
|
// persisted as an unsalted SHA-256 digest.
|
|
func init() {
|
|
twofa.SetPepperProvider(func() string { return os.Getenv(twoFAPepperEnv) })
|
|
}
|
|
|
|
// twoFADeliveryAvailable reports whether a 2FA code delivery channel exists in
|
|
// this build. Production: true only when the operator explicitly opted into the
|
|
// insecure log-delivery mode (TWO_FACTOR_ALLOW_LOG_DELIVERY=true) or a real
|
|
// email/SMS transport is wired (not yet — P6). Default false: no channel, so
|
|
// code issuance is refused and setup surfaces errTwoFADeliveryUnavailable
|
|
// instead of a silent dead-end. Delegates to the pure build-agnostic
|
|
// twoFADeliveryChannelConfigured (twofa.go); dev/test builds always return true
|
|
// (twofa_dev.go).
|
|
func twoFADeliveryAvailable() bool {
|
|
return twoFADeliveryChannelConfigured()
|
|
}
|
|
|
|
// twoFAEnsureIssueAllowed reports whether a 2FA code may be issued in this
|
|
// deployment. Production requires BOTH a delivery channel and TWO_FACTOR_PEPPER:
|
|
// without a channel (no email/SMS, no TWO_FACTOR_ALLOW_LOG_DELIVERY=true) the
|
|
// code could never reach the user — issuing one would silently lock the user
|
|
// out of the enforced saved-card-payments gate; and without the pepper every
|
|
// stored code would be an offline-brute-forceable unsalted digest. Either way
|
|
// issuance is refused (fail-closed). Delegates to the pure build-agnostic gate
|
|
// twoFAEnsureIssueAllowedStrict (twofa.go), which the test,dev suite also
|
|
// exercises directly; dev/test builds always allow issuance (twofa_dev.go).
|
|
//
|
|
// The pepper check is the ONLY hard gate here (plus the delivery channel), and
|
|
// it is also the ONLY hard gate on the payments re-issue path
|
|
// (payments.twoFAReissueIssueAllowed). PEPPER-CHANGE HAZARD (Loop B finding 2):
|
|
// the pepper keys the HMAC-SHA256 of every stored pending-code hash, so
|
|
// CHANGING TWO_FACTOR_PEPPER invalidates ALL pending codes — every stored hash
|
|
// was computed with the old pepper and can never match a code minted under the
|
|
// new one. An operator who changes the pepper must re-mint every user's code
|
|
// (or have each user re-run 2FA setup), or enforced saved-card charges will
|
|
// strand customers with 400 ErrMissingOrExpired forever.
|
|
func twoFAEnsureIssueAllowed() error {
|
|
return twoFAEnsureIssueAllowedStrict()
|
|
}
|
|
|
|
// twoFADeliverCode delivers a fresh verification code to the user. Production
|
|
// has no wired email/SMS transport (P6), so the ONLY channel is the operator's
|
|
// explicit, insecure opt-in to log delivery (TWO_FACTOR_ALLOW_LOG_DELIVERY=true
|
|
// — anyone with backend log access could defeat the 2FA gate on saved-card
|
|
// charges). WITHOUT that flag the plaintext code is NEVER written to the log;
|
|
// twoFAEnsureIssueAllowed already refused issuance, so this no-op is
|
|
// unreachable. With the flag set, the code is written to the [2FA] log line
|
|
// and an operator relays it to the user out-of-band, exactly like the
|
|
// documented dev flow — the operator has accepted the risk of log-based
|
|
// delivery. MEDIUM-3b: the user id and the plaintext code go to SEPARATE log
|
|
// lines so a single record cannot trivially pair a code with its owner.
|
|
func twoFADeliverCode(userID, label, code string) {
|
|
if os.Getenv(twoFAAllowLogDeliveryEnv) == "true" {
|
|
log.Printf("[2FA] code delivery requested (user=%s, purpose=%s)", userID, label)
|
|
log.Printf("[2FA] code: %s", code)
|
|
}
|
|
// Otherwise: deliberate no-op — never log the plaintext code by default.
|
|
}
|