Repository navigation
fix(autofix): bind the sandbox image to its pulled digest #9527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
92c9a4e
da1d9ad
01452e0
65a541c
0ff059c
9c5e728
495a13c
eb2988b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| #!/usr/bin/env node | ||
| import { appendFileSync } from 'node:fs'; | ||
| import { closeSync, constants, fstatSync, openSync, writeSync } from 'node:fs'; | ||
| import { spawn } from 'node:child_process'; | ||
| import { pathToFileURL } from 'node:url'; | ||
|
|
||
|
|
@@ -73,45 +73,221 @@ async function fetchLatestGhcrSemver() { | |
| return latest; | ||
| } | ||
|
|
||
| function pullImage(command, image) { | ||
| // Pin the daemon endpoint for every spawn below. The docker CLI resolves its | ||
| // endpoint in precedence order DOCKER_HOST, --context, DOCKER_CONTEXT, and | ||
| // finally `currentContext` in $DOCKER_CONFIG/config.json — a file shared by | ||
| // every runner registration on the self-hosted pool. Clearing DOCKER_CONTEXT | ||
| // is not enough: an empty value falls through to currentContext, so a | ||
| // co-resident process that rewrites config.json (or sets DOCKER_HOST through | ||
| // $GITHUB_ENV) can point the pull and the inspect at a daemon it controls and | ||
| // hand back whatever digest it likes. Name the context explicitly and drop | ||
| // DOCKER_HOST, so nothing above `default` in that order is reachable. There is | ||
| // deliberately no env override: a variable that selects the endpoint would be | ||
| // settable through the same channel this closes. DOCKER_CONFIG is left alone — | ||
| // it carries the registry credentials the pull needs. | ||
| export function sandboxSpawnEnv(env = process.env) { | ||
| const childEnv = { ...env, DOCKER_CONTEXT: 'default' }; | ||
| delete childEnv.DOCKER_HOST; | ||
| return childEnv; | ||
| } | ||
|
|
||
| // $GITHUB_ENV and $GITHUB_OUTPUT are runner-managed files under $RUNNER_TEMP, | ||
| // which on the shared pool is writable by anything running at the runner's | ||
| // uid. A plain append opens whatever sits at that path: a planted FIFO with no | ||
| // reader blocks open(2) until the step timeout, turning one line of writable | ||
| // state into a per-round hang. O_NONBLOCK makes that case an immediate ENXIO, | ||
| // and the post-open fstat refuses every non-regular file — the type check, not | ||
| // the path, is what holds. O_NOFOLLOW is deliberately not set: the runner may | ||
| // legitimately place these files behind a symlinked temp directory. | ||
| export function appendStepFile(file, line) { | ||
| let fd; | ||
| try { | ||
| fd = openSync( | ||
| file, | ||
| constants.O_WRONLY | | ||
| constants.O_CREAT | | ||
| constants.O_APPEND | | ||
| constants.O_NONBLOCK, | ||
| 0o600, | ||
| ); | ||
| if (!fstatSync(fd).isFile()) { | ||
| throw new Error( | ||
| `${file} is not a regular file; refusing to write step state to it.`, | ||
| ); | ||
| } | ||
| // writeSync can come up short on a signal; appendFileSync loops for us | ||
| // and this does not, so drain the buffer explicitly rather than shipping | ||
| // a truncated `image=` line that a consumer would read as a valid one. | ||
| const payload = Buffer.from(line, 'utf8'); | ||
| for (let written = 0; written < payload.length; ) { | ||
| written += writeSync(fd, payload, written, payload.length - written); | ||
| } | ||
| } finally { | ||
| if (fd !== undefined) { | ||
| closeSync(fd); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The `Digest: sha256:…` line docker prints for the tag it just resolved is | ||
| // the only pull-time content identity: the post-pull inspect can race a | ||
| // `docker tag` swap (see repoDigestOf), so the exported reference must be | ||
| // bound to what the pull itself reported, never to inspect alone. | ||
| export function parsePullDigest(pullOutput) { | ||
| return pullOutput.match(/^Digest: (sha256:[0-9a-f]{64})\s*$/m)?.[1] ?? ''; | ||
| } | ||
|
|
||
| export function pullImage(command, image) { | ||
| return new Promise((resolve) => { | ||
| const child = spawn(command, ['pull', image], { stdio: 'inherit' }); | ||
| const child = spawn(command, ['pull', image], { | ||
| stdio: ['ignore', 'pipe', 'inherit'], | ||
| env: sandboxSpawnEnv(), | ||
| }); | ||
| let stdout = ''; | ||
| let settled = false; | ||
| let timer; | ||
| const finish = (ok) => { | ||
| const finish = (result) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| clearTimeout(timer); | ||
| resolve(ok); | ||
| resolve(result); | ||
| }; | ||
| timer = setTimeout(() => { | ||
| console.error( | ||
| `::error::Timed out pulling ${image} after ${PULL_TIMEOUT_MS / 1000}s.`, | ||
| ); | ||
| child.kill('SIGKILL'); | ||
| finish(false); | ||
| finish({ ok: false, digest: '' }); | ||
| }, PULL_TIMEOUT_MS); | ||
|
|
||
| child.stdout.on('data', (chunk) => { | ||
| stdout += chunk; | ||
| process.stdout.write(chunk); | ||
| }); | ||
| child.on('error', (error) => { | ||
| console.error( | ||
| `::error::Failed to start '${command} pull ${image}': ${error.message}`, | ||
| ); | ||
| finish(false); | ||
| finish({ ok: false, digest: '' }); | ||
| }); | ||
| child.on('close', (code) => { | ||
| if (code !== 0) { | ||
| console.error( | ||
| `::error::'${command} pull ${image}' exited with code ${code}.`, | ||
| ); | ||
| finish({ ok: false, digest: '' }); | ||
| return; | ||
| } | ||
| finish(code === 0); | ||
| finish({ ok: true, digest: parsePullDigest(stdout) }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function exportImage(image) { | ||
| // The repository part of an image reference: everything before the :tag / | ||
| // @digest. A registry port keeps its colon — the tag only ever follows the | ||
| // LAST '/'. | ||
| export function repoOfImage(image) { | ||
| const withoutDigest = image.split('@')[0]; | ||
| const lastColon = withoutDigest.lastIndexOf(':'); | ||
| const lastSlash = withoutDigest.lastIndexOf('/'); | ||
| return lastColon > lastSlash | ||
| ? withoutDigest.slice(0, lastColon) | ||
| : withoutDigest; | ||
| } | ||
|
|
||
| // Resolve a PULLED image to its content digest (repo@sha256:…). The tag | ||
| // alone is a mutable local handle: `docker run <tag>` resolves against the | ||
| // local store without re-pull, and a co-resident process with daemon access | ||
| // can `docker tag` different content under the same name between resolve | ||
| // and gate. A digest reference cannot be moved by `docker tag`/`docker build`. | ||
| // The export must be the EXACT `<repo>@<expectedDigest>` RepoDigests entry: | ||
| // RepoDigests is shared by every tag of the same content, so `docker tag` | ||
| // of the pulled image adds an alphabetically-sorted entry for the new name | ||
| // and index 0 can move OFF the pulled repo (a suffix-only digest check | ||
| // still passes) — and retagged attacker content keeps ITS original repo, so | ||
| // only the pulled repo + the pull's own `Digest:` line together bind the | ||
| // export to the content the pull fetched (#9214 review). | ||
| export function repoDigestOf( | ||
| command, | ||
| image, | ||
| expectedDigest = '', | ||
| timeoutMs = FETCH_TIMEOUT_MS, | ||
| ) { | ||
| return new Promise((resolve) => { | ||
| const child = spawn( | ||
| command, | ||
| ['image', 'inspect', '--format', '{{json .RepoDigests}}', image], | ||
| { stdio: ['ignore', 'pipe', 'pipe'], env: sandboxSpawnEnv() }, | ||
| ); | ||
| let stdout = ''; | ||
| let settled = false; | ||
| let timer; | ||
| const finish = (value) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| clearTimeout(timer); | ||
| resolve(value); | ||
| }; | ||
| timer = setTimeout(() => { | ||
| console.error( | ||
| `::error::Timed out inspecting ${image} after ${timeoutMs / 1000}s.`, | ||
| ); | ||
| child.kill('SIGKILL'); | ||
| finish(''); | ||
| }, timeoutMs); | ||
|
|
||
| child.stdout.on('data', (chunk) => { | ||
| stdout += chunk; | ||
| }); | ||
| child.on('error', (error) => { | ||
| console.error( | ||
| `::error::Failed to start '${command} image inspect ${image}': ${error.message}`, | ||
| ); | ||
| finish(''); | ||
| }); | ||
| child.on('close', (code) => { | ||
| finish(code === 0 ? stdout.trim() : ''); | ||
| }); | ||
| }).then((raw) => { | ||
| // `null`/`[]` (no RepoDigests, a locally built image), `<no value>` and | ||
| // empty (the inspect failed) all mean there is no repository digest — | ||
| // the mutable tag is exactly what must not be exported. | ||
| let digests = []; | ||
| try { | ||
| const parsed = JSON.parse(raw.trim()); | ||
| if (Array.isArray(parsed)) { | ||
| digests = parsed.filter((entry) => typeof entry === 'string'); | ||
| } | ||
| } catch { | ||
| // Non-JSON output carries no digests. | ||
| } | ||
| const repo = repoOfImage(image); | ||
| const digest = expectedDigest | ||
| ? (digests.find((entry) => entry === `${repo}@${expectedDigest}`) ?? '') | ||
| : (digests.find((entry) => entry.startsWith(`${repo}@sha256:`)) ?? ''); | ||
| if (digest.includes('@sha256:')) { | ||
| return digest; | ||
| } | ||
| if (digests.length > 0) { | ||
| throw new Error( | ||
| `Pulled image ${image} resolved to digests none of which is '${repo}@${expectedDigest || 'sha256:…'}' (${digests.join(', ')}); refusing to export a foreign or mutable reference.`, | ||
| ); | ||
| } | ||
| throw new Error( | ||
| `Pulled image ${image} resolved to no repository digest ('${raw.trim()}'); refusing to export a mutable tag.`, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| export function exportImage(image) { | ||
| if (process.env.GITHUB_ENV) { | ||
| appendFileSync(process.env.GITHUB_ENV, `QWEN_SANDBOX_IMAGE=${image}\n`); | ||
| appendStepFile(process.env.GITHUB_ENV, `QWEN_SANDBOX_IMAGE=${image}\n`); | ||
| } | ||
| // Also as a step OUTPUT: $GITHUB_ENV is a file later steps can append to, | ||
| // so a consumer that must not be steered by branch code (the verification | ||
| // gate's container image) reads the expression-context value instead. | ||
| if (process.env.GITHUB_OUTPUT) { | ||
| appendStepFile(process.env.GITHUB_OUTPUT, `image=${image}\n`); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Critical] Make the pinned output the value the gates actually consume None of the six 中文说明六个
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split by footprint. In scope — resolved in 中文说明按足迹拆分处理。范围内 —— 已在 |
||
| } | ||
| console.log(`QWEN_SANDBOX_IMAGE=${image}`); | ||
| } | ||
|
|
@@ -120,8 +296,16 @@ async function main() { | |
| const requestedImage = validateRequestedImage(process.argv[2]); | ||
|
|
||
| const command = process.env.SANDBOX_COMMAND || 'docker'; | ||
| if (await pullImage(command, requestedImage)) { | ||
| exportImage(requestedImage); | ||
| const requestedPull = await pullImage(command, requestedImage); | ||
| if (requestedPull.ok) { | ||
| if (!requestedPull.digest) { | ||
| throw new Error( | ||
| `'${command} pull ${requestedImage}' reported no Digest line; refusing to export an unbound image reference.`, | ||
| ); | ||
| } | ||
| exportImage( | ||
| await repoDigestOf(command, requestedImage, requestedPull.digest), | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -136,10 +320,16 @@ async function main() { | |
| console.warn( | ||
| `::warning::Falling back from ${requestedImage} to latest GHCR semver ${fallbackImage}; sandbox image version may differ from package version.`, | ||
| ); | ||
| if (!(await pullImage(command, fallbackImage))) { | ||
| const fallbackPull = await pullImage(command, fallbackImage); | ||
| if (!fallbackPull.ok) { | ||
| throw new Error(`Fallback sandbox image failed to pull: ${fallbackImage}`); | ||
| } | ||
| exportImage(fallbackImage); | ||
| if (!fallbackPull.digest) { | ||
| throw new Error( | ||
| `'${command} pull ${fallbackImage}' reported no Digest line; refusing to export an unbound image reference.`, | ||
| ); | ||
| } | ||
| exportImage(await repoDigestOf(command, fallbackImage, fallbackPull.digest)); | ||
| } | ||
|
|
||
| if ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still deferred — this round is the gate-mandated same-run verification repair (a single commit fixing the footprint rejection by reverting
repo-hygiene.ymland rescoping the contract test), and bundling a refactor into it would exceed the repair scope. The extraction itself gets cheaper each round: the previous one aligned both skeletons (stderrinheritin both, exit-code::error::logging in both, injectable timeouts in both), sospawnGuarded(command, args, { timeoutMs, label })applying thesandboxSpawnEnv()pin internally remains a clean follow-up for the next normal round.中文说明
继续延后——本轮是门禁要求的同轮验证修复(单提交修复足迹拒绝:回退
repo-hygiene.yml并收缩契约测试范围),把重构捆绑进来会超出修复范围。抽取本身正变得越来越便宜:上一轮已把两份骨架对齐(双方 stderr 均为inherit、双方均有退出码::error::日志、双方超时均可注入),因此在内部应用sandboxSpawnEnv()端点固定的spawnGuarded(command, args, { timeoutMs, label })仍是下一个正常轮次的干净后续改动。