Unverified Commit a6d950a4 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix static asset skew protection for edge and prerenders (#90238)

- Edge Runtime was missing `globalThis.NEXT_CLIENT_ASSET_SUFFIX` at runtime
- Prerenders were always running `globalThis.NEXT_CLIENT_ASSET_SUFFIX` set

Fixups after #88828
parent d7732c57
......@@ -241,7 +241,7 @@ pub async fn get_edge_chunking_context_with_client_assets(
)
.asset_base_path(Some(asset_prefix))
.default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred,
suffix: AssetSuffix::FromGlobal(rcstr!("NEXT_CLIENT_ASSET_SUFFIX")),
static_suffix: css_url_suffix.to_resolved().await?,
})
.minify_type(if *turbo_minify.await? {
......@@ -329,7 +329,7 @@ pub async fn get_edge_chunking_context(
)
.default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred,
static_suffix: css_url_suffix,
static_suffix: ResolvedVc::cell(None),
})
// Since one can't read files in edge directly, any asset need to be fetched
// instead. This special blob url is handled by the custom fetch
......
......@@ -1053,7 +1053,7 @@ pub async fn get_server_chunking_context_with_client_assets(
)
.default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred,
static_suffix: css_url_suffix,
static_suffix: ResolvedVc::cell(None),
})
.minify_type(if *minify.await? {
MinifyType::Minify {
......@@ -1152,7 +1152,7 @@ pub async fn get_server_chunking_context(
)
.default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred,
static_suffix: css_url_suffix,
static_suffix: ResolvedVc::cell(None),
})
.minify_type(if *minify.await? {
MinifyType::Minify {
......
......@@ -840,15 +840,21 @@ export function createStaticWorker(
isolatedMemory: true,
enableWorkerThreads: config.experimental.workerThreads,
exposedMethods: staticWorkerExposedMethods,
forkOptions: process.env.NEXT_CPU_PROF
? {
env: {
NEXT_CPU_PROF: '1',
NEXT_CPU_PROF_DIR: process.env.NEXT_CPU_PROF_DIR,
__NEXT_PRIVATE_CPU_PROFILE: 'build-static-worker',
},
}
: undefined,
forkOptions: {
env: {
...(process.env.NEXT_CPU_PROF
? {
NEXT_CPU_PROF: '1',
NEXT_CPU_PROF_DIR: process.env.NEXT_CPU_PROF_DIR,
__NEXT_PRIVATE_CPU_PROFILE: 'build-static-worker',
}
: undefined),
// worker.ts copies this value into globalThis.NEXT_CLIENT_ASSET_SUFFIX
__NEXT_PRERENDER_CLIENT_ASSET_SUFFIX: config.deploymentId
? `?dpl=${config.deploymentId}`
: '',
},
},
}) as StaticWorker
}
......@@ -2068,6 +2074,7 @@ export default async function build(
pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife,
buildId,
deploymentId: config.deploymentId,
sriEnabled,
cacheMaxMemorySize: config.cacheMaxMemorySize,
})
......@@ -2290,6 +2297,7 @@ export default async function build(
pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife,
buildId,
deploymentId: config.deploymentId,
sriEnabled,
})
}
......
......@@ -757,6 +757,7 @@ export async function isPageStatic({
cacheLifeProfiles,
pprConfig,
buildId,
deploymentId,
sriEnabled,
}: {
dir: string
......@@ -783,6 +784,7 @@ export async function isPageStatic({
nextConfigOutput: 'standalone' | 'export' | undefined
pprConfig: ExperimentalPPRConfig | undefined
buildId: string
deploymentId: string
sriEnabled: boolean
}): Promise<PageIsStaticResult> {
// Skip page data collection for synthetic _global-error routes
......@@ -836,6 +838,7 @@ export async function isPageStatic({
name: edgeInfo.name,
useCache: true,
distDir,
deploymentId,
})
const mod = (
await runtime.context._ENTRIES[`middleware_${edgeInfo.name}`]
......
......@@ -2,6 +2,10 @@ import '../server/require-hook'
// Import cpu-profile to start profiling early if enabled
import '../server/lib/cpu-profile'
// Set the global asset suffix for Turbopack compiled code to use during prerendering
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX =
process.env.__NEXT_PRERENDER_CLIENT_ASSET_SUFFIX
export {
getDefinedNamedExports,
hasCustomGetInitialProps,
......
......@@ -343,14 +343,8 @@ export async function exportPages(
nextConfig,
options,
renderResumeDataCachesByPage = {},
deploymentId,
} = input
// Set the global asset suffix for Turbopack compiled code to use during prerendering
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX = deploymentId
? `?dpl=${deploymentId}`
: ''
installGlobalBehaviors(nextConfig)
if (nextConfig.enablePrerenderSourceMaps) {
......
......@@ -1717,6 +1717,7 @@ export default class NextNodeServer extends BaseServer<
request: requestData,
useCache: true,
onWarning: params.onWarning,
deploymentId: this.deploymentId,
})
}
......@@ -2014,6 +2015,7 @@ export default class NextNodeServer extends BaseServer<
params.req,
'serverComponentsHmrCache'
),
deploymentId: this.deploymentId,
})
if (result.fetchMetrics) {
......
......@@ -37,6 +37,7 @@ interface RunnerFnParams {
distDir: string
incrementalCache?: any
serverComponentsHmrCache?: ServerComponentsHmrCache
deploymentId: string
}
type RunnerFn = (params: RunnerFnParams) => Promise<FetchEventResult>
......@@ -96,6 +97,12 @@ export async function getRuntimeContext(
params.serverComponentsHmrCache
}
if (params.deploymentId) {
runtime.context.globalThis.NEXT_CLIENT_ASSET_SUFFIX = params.deploymentId
? `?dpl=${params.deploymentId}`
: ''
}
for (const paramPath of params.paths) {
evaluateInContext(paramPath)
}
......
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export function GET() {
return Response.json({ imported, url })
}
'use client'
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export default function Page() {
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
import { headers } from 'next/headers'
export default async function Layout({ children }) {
// Use headers() to opt into dynamic rendering
await headers()
return children
}
'use client'
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export default function Page() {
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
import { headers } from 'next/headers'
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export default async function Page() {
// Use headers() to opt into dynamic rendering
await headers()
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import imported from '../public/test.png'
const url = new URL('../public/test.png', import.meta.url).toString()
export default function Page() {
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
import { nextTestSetup } from 'e2e-utils'
// Skip for webpack - dpl suffix in asset URLs is only implemented for Turbopack
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'URL asset references with deploymentId',
() => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
const deploymentId = 'test-deployment-id'
describe('import src attribute', () => {
it('should include dpl query in RSC page', async () => {
const $ = await next.render$('/')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page', async () => {
const $ = await next.render$('/client')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page (after hydration)', async () => {
const browser = await next.browser('/client')
const url = await browser.elementByCss('#imported-src').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('new URL() pattern', () => {
it('should include dpl query in RSC page', async () => {
const $ = await next.render$('/')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page', async () => {
const $ = await next.render$('/client')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page (after hydration)', async () => {
const browser = await next.browser('/client')
const url = await browser.elementByCss('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('dynamic RSC page (headers)', () => {
it('should include dpl query in dynamic RSC page', async () => {
const $ = await next.render$('/dynamic')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in new URL pattern', async () => {
const $ = await next.render$('/dynamic')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('dynamic client page (headers in layout)', () => {
it('should include dpl query in dynamic client page', async () => {
const $ = await next.render$('/dynamic-client')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in new URL pattern', async () => {
const $ = await next.render$('/dynamic-client')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
it('should include dpl query after hydration', async () => {
const browser = await next.browser('/dynamic-client')
const url = await browser.elementByCss('#imported-src').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('API route', () => {
it('should return import src with dpl query', async () => {
const data = await next
.fetch('/api')
.then((res) => res.ok && res.json())
expect(data.imported.src).toContain('dpl=' + deploymentId)
})
it('should return new URL with dpl query', async () => {
const data = await next
.fetch('/api')
.then((res) => res.ok && res.json())
expect(data.url).not.toContain('dpl=')
})
})
}
)
import fs from 'fs'
import { fileURLToPath } from 'url'
import imported from '../../public/vercel.png'
const url = new URL('../../public/vercel.png', import.meta.url).toString()
const url = new URL('../../public/vercel.png', import.meta.url)
export function GET(req, res) {
return Response.json({ imported, url })
let size
try {
size = fs.readFileSync(fileURLToPath(url)).length
} catch (e) {
size = e.message
}
return Response.json({ imported: imported.src, url: url.toString(), size })
}
......@@ -5,5 +5,5 @@ export const contentType = 'text/json'
// Image generation
export default async function Image() {
return Response.json({ imported, url })
return Response.json({ imported: imported.src, url })
}
......@@ -6,7 +6,7 @@ const url = new URL('./public/vercel.png', import.meta.url).toString()
export async function middleware(req: NextRequest) {
if (req.nextUrl.toString().endsWith('/middleware')) {
return Response.json({ imported, url })
return Response.json({ imported: imported.src, url })
}
return NextResponse.next()
......
/** @type {import('next').NextConfig} */
module.exports = {
deploymentId: 'test-deployment-id',
}
const nextConfig = {}
module.exports = nextConfig
......@@ -4,7 +4,7 @@ const url = new URL('../../../public/vercel.png', import.meta.url)
export default (req, res) => {
return new Response(
JSON.stringify({
imported,
imported: imported.src,
url: url.toString(),
})
)
......
import fs from 'fs'
import { fileURLToPath } from 'url'
import imported from '../../../public/vercel.png'
const url = new URL('../../../public/vercel.png', import.meta.url)
......@@ -6,13 +7,13 @@ const url = new URL('../../../public/vercel.png', import.meta.url)
export default (req, res) => {
let size
try {
size = fs.readFileSync(url).length
size = fs.readFileSync(fileURLToPath(url)).length
} catch (e) {
size = e.message
}
res.send({
imported,
imported: imported.src,
url: url.toString(),
size,
})
......
import { retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'
import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
// | | Pages Client | Pages Server (SSR,RSC) | API Routes/Middleware/Metadata |
// |---------|-------------------------|-------------------------|--------------------------------|
......@@ -11,9 +11,12 @@ import { nextTestSetup } from 'e2e-utils'
// - a bug where App Router API routes (and Metadata) return client assets for `new URL`s.
// - a bug where Edge Page routes return client assets for `new URL`s.
describe(`Handle new URL asset references`, () => {
const { next, skipped } = nextTestSetup({
const { next, skipped, isTurbopack } = nextTestSetup({
files: __dirname,
// Workaround for `Error: invariant: htmlFsRef != null && jsonFsRef != null /ssg` errors
env: {
// rely on skew protection when deployed
NEXT_DEPLOYMENT_ID: isNextStart ? 'test-deployment-id' : undefined,
},
skipDeployment: true,
})
......@@ -21,81 +24,89 @@ describe(`Handle new URL asset references`, () => {
return
}
const serverFilePath = expect.stringMatching(
/file:.*\/.next(\/dev)?\/server\/.*\/vercel\.[0-9a-f]{8}\.png$/
)
const serverEdgeUrl = expect.stringMatching(
/^blob:.*vercel\.[0-9a-f]{8,}\.png$/
)
const clientFilePath = expect.stringMatching(
/^\/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png$/
const serverFileRegex = expect.stringMatching(
/file:.*\/.next(\/dev)?\/server\/.*\/vercel.HASH.png$/
)
const serverEdgeUrl = isTurbopack
? `blob:server/edge/assets/vercel.HASH.png`
: `blob:vercel.HASH.png`
let clientUrl: string
const expectedPageContent = (count: number) =>
'Hello ' + Array(count).fill(clientUrl).join('+')
beforeAll(() => {
let expectedToken
if (isNextDev || !isTurbopack) {
expectedToken = undefined
} else {
expectedToken = next.deploymentId
if (!expectedToken) {
throw new Error('Missing deployment id')
}
}
clientUrl = `/_next/static/media/vercel.HASH.png${expectedToken ? `?dpl=${expectedToken}` : ''}`
})
it('should respond on middleware api', async () => {
const data = await next
.fetch('/middleware')
.then((res) => res.ok && res.json())
.then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({
imported: expect.objectContaining({
src: clientFilePath,
}),
expect(json).toEqual({
imported: clientUrl,
url: serverEdgeUrl,
})
})
const expectedPage =
/^Hello \/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png(\+\/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png(\+\/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png)?)?$/
describe('app router', () => {
it('should respond on webmanifest', async () => {
const data = await next
.fetch('/manifest.webmanifest')
.then((res) => res.ok && res.json())
.then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({
expect(json).toEqual({
short_name: 'Next.js',
name: 'Next.js',
icons: [
{
src: clientFilePath,
src: clientUrl,
type: 'image/png',
sizes: '512x512',
},
],
// TODO Webpack bug?
description: process.env.IS_TURBOPACK_TEST
? serverFilePath
: clientFilePath,
description: isTurbopack ? serverFileRegex : clientUrl,
})
})
it('should respond on opengraph-image', async () => {
const data = await next
.fetch('/opengraph-image')
.then((res) => res.ok && res.json())
.then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({
imported: expect.objectContaining({
src: clientFilePath,
}),
expect(json).toEqual({
imported: clientUrl,
// TODO Webpack bug?
url: process.env.IS_TURBOPACK_TEST ? serverFilePath : clientFilePath,
url: isTurbopack ? serverFileRegex : clientUrl,
})
})
for (const page of ['/rsc', '/rsc-edge', '/client', '/client-edge']) {
// TODO Webpack bug?
let shouldSkip = process.env.IS_TURBOPACK_TEST
? false
: page.includes('edge')
let shouldSkip = isTurbopack ? false : page.includes('edge')
;(shouldSkip ? it.skip : it)(
`should render the ${page} page`,
async () => {
const $ = await next.render$(page)
// eslint-disable-next-line jest/no-standalone-expect
expect($('main').text()).toMatch(expectedPage)
expect(stripVercelPngHash($('main').text())).toEqual(
expectedPageContent(2)
)
}
)
;(shouldSkip ? it.skip : it)(
......@@ -103,46 +114,46 @@ describe(`Handle new URL asset references`, () => {
async () => {
const browser = await next.browser(page)
await retry(async () =>
expect(await browser.elementByCss('main').text()).toMatch(
expectedPage
)
expect(
stripVercelPngHash(await browser.elementByCss('main').text())
).toEqual(expectedPageContent(2))
)
}
)
}
it('should respond on API', async () => {
const data = await next.fetch('/api').then((res) => res.ok && res.json())
const data = await next.fetch('/api').then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({
imported: expect.objectContaining({
src: clientFilePath,
}),
expect(json).toEqual({
imported: clientUrl,
// TODO Webpack bug?
url: process.env.IS_TURBOPACK_TEST ? serverFilePath : clientFilePath,
url: isTurbopack ? serverFileRegex : clientUrl,
size: isTurbopack ? 30079 : expect.toBeString(),
})
})
})
describe('pages router', () => {
for (const page of [
'/pages/static',
'/pages/ssr',
'/pages/ssg',
'/pages-edge/static',
'/pages-edge/ssr',
]) {
for (const [page, count] of [
['/pages/static', 2],
['/pages/ssr', 3],
['/pages/ssg', 3],
['/pages-edge/static', 2],
['/pages-edge/ssr', 3],
] as const) {
// TODO Webpack bug?
let shouldSkip = process.env.IS_TURBOPACK_TEST
? false
: page.includes('edge')
let shouldSkip = isTurbopack ? false : page.includes('edge')
;(shouldSkip ? it.skip : it)(
`should render the ${page} page`,
async () => {
const $ = await next.render$(page)
// eslint-disable-next-line jest/no-standalone-expect
expect($('main').text()).toMatch(expectedPage)
expect(stripVercelPngHash($('main').text())).toEqual(
expectedPageContent(count)
)
}
)
;(shouldSkip ? it.skip : it)(
......@@ -150,9 +161,9 @@ describe(`Handle new URL asset references`, () => {
async () => {
const browser = await next.browser(page)
await retry(async () =>
expect(await browser.elementByCss('main').text()).toMatch(
expectedPage
)
expect(
stripVercelPngHash(await browser.elementByCss('main').text())
).toEqual(expectedPageContent(count))
)
}
)
......@@ -161,13 +172,12 @@ describe(`Handle new URL asset references`, () => {
it('should respond on API', async () => {
const data = await next
.fetch('/api/pages/')
.then((res) => res.ok && res.json())
.then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({
imported: expect.objectContaining({
src: clientFilePath,
}),
url: serverFilePath,
expect(json).toEqual({
imported: clientUrl,
url: serverFileRegex,
size: 30079,
})
})
......@@ -175,14 +185,17 @@ describe(`Handle new URL asset references`, () => {
it('should respond on edge API', async () => {
const data = await next
.fetch('/api/pages-edge/')
.then((res) => res.ok && res.json())
.then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({
imported: expect.objectContaining({
src: clientFilePath,
}),
expect(json).toEqual({
imported: clientUrl,
url: serverEdgeUrl,
})
})
})
})
function stripVercelPngHash(text: string) {
return text.replace(/vercel\.[0-9a-f]{8,}\.png/g, 'vercel.HASH.png')
}
......@@ -284,7 +284,7 @@ export class NextInstance {
? // since we can't get the build id as a build artifact,
// add it in build logs
{
'post-build': `node -e 'console.log("BUILD" + "_ID: " + require("fs").readFileSync("${this.distDir}/BUILD_ID"))'`,
'post-build': `node -e 'console.log("BUILD" + "_ID: " + fs.readFileSync("${this.distDir}/BUILD_ID") + "\\nDEPLOYMENT_ID: " + process.env.NEXT_DEPLOYMENT_ID)'`,
}
: {}),
...pkgScripts,
......@@ -639,6 +639,14 @@ export class NextInstance {
return ''
}
public get deploymentId(): string | undefined {
return undefined
}
public get deploymentIdQuery(): string {
return this.deploymentId ? `?dpl=${this.deploymentId}` : ''
}
public get cliOutput(): string {
return ''
}
......
......@@ -9,6 +9,7 @@ import { Span } from 'next/dist/trace'
export class NextDeployInstance extends NextInstance {
private _cliOutput: string
private _buildId: string
private _deploymentId: string | undefined
private _writtenHostsLine: string | null = null
protected throwIfUnavailable(): void | never {
......@@ -33,6 +34,10 @@ export class NextDeployInstance extends NextInstance {
return this._buildId
}
public get deploymentId() {
return this._deploymentId
}
private async deployUsingCustomScript(): Promise<{ url: string }> {
const deployScriptPath = process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH!
......@@ -162,13 +167,23 @@ export class NextDeployInstance extends NextInstance {
}
const buildId = this._cliOutput.match(/BUILD_ID: (.+)/)?.[1]?.trim()
if (!buildId) {
throw new Error(`Failed to get buildId from logs ${this._cliOutput}`)
}
this._buildId = buildId
const deploymentId = this._cliOutput
.match(/DEPLOYMENT_ID: (.+)/)?.[1]
?.trim()
if (!deploymentId) {
throw new Error(
`Failed to get deploymentId from logs ${this._cliOutput}`
)
}
this._deploymentId = deploymentId
require('console').log(`Got buildId: ${this._buildId}`)
require('console').log(
`Got buildId: ${this._buildId}, deploymentId: ${this._deploymentId}`
)
return
}
......
......@@ -8,6 +8,7 @@ import { quote as shellQuote } from 'shell-quote'
export class NextStartInstance extends NextInstance {
private _buildId: string
private _deploymentId: string | undefined
private _cliOutput: string = ''
private _prerenderFinishedTimeMS: number | null = null
......@@ -16,6 +17,10 @@ export class NextStartInstance extends NextInstance {
return this._buildId
}
public get deploymentId() {
return this._deploymentId
}
public get cliOutput() {
return this._cliOutput
}
......@@ -113,6 +118,21 @@ export class NextStartInstance extends NextInstance {
)
.catch(() => '')
).trim()
try {
const requiredServerFiles = JSON.parse(
await fs.readFile(
path.join(
this.testDir,
this.nextConfig?.distDir || '.next',
'required-server-files.json'
),
'utf8'
)
)
this._deploymentId =
requiredServerFiles.config?.deploymentId || undefined
} catch {}
}
console.log('running', shellQuote(startArgs))
......@@ -252,6 +272,20 @@ export class NextStartInstance extends NextInstance {
.catch(() => '')
).trim()
try {
const requiredServerFiles = JSON.parse(
await fs.readFile(
path.join(
this.testDir,
this.nextConfig?.distDir || '.next',
'required-server-files.json'
),
'utf8'
)
)
this._deploymentId = requiredServerFiles.config?.deploymentId || undefined
} catch {}
return result
}
......
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