Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork21.8k
Fix: Add type validation for sendStatus to prevent BigInt serialization error (#6756)#6758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
…on error (expressjs#6756)- Add type checking in sendStatus() method to throw TypeError for non-number inputs- Prevents uncaught 'Do not know how to serialize a BigInt' error- Add test coverage for BigInt status code input- Maintains backward compatibility with existing error patternsFixesexpressjs#6756
lib/response.js Outdated
| res.sendStatus=functionsendStatus(statusCode){ | ||
| if(typeofstatusCode!=='number'){ | ||
| thrownewTypeError('Invalid status code: '+statusCode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
| thrownewTypeError('Invalid status code: '+statusCode); | |
| thrownewTypeError(`statusCode must be a valid number to res.sendStatus`); |
I think it seems consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
agree
bjohansebas left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This looks good, although this will be released for version 6 since it would be a breaking change, and we should first release a warning in version 5. Could you create a new PR to add a deprecation message?
Vedant224 commentedSep 18, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@bjohansebas I've created the deprecation warning PR as requested. I kept it targeting master since that shows my actual 2-file changes cleanly. When I tried changing the target to 5.x it showed 36 changes (difference between branches). Should I leave it targeting master and you'll handle getting it into Express v5, or would you prefer a different approach for the branch targeting? The deprecation warning PR is ready for review - it adds the deprecate() call for non-number values in sendStatus(). |
krzysdz commentedOct 29, 2025
I don't think that it would be a breaking change - 5.x already includes validation that was added in#4212. This probably could be documented better, because it's mentioned only for The problem with serialization occurs during creation of the validation error message (see#6756 (comment)). |
jonchurch left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We centralize our validation for status codes in the.status method whichsendStatus relies on.
Any changes to the validation should happen there.
jonchurch commentedDec 12, 2025
Also it's not an uncaught exception, we expect a throw for invalid status and it will be handled. The enhancement would be the error message change, which makes this a very low prio change IMO. |
wesleytodd left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Leaving as a comment since@jonchurch has a blocking review. But once those are changed consider this a ✅ from me.
lib/response.js Outdated
| res.sendStatus=functionsendStatus(statusCode){ | ||
| if(typeofstatusCode!=='number'){ | ||
| thrownewTypeError('Invalid status code: '+statusCode); | ||
| } |
wesleytoddDec 12, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Agreed with@jonchurch, move this tores.status, but throwing forBigInt is the right way to go.
test/res.sendStatus.js Outdated
| request(app) | ||
| .get('/') | ||
| .expect(500,/TypeError.*Invalidstatuscode/,done) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This test will need to move tores.status.js after the other change.
Vedant224 commentedDec 13, 2025
@jonchurch@wesleytodd I've updated the PR to address the feedback. |
Fixes#6756
This PR adds type validation to
res.sendStatus()to prevent uncaught TypeError when BigInt values are passed as status codes.Problem:
res.sendStatus(200n)caused uncaught"Do not know how to serialize a BigInt"errorsendStatus()calledthis.status()which internally usesJSON.stringify()Solution:
sendStatus()methodTypeError: Invalid status codefor non-number inputsChanges:
lib/response.jstest/res.sendStatus.jsTesting: