Unverified Commit ae61e7e0 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Move key generation to the earlier stage (#63832)

Currently, the encryption key for Server Actions is generated during the
manifest creation. This PR moves it to be in the same position as the
build ID generation, and we will later leverage that key for other use
cases.

Closes NEXT-2959
parent e519634c
...@@ -54,6 +54,7 @@ export const NextBuildContext: Partial<{ ...@@ -54,6 +54,7 @@ export const NextBuildContext: Partial<{
// core fields // core fields
dir: string dir: string
buildId: string buildId: string
encryptionKey: string
config: NextConfigComplete config: NextConfigComplete
appDir: string appDir: string
pagesDir: string pagesDir: string
......
...@@ -182,6 +182,7 @@ import { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader ...@@ -182,6 +182,7 @@ import { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader
import type { Entrypoints } from '../server/dev/turbopack/types' import type { Entrypoints } from '../server/dev/turbopack/types'
import { buildCustomRoute } from '../lib/build-custom-route' import { buildCustomRoute } from '../lib/build-custom-route'
import { createProgress } from './progress' import { createProgress } from './progress'
import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils'
interface ExperimentalBypassForInfo { interface ExperimentalBypassForInfo {
experimentalBypassFor?: RouteHas[] experimentalBypassFor?: RouteHas[]
...@@ -765,6 +766,11 @@ export default async function build( ...@@ -765,6 +766,11 @@ export default async function build(
pages: typeof pagesDir === 'string', pages: typeof pagesDir === 'string',
} }
// Generate a random encryption key for this build.
// This key is used to encrypt cross boundary values and can be used to generate hashes.
const encryptionKey = await generateEncryptionKeyBase64()
NextBuildContext.encryptionKey = encryptionKey
const isSrcDir = path const isSrcDir = path
.relative(dir, pagesDir || appDir || '') .relative(dir, pagesDir || appDir || '')
.startsWith('src') .startsWith('src')
...@@ -1396,7 +1402,11 @@ export default async function build( ...@@ -1396,7 +1402,11 @@ export default async function build(
const currentEntryIssues: EntryIssuesMap = new Map() const currentEntryIssues: EntryIssuesMap = new Map()
const manifestLoader = new TurbopackManifestLoader({ buildId, distDir }) const manifestLoader = new TurbopackManifestLoader({
buildId,
distDir,
encryptionKey,
})
// TODO: implement this // TODO: implement this
const emptyRewritesObjToBeImplemented = { const emptyRewritesObjToBeImplemented = {
......
...@@ -106,6 +106,7 @@ export async function webpackBuildImpl( ...@@ -106,6 +106,7 @@ export async function webpackBuildImpl(
const commonWebpackOptions = { const commonWebpackOptions = {
isServer: false, isServer: false,
buildId: NextBuildContext.buildId!, buildId: NextBuildContext.buildId!,
encryptionKey: NextBuildContext.encryptionKey!,
config: config, config: config,
appDir: NextBuildContext.appDir!, appDir: NextBuildContext.appDir!,
pagesDir: NextBuildContext.pagesDir!, pagesDir: NextBuildContext.pagesDir!,
......
...@@ -300,6 +300,7 @@ export default async function getBaseWebpackConfig( ...@@ -300,6 +300,7 @@ export default async function getBaseWebpackConfig(
dir: string, dir: string,
{ {
buildId, buildId,
encryptionKey,
config, config,
compilerType, compilerType,
dev = false, dev = false,
...@@ -321,6 +322,7 @@ export default async function getBaseWebpackConfig( ...@@ -321,6 +322,7 @@ export default async function getBaseWebpackConfig(
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
}: { }: {
buildId: string buildId: string
encryptionKey: string
config: NextConfigComplete config: NextConfigComplete
compilerType: CompilerNameValues compilerType: CompilerNameValues
dev?: boolean dev?: boolean
...@@ -1867,6 +1869,7 @@ export default async function getBaseWebpackConfig( ...@@ -1867,6 +1869,7 @@ export default async function getBaseWebpackConfig(
appDir, appDir,
dev, dev,
isEdgeServer, isEdgeServer,
encryptionKey,
})), })),
hasAppDir && hasAppDir &&
!isClient && !isClient &&
......
...@@ -39,7 +39,6 @@ import { ...@@ -39,7 +39,6 @@ import {
} from '../utils' } from '../utils'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { getProxiedPluginState } from '../../build-context' import { getProxiedPluginState } from '../../build-context'
import { generateEncryptionKeyBase64 } from '../../../server/app-render/encryption-utils'
import { PAGE_TYPES } from '../../../lib/page-types' import { PAGE_TYPES } from '../../../lib/page-types'
import { isWebpackServerOnlyLayer } from '../../utils' import { isWebpackServerOnlyLayer } from '../../utils'
...@@ -47,6 +46,7 @@ interface Options { ...@@ -47,6 +46,7 @@ interface Options {
dev: boolean dev: boolean
appDir: string appDir: string
isEdgeServer: boolean isEdgeServer: boolean
encryptionKey: string
} }
const PLUGIN_NAME = 'FlightClientEntryPlugin' const PLUGIN_NAME = 'FlightClientEntryPlugin'
...@@ -166,6 +166,7 @@ function deduplicateCSSImportsForEntry(mergedCSSimports: CssImports) { ...@@ -166,6 +166,7 @@ function deduplicateCSSImportsForEntry(mergedCSSimports: CssImports) {
export class FlightClientEntryPlugin { export class FlightClientEntryPlugin {
dev: boolean dev: boolean
appDir: string appDir: string
encryptionKey: string
isEdgeServer: boolean isEdgeServer: boolean
assetPrefix: string assetPrefix: string
...@@ -174,6 +175,7 @@ export class FlightClientEntryPlugin { ...@@ -174,6 +175,7 @@ export class FlightClientEntryPlugin {
this.appDir = options.appDir this.appDir = options.appDir
this.isEdgeServer = options.isEdgeServer this.isEdgeServer = options.isEdgeServer
this.assetPrefix = !this.dev && !this.isEdgeServer ? '../' : '' this.assetPrefix = !this.dev && !this.isEdgeServer ? '../' : ''
this.encryptionKey = options.encryptionKey
} }
apply(compiler: webpack.Compiler) { apply(compiler: webpack.Compiler) {
...@@ -1000,9 +1002,7 @@ export class FlightClientEntryPlugin { ...@@ -1000,9 +1002,7 @@ export class FlightClientEntryPlugin {
{ {
node: serverActions, node: serverActions,
edge: edgeServerActions, edge: edgeServerActions,
encryptionKey: this.encryptionKey,
// Assign encryption
encryptionKey: await generateEncryptionKeyBase64(this.dev),
}, },
null, null,
this.dev ? 2 : undefined this.dev ? 2 : undefined
......
...@@ -79,6 +79,7 @@ import { ...@@ -79,6 +79,7 @@ import {
splitEntryKey, splitEntryKey,
} from './turbopack/entry-key' } from './turbopack/entry-key'
import { FAST_REFRESH_RUNTIME_RELOAD } from './messages' import { FAST_REFRESH_RUNTIME_RELOAD } from './messages'
import { generateEncryptionKeyBase64 } from '../app-render/encryption-utils'
const wsServer = new ws.Server({ noServer: true }) const wsServer = new ws.Server({ noServer: true })
const isTestMode = !!( const isTestMode = !!(
...@@ -161,7 +162,11 @@ export async function createHotReloaderTurbopack( ...@@ -161,7 +162,11 @@ export async function createHotReloaderTurbopack(
const currentTopLevelIssues: TopLevelIssuesMap = new Map() const currentTopLevelIssues: TopLevelIssuesMap = new Map()
const currentEntryIssues: EntryIssuesMap = new Map() const currentEntryIssues: EntryIssuesMap = new Map()
const manifestLoader = new TurbopackManifestLoader({ buildId, distDir }) const manifestLoader = new TurbopackManifestLoader({
buildId,
distDir,
encryptionKey: await generateEncryptionKeyBase64(),
})
// Dev specific // Dev specific
const changeSubscriptions: ChangeSubscriptions = new Map() const changeSubscriptions: ChangeSubscriptions = new Map()
......
...@@ -230,6 +230,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -230,6 +230,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
private hasPagesRouterEntrypoints: boolean private hasPagesRouterEntrypoints: boolean
private dir: string private dir: string
private buildId: string private buildId: string
private encryptionKey: string
private interceptors: any[] private interceptors: any[]
private pagesDir?: string private pagesDir?: string
private distDir: string private distDir: string
...@@ -271,6 +272,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -271,6 +272,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
pagesDir, pagesDir,
distDir, distDir,
buildId, buildId,
encryptionKey,
previewProps, previewProps,
rewrites, rewrites,
appDir, appDir,
...@@ -280,6 +282,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -280,6 +282,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
pagesDir?: string pagesDir?: string
distDir: string distDir: string
buildId: string buildId: string
encryptionKey: string
previewProps: __ApiPreviewProps previewProps: __ApiPreviewProps
rewrites: CustomRoutes['rewrites'] rewrites: CustomRoutes['rewrites']
appDir?: string appDir?: string
...@@ -290,6 +293,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -290,6 +293,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
this.hasAppRouterEntrypoints = false this.hasAppRouterEntrypoints = false
this.hasPagesRouterEntrypoints = false this.hasPagesRouterEntrypoints = false
this.buildId = buildId this.buildId = buildId
this.encryptionKey = encryptionKey
this.dir = dir this.dir = dir
this.interceptors = [] this.interceptors = []
this.pagesDir = pagesDir this.pagesDir = pagesDir
...@@ -615,6 +619,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -615,6 +619,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
const commonWebpackOptions = { const commonWebpackOptions = {
dev: true, dev: true,
buildId: this.buildId, buildId: this.buildId,
encryptionKey: this.encryptionKey,
config: this.config, config: this.config,
pagesDir: this.pagesDir, pagesDir: this.pagesDir,
rewrites: this.rewrites, rewrites: this.rewrites,
...@@ -671,6 +676,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -671,6 +676,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
compilerType: COMPILER_NAMES.client, compilerType: COMPILER_NAMES.client,
config: this.config, config: this.config,
buildId: this.buildId, buildId: this.buildId,
encryptionKey: this.encryptionKey,
pagesDir: this.pagesDir, pagesDir: this.pagesDir,
rewrites: { rewrites: {
beforeFiles: [], beforeFiles: [],
......
...@@ -7,7 +7,6 @@ import type { AppBuildManifest } from '../../../build/webpack/plugins/app-build- ...@@ -7,7 +7,6 @@ import type { AppBuildManifest } from '../../../build/webpack/plugins/app-build-
import type { PagesManifest } from '../../../build/webpack/plugins/pages-manifest-plugin' import type { PagesManifest } from '../../../build/webpack/plugins/pages-manifest-plugin'
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp' import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import type { ActionManifest } from '../../../build/webpack/plugins/flight-client-entry-plugin' import type { ActionManifest } from '../../../build/webpack/plugins/flight-client-entry-plugin'
import { generateEncryptionKeyBase64 } from '../../app-render/encryption-utils'
import type { NextFontManifest } from '../../../build/webpack/plugins/next-font-manifest-plugin' import type { NextFontManifest } from '../../../build/webpack/plugins/next-font-manifest-plugin'
import type { LoadableManifest } from '../../load-components' import type { LoadableManifest } from '../../load-components'
import { import {
...@@ -86,13 +85,23 @@ export class TurbopackManifestLoader { ...@@ -86,13 +85,23 @@ export class TurbopackManifestLoader {
private middlewareManifests: Map<EntryKey, TurbopackMiddlewareManifest> = private middlewareManifests: Map<EntryKey, TurbopackMiddlewareManifest> =
new Map() new Map()
private pagesManifests: Map<string, PagesManifest> = new Map() private pagesManifests: Map<string, PagesManifest> = new Map()
private encryptionKey: string
private readonly distDir: string private readonly distDir: string
private readonly buildId: string private readonly buildId: string
constructor({ distDir, buildId }: { buildId: string; distDir: string }) { constructor({
distDir,
buildId,
encryptionKey,
}: {
buildId: string
distDir: string
encryptionKey: string
}) {
this.distDir = distDir this.distDir = distDir
this.buildId = buildId this.buildId = buildId
this.encryptionKey = encryptionKey
} }
delete(key: EntryKey) { delete(key: EntryKey) {
...@@ -123,7 +132,7 @@ export class TurbopackManifestLoader { ...@@ -123,7 +132,7 @@ export class TurbopackManifestLoader {
const manifest: ActionManifest = { const manifest: ActionManifest = {
node: {}, node: {},
edge: {}, edge: {},
encryptionKey: await generateEncryptionKeyBase64(true), encryptionKey: this.encryptionKey,
} }
function mergeActionIds( function mergeActionIds(
......
...@@ -75,6 +75,7 @@ import { PAGE_TYPES } from '../../../lib/page-types' ...@@ -75,6 +75,7 @@ import { PAGE_TYPES } from '../../../lib/page-types'
import { createHotReloaderTurbopack } from '../../dev/hot-reloader-turbopack' import { createHotReloaderTurbopack } from '../../dev/hot-reloader-turbopack'
import { getErrorSource } from '../../../shared/lib/error-source' import { getErrorSource } from '../../../shared/lib/error-source'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import { generateEncryptionKeyBase64 } from '../../app-render/encryption-utils'
export type SetupOpts = { export type SetupOpts = {
renderServer: LazyRenderServerInstance renderServer: LazyRenderServerInstance
...@@ -162,6 +163,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -162,6 +163,7 @@ async function startWatcher(opts: SetupOpts) {
distDir: distDir, distDir: distDir,
config: opts.nextConfig, config: opts.nextConfig,
buildId: 'development', buildId: 'development',
encryptionKey: await generateEncryptionKeyBase64(),
telemetry: opts.telemetry, telemetry: opts.telemetry,
rewrites: opts.fsChecker.rewrites, rewrites: opts.fsChecker.rewrites,
previewProps: opts.fsChecker.prerenderManifest.preview, previewProps: opts.fsChecker.prerenderManifest.preview,
......
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