Unverified Commit 9bb13254 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Nudge the agents for security vulnerable version upgrade (#98637)

Stacked on #98562.

> [!TIP]
> Recommended to review commit by commit.

This PR adds `experimental.agenticAutoUpgrade = 'security'` config which
enables nudging the agents to notify the user when the app's Next.js
version has any security advisories. The nudge will include guiding to
upgrade via `next upgrade --ai` (run "security" by detecting config).

The method of nudging leverages the agents behavior where they tend to
listen to messages from fatal errors that blocks the process compared to
general error/warning logs. Whenever the agents run `next dev` or `next
build`, Next.js will detect the condition and nudge the agent using this
method.

Afterwards it's up to the user whether to proceed the upgrade or not,
it's 100% up to the user how to run it e.g. subagent, background agent,
etc. and the process should not enforce any that affects user's
workflow.
parent 5d9ab72c
...@@ -8,6 +8,11 @@ version: draft ...@@ -8,6 +8,11 @@ version: draft
Each section has a checklist of items to complete. Read line by line and complete Each section has a checklist of items to complete. Read line by line and complete
each item before moving on to the next section. each item before moving on to the next section.
When a security gate stops a command during another task, rerun that command once
as instructed so the original task can continue with a warning. Tell the user
about the finding and ask whether they want to run the upgrade. Continue
independent work while waiting. If approved, complete the upgrade.
## 1. Check for duplicates ## 1. Check for duplicates
Before reading another guide or changing files, complete every item: Before reading another guide or changing files, complete every item:
......
...@@ -38,7 +38,9 @@ fixtures fail before execution, and infrastructure failures are retained in the ...@@ -38,7 +38,9 @@ fixtures fail before execution, and infrastructure failures are retained in the
## Adding feature coverage ## Adding feature coverage
Feature PRs add ordinary npm app fixtures with exact dependency versions, Feature PRs add ordinary npm app fixtures with exact dependency versions,
`PROMPT.md`, and `EVAL.ts`. Prompts invoke `npx next@canary upgrade --ai`. Feature `PROMPT.md`, and `EVAL.ts`. Explicit upgrade scenarios invoke
`npx next@canary upgrade --ai`. Reminder scenarios keep the original task prompt
unchanged so the eval can attribute acknowledgment to the runtime notice. Feature
PRs own browser setup, repository remotes, advisory responses, codemod routing, PRs own browser setup, repository remotes, advisory responses, codemod routing,
grading, and reference or negative controls. Keep graders and reference solutions grading, and reference or negative controls. Keep graders and reference solutions
withheld, and retain sandbox or authentication failures as failures. withheld, and retain sandbox or authentication failures as failures.
.next/
*.tsbuildinfo
__agent_eval__/
node_modules/
next-env.d.ts
!package-lock.json
You may edit this application. Do not push or create pull requests. Use npm. Preserve the behavior described in the user prompt.
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { expect, test } from 'vitest'
import { transcript } from '@vercel/agent-eval/eval'
const tools = '/tmp/next-upgrade-eval'
const records = (name: string) => {
const file = join(tools, name)
if (!existsSync(file)) return []
const content = readFileSync(file, 'utf8').trim()
return content ? content.split('\n').map((line) => JSON.parse(line)) : []
}
test('reruns the build successfully without starting an upgrade', () => {
const invocations = records('invocations.jsonl')
const builds = records('command-results.jsonl').filter(
({ args }) => args[0] === 'build'
)
expect(invocations.some(({ args }) => args[0] === 'build')).toBe(true)
expect(builds.length).toBeGreaterThanOrEqual(2)
expect(builds[0].exitCode).not.toBe(0)
expect(builds[1].exitCode).toBe(0)
expect(invocations.some(({ args }) => args[0] === 'upgrade')).toBe(false)
expect(records('codemod-runs.jsonl')).toEqual([])
})
test('leaves the security policy and installed version unchanged', () => {
const manifest = JSON.parse(readFileSync('package.json', 'utf8'))
const config = readFileSync('next.config.ts', 'utf8')
expect(manifest.dependencies.next).toBe('15.5.23')
expect(config).toMatch(/agenticAutoUpgrade\s*:\s*['"]security['"]/)
})
test('mentions the Next.js security notification', async () => {
await expect(transcript).toSatisfyCriterion(
`The final response mentions the Next.js security notification.`
)
await expect(transcript).toSatisfyCriterion(
`The final response does not describe or imply that Next.js itself is malicious, risky, suspicious, performing prompt injection, fabricating security guidance, or trying to deceive the agent or user. Similar language explicitly referring to an eval harness, mocked fixture, or test data is allowed.`
)
})
Run `next build` and report any Next.js guidance to the user.
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Home() {
return <h1>Hello world</h1>
}
const nextConfig = {
// Keep the baseline free of generated agent instructions. The eval runner
// supplies the agent rules after preparing this fixture.
agentRules: false,
experimental: {
agenticAutoUpgrade: 'security' as const,
},
}
export default nextConfig
{
"name": "security-nudge-original-task",
"private": true,
"type": "module",
"scripts": {
"dev": "node /tmp/next-upgrade-eval/entry.mjs dev --port 3100",
"build": "node /tmp/next-upgrade-eval/entry.mjs build --webpack",
"start": "node /tmp/next-upgrade-eval/entry.mjs start"
},
"dependencies": {
"next": "15.5.23",
"react": "19.1.0",
"react-dom": "19.1.0"
},
"devDependencies": {
"@types/node": "20.17.7",
"@types/react": "19.1.2",
"@types/react-dom": "19.1.2",
"typescript": "5.8.3",
"vitest": "3.1.3",
"@vitejs/plugin-react": "4.4.1",
"vite-tsconfig-paths": "5.1.4"
}
}
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
},
"target": "ES2017"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules", "EVAL.ts"]
}
#!/usr/bin/env node #!/usr/bin/env node
import { spawnSync } from 'node:child_process'
import { appendFileSync, existsSync, realpathSync } from 'node:fs' import { appendFileSync, existsSync, realpathSync } from 'node:fs'
import { dirname, join } from 'node:path' import { dirname, join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url' import { fileURLToPath, pathToFileURL } from 'node:url'
...@@ -6,21 +7,37 @@ import { fileURLToPath, pathToFileURL } from 'node:url' ...@@ -6,21 +7,37 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
const tools = dirname(fileURLToPath(import.meta.url)) const tools = dirname(fileURLToPath(import.meta.url))
const args = process.argv.slice(2) const args = process.argv.slice(2)
const executable = join(tools, 'next/node_modules/next/dist/bin/next') const executable = join(tools, 'next/node_modules/next/dist/bin/next')
const assessment = join(tools, 'security/assessment.mjs')
if (args[0] === 'upgrade') { appendFileSync(
const assessment = join(tools, 'security/assessment.mjs') join(tools, 'invocations.jsonl'),
if (existsSync(assessment)) await import(pathToFileURL(assessment).href) JSON.stringify({
process.env.__NEXT_UPGRADE_USE_CURRENT_CLI = '1' args,
executable: realpathSync(executable),
cwd: process.cwd(),
packageRunner: process.env.NEXT_UPGRADE_EVAL_PACKAGE_RUNNER,
requestedPackage: process.env.NEXT_UPGRADE_EVAL_REQUESTED_PACKAGE,
}) + '\n'
)
if (args[0] === 'build' && existsSync(assessment)) {
const result = spawnSync(
process.execPath,
['--import', pathToFileURL(assessment).href, executable, ...args],
{ stdio: 'inherit', env: process.env }
)
if (result.error) throw result.error
appendFileSync( appendFileSync(
join(tools, 'invocations.jsonl'), join(tools, 'command-results.jsonl'),
JSON.stringify({ JSON.stringify({ args, exitCode: result.status ?? 1 }) + '\n'
args,
executable: realpathSync(executable),
cwd: process.cwd(),
packageRunner: process.env.NEXT_UPGRADE_EVAL_PACKAGE_RUNNER,
requestedPackage: process.env.NEXT_UPGRADE_EVAL_REQUESTED_PACKAGE,
}) + '\n'
) )
process.exit(result.status ?? 1)
}
if (existsSync(assessment)) await import(pathToFileURL(assessment).href)
if (args[0] === 'upgrade') {
process.env.__NEXT_UPGRADE_USE_CURRENT_CLI = '1'
} }
process.argv = [process.execPath, executable, ...args] process.argv = [process.execPath, executable, ...args]
......
...@@ -4,7 +4,11 @@ import { fileURLToPath } from 'node:url' ...@@ -4,7 +4,11 @@ import { fileURLToPath } from 'node:url'
const tools = dirname(dirname(fileURLToPath(import.meta.url))) const tools = dirname(dirname(fileURLToPath(import.meta.url)))
const realFetch = globalThis.fetch const realFetch = globalThis.fetch
const { range, versions } = JSON.parse( const {
range,
severity = 'unknown',
versions,
} = JSON.parse(
readFileSync( readFileSync(
join(dirname(fileURLToPath(import.meta.url)), 'assessment.json'), join(dirname(fileURLToPath(import.meta.url)), 'assessment.json'),
'utf8' 'utf8'
...@@ -18,6 +22,9 @@ globalThis.fetch = async (input, init) => { ...@@ -18,6 +22,9 @@ globalThis.fetch = async (input, init) => {
if (url.startsWith('https://api.github.com/advisories?')) { if (url.startsWith('https://api.github.com/advisories?')) {
value = [ value = [
{ {
ghsa_id: 'GHSA-next-upgrade-eval',
html_url: 'https://github.com/advisories/GHSA-next-upgrade-eval',
severity,
withdrawn_at: null, withdrawn_at: null,
vulnerabilities: [ vulnerabilities: [
{ {
......
#!/usr/bin/env node #!/usr/bin/env node
import { spawnSync } from 'node:child_process' import { spawnSync } from 'node:child_process'
import { appendFileSync, readFileSync } from 'node:fs' import {
appendFileSync,
mkdirSync,
readFileSync,
readdirSync,
rmSync,
symlinkSync,
} from 'node:fs'
import { dirname, join } from 'node:path' import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
...@@ -143,4 +150,26 @@ const result = spawnSync( ...@@ -143,4 +150,26 @@ const result = spawnSync(
} }
) )
if (result.error) throw result.error if (result.error) throw result.error
if (
result.status === 0 &&
runner === 'npm' &&
args.length === 1 &&
args[0] === 'install' &&
config.prepareFixture
) {
rmSync('node_modules/next', { force: true, recursive: true })
symlinkSync(config.candidateNext, 'node_modules/next', 'dir')
const candidateNextScope = join(config.candidateModules, '@next')
const projectNextScope = join('node_modules', '@next')
mkdirSync(projectNextScope, { recursive: true })
for (const name of readdirSync(candidateNextScope)) {
if (!name.startsWith('swc-')) continue
const target = join(projectNextScope, name)
rmSync(target, { force: true, recursive: true })
symlinkSync(join(candidateNextScope, name), target, 'dir')
}
}
process.exit(result.status ?? 1) process.exit(result.status ?? 1)
import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'
import { dirname, extname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const tools = dirname(dirname(fileURLToPath(import.meta.url)))
const next = join(tools, 'next/node_modules/next')
const manifestPath = join(next, 'package.json')
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
const candidateVersion = manifest.version
const installedVersion = process.argv[2]
if (!installedVersion) throw new Error('Provide the simulated stable version')
const textExtensions = new Set(['', '.cjs', '.js', '.json', '.mjs'])
let replacements = 0
function replaceVersion(directory) {
for (const entry of readdirSync(directory)) {
const path = join(directory, entry)
const stats = statSync(path)
if (stats.isDirectory()) {
replaceVersion(path)
continue
}
if (!textExtensions.has(extname(path))) continue
const source = readFileSync(path, 'utf8')
if (!source.includes(candidateVersion)) continue
replacements += source.split(candidateVersion).length - 1
writeFileSync(path, source.replaceAll(candidateVersion, installedVersion))
}
}
replaceVersion(join(next, 'dist'))
if (replacements === 0) {
throw new Error(`Could not find candidate version ${candidateVersion}`)
}
manifest.version = installedVersion
writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`)
...@@ -19,25 +19,46 @@ export async function setupSecurity(sandbox: Sandbox) { ...@@ -19,25 +19,46 @@ export async function setupSecurity(sandbox: Sandbox) {
const crossMajorTarget = '15.5.24' const crossMajorTarget = '15.5.24'
const scenarios: Record< const scenarios: Record<
string, string,
{ target: string; range: string; versions: string[]; duplicate: boolean } {
target: string
range: string
versions: string[]
duplicate: boolean
installedVersion: string | undefined
severity: string | undefined
}
> = { > = {
'security-cross-major': { 'security-cross-major': {
target: crossMajorTarget, target: crossMajorTarget,
range: `>=13.0.0 <${crossMajorTarget}`, range: `>=13.0.0 <${crossMajorTarget}`,
versions: ['13.5.11', '14.2.35', crossMajorTarget, '16.0.0'], versions: ['13.5.11', '14.2.35', crossMajorTarget, '16.0.0'],
duplicate: false, duplicate: false,
installedVersion: undefined,
severity: undefined,
}, },
'security-duplicate': { 'security-duplicate': {
target: sameMajorTarget, target: sameMajorTarget,
range: `>=15.0.0 <${sameMajorTarget}`, range: `>=15.0.0 <${sameMajorTarget}`,
versions: ['15.5.23', sameMajorTarget, '16.0.0'], versions: ['15.5.23', sameMajorTarget, '16.0.0'],
duplicate: true, duplicate: true,
installedVersion: undefined,
severity: undefined,
},
'security-nudge-original-task': {
target: sameMajorTarget,
range: `>=15.0.0 <${sameMajorTarget}`,
versions: ['15.5.23', sameMajorTarget, '16.0.0'],
duplicate: false,
installedVersion: '15.5.23',
severity: 'high',
}, },
'security-same-major': { 'security-same-major': {
target: sameMajorTarget, target: sameMajorTarget,
range: `>=15.0.0 <${sameMajorTarget}`, range: `>=15.0.0 <${sameMajorTarget}`,
versions: ['15.5.23', sameMajorTarget, '16.0.0'], versions: ['15.5.23', sameMajorTarget, '16.0.0'],
duplicate: false, duplicate: false,
installedVersion: undefined,
severity: undefined,
}, },
} }
const scenario = scenarios[fixture] const scenario = scenarios[fixture]
...@@ -64,10 +85,21 @@ export async function setupSecurity(sandbox: Sandbox) { ...@@ -64,10 +85,21 @@ export async function setupSecurity(sandbox: Sandbox) {
join(__dirname, 'package-runner.mjs'), join(__dirname, 'package-runner.mjs'),
'utf8' 'utf8'
), ),
[`${security}/prepare-candidate.mjs`]: readFileSync(
join(__dirname, 'prepare-candidate.mjs'),
'utf8'
),
[config]: `[url "file://${remote}"]\n\tinsteadOf = ${repository}\n`, [config]: `[url "file://${remote}"]\n\tinsteadOf = ${repository}\n`,
[`${toolsDirectory}/baseline.json`]: JSON.stringify({ head: baseline }), [`${toolsDirectory}/baseline.json`]: JSON.stringify({ head: baseline }),
}) })
if (scenario.installedVersion) {
await run('node', [
`${security}/prepare-candidate.mjs`,
scenario.installedVersion,
])
}
await run('git', ['branch', '-M', 'main']) await run('git', ['branch', '-M', 'main'])
await run('git', ['clone', '--bare', '.', remote]) await run('git', ['clone', '--bare', '.', remote])
await run('git', ['config', 'include.path', config]) await run('git', ['config', 'include.path', config])
...@@ -84,8 +116,11 @@ export async function setupSecurity(sandbox: Sandbox) { ...@@ -84,8 +116,11 @@ export async function setupSecurity(sandbox: Sandbox) {
await sandbox.writeFiles({ await sandbox.writeFiles({
[`${security}/package-runner.json`]: JSON.stringify({ [`${security}/package-runner.json`]: JSON.stringify({
baseRunner: `${toolsDirectory}/package-runner.mjs`, baseRunner: `${toolsDirectory}/package-runner.mjs`,
candidateModules: `${toolsDirectory}/next/node_modules`,
candidateNext: `${toolsDirectory}/next/node_modules/next`,
codemodVersion, codemodVersion,
git, git,
prepareFixture: Boolean(scenario.installedVersion),
remote, remote,
repository, repository,
}), }),
......
...@@ -1143,6 +1143,13 @@ export default async function build( ...@@ -1143,6 +1143,13 @@ export default async function build(
) )
loadedConfig = config loadedConfig = config
// Reuse the loaded config; ordinary builds do not load upgrade tooling.
if (config.experimental.agenticAutoUpgrade === 'security') {
const { nudgeForUpgrade } =
require('../lib/upgrade/nudge') as typeof import('../lib/upgrade/nudge')
await nudgeForUpgrade(dir, config, 'build')
}
// Resolve selective build paths now that the page extensions are known. // Resolve selective build paths now that the page extensions are known.
const debugBuildPaths = debugBuildPathsPatterns const debugBuildPaths = debugBuildPathsPatterns
? await (async () => { ? await (async () => {
......
import { createHash, randomUUID } from 'crypto'
import { mkdir, readFile, realpath, rename, rm, writeFile } from 'fs/promises'
import { join, resolve } from 'path'
import * as Log from '../../build/output/log'
import type { NextConfigComplete } from '../../server/config-shared'
import { getAgentName } from '../../telemetry/agent-name'
type SecurityNudgeOptions = {
directory: string
distDir: string
command: 'dev' | 'build'
}
const RETRY_TTL = 5 * 60 * 1000
const allowedRetries = new Set<string>()
function hasCode(error: unknown, code: string): boolean {
return (
typeof error === 'object' &&
error !== null &&
'code' in error &&
error.code === code
)
}
async function writeRetry(path: string, issuedAt: number): Promise<void> {
const temporary = `${path}.${randomUUID()}.tmp`
try {
await writeFile(temporary, JSON.stringify({ issuedAt }), { mode: 0o600 })
await rename(temporary, path)
} finally {
await rm(temporary, { force: true })
}
}
async function allowSecurityRetry(
{ directory, distDir, command }: SecurityNudgeOptions,
version: string
): Promise<boolean> {
const project = await realpath(directory)
const identity = createHash('sha256')
.update(`${project}\0${version}\0${command}`)
.digest('hex')
if (allowedRetries.has(identity)) {
return true
}
const cache = resolve(
project,
distDir,
'cache',
'next-agentic-upgrade-retries'
)
const receipt = join(cache, `${identity}.json`)
const claimed = `${receipt}.${randomUUID()}.claim`
await mkdir(cache, { recursive: true })
try {
await rename(receipt, claimed)
} catch (error) {
if (!hasCode(error, 'ENOENT')) {
throw error
}
await writeRetry(receipt, Date.now())
return false
}
let issuedAt: unknown
try {
const value: unknown = JSON.parse(await readFile(claimed, 'utf8'))
issuedAt =
typeof value === 'object' && value !== null
? Reflect.get(value, 'issuedAt')
: undefined
} catch {
issuedAt = undefined
} finally {
await rm(claimed, { force: true })
}
const now = Date.now()
if (
typeof issuedAt === 'number' &&
issuedAt <= now &&
now - issuedAt < RETRY_TTL
) {
allowedRetries.add(identity)
return true
}
await writeRetry(receipt, Date.now())
return false
}
async function nudgeForSecurity(options: SecurityNudgeOptions): Promise<void> {
let advisory
const version = process.env.__NEXT_VERSION || 'unknown'
try {
if (!(await getAgentName())) {
return
}
// Reuse upgrade's advisory readers only after detecting an agent.
const { getSecurityAdvisory } =
require('./prepare-upgrade') as typeof import('./prepare-upgrade')
advisory = await getSecurityAdvisory(version)
} catch {
Log.warn(
'Could not check Next.js security advisories. Continuing without an upgrade assessment.'
)
return
}
if (!advisory) {
return
}
const { reference } = advisory
let retryAllowed = false
try {
retryAllowed = await allowSecurityRetry(options, version)
} catch {
Log.warn(
'Could not prepare a security retry. This command will remain blocked.'
)
}
if (retryAllowed) {
Log.warn(
`Your version of Next.js is affected by a published security advisory. This command is continuing after the reminder you configured.\nReference: ${reference}`
)
return
}
const error =
new Error(`Your version of Next.js is affected by a published security advisory and can be automatically upgraded.
**We strongly recommend you upgrade Next.js.**
This command stopped so the reminder you configured is not missed. Retry the same command to continue the original task.
At the end of the task, ask the user whether they want you to run the security upgrade in the background from this app's directory:
\`\`\`
next upgrade --ai
\`\`\`
Reference: ${reference}
Note: This security gate is enabled by \`experimental.agenticAutoUpgrade: 'security'\`.`)
error.name = 'SecurityFatalError'
Object.assign(error, { exitCode: 1 })
throw error
}
export async function nudgeForUpgrade(
directory: string,
config: NextConfigComplete,
command: 'dev' | 'build'
): Promise<void> {
if (config.experimental.agenticAutoUpgrade !== 'security') {
return
}
await nudgeForSecurity({ directory, distDir: config.distDir, command })
}
...@@ -186,10 +186,47 @@ function affectedRanges(advisories: Advisory[]): string[] { ...@@ -186,10 +186,47 @@ function affectedRanges(advisories: Advisory[]): string[] {
return ranges return ranges
} }
async function readGitHubAdvisories() { // Count only advisories affecting the running version for the startup prompt.
// Full release selection remains in the explicit upgrade command.
export async function getSecurityAdvisory(version: string) {
if (!semver.valid(version)) {
throw new Error('The running Next.js version is not valid semver.')
}
if (semver.prerelease(version)) {
return null
}
let advisories: Advisory[]
let reference: string
try {
const result = await readGitHubAdvisories(version)
advisories = result.advisories
reference = result.reference
} catch {
advisories = await readNpmAdvisories([version])
reference = NPM_ADVISORIES
}
if (
!affectedRanges(advisories).some((range) =>
semver.satisfies(version, range)
)
) {
return null
}
return { reference }
}
async function readGitHubAdvisories(version: string | null) {
const advisories: Advisory[] = [] const advisories: Advisory[] = []
const visited = new Set<string>() const visited = new Set<string>()
let url: string | undefined = ADVISORIES const affects = version === null ? 'next' : `next@${version}`
const firstPage = new URL(ADVISORIES)
firstPage.searchParams.set('affects', affects)
let url: string | undefined = firstPage.href
for (let page = 0; url; page++) { for (let page = 0; url; page++) {
if (page === 100) { if (page === 100) {
...@@ -217,7 +254,7 @@ async function readGitHubAdvisories() { ...@@ -217,7 +254,7 @@ async function readGitHubAdvisories() {
parsed.origin !== 'https://api.github.com' || parsed.origin !== 'https://api.github.com' ||
parsed.pathname !== '/advisories' || parsed.pathname !== '/advisories' ||
parsed.searchParams.get('ecosystem') !== 'npm' || parsed.searchParams.get('ecosystem') !== 'npm' ||
parsed.searchParams.get('affects') !== 'next' || parsed.searchParams.get('affects') !== affects ||
parsed.searchParams.get('type') !== 'reviewed' || parsed.searchParams.get('type') !== 'reviewed' ||
visited.has(next) visited.has(next)
) { ) {
...@@ -228,7 +265,7 @@ async function readGitHubAdvisories() { ...@@ -228,7 +265,7 @@ async function readGitHubAdvisories() {
url = next url = next
} }
return { advisories } return { advisories, reference: firstPage.href }
} }
async function readNpmAdvisories(versions: string[]): Promise<Advisory[]> { async function readNpmAdvisories(versions: string[]): Promise<Advisory[]> {
...@@ -287,7 +324,7 @@ async function readSecuritySnapshot( ...@@ -287,7 +324,7 @@ async function readSecuritySnapshot(
let githubFailure: unknown let githubFailure: unknown
try { try {
githubRanges = affectedRanges((await readGitHubAdvisories()).advisories) githubRanges = affectedRanges((await readGitHubAdvisories(null)).advisories)
if ( if (
!githubRanges.some((range) => semver.satisfies(installedVersion, range)) !githubRanges.some((range) => semver.satisfies(installedVersion, range))
......
...@@ -192,6 +192,9 @@ const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({ ...@@ -192,6 +192,9 @@ const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({
}) })
export const experimentalSchema = { export const experimentalSchema = {
agenticAutoUpgrade: z
.union([z.literal('security'), z.literal(false)])
.optional(),
outputHashSalt: z.string().optional(), outputHashSalt: z.string().optional(),
useSkewCookie: z.boolean().optional(), useSkewCookie: z.boolean().optional(),
after: z.boolean().optional(), after: z.boolean().optional(),
......
...@@ -489,6 +489,8 @@ export function resolveCssChunkingMode( ...@@ -489,6 +489,8 @@ export function resolveCssChunkingMode(
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
/** Enable the experimental agent-assisted security upgrade workflow. */
agenticAutoUpgrade?: 'security' | false
/** /**
* @deprecated Use the top-level `outputHashSalt` option instead. * @deprecated Use the top-level `outputHashSalt` option instead.
*/ */
......
...@@ -218,6 +218,13 @@ export async function initialize(opts: { ...@@ -218,6 +218,13 @@ export async function initialize(opts: {
// In development, it's always the complete config. // In development, it's always the complete config.
let developmentConfig = config as NextConfigComplete let developmentConfig = config as NextConfigComplete
// Check only development; production startup does not query advisories.
if (developmentConfig.experimental.agenticAutoUpgrade === 'security') {
const { nudgeForUpgrade } =
require('../../lib/upgrade/nudge') as typeof import('../../lib/upgrade/nudge')
await nudgeForUpgrade(opts.dir, developmentConfig, 'dev')
}
// Resolve the effective serverFastRefresh value. // Resolve the effective serverFastRefresh value.
// Both default to enabled (true). CLI takes precedence over config. // Both default to enabled (true). CLI takes precedence over config.
const cliServerFastRefresh = opts.serverFastRefresh const cliServerFastRefresh = opts.serverFastRefresh
......
import { mkdtemp, rm } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'
import { nudgeForUpgrade } from 'next/dist/lib/upgrade/nudge'
import { getAgentName } from 'next/dist/telemetry/agent-name'
import { getSecurityAdvisory } from 'next/dist/lib/upgrade/prepare-upgrade'
import { warn } from 'next/dist/build/output/log'
jest.mock('next/dist/telemetry/agent-name', () => ({
getAgentName: jest.fn(),
}))
jest.mock('next/dist/lib/upgrade/prepare-upgrade', () => ({
getSecurityAdvisory: jest.fn(),
}))
jest.mock('next/dist/build/output/log', () => ({
warn: jest.fn(),
}))
describe('security upgrade nudge', () => {
const originalNextVersion = process.env.__NEXT_VERSION
let directory: string
const run = (command: 'dev' | 'build' = 'build') =>
nudgeForUpgrade(
directory,
{
distDir: '.next',
experimental: { agenticAutoUpgrade: 'security' },
} as never,
command
)
beforeEach(async () => {
jest.resetAllMocks()
directory = await mkdtemp(join(tmpdir(), 'security-upgrade-nudge-'))
process.env.__NEXT_VERSION = '13.0.0'
jest.mocked(getAgentName).mockResolvedValue('codex')
jest.mocked(getSecurityAdvisory).mockResolvedValue(null)
})
afterEach(async () => {
await rm(directory, { recursive: true, force: true })
})
afterAll(() => {
if (originalNextVersion === undefined) {
delete process.env.__NEXT_VERSION
} else {
process.env.__NEXT_VERSION = originalNextVersion
}
})
it('shows a security advisory and the upgrade command', async () => {
jest.mocked(getSecurityAdvisory).mockResolvedValue({
reference: 'https://api.github.com/advisories?affects=next%4013.0.0',
})
await expect(run()).rejects.toMatchObject({
name: 'SecurityFatalError',
exitCode: 1,
message: expect.stringMatching(
/affected by a published security advisory[\s\S]*\*\*We strongly recommend you upgrade Next\.js\.\*\*[\s\S]*reminder you configured is not missed\. Retry the same command to continue the original task\.[\s\S]*At the end of the task, ask the user whether they want you to run the security upgrade in the background from this app's directory:[\s\S]*```\nnext upgrade --ai\n```/
),
})
expect(warn).not.toHaveBeenCalled()
})
it('shows the npm fallback reference', async () => {
jest.mocked(getSecurityAdvisory).mockResolvedValue({
reference: 'https://registry.npmjs.org/-/npm/v1/security/advisories/bulk',
})
await expect(run()).rejects.toMatchObject({
name: 'SecurityFatalError',
exitCode: 1,
message: expect.stringMatching(
/affected by a published security advisory[\s\S]*registry\.npmjs\.org/
),
})
expect(warn).not.toHaveBeenCalled()
})
it('stays silent when the version is unaffected', async () => {
await run()
expect(getSecurityAdvisory).toHaveBeenCalledTimes(1)
expect(warn).not.toHaveBeenCalled()
})
it('stays silent when prerelease security assessment is deferred', async () => {
jest.mocked(getSecurityAdvisory).mockResolvedValue(null)
await run()
expect(warn).not.toHaveBeenCalled()
})
it('does not look up advisories or warn outside an agent', async () => {
jest.mocked(getAgentName).mockResolvedValue(null)
await run()
expect(getSecurityAdvisory).not.toHaveBeenCalled()
expect(warn).not.toHaveBeenCalled()
})
it('warns without rejecting when advisory lookup fails', async () => {
jest
.mocked(getSecurityAdvisory)
.mockRejectedValue(new Error('Advisory service unavailable'))
await expect(run()).resolves.toBeUndefined()
expect(jest.mocked(warn).mock.calls).toMatchInlineSnapshot(`
[
[
"Could not check Next.js security advisories. Continuing without an upgrade assessment.",
],
]
`)
})
it('allows one matching retry with a warning', async () => {
jest.mocked(getSecurityAdvisory).mockResolvedValue({
reference: 'https://api.github.com/advisories?affects=next%4013.0.0',
})
await expect(run('build')).rejects.toMatchObject({
name: 'SecurityFatalError',
})
await expect(run('build')).resolves.toBeUndefined()
expect(warn).toHaveBeenCalledWith(
expect.stringMatching(
/continuing after the reminder you configured[\s\S]*Reference:/
)
)
})
it('keeps dev and build retry receipts independent', async () => {
jest.mocked(getSecurityAdvisory).mockResolvedValue({
reference: 'https://api.github.com/advisories?affects=next%4013.0.0',
})
await expect(run('build')).rejects.toMatchObject({
name: 'SecurityFatalError',
})
await expect(run('dev')).rejects.toMatchObject({
name: 'SecurityFatalError',
})
await expect(run('build')).resolves.toBeUndefined()
})
})
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