Skip to content
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

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

Merged
merged 6 commits into from
Aug 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
@@ -716,9 +716,27 @@ export default class NextNodeServer extends BaseServer {
const params = getPathMatch('/_next/data/:path*')(parsedUrl.pathname)

// ignore for non-next data URLs
if (!params || !params.path || params.path[0] !== this.buildId) {
return { finished: false }
if (!params || !params.path) {
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
params.path.shift()

Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import glob from 'glob'
import fs from 'fs-extra'
import cheerio from 'cheerio'
import { join } from 'path'
import { nanoid } from 'nanoid'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import {
@@ -1319,4 +1320,19 @@ describe('should set-up next', () => {
expect(resImageResponse.status).toBe(200)
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)
})
})