Repository navigation
Conversation
`type-is` throws a `TypeError` ("argument string is required") for a
Content-Type header with an empty media type, such as `;charset=utf-8`.
`Request#is()` let that error escape, so:
- `TokenHandler#handle()` rejected with the raw `TypeError` because the
content-type check runs before its error handling, and
- `AuthenticateHandler#handle()` turned it into a `ServerError` (503) when
the access token was sent in the request body.
`Request#is()` now treats a Content-Type that cannot be parsed as not
matching, so both handlers reject the request with an
`InvalidRequestError` (400) like any other wrong content type.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The localized fix preserves successful matching and includes targeted regression coverage, with no unresolved findings.
0 open findings
What changed in this PR
Makes malformed Content-Type headers follow existing OAuth validation paths, producing invalid_request instead of unexpected errors.
Changes:
- Returns
falsewhen Content-Type parsing throws inRequest#is(). - Adds regression coverage for request matching and both affected handlers.
| File | Description |
|---|---|
| test/integration/request_test.js | Tests malformed Content-Type matching. |
| test/integration/handlers/token-handler_test.js | Tests token endpoint rejection. |
| test/integration/handlers/authenticate-handler_test.js | Tests body-token validation rejection. |
| lib/request.js | Converts parsing exceptions into non-matches. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This branch has not been deployed
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.
Summary
A request with a
Content-Typeheader whose media type is empty, such as;charset=utf-8, makestype-is(2.1.0) throwTypeError: argument string is required.Request#is()lets that error escape, so:TokenHandler#handle()rejects with the rawTypeErrorand not an OAuth error. Itsrequest.is('application/x-www-form-urlencoded')check runs before thetryblock, so the error is never normalised.AuthenticateHandler#handle(), when the access token is sent in the body, wraps theTypeErrorin aServerError(503) for what is a client error.This PR makes
Request#is()treat a Content-Type thattype-iscannot parse as not matching (false). That is already how it treats a missing or otherwise invalid type. Both handlers now reject the request withInvalidRequestError(400):Invalid request: content must be application/x-www-form-urlencoded, the same as for any other wrong content type.I fixed it in
Request#is(), not by moving theTokenHandlercheck into itstryblock. Moving the check would only turn the rawTypeErrorinto a 503, and it would leaveAuthenticateHandlerunfixed.Linked issue(s)
None. Found while reviewing other open PRs.
Involved parts of the project
lib/request.js:Request#is()TokenHandler) and bearer-token-in-body authentication (AuthenticateHandler)Added tests?
Yes. Each of these fails on
masterwithTypeError: argument string is requiredand passes here:Request integration › is() › should return false if the content type is malformedTokenHandler integration › handle() › should throw an error if the media type is malformedAuthenticateHandler integration › getTokenFromRequestBody() › should throw an error if the media type is malformedbiome check .is clean and the full mocha suite passes (467).OAuth2 standard
RFC 6749 §3.2 requires
application/x-www-form-urlencodedfor token requests, and RFC 6750 §2.2 requires it for bearer tokens in the body. A malformed Content-Type is a malformed request, so it getsinvalid_request(RFC 6749 §5.2 / RFC 6750 §3.1), not a server error.Reproduction
🤖 Generated with Claude Code