Unverified Commit b0f3460a authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[15.5.x] Fix ISR misses with backslashes in segments when deployed on Windows

https://github.com/vercel/next-js-mirror/pull/157
parent 409772ec
...@@ -810,7 +810,8 @@ jobs: ...@@ -810,7 +810,8 @@ jobs:
test/e2e/app-dir/app/index.test.ts \ test/e2e/app-dir/app/index.test.ts \
test/e2e/app-dir/app-edge/app-edge.test.ts \ test/e2e/app-dir/app-edge/app-edge.test.ts \
test/e2e/app-dir/metadata-edge/index.test.ts \ test/e2e/app-dir/metadata-edge/index.test.ts \
test/e2e/app-dir/non-root-project-monorepo/non-root-project-monorepo.test.ts test/e2e/app-dir/non-root-project-monorepo/non-root-project-monorepo.test.ts \
test/e2e/incremental-cache-path-traversal/index.test.ts
stepName: 'test-prod-windows' stepName: 'test-prod-windows'
runs_on_labels: '["windows-latest-8-core-oss"]' runs_on_labels: '["windows-latest-8-core-oss"]'
buildNativeTarget: 'x86_64-pc-windows-msvc' buildNativeTarget: 'x86_64-pc-windows-msvc'
......
...@@ -796,5 +796,6 @@ ...@@ -796,5 +796,6 @@
"795": "Invalid target hostname \"%s\" for proxy request, expected \"%s\"", "795": "Invalid target hostname \"%s\" for proxy request, expected \"%s\"",
"796": "Missing initURL", "796": "Missing initURL",
"797": "Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.", "797": "Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.",
"798": "TypeScript %s is not supported by this version of Next.js. The TypeScript 7 native compiler does not provide the JavaScript compiler API that Next.js requires. Install TypeScript 6 (e.g. %s) or upgrade to a Next.js v16.2.11 or later to get support for TypeScript 7." "798": "TypeScript %s is not supported by this version of Next.js. The TypeScript 7 native compiler does not provide the JavaScript compiler API that Next.js requires. Install TypeScript 6 (e.g. %s) or upgrade to a Next.js v16.2.11 or later to get support for TypeScript 7.",
"799": "Invalid file path: %s"
} }
...@@ -428,26 +428,31 @@ export default class FileSystemCache implements CacheHandler { ...@@ -428,26 +428,31 @@ export default class FileSystemCache implements CacheHandler {
await writer.wait() await writer.wait()
} }
private getFilePath(pathname: string, kind: IncrementalCacheKind): string { private getFilePath(key: string, kind: IncrementalCacheKind): string {
let rootDir: string
switch (kind) { switch (kind) {
case IncrementalCacheKind.FETCH: case IncrementalCacheKind.FETCH:
// we store in .next/cache/fetch-cache so it can be persisted // we store in .next/cache/fetch-cache so it can be persisted
// across deploys // across deploys
return path.join( rootDir = path.join(this.serverDistDir, '..', 'cache', 'fetch-cache')
this.serverDistDir, break
'..',
'cache',
'fetch-cache',
pathname
)
case IncrementalCacheKind.PAGES: case IncrementalCacheKind.PAGES:
return path.join(this.serverDistDir, 'pages', pathname) rootDir = path.join(this.serverDistDir, 'pages')
break
case IncrementalCacheKind.IMAGE: case IncrementalCacheKind.IMAGE:
case IncrementalCacheKind.APP_PAGE: case IncrementalCacheKind.APP_PAGE:
case IncrementalCacheKind.APP_ROUTE: case IncrementalCacheKind.APP_ROUTE:
return path.join(this.serverDistDir, 'app', pathname) rootDir = path.join(this.serverDistDir, 'app')
break
default: default:
throw new Error(`Unexpected file path kind: ${kind}`) throw new Error(`Unexpected file path kind: ${kind}`)
} }
const filePath = path.join(rootDir, key)
if (!(filePath.startsWith(rootDir + path.sep) || filePath === rootDir)) {
throw new Error(`Invalid file path: ${filePath}`)
}
return filePath
} }
} }
...@@ -4,7 +4,7 @@ export default function escapePathDelimiters( ...@@ -4,7 +4,7 @@ export default function escapePathDelimiters(
escapeEncoded?: boolean escapeEncoded?: boolean
): string { ): string {
return segment.replace( return segment.replace(
new RegExp(`([/#?]${escapeEncoded ? '|%(2f|23|3f|5c)' : ''})`, 'gi'), new RegExp(`([/#?\\\\]${escapeEncoded ? '|%(2f|23|3f|5c)' : ''})`, 'gi'),
(char: string) => encodeURIComponent(char) (char: string) => encodeURIComponent(char)
) )
} }
export const revalidate = 3600
export function generateStaticParams() {
return [{ rest: ['seed'] }]
}
export default async function AppCachePage({ params }) {
const { rest } = await params
const captured = `ordinary-bound-value:${rest.join('/')}`
// A closure-bound "use server" action. Its presence makes `next build` emit
// server-reference-manifest.json (with an encryptionKey), which is the private
// file the traversal later discloses.
async function harmlessBoundAction(formData) {
'use server'
return `${captured}:${formData.get('value')}`
}
return (
<main>
<p id="app-rest">{rest.join('/')}</p>
<form action={harmlessBoundAction}>
<input name="value" defaultValue="harmless" />
<button type="submit">submit</button>
</form>
</main>
)
}
export const metadata = {
title: 'incremental-cache path traversal repro',
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { nextTestSetup } from 'e2e-utils'
import path from 'path'
const enableCacheComponents = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
const isAdapterTest = process.env.NEXT_ENABLE_ADAPTER === '1'
const describeSkipCacheComponents = enableCacheComponents
? describe.skip
: describe
describeSkipCacheComponents('incremental-cache path traversal', () => {
const { isNextDeploy, next } = nextTestSetup({ files: __dirname })
it('serves arbitrary json files', async () => {
const sep = path.sep
const trav = `..${encodeURIComponent(sep)}..${encodeURIComponent(sep)}server-reference-manifest`
await next.fetch(`/app-cache/${trav}`)
const res = await next.fetch(
`/_next/data/${encodeURIComponent(next.buildId)}/pages-cache/${trav}.json`
)
const text = await res.text()
let json: { encryptionKey?: unknown } | undefined
try {
json = JSON.parse(text)
} catch {
json = undefined
}
expect({ status: res.status, json: json }).toEqual({
status: 200,
json: {
pageProps: {
rest: isNextDeploy
? isAdapterTest
? ['..', '..', 'server-reference-manifest']
: ['../../server-reference-manifest']
: [`..${sep}..${sep}server-reference-manifest`],
},
__N_SSG: true,
},
})
})
})
/** @type {import('next').NextConfig} */
module.exports = {}
export async function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
}
}
export async function getStaticProps({ params }) {
return {
props: {
rest: params?.rest ?? [],
},
revalidate: 3600,
}
}
export default function PagesCachePage({ rest }) {
return <main id="pages-rest">{rest.join('/')}</main>
}
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