-
Notifications
You must be signed in to change notification settings - Fork 317
fix: sanitize utils and sanitize all response code and messages #358
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 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c2419a4
feat: sanetize utils and sanetize all response code and messages
pi0 4e89ece
add leftover
pi0 64524be
first param is required for sanetizeStatusCode
pi0 700b8c2
fix typo
pi0 eee34ae
rename regex name
pi0 2915885
rename file too
pi0 105144e
warn on error statusMessage sanitization
pi0 6c4aded
update warn
pi0 9347482
Update src/error.ts
pi0 56db7f7
make soft warn
pi0 a2676d0
update message
pi0 9c60b9e
Update src/error.ts
pi0 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| import type { H3Event } from "./event"; | ||
| import { MIMES, setResponseStatus } from "./utils"; | ||
| import { | ||
| MIMES, | ||
| setResponseStatus, | ||
| sanitizeStatusMessage, | ||
| sanitizeStatusCode, | ||
| } from "./utils"; | ||
|
|
||
| /** | ||
| * H3 Runtime Error | ||
|
|
@@ -20,12 +25,12 @@ export class H3Error extends Error { | |
| H3Error, | ||
| "message" | "statusCode" | "statusMessage" | "data" | ||
| > = { | ||
| message: this.message, | ||
| statusCode: this.statusCode, | ||
| message: sanitizeStatusMessage(this.message), | ||
| statusCode: sanitizeStatusCode(this.statusCode, 500), | ||
| }; | ||
|
|
||
| if (this.statusMessage) { | ||
| obj.statusMessage = this.statusMessage; | ||
| obj.statusMessage = sanitizeStatusMessage(this.statusMessage); | ||
| } | ||
| if (this.data !== undefined) { | ||
| obj.data = this.data; | ||
|
|
@@ -83,15 +88,24 @@ export function createError( | |
| } | ||
|
|
||
| if (input.statusCode) { | ||
| err.statusCode = input.statusCode; | ||
| err.statusCode = sanitizeStatusCode(input.statusCode, err.statusCode); | ||
| } else if (input.status) { | ||
| err.statusCode = input.status; | ||
| err.statusCode = sanitizeStatusCode(input.status, err.statusCode); | ||
| } | ||
| if (input.statusMessage) { | ||
| err.statusMessage = input.statusMessage; | ||
| } else if (input.statusText) { | ||
| err.statusMessage = input.statusText; | ||
| } | ||
| if (err.statusMessage) { | ||
| const originalMessage = err.statusMessage; | ||
| err.statusMessage = sanitizeStatusMessage(err.statusMessage); | ||
| if (err.statusMessage !== originalMessage) { | ||
| console.warn( | ||
| "[h3] `statusMessage` has been sanitized. Please prefer using `message` for longer messages." | ||
| ); | ||
| } | ||
|
Comment on lines
+101
to
+108
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks great to me 👌 |
||
| } | ||
|
|
||
| if (input.fatal !== undefined) { | ||
| err.fatal = input.fatal; | ||
|
|
||
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 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 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,23 @@ | ||
| // Allowed characters: horizontal tabs, spaces or visible ascii characters: https://www.rfc-editor.org/rfc/rfc7230#section-3.1.2 | ||
| // eslint-disable-next-line no-control-regex | ||
| const DISALLOWED_STATUS_CHARS = /[^\u0009\u0020-\u007E]/g; | ||
|
|
||
| export function sanitizeStatusMessage(statusMessage = ""): string { | ||
| return statusMessage.replace(DISALLOWED_STATUS_CHARS, ""); | ||
| } | ||
|
|
||
| export function sanitizeStatusCode( | ||
| statusCode: string | number, | ||
| defaultStatusCode = 200 | ||
| ): number { | ||
| if (!statusCode) { | ||
| return defaultStatusCode; | ||
| } | ||
| if (typeof statusCode === "string") { | ||
| statusCode = Number.parseInt(statusCode, 10); | ||
| } | ||
| if (statusCode < 100 || statusCode > 999) { | ||
| return defaultStatusCode; | ||
| } | ||
| return statusCode; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.