Unverified Commit 26c19b1f authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

return a 404 when `/next/_data` build IDs are mismatched (#53596)

`normalizeNextData` in `next-server` used to exit early with a 404 when there was a build ID mismatch, but a recent refactor accidentally removed this functionality. This PR updates it to behave similarly to [web-server](https://github.com/vercel/next.js/blob/canary/packages/next/src/server/web-server.ts#L180-L187) where we render404 if there's a mismatch.

[slack x-ref](https://vercel.slack.com/archives/C03S8ED1DKM/p1691111541353749)
parent b7aa7d4a
...@@ -716,9 +716,27 @@ export default class NextNodeServer extends BaseServer { ...@@ -716,9 +716,27 @@ export default class NextNodeServer extends BaseServer {
const params = getPathMatch('/_next/data/:path*')(parsedUrl.pathname) const params = getPathMatch('/_next/data/:path*')(parsedUrl.pathname)
// ignore for non-next data URLs // ignore for non-next data URLs
if (!params || !params.path || params.path[0] !== this.buildId) { if (!params || !params.path) {
return { finished: false } return {
finished: false,
}
} }
if (params.path[0] !== this.buildId) {
// ignore if its a middleware request
if (req.headers['x-middleware-invoke']) {
return {
finished: false,
}
}
// Make sure to 404 if the buildId isn't correct
await this.render404(req, res, parsedUrl)
return {
finished: true,
}
}
// remove buildId from URL // remove buildId from URL
params.path.shift() params.path.shift()
......
...@@ -2,6 +2,7 @@ import glob from 'glob' ...@@ -2,6 +2,7 @@ import glob from 'glob'
import fs from 'fs-extra' import fs from 'fs-extra'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
import { nanoid } from 'nanoid'
import { createNext, FileRef } from 'e2e-utils' import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
import { import {
...@@ -1319,4 +1320,19 @@ describe('should set-up next', () => { ...@@ -1319,4 +1320,19 @@ describe('should set-up next', () => {
expect(resImageResponse.status).toBe(200) expect(resImageResponse.status).toBe(200)
expect(resImageResponse.headers.get('content-type')).toBe('image/png') expect(resImageResponse.headers.get('content-type')).toBe('image/png')
}) })
it('should correctly handle a mismatch in buildIds when normalizing next data', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${nanoid()}/index.json`,
undefined,
{
headers: {
'x-matched-path': '/[teamSlug]/[project]/[id]/[suffix]',
},
}
)
expect(res.status).toBe(404)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment