Unverified Commit 447e018f authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Nudge security upgrades for manual `next dev|build` (#98872)

Counter part of agent nudge. Prompt user when runs `next dev` or `next
build` based on `experimental.agenticAutoUpgrade` value. This PR covers
`security`.
parent 1a723afd
import type { NudgeKind } from '../lib/upgrade/nudge'
import type { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import type {
ExportPathMap,
......@@ -1067,8 +1068,9 @@ export default async function build(
experimentalBuildMode: 'default' | 'compile' | 'generate' | 'generate-env',
traceUploadUrl: string | undefined,
debugBuildPathsPatterns: string[] | undefined,
enabledFeatures: Record<string, unknown> = {}
): Promise<void> {
enabledFeatures: Record<string, unknown> = {},
allowHumanUpgrade = false
): Promise<NudgeKind | 'interrupt' | void> {
const isCompileMode = experimentalBuildMode === 'compile'
const isGenerateMode = experimentalBuildMode === 'generate'
NextBuildContext.isCompileMode = isCompileMode
......@@ -1108,7 +1110,7 @@ export default async function build(
NextBuildContext.noMangling = noMangling
NextBuildContext.debugPrerender = debugPrerender
await nextBuildSpan.traceAsyncFn(async () => {
return await nextBuildSpan.traceAsyncFn(async () => {
// attempt to load global env values so they are available in next.config.js
const { loadedEnvFiles } = nextBuildSpan
.traceChild('load-dotenv')
......@@ -1151,14 +1153,39 @@ export default async function build(
if (
config.experimental.agenticAutoUpgrade === 'security' ||
config.experimental.agenticAutoUpgrade === 'latest' ||
config.experimental.agenticAutoUpgrade === 'future'
config.experimental.agenticAutoUpgrade === 'future' ||
process.env.__NEXT_AGENTIC_AUTO_UPGRADE
) {
const { nudgeForUpgrade } =
const { nudgeUpgrade, getUpgradeContext } =
require('../lib/upgrade/nudge') as typeof import('../lib/upgrade/nudge')
pendingUpgradeNudge = nudgeForUpgrade(dir, config, 'build')
// Build work proceeds in parallel, but a fatal security result must be
// observed before the command reports successful completion.
void pendingUpgradeNudge.catch(() => {})
const upgradeContext = getUpgradeContext(config)
if (allowHumanUpgrade) {
// TODO: Do not block the build while prompting for an upgrade.
// Preserve all logs for display after the prompt and stop the build before Update.
const action = await nudgeUpgrade(
dir,
upgradeContext,
'build',
new AbortController().signal
).catch((error) => {
Log.warn(`Could not offer the upgrade: ${String(error)}`)
})
if (
action === 'update' &&
upgradeContext.experimental.agenticAutoUpgrade
) {
return upgradeContext.experimental.agenticAutoUpgrade
}
if (action === 'interrupt') {
return 'interrupt' as const
}
} else {
// Agent checks retain their parallel behavior; humans decide before building.
pendingUpgradeNudge = nudgeUpgrade(dir, upgradeContext, 'build').then(
() => {}
)
void pendingUpgradeNudge.catch(() => {})
}
}
// Resolve selective build paths now that the page extensions are known.
......
......@@ -35,14 +35,20 @@ export type NextBuildOptions = {
const nextBuild = async (options: NextBuildOptions, directory?: string) => {
process.title = `next-build (v${process.env.__NEXT_VERSION})`
process.on('SIGTERM', () => {
const onTerminate = () => {
saveCpuProfile()
process.exit(143)
})
process.on('SIGINT', () => {
}
const onInterrupt = () => {
saveCpuProfile()
process.exit(130)
})
}
const onHangup = () => {
saveCpuProfile()
process.exit(129)
}
process.on('SIGTERM', onTerminate)
process.on('SIGINT', onInterrupt)
const {
experimentalAnalyze,
......@@ -120,6 +126,14 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
}).filter(([_, value]) => value !== undefined && value !== false)
)
const { shouldPromptForUpgrade, runUpgrade } = await import(
'../lib/upgrade/nudge.js'
)
const humanUpgrade = await shouldPromptForUpgrade()
if (humanUpgrade) {
process.on('SIGHUP', onHangup)
}
return build(
dir,
experimentalAnalyze,
......@@ -132,8 +146,20 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
experimentalBuildMode,
traceUploadUrl,
debugBuildPathsPatterns,
enabledFeatures
enabledFeatures,
humanUpgrade
)
.then(async (action) => {
if (action === 'interrupt') {
process.exit(130)
}
if (action) {
process.off('SIGTERM', onTerminate)
process.off('SIGINT', onInterrupt)
process.off('SIGHUP', onHangup)
process.exit(await runUpgrade(dir, action))
}
})
.catch((err) => {
if (experimentalDebugMemoryUsage) {
disableMemoryDebuggingMode()
......
......@@ -29,6 +29,7 @@ import uploadTrace from '../trace/upload-trace'
import { initialEnv } from '@next/env'
import { fork } from 'child_process'
import type { ChildProcess } from 'child_process'
import type { UpgradeContext } from '../lib/upgrade/nudge'
import {
getReservedPortExplanation,
isPortIsReserved,
......@@ -72,6 +73,10 @@ let distDir: string | undefined
let isTurbopack: boolean
let traceUploadUrl: string
let sessionStopHandled = false
let upgradeController: AbortController | null = null
let upgradeOffered = false
let upgradeInProgress = false
let interruption: NodeJS.Signals | null = null
let devSpanAttrs: { 'rage-restart': boolean; 'missing-next-dir': boolean } = {
'rage-restart': false,
'missing-next-dir': false,
......@@ -105,10 +110,19 @@ const CHILD_EXIT_TIMEOUT_MS = parseInt(
const shouldWaitForChildExit =
process.env.__NEXT_DEV_WAIT_FOR_TURBOPACK_SHUTDOWN === '1'
const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
if (signal != null && child?.pid) child.kill(signal)
if (sessionStopHandled) return
const handleSessionStop = async (
signal: NodeJS.Signals | number | null,
exit = true
) => {
if (signal != null && child?.pid) {
child.kill(signal)
}
if (sessionStopHandled) {
return
}
sessionStopHandled = true
const interruptedUpgrade = upgradeInProgress
upgradeController?.abort()
// Capture the child's exit code if it has already exited and caused the
// session stop (via the 'exit' event), otherwise assume success (0).
......@@ -191,11 +205,29 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
// the program, or the cursor could remain hidden
process.stdout.write('\x1B[?25h')
process.stdout.write('\n')
process.exit(exitCode)
if (exit) {
process.exit(
interruption && (interruption === 'SIGHUP' || interruptedUpgrade)
? 128 + os.constants.signals[interruption]
: exitCode
)
}
}
process.on('SIGINT', () => handleSessionStop('SIGINT'))
process.on('SIGTERM', () => handleSessionStop('SIGTERM'))
const onInterrupt = () => {
interruption = 'SIGINT'
void handleSessionStop('SIGINT')
}
const onTerminate = () => {
interruption = 'SIGTERM'
void handleSessionStop('SIGTERM')
}
const onHangup = () => {
interruption = 'SIGHUP'
void handleSessionStop('SIGTERM')
}
process.on('SIGINT', onInterrupt)
process.on('SIGTERM', onTerminate)
// exit event must be synchronous
process.on('exit', () => {
......@@ -218,6 +250,52 @@ const nextDev = async (
dir = getProjectDir(process.env.NEXT_PRIVATE_DEV_DIR || directory)
const { shouldPromptForUpgrade, runUpgrade, nudgeUpgrade } = await import(
'../lib/upgrade/nudge.js'
)
const humanUpgrade = await shouldPromptForUpgrade()
async function offerUpgrade(worker: ChildProcess, context: UpgradeContext) {
process.on('SIGHUP', onHangup)
upgradeOffered = true
upgradeInProgress = true
const controller = new AbortController()
upgradeController = controller
let action
try {
action = await nudgeUpgrade(dir, context, 'dev', controller.signal)
} catch (error) {
Log.warn(`Could not offer the upgrade: ${String(error)}`)
} finally {
upgradeController = null
}
if (controller.signal.aborted || sessionStopHandled) {
upgradeInProgress = false
process.off('SIGHUP', onHangup)
return
}
if (action === 'interrupt') {
onInterrupt()
return
}
if (action === 'update' && context.experimental.agenticAutoUpgrade) {
await handleSessionStop('SIGTERM', false)
if (interruption) {
process.exit(128 + os.constants.signals[interruption])
}
process.off('SIGINT', onInterrupt)
process.off('SIGTERM', onTerminate)
process.off('SIGHUP', onHangup)
process.exit(
await runUpgrade(dir, context.experimental.agenticAutoUpgrade)
)
}
upgradeInProgress = false
process.off('SIGHUP', onHangup)
if (worker.connected) {
worker.send({ nextUpgradeContinue: true })
}
}
// Check if pages dir exists and warn if not
if (!(await fileExists(dir, FileType.Directory))) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
......@@ -406,6 +484,8 @@ const nextDev = async (
__NEXT_DEV_SERVER: '1',
NEXT_PRIVATE_START_TIME: process.env.NEXT_PRIVATE_START_TIME,
NEXT_PRIVATE_WORKER: '1',
NEXT_PRIVATE_UPGRADE_PROMPT:
humanUpgrade && !upgradeOffered ? '1' : undefined,
NEXT_PRIVATE_TRACE_ID: traceId,
NEXT_PRIVATE_ENABLED_FEATURES: JSON.stringify(enabledFeatures),
NEXT_PRIVATE_DEV_SPAN_ATTRS: JSON.stringify(devSpanAttrs),
......@@ -434,7 +514,16 @@ const nextDev = async (
child.on('message', (msg: any) => {
if (msg && typeof msg === 'object') {
if (msg.nextWorkerReady) {
if (msg.nextUpgradeContext) {
distDir = msg.nextUpgradeContext.distDir
void offerUpgrade(child!, msg.nextUpgradeContext).catch(
async (error) => {
console.error(error)
await handleSessionStop('SIGTERM', false)
process.exit(1)
}
)
} else if (msg.nextWorkerReady) {
child?.send({ nextWorkerOptions: startServerOptions })
} else if (msg.nextServerReady && !resolved) {
if (msg.port) {
......@@ -454,7 +543,15 @@ const nextDev = async (
})
child.on('exit', async (code, signal) => {
if (sessionStopHandled || signal) {
upgradeController?.abort()
if (sessionStopHandled) {
return
}
if (signal) {
if (upgradeInProgress) {
interruption ??= signal
await handleSessionStop(null)
}
return
}
if (code === RESTART_EXIT_CODE) {
......
......@@ -5,7 +5,7 @@ export type UpgradeDocument = `docs/${string}.md` | `skills/${string}/SKILL.md`
type FutureDefault = {
name: string
availableSince: string
isAdopted(config: NextConfigComplete): boolean
isAdopted(config: Pick<NextConfigComplete, 'cacheComponents'>): boolean
adoptionDoc: readonly UpgradeDocument[]
optimizationDoc: readonly UpgradeDocument[]
}
......
This diff is collapsed.
......@@ -98,7 +98,8 @@ export type UpgradeAssessment = {
// target, so expire cached results and refresh metadata before execution.
export async function getUpgradeAssessment(
installedVersion: string,
policy: 'security' | 'latest' | 'future'
policy: 'security' | 'latest' | 'future',
onlyIfAffected: boolean = false
): Promise<UpgradeAssessment> {
if (!semver.valid(installedVersion)) {
throw new Error('The running Next.js version is not valid semver.')
......@@ -109,7 +110,7 @@ export async function getUpgradeAssessment(
)
}
const canary = isCanary(installedVersion)
if (canary && policy === 'security') {
if (canary && (policy === 'security' || onlyIfAffected)) {
return {
affected: null,
reference: null,
......@@ -135,7 +136,9 @@ export async function getUpgradeAssessment(
}
}
if (policy === 'security' && !affected) {
// A dismissed release reminder still checks advisories, but does not need
// target metadata unless an advisory applies.
if ((policy === 'security' || onlyIfAffected) && !affected) {
return {
...assessment,
upgrade: {
......
import { emitKeypressEvents, type Key } from 'readline'
import { PassThrough } from 'stream'
import cliSelect from 'next/dist/compiled/cli-select'
import { bold, cyan } from '../picocolors'
export type UpgradeAction = 'update' | 'skip' | 'dismiss' | 'interrupt'
export async function promptUpgrade(
message: string,
signal: AbortSignal,
canUpdate: boolean = true
): Promise<UpgradeAction> {
if (signal.aborted) {
return 'skip'
}
const input = process.stdin
const terminal = process.stdout
const values = {
...(canUpdate ? { update: 'Upgrade now' } : {}),
skip: 'Skip',
dismiss: 'Skip until next version',
}
const labels = Object.values(values)
const heading = `${message}\n\n`
const wasRaw = input.isRaw ?? false
const wasFlowing = input.readableFlowing
// Isolate cancellation from other consumers of stdin. cli-select can close
// this stream without changing the real terminal's original raw/flow state.
const keys = Object.assign(new PassThrough(), { setRawMode() {} })
let interrupted = false
let cancelled = false
let resized = false
let selectedIndex = 0
const renderValue = (value: string, selected: boolean) => {
if (value === labels[0]) {
// We own this screen. Redraw from the top instead of relying on
// cli-select's one-row-per-choice cursor movement when choices wrap.
terminal.write(`\x1b[H\x1b[2J${heading}`)
}
if (selected) {
selectedIndex = labels.indexOf(value)
}
return selected ? cyan(bold(value)) : value
}
const cancel = () => {
cancelled = true
keys.emit('keypress', '', { name: 'escape' })
}
const onKey = (text: string, key: Key) => {
if (key?.ctrl && key.name === 'c') {
interrupted = true
}
if (key?.name === 'escape') {
cancelled = true
}
keys.emit('keypress', text, key)
}
const onResize = () => {
resized = true
// A terminal write can emit resize while cli-select is still opening.
// Wait until its selection callback is installed before cancelling it.
queueMicrotask(() => {
if (resized && !restored) {
keys.emit('keypress', '', { name: 'escape' })
}
})
}
let restored = false
const restore = () => {
if (restored) {
return
}
restored = true
input.removeListener('keypress', onKey)
terminal.removeListener('resize', onResize)
signal.removeEventListener('abort', cancel)
process.removeListener('exit', restore)
keys.destroy()
try {
input.setRawMode(wasRaw)
} finally {
if (wasFlowing !== true) {
input.pause()
}
terminal.write('\x1b[?1049l\x1b[?25h')
}
}
// CLI signal handlers may exit synchronously, before the promise settles.
process.once('exit', restore)
try {
// Keep startup output on the normal screen while the menu owns the terminal.
terminal.write('\x1b[?1049h')
emitKeypressEvents(input)
input.on('keypress', onKey)
terminal.on('resize', onResize)
signal.addEventListener('abort', cancel, { once: true })
input.setRawMode(true)
input.resume()
while (true) {
resized = false
const selection = cliSelect({
values,
defaultValue: selectedIndex,
selected: cyan('❯'),
unselected: ' ',
indentation: 2,
cleanup: true,
// cli-select types inputStream as a WriteStream, but only consumes
// keypress events and the raw-mode methods supplied by this proxy.
inputStream: keys as unknown as NodeJS.WriteStream,
outputStream: terminal,
valueRenderer: renderValue,
})
if (signal.aborted || cancelled) {
cancel()
}
try {
const { id } = await selection
return signal.aborted || cancelled ? 'skip' : (id as UpgradeAction)
} catch (error) {
// cli-select rejects without a reason for Escape / Ctrl+C.
if (error) {
throw error
}
if (!resized || signal.aborted || cancelled || interrupted) {
return interrupted ? 'interrupt' : 'skip'
}
}
}
} finally {
restore()
}
}
......@@ -13,12 +13,15 @@ export function runChildProcess(
return new Promise((resolve, reject) => {
const onInterrupt = () => child.kill('SIGINT')
const onTerminate = () => child.kill('SIGTERM')
const onHangup = () => child.kill('SIGHUP')
process.on('SIGINT', onInterrupt)
process.on('SIGTERM', onTerminate)
process.on('SIGHUP', onHangup)
const cleanup = () => {
process.removeListener('SIGINT', onInterrupt)
process.removeListener('SIGTERM', onTerminate)
process.removeListener('SIGHUP', onHangup)
}
child.once('error', (error: Error) => {
......
......@@ -222,12 +222,31 @@ export async function initialize(opts: {
if (
developmentConfig.experimental.agenticAutoUpgrade === 'security' ||
developmentConfig.experimental.agenticAutoUpgrade === 'latest' ||
developmentConfig.experimental.agenticAutoUpgrade === 'future'
developmentConfig.experimental.agenticAutoUpgrade === 'future' ||
process.env.__NEXT_AGENTIC_AUTO_UPGRADE
) {
const { nudgeForUpgrade } =
const { nudgeUpgrade, getUpgradeContext } =
require('../../lib/upgrade/nudge') as typeof import('../../lib/upgrade/nudge')
void nudgeForUpgrade(opts.dir, developmentConfig, 'dev').catch(
(error) => {
if (process.env.NEXT_PRIVATE_UPGRADE_PROMPT === '1' && process.send) {
// TODO: Do not block dev startup while prompting for an upgrade.
// Preserve all logs for display after the prompt and stop dev before Update.
// The existing dev worker pauses here while its parent owns the menu.
await new Promise<void>((resolve) => {
const resume = (message: {
nextUpgradeContinue: boolean | undefined
}) => {
if (message.nextUpgradeContinue) {
process.off('message', resume)
resolve()
}
}
process.on('message', resume)
process.send!({
nextUpgradeContext: getUpgradeContext(developmentConfig),
})
})
} else {
void nudgeUpgrade(opts.dir, developmentConfig, 'dev').catch((error) => {
const { printAndExit } =
require('./utils') as typeof import('./utils')
const exitCode =
......@@ -238,8 +257,8 @@ export async function initialize(opts: {
error instanceof Error ? error.message : String(error),
typeof exitCode === 'number' ? exitCode : 1
)
}
)
})
}
}
// Resolve the effective serverFastRefresh value.
......
......@@ -201,6 +201,27 @@ describe('prepare latest upgrade', () => {
}
)
it('does not request canary target metadata after a release dismissal', async () => {
global.fetch = jest.fn()
await expect(
getUpgradeAssessment('16.4.0-canary.1', 'future', true)
).resolves.toMatchObject({ affected: null, upgrade: { status: 'blocked' } })
expect(global.fetch).toHaveBeenCalledTimes(0)
})
it('checks advisories without target metadata after a release dismissal', async () => {
mockLatestVersion('17.0.0')
await expect(
getUpgradeAssessment('16.4.0', 'future', true)
).resolves.toMatchObject({
affected: false,
upgrade: { status: 'unaffected' },
})
expect(
jest.mocked(global.fetch).mock.calls.map(([url]) => String(url))
).toEqual([expect.stringContaining('https://api.github.com/advisories?')])
})
describe('shared stable eligibility', () => {
const installed = '17.2.0'
it.each(['security', 'latest', 'future'] as const)(
......
This diff is collapsed.
This diff is collapsed.
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