From 6b12c60c61ee80cb0443ccd20de82ca9b4422ddd Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 28 Jul 2025 09:06:33 -0400 Subject: [PATCH] fix(next/image): fix image-optimizer.ts headers (#82114) The headers were forwarded to the serverless function for "internal" images but not "external" images. This changes the behavior to be the same for both such that neither receive headers. --- packages/next/src/server/image-optimizer.ts | 1 - .../app/pages/api/conditional-cookie.js | 11 +++++++++++ test/integration/image-optimizer/test/util.ts | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/integration/image-optimizer/app/pages/api/conditional-cookie.js diff --git a/packages/next/src/server/image-optimizer.ts b/packages/next/src/server/image-optimizer.ts index aef6ed227f879..0b69e5b3fdd8b 100644 --- a/packages/next/src/server/image-optimizer.ts +++ b/packages/next/src/server/image-optimizer.ts @@ -634,7 +634,6 @@ export async function fetchInternalImage( const mocked = createRequestResponseMocks({ url: href, method: _req.method || 'GET', - headers: _req.headers, socket: _req.socket, }) diff --git a/test/integration/image-optimizer/app/pages/api/conditional-cookie.js b/test/integration/image-optimizer/app/pages/api/conditional-cookie.js new file mode 100644 index 0000000000000..f67ce6872504c --- /dev/null +++ b/test/integration/image-optimizer/app/pages/api/conditional-cookie.js @@ -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') + } +} diff --git a/test/integration/image-optimizer/test/util.ts b/test/integration/image-optimizer/test/util.ts index 2a8c43431e4fb..559222a5398ef 100644 --- a/test/integration/image-optimizer/test/util.ts +++ b/test/integration/image-optimizer/test/util.ts @@ -308,6 +308,13 @@ export function runTests(ctx: RunTestsCtx) { expect(ctx.nextOutput).toContain(animatedWarnText) }) + it('should not forward cookie header', async () => { + const query = { w: ctx.w, q: 30, url: '/api/conditional-cookie' } + const opts = { headers: { accept: 'image/webp', cookie: '1' } } + const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) + expect(res.status).toBe(400) + }) + if (ctx.nextConfigImages?.dangerouslyAllowSVG) { it('should maintain vector svg', async () => { const query = { w: ctx.w, q: 90, url: '/test.svg' }