-
-
Notifications
You must be signed in to change notification settings - Fork 3
Prevents setting headers multiple times #552
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| export default (logger, validateResponse) => (context, request, response) => { | ||
| // Prevent sending headers if they're already sent | ||
| if (response.headersSent) { | ||
| return response.end() | ||
| } | ||
|
||
|
|
||
| const responseDoesntNeedValidation = response.statusCode >= 400 | ||
| if (responseDoesntNeedValidation) { | ||
| return response.json(context.response) | ||
|
|
@@ -22,12 +27,15 @@ export default (logger, validateResponse) => (context, request, response) => { | |
| response: context.response | ||
| }) | ||
| } | ||
| return response.status(502).json({ | ||
| errors: valid.errors, | ||
| status: 502, | ||
| timestamp: new Date(), | ||
| message: 'Bad response' | ||
| }) | ||
| if (!response.headersSent) { | ||
| return response.status(502).json({ | ||
| errors: valid.errors, | ||
| status: 502, | ||
| timestamp: new Date(), | ||
| message: 'Bad response' | ||
| }) | ||
| } | ||
| return response.end() | ||
| } | ||
|
Comment on lines
+30
to
39
|
||
|
|
||
| if (!context.response) { | ||
|
|
||
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 early return when headers are already sent is inconsistent with the pattern used in other handlers (unauthorized.js, request-validation.js, not-found.js), where the handler still returns a response object even if headers were already sent - they just skip setting the status code. Returning undefined here might prevent downstream code from understanding what should have been returned. Consider removing this early return and instead checking headersSent before each response.json/response.send/response.end call, which would allow the handler to still process and validate the response while avoiding sending headers twice.
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.
@copilot open a new pull request to apply changes based on this feedback