Unverified Commit c6ef857d authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Subresource Integrity for App Directory (#39729)

<!--
Thanks for opening a PR! Your contribution is much appreciated.
In order to make sure your PR is handled as smoothly as possible we
request that you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

This serves to add support for [Subresource
Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)
hashes for scripts added from the new app directory. This also has
support for utilizing nonce values passed from request headers (expected
to be generated per request in middleware) in the bootstrapping scripts
via the `Content-Security-Policy` header as such:

```
Content-Security-Policy: script-src 'nonce-2726c7f26c'
```

Which results in the inline scripts having a new `nonce` attribute hash
added. These features combined support for setting an aggressive Content
Security Policy on scripts loaded.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 1858fa96
...@@ -729,6 +729,10 @@ ...@@ -729,6 +729,10 @@
{ {
"title": "middleware-parse-user-agent", "title": "middleware-parse-user-agent",
"path": "/errors/middleware-parse-user-agent.md" "path": "/errors/middleware-parse-user-agent.md"
},
{
"title": "nonce-contained-invalid-characters",
"path": "/errors/nonce-contained-invalid-characters.md"
} }
] ]
} }
......
# nonce contained invalid characters
#### Why This Error Occurred
This happens when there is a request that contains a `Content-Security-Policy`
header that contains a `script-src` directive with a nonce value that contains
invalid characters (any one of `<>&` characters). For example:
- `'nonce-<script />'`: not allowed
- `'nonce-/>script<>'`: not allowed
- `'nonce-PHNjcmlwdCAvPg=='`: allowed
- `'nonce-Lz5zY3JpcHQ8Pg=='`: allowed
#### Possible Ways to Fix It
Replace the nonce value with a base64 encoded value.
### Useful Links
- [Content Security Policy Sources](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#sources)
...@@ -78,6 +78,7 @@ ...@@ -78,6 +78,7 @@
"@types/http-proxy": "1.17.3", "@types/http-proxy": "1.17.3",
"@types/jest": "24.0.13", "@types/jest": "24.0.13",
"@types/node": "13.11.0", "@types/node": "13.11.0",
"@types/node-fetch": "2.6.1",
"@types/react": "16.9.17", "@types/react": "16.9.17",
"@types/react-dom": "16.9.4", "@types/react-dom": "16.9.4",
"@types/relay-runtime": "13.0.0", "@types/relay-runtime": "13.0.0",
......
...@@ -212,6 +212,7 @@ export function getEdgeServerEntry(opts: { ...@@ -212,6 +212,7 @@ export function getEdgeServerEntry(opts: {
stringifiedConfig: JSON.stringify(opts.config), stringifiedConfig: JSON.stringify(opts.config),
pagesType: opts.pagesType, pagesType: opts.pagesType,
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'), appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
sriEnabled: !opts.isDev && !!opts.config.experimental.sri?.algorithm,
} }
return { return {
......
...@@ -1082,13 +1082,13 @@ export async function isPageStatic({ ...@@ -1082,13 +1082,13 @@ export async function isPageStatic({
getStaticProps: mod.getStaticProps, getStaticProps: mod.getStaticProps,
} }
} else { } else {
componentsResult = await loadComponents( componentsResult = await loadComponents({
distDir, distDir,
page, pathname: page,
serverless, serverless,
false, hasServerComponents: false,
false isAppPath: false,
) })
} }
const Comp = componentsResult.Component const Comp = componentsResult.Component
...@@ -1214,13 +1214,13 @@ export async function hasCustomGetInitialProps( ...@@ -1214,13 +1214,13 @@ export async function hasCustomGetInitialProps(
): Promise<boolean> { ): Promise<boolean> {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig) require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
const components = await loadComponents( const components = await loadComponents({
distDir, distDir,
page, pathname: page,
isLikeServerless, serverless: isLikeServerless,
false, hasServerComponents: false,
false isAppPath: false,
) })
let mod = components.ComponentMod let mod = components.ComponentMod
if (checkingApp) { if (checkingApp) {
...@@ -1239,13 +1239,13 @@ export async function getNamedExports( ...@@ -1239,13 +1239,13 @@ export async function getNamedExports(
runtimeEnvConfig: any runtimeEnvConfig: any
): Promise<Array<string>> { ): Promise<Array<string>> {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig) require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
const components = await loadComponents( const components = await loadComponents({
distDir, distDir,
page, pathname: page,
isLikeServerless, serverless: isLikeServerless,
false, hasServerComponents: false,
false isAppPath: false,
) })
let mod = components.ComponentMod let mod = components.ComponentMod
return Object.keys(mod) return Object.keys(mod)
......
...@@ -61,6 +61,7 @@ import loadJsConfig from './load-jsconfig' ...@@ -61,6 +61,7 @@ import loadJsConfig from './load-jsconfig'
import { loadBindings } from './swc' import { loadBindings } from './swc'
import { clientComponentRegex } from './webpack/loaders/utils' import { clientComponentRegex } from './webpack/loaders/utils'
import { AppBuildManifestPlugin } from './webpack/plugins/app-build-manifest-plugin' import { AppBuildManifestPlugin } from './webpack/plugins/app-build-manifest-plugin'
import { SubresourceIntegrityPlugin } from './webpack/plugins/subresource-integrity-plugin'
const NEXT_PROJECT_ROOT = pathJoin(__dirname, '..', '..') const NEXT_PROJECT_ROOT = pathJoin(__dirname, '..', '..')
const NEXT_PROJECT_ROOT_DIST = pathJoin(NEXT_PROJECT_ROOT, 'dist') const NEXT_PROJECT_ROOT_DIST = pathJoin(NEXT_PROJECT_ROOT, 'dist')
...@@ -1750,7 +1751,11 @@ export default async function getBaseWebpackConfig( ...@@ -1750,7 +1751,11 @@ export default async function getBaseWebpackConfig(
}), }),
// MiddlewarePlugin should be after DefinePlugin so NEXT_PUBLIC_* // MiddlewarePlugin should be after DefinePlugin so NEXT_PUBLIC_*
// replacement is done before its process.env.* handling // replacement is done before its process.env.* handling
isEdgeServer && new MiddlewarePlugin({ dev }), isEdgeServer &&
new MiddlewarePlugin({
dev,
sriEnabled: !dev && !!config.experimental.sri?.algorithm,
}),
isClient && isClient &&
new BuildManifestPlugin({ new BuildManifestPlugin({
buildId, buildId,
...@@ -1800,6 +1805,10 @@ export default async function getBaseWebpackConfig( ...@@ -1800,6 +1805,10 @@ export default async function getBaseWebpackConfig(
dev, dev,
isEdgeServer, isEdgeServer,
})), })),
!dev &&
isClient &&
!!config.experimental.sri?.algorithm &&
new SubresourceIntegrityPlugin(config.experimental.sri.algorithm),
!dev && !dev &&
isClient && isClient &&
new (require('./webpack/plugins/telemetry-plugin').TelemetryPlugin)( new (require('./webpack/plugins/telemetry-plugin').TelemetryPlugin)(
......
...@@ -14,6 +14,7 @@ export type EdgeSSRLoaderQuery = { ...@@ -14,6 +14,7 @@ export type EdgeSSRLoaderQuery = {
stringifiedConfig: string stringifiedConfig: string
appDirLoader?: string appDirLoader?: string
pagesType?: 'app' | 'pages' | 'root' pagesType?: 'app' | 'pages' | 'root'
sriEnabled: boolean
} }
export default async function edgeSSRLoader(this: any) { export default async function edgeSSRLoader(this: any) {
...@@ -30,6 +31,7 @@ export default async function edgeSSRLoader(this: any) { ...@@ -30,6 +31,7 @@ export default async function edgeSSRLoader(this: any) {
stringifiedConfig, stringifiedConfig,
appDirLoader: appDirLoaderBase64, appDirLoader: appDirLoaderBase64,
pagesType, pagesType,
sriEnabled,
} = this.getOptions() } = this.getOptions()
const appDirLoader = Buffer.from( const appDirLoader = Buffer.from(
...@@ -94,6 +96,9 @@ export default async function edgeSSRLoader(this: any) { ...@@ -94,6 +96,9 @@ export default async function edgeSSRLoader(this: any) {
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
const rscManifest = self.__RSC_MANIFEST const rscManifest = self.__RSC_MANIFEST
const rscCssManifest = self.__RSC_CSS_MANIFEST const rscCssManifest = self.__RSC_CSS_MANIFEST
const subresourceIntegrityManifest = ${
sriEnabled ? 'self.__SUBRESOURCE_INTEGRITY_MANIFEST' : 'undefined'
}
const render = getRender({ const render = getRender({
dev: ${dev}, dev: ${dev},
...@@ -109,6 +114,7 @@ export default async function edgeSSRLoader(this: any) { ...@@ -109,6 +114,7 @@ export default async function edgeSSRLoader(this: any) {
reactLoadableManifest, reactLoadableManifest,
serverComponentManifest: ${isServerComponent} ? rscManifest : null, serverComponentManifest: ${isServerComponent} ? rscManifest : null,
serverCSSManifest: ${isServerComponent} ? rscCssManifest : null, serverCSSManifest: ${isServerComponent} ? rscCssManifest : null,
subresourceIntegrityManifest,
config: ${stringifiedConfig}, config: ${stringifiedConfig},
buildId: ${JSON.stringify(buildId)}, buildId: ${JSON.stringify(buildId)},
}) })
......
...@@ -23,6 +23,7 @@ export function getRender({ ...@@ -23,6 +23,7 @@ export function getRender({
appRenderToHTML, appRenderToHTML,
pagesRenderToHTML, pagesRenderToHTML,
serverComponentManifest, serverComponentManifest,
subresourceIntegrityManifest,
serverCSSManifest, serverCSSManifest,
config, config,
buildId, buildId,
...@@ -38,6 +39,7 @@ export function getRender({ ...@@ -38,6 +39,7 @@ export function getRender({
Document: DocumentType Document: DocumentType
buildManifest: BuildManifest buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest reactLoadableManifest: ReactLoadableManifest
subresourceIntegrityManifest?: Record<string, string>
serverComponentManifest: any serverComponentManifest: any
serverCSSManifest: any serverCSSManifest: any
appServerMod: any appServerMod: any
...@@ -48,6 +50,7 @@ export function getRender({ ...@@ -48,6 +50,7 @@ export function getRender({
dev, dev,
buildManifest, buildManifest,
reactLoadableManifest, reactLoadableManifest,
subresourceIntegrityManifest,
Document, Document,
App: appMod.default as AppType, App: appMod.default as AppType,
} }
......
...@@ -17,6 +17,7 @@ import { ...@@ -17,6 +17,7 @@ import {
MIDDLEWARE_REACT_LOADABLE_MANIFEST, MIDDLEWARE_REACT_LOADABLE_MANIFEST,
NEXT_CLIENT_SSR_ENTRY_SUFFIX, NEXT_CLIENT_SSR_ENTRY_SUFFIX,
FLIGHT_SERVER_CSS_MANIFEST, FLIGHT_SERVER_CSS_MANIFEST,
SUBRESOURCE_INTEGRITY_MANIFEST,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
export interface EdgeFunctionDefinition { export interface EdgeFunctionDefinition {
...@@ -74,12 +75,19 @@ function isUsingIndirectEvalAndUsedByExports(args: { ...@@ -74,12 +75,19 @@ function isUsingIndirectEvalAndUsedByExports(args: {
return false return false
} }
function getEntryFiles(entryFiles: string[], meta: EntryMetadata) { function getEntryFiles(
entryFiles: string[],
meta: EntryMetadata,
opts: { sriEnabled: boolean }
) {
const files: string[] = [] const files: string[] = []
if (meta.edgeSSR) { if (meta.edgeSSR) {
if (meta.edgeSSR.isServerComponent) { if (meta.edgeSSR.isServerComponent) {
files.push(`server/${FLIGHT_MANIFEST}.js`) files.push(`server/${FLIGHT_MANIFEST}.js`)
files.push(`server/${FLIGHT_SERVER_CSS_MANIFEST}.js`) files.push(`server/${FLIGHT_SERVER_CSS_MANIFEST}.js`)
if (opts.sriEnabled) {
files.push(`server/${SUBRESOURCE_INTEGRITY_MANIFEST}.js`)
}
files.push( files.push(
...entryFiles ...entryFiles
.filter( .filter(
...@@ -112,8 +120,9 @@ function getEntryFiles(entryFiles: string[], meta: EntryMetadata) { ...@@ -112,8 +120,9 @@ function getEntryFiles(entryFiles: string[], meta: EntryMetadata) {
function getCreateAssets(params: { function getCreateAssets(params: {
compilation: webpack.Compilation compilation: webpack.Compilation
metadataByEntry: Map<string, EntryMetadata> metadataByEntry: Map<string, EntryMetadata>
opts: { sriEnabled: boolean }
}) { }) {
const { compilation, metadataByEntry } = params const { compilation, metadataByEntry, opts } = params
return (assets: any) => { return (assets: any) => {
const middlewareManifest: MiddlewareManifest = { const middlewareManifest: MiddlewareManifest = {
sortedMiddleware: [], sortedMiddleware: [],
...@@ -145,7 +154,7 @@ function getCreateAssets(params: { ...@@ -145,7 +154,7 @@ function getCreateAssets(params: {
const edgeFunctionDefinition: EdgeFunctionDefinition = { const edgeFunctionDefinition: EdgeFunctionDefinition = {
env: Array.from(metadata.env), env: Array.from(metadata.env),
files: getEntryFiles(entrypoint.getFiles(), metadata), files: getEntryFiles(entrypoint.getFiles(), metadata, opts),
name: entrypoint.name, name: entrypoint.name,
page: page, page: page,
matchers, matchers,
...@@ -708,13 +717,15 @@ function getExtractMetadata(params: { ...@@ -708,13 +717,15 @@ function getExtractMetadata(params: {
} }
export default class MiddlewarePlugin { export default class MiddlewarePlugin {
dev: boolean private readonly dev: boolean
private readonly sriEnabled: boolean
constructor({ dev }: { dev: boolean }) { constructor({ dev, sriEnabled }: { dev: boolean; sriEnabled: boolean }) {
this.dev = dev this.dev = dev
this.sriEnabled = sriEnabled
} }
apply(compiler: webpack.Compiler) { public apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap(NAME, (compilation, params) => { compiler.hooks.compilation.tap(NAME, (compilation, params) => {
const { hooks } = params.normalModuleFactory const { hooks } = params.normalModuleFactory
/** /**
...@@ -751,7 +762,11 @@ export default class MiddlewarePlugin { ...@@ -751,7 +762,11 @@ export default class MiddlewarePlugin {
name: 'NextJsMiddlewareManifest', name: 'NextJsMiddlewareManifest',
stage: (webpack as any).Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, stage: (webpack as any).Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
}, },
getCreateAssets({ compilation, metadataByEntry }) getCreateAssets({
compilation,
metadataByEntry,
opts: { sriEnabled: this.sriEnabled },
})
) )
}) })
} }
......
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import crypto from 'crypto'
import { SUBRESOURCE_INTEGRITY_MANIFEST } from '../../../shared/lib/constants'
const PLUGIN_NAME = 'SubresourceIntegrityPlugin'
export type SubresourceIntegrityAlgorithm = 'sha256' | 'sha384' | 'sha512'
export class SubresourceIntegrityPlugin {
constructor(private readonly algorithm: SubresourceIntegrityAlgorithm) {}
public apply(compiler: webpack.Compiler) {
compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.afterOptimizeAssets.tap(
{
name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets) => {
// Collect all the entrypoint files.
let files = new Set<string>()
for (const entrypoint of compilation.entrypoints.values()) {
const iterator = entrypoint?.getFiles()
if (!iterator) {
continue
}
for (const file of iterator) {
files.add(file)
}
}
// For each file, deduped, calculate the file hash.
const hashes: Record<string, string> = {}
for (const file of files.values()) {
// Get the buffer for the asset.
const asset = assets[file]
if (!asset) {
throw new Error(`could not get asset: ${file}`)
}
// Get the buffer for the asset.
const buffer = asset.buffer()
// Create the hash for the content.
const hash = crypto
.createHash(this.algorithm)
.update(buffer)
.digest()
.toString('base64')
hashes[file] = `${this.algorithm}-${hash}`
}
const json = JSON.stringify(hashes, null, 2)
const file = 'server/' + SUBRESOURCE_INTEGRITY_MANIFEST
assets[file + '.js'] = new sources.RawSource(
'self.__SUBRESOURCE_INTEGRITY_MANIFEST=' + json
// Work around webpack 4 type of RawSource being used
// TODO: use webpack 5 type by default
) as unknown as webpack.sources.RawSource
assets[file + '.json'] = new sources.RawSource(
json
// Work around webpack 4 type of RawSource being used
// TODO: use webpack 5 type by default
) as unknown as webpack.sources.RawSource
}
)
})
}
}
...@@ -290,13 +290,13 @@ export default async function exportPage({ ...@@ -290,13 +290,13 @@ export default async function exportPage({
getServerSideProps, getServerSideProps,
getStaticProps, getStaticProps,
pageConfig, pageConfig,
} = await loadComponents( } = await loadComponents({
distDir, distDir,
page, pathname: page,
serverless, serverless,
!!serverComponents, hasServerComponents: !!serverComponents,
isAppPath isAppPath,
) })
const ampState = { const ampState = {
ampFirst: pageConfig?.amp === true, ampFirst: pageConfig?.amp === true,
hasQuery: Boolean(query.amp), hasQuery: Boolean(query.amp),
...@@ -357,13 +357,13 @@ export default async function exportPage({ ...@@ -357,13 +357,13 @@ export default async function exportPage({
throw new Error(`Failed to render serverless page`) throw new Error(`Failed to render serverless page`)
} }
} else { } else {
const components = await loadComponents( const components = await loadComponents({
distDir, distDir,
page, pathname: page,
serverless, serverless,
!!serverComponents, hasServerComponents: !!serverComponents,
isAppPath isAppPath,
) })
const ampState = { const ampState = {
ampFirst: components.pageConfig?.amp === true, ampFirst: components.pageConfig?.amp === true,
hasQuery: Boolean(query.amp), hasQuery: Boolean(query.amp),
......
...@@ -17,7 +17,7 @@ import { ...@@ -17,7 +17,7 @@ import {
continueFromInitialStream, continueFromInitialStream,
} from './node-web-streams-helper' } from './node-web-streams-helper'
import { isDynamicRoute } from '../shared/lib/router/utils' import { isDynamicRoute } from '../shared/lib/router/utils'
import { htmlEscapeJsonString } from './htmlescape' import { ESCAPE_REGEX, htmlEscapeJsonString } from './htmlescape'
import { shouldUseReactRoot, stripInternalQueries } from './utils' import { shouldUseReactRoot, stripInternalQueries } from './utils'
import { NextApiRequestCookies } from './api-utils' import { NextApiRequestCookies } from './api-utils'
import { matchSegment } from '../client/components/match-segments' import { matchSegment } from '../client/components/match-segments'
...@@ -135,7 +135,8 @@ function useFlightResponse( ...@@ -135,7 +135,8 @@ function useFlightResponse(
writable: WritableStream<Uint8Array>, writable: WritableStream<Uint8Array>,
cachePrefix: string, cachePrefix: string,
req: ReadableStream<Uint8Array>, req: ReadableStream<Uint8Array>,
serverComponentManifest: any serverComponentManifest: any,
nonce?: string
) { ) {
const id = cachePrefix + ',' + (React as any).useId() const id = cachePrefix + ',' + (React as any).useId()
let entry = rscCache.get(id) let entry = rscCache.get(id)
...@@ -150,13 +151,17 @@ function useFlightResponse( ...@@ -150,13 +151,17 @@ function useFlightResponse(
// We only attach CSS chunks to the inlined data. // We only attach CSS chunks to the inlined data.
const forwardReader = forwardStream.getReader() const forwardReader = forwardStream.getReader()
const writer = writable.getWriter() const writer = writable.getWriter()
const startScriptTag = nonce
? `<script nonce=${JSON.stringify(nonce)}>`
: '<script>'
function process() { function process() {
forwardReader.read().then(({ done, value }) => { forwardReader.read().then(({ done, value }) => {
if (!bootstrapped) { if (!bootstrapped) {
bootstrapped = true bootstrapped = true
writer.write( writer.write(
encodeText( encodeText(
`<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString( `${startScriptTag}(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString(
JSON.stringify([0, id]) JSON.stringify([0, id])
)})</script>` )})</script>`
) )
...@@ -167,7 +172,7 @@ function useFlightResponse( ...@@ -167,7 +172,7 @@ function useFlightResponse(
writer.close() writer.close()
} else { } else {
const responsePartial = decodeText(value) const responsePartial = decodeText(value)
const scripts = `<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString( const scripts = `${startScriptTag}(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString(
JSON.stringify([1, id, responsePartial]) JSON.stringify([1, id, responsePartial])
)})</script>` )})</script>`
...@@ -205,7 +210,8 @@ function createServerComponentRenderer( ...@@ -205,7 +210,8 @@ function createServerComponentRenderer(
serverContexts: Array< serverContexts: Array<
[ServerContextName: string, JSONValue: Object | number | string] [ServerContextName: string, JSONValue: Object | number | string]
> >
} },
nonce?: string
) { ) {
// We need to expose the `__webpack_require__` API globally for // We need to expose the `__webpack_require__` API globally for
// react-server-dom-webpack. This is a hack until we find a better way. // react-server-dom-webpack. This is a hack until we find a better way.
...@@ -240,7 +246,8 @@ function createServerComponentRenderer( ...@@ -240,7 +246,8 @@ function createServerComponentRenderer(
writable, writable,
cachePrefix, cachePrefix,
reqStream, reqStream,
serverComponentManifest serverComponentManifest,
nonce
) )
return response.readRoot() return response.readRoot()
} }
...@@ -406,6 +413,56 @@ function getCssInlinedLinkTags( ...@@ -406,6 +413,56 @@ function getCssInlinedLinkTags(
return [...chunks] return [...chunks]
} }
function getScriptNonceFromHeader(cspHeaderValue: string): string | undefined {
const directives = cspHeaderValue
// Directives are split by ';'.
.split(';')
.map((directive) => directive.trim())
// First try to find the directive for the 'script-src', otherwise try to
// fallback to the 'default-src'.
const directive =
directives.find((dir) => dir.startsWith('script-src')) ||
directives.find((dir) => dir.startsWith('default-src'))
// If no directive could be found, then we're done.
if (!directive) {
return
}
// Extract the nonce from the directive
const nonce = directive
.split(' ')
// Remove the 'strict-src'/'default-src' string, this can't be the nonce.
.slice(1)
.map((source) => source.trim())
// Find the first source with the 'nonce-' prefix.
.find(
(source) =>
source.startsWith("'nonce-") &&
source.length > 8 &&
source.endsWith("'")
)
// Grab the nonce by trimming the 'nonce-' prefix.
?.slice(7, -1)
// If we could't find the nonce, then we're done.
if (!nonce) {
return
}
// Don't accept the nonce value if it contains HTML escape characters.
// Technically, the spec requires a base64'd value, but this is just an
// extra layer.
if (ESCAPE_REGEX.test(nonce)) {
throw new Error(
'Nonce value from Content-Security-Policy contained HTML escape characters.\nLearn more: https://nextjs.org/docs/messages/nonce-contained-invalid-characters'
)
}
return nonce
}
export async function renderToHTMLOrFlight( export async function renderToHTMLOrFlight(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
...@@ -426,6 +483,7 @@ export async function renderToHTMLOrFlight( ...@@ -426,6 +483,7 @@ export async function renderToHTMLOrFlight(
const { const {
buildManifest, buildManifest,
subresourceIntegrityManifest,
serverComponentManifest, serverComponentManifest,
serverCSSManifest = {}, serverCSSManifest = {},
supportsDynamicHTML, supportsDynamicHTML,
...@@ -999,6 +1057,13 @@ export async function renderToHTMLOrFlight( ...@@ -999,6 +1057,13 @@ export async function renderToHTMLOrFlight(
// TODO-APP: validate req.url as it gets passed to render. // TODO-APP: validate req.url as it gets passed to render.
const initialCanonicalUrl = req.url! const initialCanonicalUrl = req.url!
// Get the nonce from the incomming request if it has one.
const csp = req.headers['content-security-policy']
let nonce: string | undefined
if (csp && typeof csp === 'string') {
nonce = getScriptNonceFromHeader(csp)
}
/** /**
* A new React Component that renders the provided React Component * A new React Component that renders the provided React Component
* using Flight which can then be rendered to HTML. * using Flight which can then be rendered to HTML.
...@@ -1027,7 +1092,8 @@ export async function renderToHTMLOrFlight( ...@@ -1027,7 +1092,8 @@ export async function renderToHTMLOrFlight(
transformStream: serverComponentsInlinedTransformStream, transformStream: serverComponentsInlinedTransformStream,
serverComponentManifest, serverComponentManifest,
serverContexts, serverContexts,
} },
nonce
) )
const flushEffectsCallbacks: Set<() => React.ReactNode> = new Set() const flushEffectsCallbacks: Set<() => React.ReactNode> = new Set()
...@@ -1080,8 +1146,14 @@ export async function renderToHTMLOrFlight( ...@@ -1080,8 +1146,14 @@ export async function renderToHTMLOrFlight(
ReactDOMServer, ReactDOMServer,
element: content, element: content,
streamOptions: { streamOptions: {
nonce,
// Include hydration scripts in the HTML // Include hydration scripts in the HTML
bootstrapScripts: buildManifest.rootMainFiles.map( bootstrapScripts: subresourceIntegrityManifest
? buildManifest.rootMainFiles.map((src) => ({
src: `${renderOpts.assetPrefix || ''}/_next/` + src,
integrity: subresourceIntegrityManifest[src],
}))
: buildManifest.rootMainFiles.map(
(src) => `${renderOpts.assetPrefix || ''}/_next/` + src (src) => `${renderOpts.assetPrefix || ''}/_next/` + src
), ),
}, },
......
...@@ -245,6 +245,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -245,6 +245,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
params: Params params: Params
isAppPath: boolean isAppPath: boolean
appPaths?: string[] | null appPaths?: string[] | null
sriEnabled?: boolean
}): Promise<FindComponentsResult | null> }): Promise<FindComponentsResult | null>
protected abstract getFontManifest(): FontManifest | undefined protected abstract getFontManifest(): FontManifest | undefined
protected abstract getPrerenderManifest(): PrerenderManifest protected abstract getPrerenderManifest(): PrerenderManifest
...@@ -1546,8 +1547,8 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1546,8 +1547,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
params: ctx.renderOpts.params || {}, params: ctx.renderOpts.params || {},
isAppPath: Array.isArray(appPaths), isAppPath: Array.isArray(appPaths),
appPaths, appPaths,
sriEnabled: !!this.nextConfig.experimental.sri?.algorithm,
}) })
if (result) { if (result) {
try { try {
return await this.renderToResponseWithComponents(ctx, result) return await this.renderToResponseWithComponents(ctx, result)
......
...@@ -338,6 +338,15 @@ const configSchema = { ...@@ -338,6 +338,15 @@ const configSchema = {
sharedPool: { sharedPool: {
type: 'boolean', type: 'boolean',
}, },
sri: {
properties: {
algorithm: {
enum: ['sha256', 'sha384', 'sha512'] as any,
type: 'string',
},
},
type: 'object',
},
swcFileReading: { swcFileReading: {
type: 'boolean', type: 'boolean',
}, },
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
imageConfigDefault, imageConfigDefault,
} from '../shared/lib/image-config' } from '../shared/lib/image-config'
import { ServerRuntime } from 'next/types' import { ServerRuntime } from 'next/types'
import { SubresourceIntegrityAlgorithm } from '../build/webpack/plugins/subresource-integrity-plugin'
export type NextConfigComplete = Required<NextConfig> & { export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete> images: Required<ImageConfigComplete>
...@@ -146,6 +147,9 @@ export interface ExperimentalConfig { ...@@ -146,6 +147,9 @@ export interface ExperimentalConfig {
* [webpack/webpack#ModuleNotoundError.js#L13-L42](https://github.com/webpack/webpack/blob/2a0536cf510768111a3a6dceeb14cb79b9f59273/lib/ModuleNotFoundError.js#L13-L42) * [webpack/webpack#ModuleNotoundError.js#L13-L42](https://github.com/webpack/webpack/blob/2a0536cf510768111a3a6dceeb14cb79b9f59273/lib/ModuleNotFoundError.js#L13-L42)
*/ */
fallbackNodePolyfills?: false fallbackNodePolyfills?: false
sri?: {
algorithm?: SubresourceIntegrityAlgorithm
}
} }
export type ExportPathMap = { export type ExportPathMap = {
......
...@@ -38,13 +38,13 @@ export async function loadStaticPaths( ...@@ -38,13 +38,13 @@ export async function loadStaticPaths(
require('../../shared/lib/runtime-config').setConfig(config) require('../../shared/lib/runtime-config').setConfig(config)
setHttpAgentOptions(httpAgentOptions) setHttpAgentOptions(httpAgentOptions)
const components = await loadComponents( const components = await loadComponents({
distDir, distDir,
pathname, pathname,
serverless, serverless,
false, hasServerComponents: false,
false isAppPath: false,
) })
if (!components.getStaticPaths) { if (!components.getStaticPaths) {
// we shouldn't get to this point since the worker should // we shouldn't get to this point since the worker should
......
...@@ -9,7 +9,7 @@ const ESCAPE_LOOKUP: { [match: string]: string } = { ...@@ -9,7 +9,7 @@ const ESCAPE_LOOKUP: { [match: string]: string } = {
'\u2029': '\\u2029', '\u2029': '\\u2029',
} }
const ESCAPE_REGEX = /[&><\u2028\u2029]/g export const ESCAPE_REGEX = /[&><\u2028\u2029]/g
export function htmlEscapeJsonString(str: string): string { export function htmlEscapeJsonString(str: string): string {
return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match]) return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match])
......
...@@ -30,6 +30,7 @@ export type LoadComponentsReturnType = { ...@@ -30,6 +30,7 @@ export type LoadComponentsReturnType = {
Component: NextComponentType Component: NextComponentType
pageConfig: PageConfig pageConfig: PageConfig
buildManifest: BuildManifest buildManifest: BuildManifest
subresourceIntegrityManifest?: Record<string, string>
reactLoadableManifest: ReactLoadableManifest reactLoadableManifest: ReactLoadableManifest
serverComponentManifest?: any serverComponentManifest?: any
Document: DocumentType Document: DocumentType
...@@ -59,13 +60,19 @@ export async function loadDefaultErrorComponents(distDir: string) { ...@@ -59,13 +60,19 @@ export async function loadDefaultErrorComponents(distDir: string) {
} }
} }
export async function loadComponents( export async function loadComponents({
distDir: string, distDir,
pathname: string, pathname,
serverless: boolean, serverless,
hasServerComponents: boolean, hasServerComponents,
isAppPath,
}: {
distDir: string
pathname: string
serverless: boolean
hasServerComponents: boolean
isAppPath: boolean isAppPath: boolean
): Promise<LoadComponentsReturnType> { }): Promise<LoadComponentsReturnType> {
if (serverless) { if (serverless) {
const ComponentMod = await requirePage(pathname, distDir, serverless) const ComponentMod = await requirePage(pathname, distDir, serverless)
if (typeof ComponentMod === 'string') { if (typeof ComponentMod === 'string') {
......
...@@ -248,20 +248,20 @@ export default class NextNodeServer extends BaseServer { ...@@ -248,20 +248,20 @@ export default class NextNodeServer extends BaseServer {
if (!options.dev) { if (!options.dev) {
// pre-warm _document and _app as these will be // pre-warm _document and _app as these will be
// needed for most requests // needed for most requests
loadComponents( loadComponents({
this.distDir, distDir: this.distDir,
'/_document', pathname: '/_document',
this._isLikeServerless, serverless: this._isLikeServerless,
false, hasServerComponents: false,
false isAppPath: false,
).catch(() => {}) }).catch(() => {})
loadComponents( loadComponents({
this.distDir, distDir: this.distDir,
'/_app', pathname: '/_app',
this._isLikeServerless, serverless: this._isLikeServerless,
false, hasServerComponents: false,
false isAppPath: false,
).catch(() => {}) }).catch(() => {})
} }
} }
...@@ -932,39 +932,37 @@ export default class NextNodeServer extends BaseServer { ...@@ -932,39 +932,37 @@ export default class NextNodeServer extends BaseServer {
params: Params | null params: Params | null
isAppPath: boolean isAppPath: boolean
}): Promise<FindComponentsResult | null> { }): Promise<FindComponentsResult | null> {
let paths = [ const paths: string[] = [pathname]
if (query.amp) {
// try serving a static AMP version first // try serving a static AMP version first
query.amp paths.unshift(
? (isAppPath (isAppPath ? normalizeAppPath(pathname) : normalizePagePath(pathname)) +
? normalizeAppPath(pathname) '.amp'
: normalizePagePath(pathname)) + '.amp' )
: null, }
pathname,
].filter(Boolean)
if (query.__nextLocale) { if (query.__nextLocale) {
paths = [ paths.unshift(
...paths.map( ...paths.map(
(path) => `/${query.__nextLocale}${path === '/' ? '' : path}` (path) => `/${query.__nextLocale}${path === '/' ? '' : path}`
), )
...paths, )
]
} }
for (const pagePath of paths) { for (const pagePath of paths) {
try { try {
const components = await loadComponents( const components = await loadComponents({
this.distDir, distDir: this.distDir,
pagePath!, pathname: pagePath,
!this.renderOpts.dev && this._isLikeServerless, serverless: !this.renderOpts.dev && this._isLikeServerless,
!!this.renderOpts.serverComponents, hasServerComponents: !!this.renderOpts.serverComponents,
isAppPath isAppPath,
) })
if ( if (
query.__nextLocale && query.__nextLocale &&
typeof components.Component === 'string' && typeof components.Component === 'string' &&
!pagePath?.startsWith(`/${query.__nextLocale}`) !pagePath.startsWith(`/${query.__nextLocale}`)
) { ) {
// if loading an static HTML file the locale is required // if loading an static HTML file the locale is required
// to be present since all HTML files are output under their locale // to be present since all HTML files are output under their locale
......
...@@ -26,6 +26,7 @@ export const APP_PATHS_MANIFEST = 'app-paths-manifest.json' ...@@ -26,6 +26,7 @@ export const APP_PATHS_MANIFEST = 'app-paths-manifest.json'
export const APP_PATH_ROUTES_MANIFEST = 'app-path-routes-manifest.json' export const APP_PATH_ROUTES_MANIFEST = 'app-path-routes-manifest.json'
export const BUILD_MANIFEST = 'build-manifest.json' export const BUILD_MANIFEST = 'build-manifest.json'
export const APP_BUILD_MANIFEST = 'app-build-manifest.json' export const APP_BUILD_MANIFEST = 'app-build-manifest.json'
export const SUBRESOURCE_INTEGRITY_MANIFEST = 'subresource-integrity-manifest'
export const EXPORT_MARKER = 'export-marker.json' export const EXPORT_MARKER = 'export-marker.json'
export const EXPORT_DETAIL = 'export-detail.json' export const EXPORT_DETAIL = 'export-detail.json'
export const PRERENDER_MANIFEST = 'prerender-manifest.json' export const PRERENDER_MANIFEST = 'prerender-manifest.json'
......
...@@ -40,6 +40,7 @@ importers: ...@@ -40,6 +40,7 @@ importers:
'@types/http-proxy': 1.17.3 '@types/http-proxy': 1.17.3
'@types/jest': 24.0.13 '@types/jest': 24.0.13
'@types/node': 13.11.0 '@types/node': 13.11.0
'@types/node-fetch': 2.6.1
'@types/react': 16.9.17 '@types/react': 16.9.17
'@types/react-dom': 16.9.4 '@types/react-dom': 16.9.4
'@types/relay-runtime': 13.0.0 '@types/relay-runtime': 13.0.0
...@@ -194,6 +195,7 @@ importers: ...@@ -194,6 +195,7 @@ importers:
'@types/http-proxy': 1.17.3 '@types/http-proxy': 1.17.3
'@types/jest': 24.0.13 '@types/jest': 24.0.13
'@types/node': 13.11.0 '@types/node': 13.11.0
'@types/node-fetch': 2.6.1
'@types/react': 16.9.17 '@types/react': 16.9.17
'@types/react-dom': 16.9.4 '@types/react-dom': 16.9.4
'@types/relay-runtime': 13.0.0 '@types/relay-runtime': 13.0.0
......
...@@ -4,6 +4,9 @@ module.exports = { ...@@ -4,6 +4,9 @@ module.exports = {
serverComponents: true, serverComponents: true,
legacyBrowsers: false, legacyBrowsers: false,
browsersListForSwc: true, browsersListForSwc: true,
sri: {
algorithm: 'sha256',
},
}, },
// assetPrefix: '/assets', // assetPrefix: '/assets',
rewrites: async () => { rewrites: async () => {
......
import { createNext, FileRef } from 'e2e-utils' import { createNext, FileRef } from 'e2e-utils'
import crypto from 'crypto'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
import { check, fetchViaHTTP, renderViaHTTP, waitFor } from 'next-test-utils' import { check, fetchViaHTTP, renderViaHTTP, waitFor } from 'next-test-utils'
import path from 'path' import path from 'path'
...@@ -1194,6 +1195,128 @@ describe('app dir', () => { ...@@ -1194,6 +1195,128 @@ describe('app dir', () => {
}) })
}) })
}) })
;(isDev ? describe.skip : describe)('Subresource Integrity', () => {
function fetchWithPolicy(policy: string | null) {
return fetchViaHTTP(next.url, '/dashboard', undefined, {
headers: policy
? {
'Content-Security-Policy': policy,
}
: {},
})
}
async function renderWithPolicy(policy: string | null) {
const res = await fetchWithPolicy(policy)
expect(res.ok).toBe(true)
const html = await res.text()
return cheerio.load(html)
}
it('does not include nonce when not enabled', async () => {
const policies = [
`script-src 'nonce-'`, // invalid nonce
'style-src "nonce-cmFuZG9tCg=="', // no script or default src
'', // empty string
]
for (const policy of policies) {
const $ = await renderWithPolicy(policy)
// Find all the script tags without src attributes and with nonce
// attributes.
const elements = $('script[nonce]:not([src])')
// Expect there to be none.
expect(elements.length).toBe(0)
}
})
it('includes a nonce value with inline scripts when Content-Security-Policy header is defined', async () => {
// A random nonce value, base64 encoded.
const nonce = 'cmFuZG9tCg=='
// Validate all the cases where we could parse the nonce.
const policies = [
`script-src 'nonce-${nonce}'`, // base case
` script-src 'nonce-${nonce}' `, // extra space added around sources and directive
`style-src 'self'; script-src 'nonce-${nonce}'`, // extra directives
`script-src 'self' 'nonce-${nonce}' 'nonce-othernonce'`, // extra nonces
`default-src 'nonce-othernonce'; script-src 'nonce-${nonce}';`, // script and then fallback case
`default-src 'nonce-${nonce}'`, // fallback case
]
for (const policy of policies) {
const $ = await renderWithPolicy(policy)
// Find all the script tags without src attributes.
const elements = $('script:not([src])')
// Expect there to be at least 1 script tag without a src attribute.
expect(elements.length).toBeGreaterThan(0)
// Expect all inline scripts to have the nonce value.
elements.each((i, el) => {
expect(el.attribs['nonce']).toBe(nonce)
})
}
})
it('includes an integrity attribute on scripts', async () => {
const html = await renderViaHTTP(next.url, '/dashboard')
const $ = cheerio.load(html)
// Find all the script tags with src attributes.
const elements = $('script[src]')
// Expect there to be at least 1 script tag with a src attribute.
expect(elements.length).toBeGreaterThan(0)
// Collect all the scripts with integrity hashes so we can verify them.
const files: [string, string][] = []
// For each of these attributes, ensure that there's an integrity
// attribute and starts with the correct integrity hash prefix.
elements.each((i, el) => {
const integrity = el.attribs['integrity']
expect(integrity).toBeDefined()
expect(integrity).toStartWith('sha256-')
const src = el.attribs['src']
expect(src).toBeDefined()
files.push([src, integrity])
})
// For each script tag, ensure that the integrity attribute is the
// correct hash of the script tag.
for (const [src, integrity] of files) {
const res = await fetchViaHTTP(next.url, src)
expect(res.status).toBe(200)
const content = await res.text()
const hash = crypto
.createHash('sha256')
.update(content)
.digest()
.toString('base64')
expect(integrity).toEndWith(hash)
}
})
it('throws when escape characters are included in nonce', async () => {
const res = await fetchWithPolicy(
`script-src 'nonce-"><script></script>"'`
)
expect(res.status).toBe(500)
})
})
} }
describe('without assetPrefix', () => { describe('without assetPrefix', () => {
......
...@@ -15,6 +15,7 @@ import { ...@@ -15,6 +15,7 @@ import {
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import isAnimated from 'next/dist/compiled/is-animated' import isAnimated from 'next/dist/compiled/is-animated'
import type { RequestInit } from 'node-fetch'
const largeSize = 1080 // defaults defined in server/config.ts const largeSize = 1080 // defaults defined in server/config.ts
const sharpMissingText = `For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended` const sharpMissingText = `For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended`
...@@ -115,10 +116,15 @@ async function expectAvifSmallerThanWebp(w, q, appPort) { ...@@ -115,10 +116,15 @@ async function expectAvifSmallerThanWebp(w, q, appPort) {
expect(avif).toBeLessThanOrEqual(webp) expect(avif).toBeLessThanOrEqual(webp)
} }
async function fetchWithDuration(...args) { async function fetchWithDuration(
console.warn('Fetching', args[1], args[2]) appPort: string | number,
pathname: string,
query?: Record<string, any> | string,
opts?: RequestInit
) {
console.warn('Fetching', pathname, query)
const start = Date.now() const start = Date.now()
const res = await fetchViaHTTP(...args) const res = await fetchViaHTTP(appPort, pathname, query, opts)
const buffer = await res.buffer() const buffer = await res.buffer()
const duration = Date.now() - start const duration = Date.now() - start
return { duration, buffer, res } return { duration, buffer, res }
...@@ -140,7 +146,10 @@ export function runTests(ctx) { ...@@ -140,7 +146,10 @@ export function runTests(ctx) {
slowImageServer.port slowImageServer.port
}/slow.png?delay=${1}&status=308` }/slow.png?delay=${1}&status=308`
const query = { url, w: ctx.w, q: 39 } const query = { url, w: ctx.w, q: 39 }
const opts = { headers: { accept: 'image/webp' }, redirect: 'manual' } const opts: RequestInit = {
headers: { accept: 'image/webp' },
redirect: 'manual',
}
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(500) expect(res.status).toBe(500)
......
...@@ -83,6 +83,12 @@ export function initNextServerScript( ...@@ -83,6 +83,12 @@ export function initNextServerScript(
}) })
} }
/**
* @param {string | number} appPortOrUrl
* @param {string} [url]
* @param {string} [hostname]
* @returns
*/
export function getFullUrl(appPortOrUrl, url, hostname) { export function getFullUrl(appPortOrUrl, url, hostname) {
let fullUrl = let fullUrl =
typeof appPortOrUrl === 'string' && appPortOrUrl.startsWith('http') typeof appPortOrUrl === 'string' && appPortOrUrl.startsWith('http')
...@@ -110,11 +116,24 @@ export function renderViaAPI(app, pathname, query) { ...@@ -110,11 +116,24 @@ export function renderViaAPI(app, pathname, query) {
return app.renderToHTML({ url }, {}, pathname, query) return app.renderToHTML({ url }, {}, pathname, query)
} }
/**
* @param {string | number} appPort
* @param {string} pathname
* @param {Record<string, any> | string | undefined} [query]
* @param {import('node-fetch').RequestInit} [opts]
* @returns {Promise<string>}
*/
export function renderViaHTTP(appPort, pathname, query, opts) { export function renderViaHTTP(appPort, pathname, query, opts) {
return fetchViaHTTP(appPort, pathname, query, opts).then((res) => res.text()) return fetchViaHTTP(appPort, pathname, query, opts).then((res) => res.text())
} }
/** @return {Promise<Response & {buffer: any} & {headers: any}>} */ /**
* @param {string | number} appPort
* @param {string} pathname
* @param {Record<string, any> | string | undefined} [query]
* @param {import('node-fetch').RequestInit} [opts]
* @returns {Promise<Response & {buffer: any} & {headers: any}>}
*/
export function fetchViaHTTP(appPort, pathname, query, opts) { export function fetchViaHTTP(appPort, pathname, query, opts) {
const url = `${pathname}${ const url = `${pathname}${
typeof query === 'string' ? query : query ? `?${qs.stringify(query)}` : '' typeof query === 'string' ? query : query ? `?${qs.stringify(query)}` : ''
......
...@@ -171,7 +171,7 @@ describe('should set-up next', () => { ...@@ -171,7 +171,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'show') await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gsp', undefined, { const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
...@@ -182,7 +182,7 @@ describe('should set-up next', () => { ...@@ -182,7 +182,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'hide') await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, { const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
expect(res2.status).toBe(404) expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe( expect(res2.headers.get('cache-control')).toBe(
...@@ -194,7 +194,7 @@ describe('should set-up next', () => { ...@@ -194,7 +194,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'show') await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gssp', undefined, { const res = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
...@@ -204,7 +204,7 @@ describe('should set-up next', () => { ...@@ -204,7 +204,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'hide') await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, { const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
await next.patchFile('standalone/data.txt', 'show') await next.patchFile('standalone/data.txt', 'show')
......
...@@ -422,7 +422,7 @@ describe('should set-up next', () => { ...@@ -422,7 +422,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'show') await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gsp', undefined, { const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
...@@ -433,7 +433,7 @@ describe('should set-up next', () => { ...@@ -433,7 +433,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'hide') await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, { const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
expect(res2.status).toBe(404) expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe( expect(res2.headers.get('cache-control')).toBe(
...@@ -445,7 +445,7 @@ describe('should set-up next', () => { ...@@ -445,7 +445,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'show') await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gssp', undefined, { const res = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
...@@ -455,7 +455,7 @@ describe('should set-up next', () => { ...@@ -455,7 +455,7 @@ describe('should set-up next', () => {
await next.patchFile('standalone/data.txt', 'hide') await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, { const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ', redirect: 'manual',
}) })
await next.patchFile('standalone/data.txt', 'show') await next.patchFile('standalone/data.txt', 'show')
......
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