-
Notifications
You must be signed in to change notification settings - Fork 0
Handle Bad Content-Type Headers RESTfully
#39
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d8f3675
First pass at fixing Content-Type header processing.
thehabes cd98bc4
Changes while testing and reviewing
thehabes 909f655
Changes while testing and reviewing
thehabes 132f03f
Cleanup the DELETE stuff. DELETE requests don't do bodies.
thehabes 7bc4635
Cleanup the DELETE stuff. DELETE requests don't do bodies.
thehabes 0367c49
Cleanup the DELETE stuff. DELETE requests don't do bodies.
thehabes 074396d
Changes while reviewing and testing
thehabes dc7f5f7
Changes while reviewing and testing
thehabes b660ac3
Missing, blank, or null Content-Type headers are also 415
thehabes 4d129d4
reject content-type headers that have multiple or duplicate types
thehabes 10f5589
refactor how Content-Type headers are checked
thehabes 6541efc
refactor how Content-Type headers are checked
thehabes e3a9dc1
refactor how Content-Type headers are checked
thehabes 7364736
no shebang
thehabes 1ae3dba
changes while testing and reviewing
thehabes d36fe53
Changes while testing and reviewing
thehabes ab6cf16
Clean out dead index handler and router
thehabes 29e77aa
rename
thehabes 9f3eb77
Bring in content-type processing from lessons learned in rerum
thehabes 53a63b5
Handle success and error responses and send them through
thehabes 4dec674
changes during review
thehabes 4ebe895
changes during review
thehabes 50da757
changes during review
thehabes c3bda63
changes during review
thehabes c254e77
changes during review
thehabes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** | ||
| * Detects multiple MIME types smuggled into a single Content-Type header. | ||
| * The following are the cases that should result in a 415 (not a 500) | ||
|
|
||
| - application/json text/plain | ||
| - application/json, text/plain | ||
| - text/plain; application/json | ||
| - text/plain; a=b, application/json | ||
| - application/json; a=b; text/plain; | ||
| - application/json; a=b text/plain; | ||
| - application/json; charset=utf-8, text/plain | ||
| - application/json; | ||
|
|
||
| * @param {string} contentType - Lowercased Content-Type header value | ||
| * @returns {boolean} True if multiple MIME types are detected | ||
| */ | ||
| const hasMultipleContentTypes = (contentType) => { | ||
| const segments = contentType.split(";") | ||
| const mimeSegment = segments[0].trim() | ||
| // No commas or spaces allowed in MIME types | ||
| if (mimeSegment.includes(",") || mimeSegment.includes(" ")) return true | ||
| // Parameter values are tokens (no spaces/commas) or quoted strings per RFC 2045. | ||
| // Commas or spaces outside quotes indicate a smuggled MIME type. | ||
| return segments.slice(1).some(segment => { | ||
| const trimmed = segment.trim() | ||
| if (!trimmed.includes("=")) return true | ||
| const withoutQuoted = trimmed.replace(/"[^"]*"/g, "") | ||
| if (withoutQuoted.includes(",") || withoutQuoted.includes(" ")) return true | ||
| return false | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Middleware to verify Content-Type headers for endpoints receiving JSON bodies. | ||
| * Responds with a 415 Invalid Media Type for Content-Type headers that are not for JSON bodies. | ||
| * | ||
| * @param {Object} req - Express request object | ||
| * @param {Object} res - Express response object | ||
| * @param {Function} next - Express next middleware function | ||
| */ | ||
| const verifyJsonContentType = function (req, res, next) { | ||
| const contentType = (req.get("Content-Type") ?? "").toLowerCase() | ||
| const mimeType = contentType.split(";")[0].trim() | ||
| if (!mimeType) { | ||
| const err = new Error(`Missing or empty Content-Type header.`) | ||
| err.status = 415 | ||
| return next(err) | ||
| } | ||
| if (hasMultipleContentTypes(contentType)) { | ||
| const err = new Error(`Multiple Content-Type values are not allowed. Provide exactly one Content-Type header.`) | ||
| err.status = 415 | ||
| return next(err) | ||
| } | ||
| if (mimeType === "application/json" || mimeType === "application/ld+json") return next() | ||
| const err = new Error(`Unsupported Content-Type: ${contentType}. This endpoint requires application/json or application/ld+json.`) | ||
| err.status = 415 | ||
| return next(err) | ||
| } | ||
|
|
||
| export default { verifyJsonContentType } |
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
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
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
['application/json', 'application/ld+json']is duplicated here and again inALLOWED_CONTENT_TYPESbelow. To avoid drift (e.g., updating one list but not the other), consider defining the allowed list once and reusing it for bothexpress.json({ type: ... })and the validation middleware.