From 0b646c89b7d90a188a2bf22703016724bf85bc47 Mon Sep 17 00:00:00 2001 From: callumalpass Date: Sun, 29 Nov 2026 01:14:17 +1100 Subject: [PATCH] Validate frozen patch_hash pins for runtime originals --- scripts/lib/writer-runtime-originals.mjs | 32 +++++++++++-- tests/writer-hosting-policy.test.mjs | 58 +++++++++++++++++++++++- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/scripts/lib/writer-runtime-originals.mjs b/scripts/lib/writer-runtime-originals.mjs index c1e14633..af15132f 100644 --- a/scripts/lib/writer-runtime-originals.mjs +++ b/scripts/lib/writer-runtime-originals.mjs @@ -11,19 +11,43 @@ const definitions=[ ]; const hash=(b,algorithm='sha256')=>createHash(algorithm).update(b).digest(algorithm==='sha512'?'base64':'hex'); const check=(ok,reason)=>{if(!ok)throw Error('Runtime original refusal: '+reason);}; +function patchTouchesRestrictedPath(patchText,targets) { + if(/^(?:GIT binary patch|Binary files )/m.test(patchText))return true; + for(const line of patchText.split(/\r?\n/)) { + if(!/^(?:diff --git |--- |\+\+\+ |rename (?:from|to) |copy (?:from|to) )/.test(line))continue; + if(/(?:^|[/\s"'])\.\.(?:[/\s"']|$)/.test(line))return true; + for(const target of targets)if(line.includes(target))return true; + } + return false; +} export async function runtimeSourcePins(repoRoot) { const pkg=JSON.parse(await readFile(join(repoRoot,'apps/writer/package.json'),'utf8')); const lock=await readFile(join(repoRoot,'pnpm-lock.yaml'),'utf8'); const importer=lock.split('\n apps/writer:\n')[1]?.split(/\n [^ ]/)[0]; - return definitions.map(d=>{ + const patchedSection=lock.split('\nimporters:')[0].split('\npatchedDependencies:\n')[1]?.split(/\n[^ ]/)[0]??''; + const patchedLines=patchedSection.split('\n'); + const pins=[]; + for(const d of definitions) { const version=pkg.dependencies?.[d.name];check(/^\d+\.\d+\.\d+$/.test(version??''),'exact original version required'); const quoted=d.name.startsWith('@')?`'${d.name}'`:d.name; - check(importer?.includes(` ${quoted}:\n specifier: ${version}\n version: ${version}\n`),'frozen importer pin'); + const pinPrefix=` ${quoted}:\n specifier: ${version}\n version: `; + const pinIndex=importer?.indexOf(pinPrefix)??-1; + check(pinIndex!==-1&&importer.indexOf(pinPrefix,pinIndex+1)===-1,'frozen importer pin'); + const resolved=importer.slice(pinIndex+pinPrefix.length).split('\n')[0]; + const patchHash=resolved.startsWith(`${version}(patch_hash=`)&&resolved.endsWith(')')?resolved.slice(version.length+12,-1):null; + check(resolved===version||/^[a-f0-9]{64}$/.test(patchHash??''),'frozen importer pin'); const key=d.name.startsWith('@')?`'${d.name}@${version}'`:`${d.name}@${version}`; + if(patchHash) { + check(patchedLines.includes(` ${key}: ${patchHash}`),'frozen patch digest'); + const patchBytes=await originalFile(repoRoot,`patches/${d.name.replace('/','__')}@${version}.patch`); + check(hash(patchBytes)===patchHash,'committed patch digest'); + check(!patchTouchesRestrictedPath(patchBytes.toString('utf8'),[d.member,'package.json']),'patch must not modify official WASM or package manifest'); + } const record=lock.split(`\n ${key}:\n resolution: {integrity: `)[1]?.split('}')[0]; check(/^sha512-[A-Za-z0-9+/]{86}==$/.test(record??''),'independent root lock SHA512 integrity'); - return {...d,version,sri:record,url:`https://registry.npmjs.org/${d.name}/-/${d.basename}-${version}.tgz`}; - }); + pins.push({...d,version,sri:record,url:`https://registry.npmjs.org/${d.name}/-/${d.basename}-${version}.tgz`}); + } + return pins; } async function originalFile(root,path) { const full=join(root,path);check((await lstat(full)).isFile()&&await realpath(full)===full,'regular original, no symlinks'); diff --git a/tests/writer-hosting-policy.test.mjs b/tests/writer-hosting-policy.test.mjs index 4f0a7de3..f444b852 100644 --- a/tests/writer-hosting-policy.test.mjs +++ b/tests/writer-hosting-policy.test.mjs @@ -1,14 +1,16 @@ import assert from 'node:assert/strict'; import test from 'node:test'; -import { readFile, rm, writeFile } from 'node:fs/promises'; +import { readFile, rm, symlink, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { inventoryDirectory } from '../scripts/lib/native-publish-bytes.mjs'; import { assertRuntimeOriginals, deriveRuntimeOriginals, runtimeSourcePins } from '../scripts/lib/writer-runtime-originals.mjs'; import { sourceHostingPolicy, uploadConfiguration, readHostingMetadata, assertHostingMetadata } from '../scripts/lib/writer-hosting-policy.mjs'; import { assertInventoryCoverage } from '../scripts/publisher/checks.mjs'; import { verifyPublication } from '../scripts/publisher/hosting.mjs'; import { verifyServedAssets } from '../apps/writer/scripts/verify-served.mjs'; -import { hostingFixture } from './writer-hosting-fixture.mjs'; +import { hash, hostingFixture } from './writer-hosting-fixture.mjs'; +const repoRoot=fileURLToPath(new URL('../',import.meta.url)); const origin='https://staging.mdbase-writer.pages.dev'; test('static bytes alone cannot bypass required source runtime/deployment policy',async t=>{ const f=await hostingFixture(t); @@ -225,3 +227,55 @@ test('verifyPublication polls the immutable deployment URL within its bound befo if(prevRoot===undefined)delete process.env.WRITER_SOURCE_ROOT;else process.env.WRITER_SOURCE_ROOT=prevRoot; } }); +test('repository runtime source pins validate against the real lockfile and committed patches',async()=>{ + const pins=await runtimeSourcePins(repoRoot); + assert.deepEqual(pins.map(p=>p.name),['@myriaddreamin/typst-ts-web-compiler','pandoc-wasm']); + assert.ok(pins.every(p=>/^sha512-[A-Za-z0-9+/]{86}==$/.test(p.sri))); +}); +test('patched runtime dependency accepts a verified non-WASM patch and refuses tampering or WASM/manifest patches',async t=>{ + const f=await hostingFixture(t); + const patchBody='diff --git a/src/core.js b/src/core.js\n--- a/src/core.js\n+++ b/src/core.js\n@@ -1 +1 @@\n-a\n+b\n'; + const patchDigest=hash(Buffer.from(patchBody)); + const baseLock=await readFile(join(f.repoRoot,'pnpm-lock.yaml'),'utf8'); + const writePatchedLock=async(digest=patchDigest,importerVersion=`1.1.0(patch_hash=${digest})`,includeHeader=true)=>{ + const patchedHeader=includeHeader?`patchedDependencies:\n pandoc-wasm@1.1.0: ${digest}\n`:''; + const lock=baseLock.replace('importers:\n',`${patchedHeader}importers:\n`).replace(' pandoc-wasm:\n specifier: 1.1.0\n version: 1.1.0\n',` pandoc-wasm:\n specifier: 1.1.0\n version: ${importerVersion}\n`); + await writeFile(join(f.repoRoot,'pnpm-lock.yaml'),lock); + }; + const patchFile=join(f.repoRoot,'patches/pandoc-wasm@1.1.0.patch'); + await f.put(patchFile,patchBody); + await writePatchedLock(); + assert.deepEqual(await assertRuntimeOriginals(f.repoRoot,f.artifactRoot),f.runtime); + + await writePatchedLock(patchDigest,`1.1.0(patch_hash=${patchDigest})`,false); + await assert.rejects(runtimeSourcePins(f.repoRoot),/frozen patch digest/); + + await writePatchedLock('f'.repeat(64),`1.1.0(patch_hash=${patchDigest})`,true); + await assert.rejects(runtimeSourcePins(f.repoRoot),/frozen patch digest/); + + await writePatchedLock(); + await writeFile(patchFile,patchBody+'\n'); + await assert.rejects(runtimeSourcePins(f.repoRoot),/committed patch digest/); + + await rm(patchFile); + const realTarget=join(f.repoRoot,'patches/other.patch'); + await f.put(realTarget,patchBody); + await symlink(realTarget,patchFile); + await assert.rejects(runtimeSourcePins(f.repoRoot),/no symlinks/); + await rm(patchFile); + + for(const badPatch of [ + 'diff --git a/src/pandoc.wasm b/src/pandoc.wasm\n--- a/src/pandoc.wasm\n+++ b/src/pandoc.wasm\n', + 'diff --git a/package.json b/package.json\n--- a/package.json\n+++ b/package.json\n', + 'diff --git a/src/core.js b/src/core.js\nGIT binary patch\nliteral 0\n', + 'diff --git a/src/../src/core.js b/src/../src/core.js\n--- a/src/../src/core.js\n+++ b/src/../src/core.js\n', + ]) { + const badDigest=hash(Buffer.from(badPatch)); + await f.put(patchFile,badPatch); + await writePatchedLock(badDigest); + await assert.rejects(runtimeSourcePins(f.repoRoot),/patch must not modify official WASM or package manifest/); + } + + await writePatchedLock(patchDigest,'1.1.0(peer@1.0.0)',true); + await assert.rejects(runtimeSourcePins(f.repoRoot),/frozen importer pin/); +});