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