-
Notifications
You must be signed in to change notification settings - Fork 29k
[backport]: fix(next/image): fix image-optimizer.ts headers (#82114) #82175
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
+18
−1
Merged
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions
11
test/integration/image-optimizer/app/pages/api/conditional-cookie.js
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,11 @@ | ||
const pixel = | ||
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPj/HwADBwIAMCbHYQAAAABJRU5ErkJggg==' | ||
|
||
export default function handler(req, res) { | ||
if (req.headers['cookie']) { | ||
res.setHeader('content-type', 'image/png') | ||
res.end(Buffer.from(pixel, 'base64')) | ||
} else { | ||
res.status(401).end('cookie was not found') | ||
} | ||
} |
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.
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.
The error handling logic in
fetchInternalImage
only catches falsy status codes but ignores HTTP error status codes like 401, 404, or 500.View Details
Analysis
The condition
if (!mocked.res.statusCode)
on line 643 only handles cases where the status code is falsy (0, undefined, null), but it doesn't properly handle HTTP error status codes like 401, 404, or 500. When an API returns a 401 status code (as demonstrated by the new test), this condition evaluates to false (since 401 is truthy), and the function proceeds to treat the error response as valid image data.This causes the error to be handled later in the pipeline when
imageOptimizer
detects that the response isn't a valid image type, resulting in a generic "The requested resource isn't a valid image" error instead of properly propagating the original API error. While this accidentally produces the expected 400 status code in the test, it's semantically incorrect and produces misleading error messages.The logic should check for successful HTTP status codes (typically 2xx range) rather than just truthy/falsy values.
Recommendation
Change the condition on line 643 to properly handle HTTP error status codes. Replace:
with:
This will properly catch both missing status codes and HTTP error status codes (400+), allowing the function to throw an appropriate error with the actual status code and a more meaningful error message.