UID2-7292: prevent optout crash on malformed payload#189
Merged
Conversation
A throw inside the crypto.scrypt native callback in decrypt() escaped the Promise, and a ZodError from the async handler's parse() became an unhandled rejection -- either could take the process down. Fix both at source: synchronous IV-length check + try/catch around the decipher setup + 'error' listener on the decipher stream; parse() wrapped in try/catch returning 400 via next(createError(400)). Also add a diagnostic-only unhandledRejection listener logging via errorLogger so any future regression surfaces in monitoring. No uncaughtException handler -- Node's docs warn that resuming after one leaves the process in undefined state; the k8s pod supervisor restarts cleanly instead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
sunnywu
approved these changes
Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A malformed payload to
POST /(step=optout_submit) was crashing the Node process. Two distinct paths could escape as unhandled rejections / uncaught exceptions:decrypt()insrc/routes/encryption.ts.crypto.scrypt's native callback contained synchronous code (createDecipheriv,decipher.write,decipher.end) sitting outside the Promise'stryscope. A throw from any of those — most notablycreateDecipherivrejecting an IV of the wrong length — escaped the Promise as an uncaught exception. The decipher stream's'error'event (bad padding from a wrong key or junk ciphertext) was also unobserved.handleOptoutSubmitinsrc/routes/index.ts.OptoutSubmitRequest.parse(req.body)threw aZodErrordirectly inside the async route handler. With Express 4 having no native async-error forwarding, that throw became an unhandled rejection.What changed
src/routes/encryption.ts!== 16) synchronously before entering thenew Promise(...)body, so the error surfaces as a normal Promise rejection the caller cantry/catch.createDecipheriv+ stream setup inside thescryptcallback intry/catch, routing any throw throughreject.'error'event so async failures (bad padding) also reject the Promise instead of going unobserved.src/routes/index.tsOptoutSubmitRequest.parse(req.body)intry/catch; on failure, log viaerrorLoggerand respond with 400 vianext(createError(400)).src/bin/www.tsunhandledRejectionlistener that logs viaerrorLogger(so any future regression at least surfaces in monitoring).uncaughtExceptionhandler. Node's docs are explicit that resuming after one leaves the process in undefined state; the pod supervisor (k8s) is the right restart mechanism.Follow-up (separate PR)
The file-level try/catch +
next(createError(400))pattern is now duplicated three times insrc/routes/index.ts(email_prompt, optout_submit, defaultRouteHandler). The right idiomatic fix is a centralized Express error middleware + a smallvalidateBody(schema)middleware (and ideally an Express 4 → 5 upgrade so async handler rejections forward natively). Out of scope for a crash-fix PR; will follow up.Test plan
yarn build) and lint passyarn testpasses locally/→ 400, no crash{ step: 'optout_submit', encrypted: 'garbage' }(wrong format) → friendly error page, no crash'error'logged