Unverified Commit 7061f942 authored by Tim Caswell's avatar Tim Caswell Committed by GitHub

Escape the '.' in '.json' when making static data routes. (#73850)

## Problem

When generating route regular expressions for data routes, some code
paths appear to not escape the `.` in `.json`. This causes routing to be
slightly slower, prevents some optimizations related to static
postfixes, and potentially allows for unintended request paths to be
accepted as value.

## Solution

After much tracing and digging, I found a code path writing regular
expressions directly without escaping. In this case the solution is to
escape the `.` using `\\.` when generating static data routes.

A simple next app was made to exercise this path with a route
`pages/server-time.tsx` that contains a simple `getServerSideProps`.

```tsx
import type { InferGetServerSidePropsType, GetServerSideProps } from 'next'
 
type TimeDate = {
  timestamp: number
}
 
export const getServerSideProps = (async () => {
  return { props: { timestamp: Date.now() } }
}) satisfies GetServerSideProps<TimeDate>
 
export default function Page({
  timestamp,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
  return (
    <main>
      <p>{new Date(timestamp).toUTCString()}</p>
    </main>
  )
}
```

Before the change, the generated route regexp was
`"^/_next/data/[build_id]/server-time.json$"`, but after then change it
becomes `"^/_next/data/[build_id]/server-time\\.json$"`

The related tests were updated to expect properly escaped `\\.json$`
endings to the regexps.
parent 6d05f248
...@@ -33,7 +33,7 @@ export function buildDataRoute(page: string, buildId: string) { ...@@ -33,7 +33,7 @@ export function buildDataRoute(page: string, buildId: string) {
`^${path.posix.join( `^${path.posix.join(
'/_next/data', '/_next/data',
escapeStringRegexp(buildId), escapeStringRegexp(buildId),
`${pagePath}.json` `${pagePath}\\.json`
)}$` )}$`
).source ).source
) )
......
...@@ -163,7 +163,7 @@ describe('edge-render-getserversideprops', () => { ...@@ -163,7 +163,7 @@ describe('edge-render-getserversideprops', () => {
expect(manifest.dataRoutes).toEqual([ expect(manifest.dataRoutes).toEqual([
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(next.buildId)}/index.json$` `^/_next/data/${escapeStringRegexp(next.buildId)}/index\\.json$`
), ),
page: '/', page: '/',
}, },
......
...@@ -26,19 +26,19 @@ let next: NextInstance ...@@ -26,19 +26,19 @@ let next: NextInstance
const expectedManifestRoutes = () => [ const expectedManifestRoutes = () => [
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/index.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/index\\.json$`
), ),
page: '/', page: '/',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/another.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/another\\.json$`
), ),
page: '/another', page: '/another',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog\\.json$`
), ),
page: '/blog', page: '/blog',
}, },
...@@ -83,44 +83,44 @@ const expectedManifestRoutes = () => [ ...@@ -83,44 +83,44 @@ const expectedManifestRoutes = () => [
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/custom-cache.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/custom-cache\\.json$`
), ),
page: '/custom-cache', page: '/custom-cache',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/default-revalidate.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/default-revalidate\\.json$`
), ),
page: '/default-revalidate', page: '/default-revalidate',
}, },
{ {
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex( dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
buildId buildId
)}\\/early-request-end.json$`, )}\\/early-request-end\\.json$`,
page: '/early-request-end', page: '/early-request-end',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/enoent.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/enoent\\.json$`
), ),
page: '/enoent', page: '/enoent',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/invalid-keys.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/invalid-keys\\.json$`
), ),
page: '/invalid-keys', page: '/invalid-keys',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/non-json.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/non-json\\.json$`
), ),
page: '/non-json', page: '/non-json',
}, },
{ {
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex( dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
buildId buildId
)}\\/not-found.json$`, )}\\/not-found\\.json$`,
page: '/not-found', page: '/not-found',
}, },
{ {
...@@ -137,13 +137,13 @@ const expectedManifestRoutes = () => [ ...@@ -137,13 +137,13 @@ const expectedManifestRoutes = () => [
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/promise.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/promise\\.json$`
), ),
page: '/promise', page: '/promise',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/promise\\/mutate-res.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/promise\\/mutate-res\\.json$`
), ),
page: '/promise/mutate-res', page: '/promise/mutate-res',
}, },
...@@ -151,7 +151,7 @@ const expectedManifestRoutes = () => [ ...@@ -151,7 +151,7 @@ const expectedManifestRoutes = () => [
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
buildId buildId
)}\\/promise\\/mutate-res-no-streaming.json$` )}\\/promise\\/mutate-res-no-streaming\\.json$`
), ),
page: '/promise/mutate-res-no-streaming', page: '/promise/mutate-res-no-streaming',
}, },
...@@ -159,25 +159,25 @@ const expectedManifestRoutes = () => [ ...@@ -159,25 +159,25 @@ const expectedManifestRoutes = () => [
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
buildId buildId
)}\\/promise\\/mutate-res-props.json$` )}\\/promise\\/mutate-res-props\\.json$`
), ),
page: '/promise/mutate-res-props', page: '/promise/mutate-res-props',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/refresh.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/refresh\\.json$`
), ),
page: '/refresh', page: '/refresh',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/slow.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/slow\\.json$`
), ),
page: '/slow', page: '/slow',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/something.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/something\\.json$`
), ),
page: '/something', page: '/something',
}, },
......
...@@ -1334,7 +1334,7 @@ describe('Prerender', () => { ...@@ -1334,7 +1334,7 @@ describe('Prerender', () => {
expect(dataRoutes).toEqual([ expect(dataRoutes).toEqual([
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(next.buildId)}\\/index.json$` `^\\/_next\\/data\\/${escapeRegex(next.buildId)}\\/index\\.json$`
), ),
page: '/', page: '/',
}, },
...@@ -1342,7 +1342,7 @@ describe('Prerender', () => { ...@@ -1342,7 +1342,7 @@ describe('Prerender', () => {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/another.json$` )}\\/another\\.json$`
), ),
page: '/another', page: '/another',
}, },
...@@ -1364,7 +1364,7 @@ describe('Prerender', () => { ...@@ -1364,7 +1364,7 @@ describe('Prerender', () => {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/bad-gssp.json$` )}\\/bad-gssp\\.json$`
), ),
page: '/bad-gssp', page: '/bad-gssp',
}, },
...@@ -1372,7 +1372,7 @@ describe('Prerender', () => { ...@@ -1372,7 +1372,7 @@ describe('Prerender', () => {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/bad-ssr.json$` )}\\/bad-ssr\\.json$`
), ),
page: '/bad-ssr', page: '/bad-ssr',
}, },
...@@ -1414,7 +1414,7 @@ describe('Prerender', () => { ...@@ -1414,7 +1414,7 @@ describe('Prerender', () => {
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(next.buildId)}\\/blog.json$` `^\\/_next\\/data\\/${escapeRegex(next.buildId)}\\/blog\\.json$`
), ),
page: '/blog', page: '/blog',
}, },
...@@ -1493,7 +1493,7 @@ describe('Prerender', () => { ...@@ -1493,7 +1493,7 @@ describe('Prerender', () => {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/default-revalidate.json$` )}\\/default-revalidate\\.json$`
), ),
page: '/default-revalidate', page: '/default-revalidate',
}, },
...@@ -1530,7 +1530,7 @@ describe('Prerender', () => { ...@@ -1530,7 +1530,7 @@ describe('Prerender', () => {
// dataRouteRegex: normalizeRegEx( // dataRouteRegex: normalizeRegEx(
// `^\\/_next\\/data\\/${escapeRegex( // `^\\/_next\\/data\\/${escapeRegex(
// next.buildId // next.buildId
// )}\\/index\\/index.json$` // )}\\/index\\/index\\.json$`
// ), // ),
// page: '/index', // page: '/index',
// }, // },
...@@ -1551,13 +1551,13 @@ describe('Prerender', () => { ...@@ -1551,13 +1551,13 @@ describe('Prerender', () => {
{ {
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex( dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/large-page-data.json$`, )}\\/large-page-data\\.json$`,
page: '/large-page-data', page: '/large-page-data',
}, },
{ {
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex( dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/large-page-data-ssr.json$`, )}\\/large-page-data-ssr\\.json$`,
page: '/large-page-data-ssr', page: '/large-page-data-ssr',
}, },
{ {
...@@ -1592,7 +1592,7 @@ describe('Prerender', () => { ...@@ -1592,7 +1592,7 @@ describe('Prerender', () => {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/preview.json$` )}\\/preview\\.json$`
), ),
page: '/preview', page: '/preview',
}, },
...@@ -1600,13 +1600,13 @@ describe('Prerender', () => { ...@@ -1600,13 +1600,13 @@ describe('Prerender', () => {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(
next.buildId next.buildId
)}\\/something.json$` )}\\/something\\.json$`
), ),
page: '/something', page: '/something',
}, },
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(next.buildId)}\\/ssr.json$` `^\\/_next\\/data\\/${escapeRegex(next.buildId)}\\/ssr\\.json$`
), ),
page: '/ssr', page: '/ssr',
}, },
......
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