app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

commit b6f29419d0d45dca38680bc043def22e8a6a46be
parent f02df937d0bb027d70e0c9fb85088f327d7d258c
Author: triesap <tyson@radroots.org>
Date:   Mon,  5 Oct 2026 01:37:01 +0000

build: separate web artifact provenance from native delivery authority (HCR-011)

- Audit the actual static payload against owned compiler inputs and public provenance.
- Bound output inventory and reject native, private and test contamination.
- Qualify exact full-tree source archive fixtures without changing native delivery contracts.
- Verify standalone web lanes and independent source review with preserved native inputs.

Diffstat:
Mweb/package.json | 2+-
Aweb/tests/provenance/output.test.mjs | 390+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/provenance/source-archive.test.mjs | 293+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tools/build-info.mjs | 10+++++++++-
Aweb/tools/check-output.mjs | 402+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 1095 insertions(+), 2 deletions(-)

diff --git a/web/package.json b/web/package.json @@ -11,7 +11,7 @@ }, "scripts": { "dev": "node tools/check-source.mjs && vite", - "build": "node tools/check-source.mjs && node tools/build-info.mjs && vite build", + "build": "node tools/check-source.mjs && node tools/build-info.mjs && vite build && node tools/check-output.mjs", "check": "node tools/check-source.mjs && svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --fail-on-warnings", "lint": "node tools/check-source.mjs && svelte-kit sync && eslint . --max-warnings 0 && prettier --check .", "test:unit": "node tools/check-source.mjs && svelte-kit sync && vitest run --config vitest.config.ts && node --test --test-timeout=10000 tests/provenance/*.test.mjs", diff --git a/web/tests/provenance/output.test.mjs b/web/tests/provenance/output.test.mjs @@ -0,0 +1,390 @@ +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { + cp, + link, + lstat, + mkdir, + mkdtemp, + readFile, + realpath, + rm, + symlink, + writeFile +} from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; +import { test } from 'node:test'; +import { setInterval, clearInterval } from 'node:timers'; +import { fileURLToPath } from 'node:url'; +import { generateBuildInfo } from '../../tools/build-info.mjs'; +import { auditOutput } from '../../tools/check-output.mjs'; + +await test('actual output qualification', { timeout: 30000 }, async (t) => { + const source = fileURLToPath(new URL('../../../', import.meta.url)); + const base = await mkdtemp(path.join(os.tmpdir(), 'hc actual output ')); + t.after(() => rm(base, { recursive: true, force: true })); + /** @param {string} root @param {string[]} args */ + const git = (root, ...args) => + execFileSync('git', ['-C', root, ...args], { + env: Object.fromEntries( + Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')) + ), + encoding: 'utf8' + }); + // Compile actual current source once in isolation. No install/native subprocess. + for (const name of git( + source, + 'ls-files', + '--cached', + '--others', + '--exclude-standard', + '-z', + '--', + 'web', + '.gitignore', + 'core/Cargo.lock', + 'radroots.lib.source-lock.v1.toml' + ) + .split('\0') + .filter(Boolean)) { + const stat = await lstat(path.join(source, name)); + assert.ok( + stat.isFile() && stat.nlink === 1 && stat.size <= 8 * 1024 * 1024, + 'Only safe actual source fixture inputs' + ); + await mkdir(path.dirname(path.join(base, name)), { recursive: true }); + await writeFile( + path.join(base, name), + await readFile(path.join(source, name)) + ); + } + git(base, 'init', '-q'); + git(base, 'add', '.'); + git( + base, + '-c', + 'user.name=Output Test', + '-c', + 'user.email=output@example.invalid', + 'commit', + '-qm', + 'actual inputs' + ); + await cp( + path.join(source, 'web/node_modules'), + path.join(base, 'web/node_modules'), + { recursive: true, verbatimSymlinks: true } + ); + await generateBuildInfo(path.join(base, 'web')); + execFileSync( + process.execPath, + [path.join(source, 'web/node_modules/vite/bin/vite.js'), 'build'], + { cwd: path.join(base, 'web'), stdio: 'pipe', timeout: 10000 } + ); + + /** @param {import('node:test').TestContext} t */ + async function fixture(t) { + const root = await realpath( + await mkdtemp(path.join(os.tmpdir(), 'hc output mutation ')) + ); + t.after(() => rm(root, { recursive: true, force: true })); + await cp(base, root, { + recursive: true, + filter: (name) => name !== path.join(base, 'web/node_modules') + }); + const web = path.join(root, 'web'); + return { root, web, audit: () => auditOutput(web) }; + } + await t.test( + 'actual compiled payload passes and build dispatch invokes the guard last', + async (t) => { + const f = await fixture(t); + const files = await f.audit(); + assert.equal(files.length, 15); + assert.ok(files.includes('200.html')); + const pkg = JSON.parse( + await readFile(path.join(f.web, 'package.json'), 'utf8') + ); + assert.match( + pkg.scripts.build, + /vite build && node tools\/check-output\.mjs$/ + ); + } + ); + for (const name of [ + 'notes.txt', + '_app/immutable/chunks/innocent.ABC12345.js', + 'docs/parent.html', + 'state.css', + 'source.map', + 'contracts/vector.json' + ]) { + await t.test(`undeclared copied output is rejected: ${name}`, async (t) => { + const f = await fixture(t); + const file = path.join(f.web, 'build', name); + await mkdir(path.dirname(file), { recursive: true }); + await writeFile(file, 'ordinary public text'); + await assert.rejects(f.audit); + }); + } + for (const payload of [ + Buffer.from('SQLite format 3\0'), + Buffer.from([0x7f, 0x45, 0x4c, 0x46]), + Buffer.from([0xcf, 0xfa, 0xed, 0xfe]), + Buffer.from('PK\x03\x04'), + Buffer.from('MZnative'), + Buffer.from('generated UniFFI bindings'), + Buffer.from('HC_TEST_ONLY_SECRET'), + Buffer.from('nsec1' + 'a'.repeat(60)), + Buffer.from('-----BEGIN PRIVATE KEY-----'), + Buffer.from('private_transcript: controlled disclosure'), + Buffer.from('class TestSigner {}') + ]) { + await t.test( + `manifest-admitted contamination ${payload.subarray(0, 18).toString('hex')}`, + async (t) => { + const f = await fixture(t); + const manifest = JSON.parse( + await readFile( + path.join(f.web, '.svelte-kit/output/client/.vite/manifest.json'), + 'utf8' + ) + ); + const name = Object.values(manifest).find( + (entry) => entry.name === 'client' + ).file; + // Poison both sides: byte mapping alone does not qualify contents. + for (const directory of ['build', '.svelte-kit/output/client']) + await writeFile(path.join(f.web, directory, name), payload); + await assert.rejects(f.audit, /contamination|binary|text/); + } + ); + } + for (const name of [ + 'index.html', + '200.html', + '_app/version.json', + 'build-info.json' + ]) { + await t.test(`missing/tampered owned output: ${name}`, async (t) => { + const f = await fixture(t); + const file = path.join(f.web, 'build', name); + const original = await readFile(file); + await rm(file); + await assert.rejects(f.audit); + await writeFile(file, Buffer.concat([original, Buffer.from('tampered')])); + await assert.rejects(f.audit); + }); + } + for (const mutation of [ + 'unknown', + 'private', + 'revision', + 'dirty', + 'lock', + 'config', + 'source', + 'generated' + ]) { + await t.test(`provenance fails closed: ${mutation}`, async (t) => { + const f = await fixture(t); + const metadata = JSON.parse( + await readFile(path.join(f.web, 'build/build-info.json'), 'utf8') + ); + if (mutation === 'unknown') metadata.web_source.unknown = 'extra'; + if (mutation === 'private') + metadata.private_transcript = 'controlled disclosure'; + if (mutation === 'revision') + metadata.web_source.revision = 'a'.repeat(40); + if (mutation === 'dirty') + metadata.web_source.dirty = !metadata.web_source.dirty; + if (mutation === 'lock') + await writeFile(path.join(f.web, 'pnpm-lock.yaml'), 'changed lock'); + if (mutation === 'config') + await writeFile(path.join(f.web, 'vite.config.ts'), 'changed config'); + if (mutation === 'source') + await writeFile(path.join(f.web, 'src/app.html'), 'changed source'); + if (mutation === 'generated') + git(f.root, 'add', '-f', 'web/static/build-info.json'); + if (['unknown', 'private', 'revision', 'dirty'].includes(mutation)) { + for (const directory of [ + 'build', + 'static', + '.svelte-kit/output/client' + ]) + await writeFile( + path.join(f.web, directory, 'build-info.json'), + JSON.stringify(metadata) + ); + } + await assert.rejects(f.audit); + }); + } + for (const kind of [ + 'symlink', + 'hardlink', + 'fifo', + 'directory', + 'oversize', + 'parent' + ]) { + await t.test(`unsafe output refuses ${kind} promptly`, async (t) => { + const f = await fixture(t); + const file = path.join(f.web, 'build/index.html'); + await rm(file); + if (kind === 'symlink') await symlink('../src/app.html', file); + if (kind === 'hardlink') + await link(path.join(f.web, 'src/app.html'), file); + if (kind === 'fifo') execFileSync('mkfifo', [file]); + if (kind === 'directory') await mkdir(file); + if (kind === 'oversize') + await writeFile(file, Buffer.alloc(8 * 1024 * 1024 + 1)); + if (kind === 'parent') { + await rm(path.join(f.web, 'build'), { recursive: true }); + await symlink(path.join(base, 'web/build'), path.join(f.web, 'build')); + } + await assert.rejects(f.audit); + }); + } + await t.test( + 'compiler manifest cannot authorize arbitrary resources, escapes or unresolved imports', + async (t) => { + const f = await fixture(t); + const file = path.join( + f.web, + '.svelte-kit/output/client/.vite/manifest.json' + ); + const original = await readFile(file, 'utf8'); + for (const mutation of ['extra', 'escape', 'reference', 'unknown']) { + const manifest = JSON.parse(original); + const record = Object.values(manifest)[0]; + if (mutation === 'extra') + manifest.extra = { + name: 'provider', + file: '_app/immutable/chunks/provider.ABC12345.js' + }; + if (mutation === 'escape') record.file = '../outside.js'; + if (mutation === 'reference') record.imports = ['absent']; + if (mutation === 'unknown') record.secret = 'private'; + await writeFile(file, JSON.stringify(manifest)); + await assert.rejects(f.audit); + } + } + ); + + await t.test( + 'generated provenance must remain ignored even when freshly regenerated', + async (t) => { + const f = await fixture(t); + await writeFile(path.join(f.web, '.gitignore'), ''); + await generateBuildInfo(f.web); + await assert.rejects(f.audit, /must be ignored/); + } + ); + for (const name of [ + 'static/build-info.json', + '.svelte-kit/output/client/.vite/manifest.json', + '.svelte-kit/output/prerendered/pages/index.html' + ]) { + for (const kind of ['fifo', 'symlink', 'hardlink']) { + await t.test( + `unsafe compiler/static counterpart ${name}: ${kind}`, + async (t) => { + const f = await fixture(t); + const file = path.join(f.web, name); + await rm(file); + if (kind === 'fifo') execFileSync('mkfifo', [file]); + if (kind === 'symlink') + await symlink(path.join(base, 'web/src/app.html'), file); + if (kind === 'hardlink') + await link(path.join(f.web, 'src/app.html'), file); + await assert.rejects(f.audit); + } + ); + } + } + await t.test( + 'empty output directory and excessive depth are rejected', + async (t) => { + const f = await fixture(t); + await mkdir(path.join(f.web, 'build/extra')); + await assert.rejects(f.audit); + await rm(path.join(f.web, 'build/extra'), { recursive: true }); + await mkdir(path.join(f.web, 'build/a/b/c/d/e/f/g/h/i'), { + recursive: true + }); + await assert.rejects(f.audit); + } + ); + await t.test( + 'forged public output failures do not echo private poisoned bytes', + async (t) => { + const f = await fixture(t); + const marker = 'CONTROLLED_SENSITIVE_DO_NOT_ECHO'; + const file = path.join(f.web, 'static/build-info.json'); + await writeFile(file, marker); + await assert.rejects(f.audit, (error) => !String(error).includes(marker)); + } + ); + await t.test( + 'output entry and aggregate-byte bounds reject oversized inventories', + async (t) => { + const f = await fixture(t); + for (let i = 0; i < 513; i++) + await writeFile(path.join(f.web, 'build', `tiny-${i}.js`), 'x'); + const baseline = process.memoryUsage().arrayBuffers; + let peak = baseline; + const timer = setInterval(() => { + peak = Math.max(peak, process.memoryUsage().arrayBuffers); + }, 1); + try { + await assert.rejects(f.audit, /Unbounded output inventory/); + } finally { + clearInterval(timer); + } + assert.ok( + peak - baseline < 64 * 1024 * 1024, + 'Tiny files must not retain a maximum-sized buffer per entry' + ); + t.diagnostic(`many-small-entry buffer growth: ${peak - baseline} bytes`); + for (let i = 0; i < 513; i++) + await rm(path.join(f.web, 'build', `tiny-${i}.js`)); + for (let i = 0; i < 33; i++) + await writeFile( + path.join(f.web, 'build', `large-${i}.js`), + Buffer.alloc(1024 * 1024, 97) + ); + await assert.rejects(f.audit, /Unbounded output bytes/); + } + ); + await t.test( + 'actual compiler admits nonempty approved theme/app CSS', + async () => { + const layout = path.join(base, 'web/src/routes/+layout.svelte'); + await writeFile( + layout, + (await readFile(layout, 'utf8')).replace( + '<script lang="ts">', + "<script lang=\"ts\">\n import '../theme.css';\n import '../app.css';" + ) + ); + await writeFile( + path.join(base, 'web/src/theme.css'), + ':root { --hc-accent: #336633; }\n' + ); + await writeFile( + path.join(base, 'web/src/app.css'), + 'main { color: var(--hc-accent); }\n' + ); + await generateBuildInfo(path.join(base, 'web')); + execFileSync( + process.execPath, + [path.join(base, 'web/node_modules/vite/bin/vite.js'), 'build'], + { cwd: path.join(base, 'web'), stdio: 'pipe', timeout: 10000 } + ); + const files = await auditOutput(await realpath(path.join(base, 'web'))); + assert.ok(files.some((name) => name.endsWith('.css'))); + } + ); +}); diff --git a/web/tests/provenance/source-archive.test.mjs b/web/tests/provenance/source-archive.test.mjs @@ -0,0 +1,293 @@ +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { readFile } from 'node:fs/promises'; +import { test } from 'node:test'; +import { fileURLToPath } from 'node:url'; + +const gitEnv = Object.fromEntries( + Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')) +); +const root = fileURLToPath(new URL('../../../', import.meta.url)); +/** @param {string[]} args */ +const git = (...args) => + execFileSync('git', ['-C', root, ...args], { + env: gitEnv, + maxBuffer: 64 * 1024 * 1024 + }); +const revision = git('rev-parse', 'HEAD').toString().trim(); +const epoch = Number( + git('show', '-s', '--format=%ct', revision).toString().trim() +); +const tree = git('ls-tree', '-rz', '--full-tree', revision) + .toString() + .split('\0') + .filter(Boolean) + .map((entry) => { + const match = /^(100644|100755) blob ([a-f0-9]{40})\t(.+)$/.exec(entry); + assert.ok(match, 'Full source tree must contain regular Git blobs only'); + return { + mode: match[1] === '100755' ? 0o755 : 0o644, + blob: match[2], + name: match[3] + }; + }) + .sort((a, b) => Buffer.compare(Buffer.from(a.name), Buffer.from(b.name))); +// One actual Git blob batch, independent from filesystem/generated outputs. +const raw = execFileSync('git', ['-C', root, 'cat-file', '--batch'], { + input: tree.map((entry) => entry.blob).join('\n') + '\n', + env: gitEnv, + maxBuffer: 64 * 1024 * 1024 +}); +let cursor = 0; +const inputs = tree.map((entry) => { + const end = raw.indexOf(10, cursor); + const header = raw.subarray(cursor, end).toString().split(' '); + assert.equal(header[0], entry.blob); + assert.equal(header[1], 'blob'); + const size = Number(header[2]); + const bytes = raw.subarray(end + 1, end + 1 + size); + assert.equal(bytes.length, size); + cursor = end + size + 2; + return { ...entry, bytes }; +}); +assert.equal(cursor, raw.length); +/** @param {Buffer} header @param {number} offset @param {number} width @param {number} value */ +function octal(header, offset, width, value) { + const field = value.toString(8).padStart(width - 1, '0') + '\0'; + assert.equal(field.length, width); + header.write(field, offset, width, 'ascii'); +} +/** @param {typeof inputs} entries */ +function encodeFixture(entries) { + // Test-only canonical materialization: never a release/distribution producer. + const blocks = []; + for (const entry of entries) { + const header = Buffer.alloc(512); + let name = entry.name; + let prefix = ''; + if (Buffer.byteLength(name) > 100) { + const split = name.lastIndexOf('/'); + prefix = name.slice(0, split); + name = name.slice(split + 1); + } + assert.ok( + Buffer.byteLength(name) <= 100 && Buffer.byteLength(prefix) <= 155 + ); + header.write(name, 0, 100); + octal(header, 100, 8, entry.mode); + octal(header, 108, 8, 0); + octal(header, 116, 8, 0); + octal(header, 124, 12, entry.bytes.length); + octal(header, 136, 12, epoch); + header.fill(32, 148, 156); + header[156] = 48; + header.write('ustar\0', 257, 6); + header.write('00', 263, 2); + header.write(prefix, 345, 155); + const checksum = header.reduce((sum, value) => sum + value, 0); + header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8); + blocks.push( + header, + entry.bytes, + Buffer.alloc((512 - (entry.bytes.length % 512)) % 512) + ); + } + return Buffer.concat([...blocks, Buffer.alloc(1024)]); +} +/** @param {Buffer} bytes */ +function decodeAndCompare(bytes) { + let offset = 0; + let count = 0; + let previous = Buffer.alloc(0); + while ( + offset < bytes.length && + !bytes.subarray(offset, offset + 512).equals(Buffer.alloc(512)) + ) { + const header = bytes.subarray(offset, offset + 512); + assert.equal(header.length, 512, 'Truncated header'); + /** @param {number} start @param {number} width */ + const field = (start, width) => { + const part = header.subarray(start, start + width); + const zero = part.indexOf(0); + if (zero !== -1) + assert.ok( + part.subarray(zero).every((value) => value === 0), + 'Noncanonical field padding' + ); + return new TextDecoder('utf-8', { fatal: true }).decode( + zero === -1 ? part : part.subarray(0, zero) + ); + }; + /** @param {number} start @param {number} width */ + const number = (start, width) => { + const value = field(start, width); + assert.match(value, /^[0-7]+$/); + return Number.parseInt(value, 8); + }; + const prefix = field(345, 155); + const name = (prefix ? prefix + '/' : '') + field(0, 100); + assert.ok( + name && + !name.startsWith('/') && + !name.split('/').some((part) => !part || part === '.' || part === '..'), + 'Unsafe archive path' + ); + const ordered = Buffer.from(name); + assert.ok( + Buffer.compare(previous, ordered) < 0, + 'Bytewise order/duplicate violation' + ); + previous = ordered; + assert.equal( + header[156], + 48, + 'Only regular files, no PAX/directory/link/submodule' + ); + assert.equal(field(157, 100), '', 'No link target'); + assert.equal(header.subarray(257, 263).toString(), 'ustar\0'); + assert.equal(header.subarray(263, 265).toString(), '00'); + for (const [start, width] of [ + [265, 32], + [297, 32] + ]) + assert.equal(field(start, width), '', 'No owner names'); + assert.equal(number(108, 8), 0); + assert.equal(number(116, 8), 0); + assert.ok( + header.subarray(329, 345).every((value) => value === 0), + 'No device metadata' + ); + assert.ok( + header.subarray(500).every((value) => value === 0), + 'No header extension' + ); + assert.equal(number(136, 12), epoch, 'Actual candidate epoch'); + const expected = inputs[count++]; + assert.ok(expected, 'Extra archive entry'); + assert.equal(name, expected.name, 'Exact unprefixed full-tree path'); + assert.equal(number(100, 8), expected.mode, 'Exact Git executable mode'); + const size = number(124, 12); + assert.equal(size, expected.bytes.length); + const checksum = header.reduce( + (sum, value, index) => sum + (index >= 148 && index < 156 ? 32 : value), + 0 + ); + assert.match(header.subarray(148, 156).toString(), /^[0-7]{6}\0 $/); + assert.equal( + Number.parseInt(header.subarray(148, 154).toString(), 8), + checksum + ); + offset += 512; + assert.deepEqual( + bytes.subarray(offset, offset + size), + expected.bytes, + 'Actual Git blob bytes' + ); + offset += size; + const padding = (512 - (size % 512)) % 512; + assert.ok( + bytes.subarray(offset, offset + padding).every((value) => value === 0), + 'Zero content padding' + ); + offset += padding; + } + assert.equal( + count, + inputs.length, + 'Complete tracked tree, never runtime-filtered' + ); + assert.equal(bytes.length - offset, 1024, 'Exactly two zero trailer blocks'); + assert.ok(bytes.subarray(offset).every((value) => value === 0)); +} +const archive = encodeFixture(inputs); +await test('native v3 JSON and Kotlin canonical authority remain exact and separate from web provenance', async () => { + const contractBytes = await readFile( + new URL( + '../../../contracts/release/harvestcircle-artifact-contract.v3.json', + import.meta.url + ) + ); + const kotlin = await readFile( + new URL( + '../../../build-logic/contracts/src/main/kotlin/org/harvestcircle/buildlogic/contracts/HarvestCircleArtifactContract.kt', + import.meta.url + ), + 'utf8' + ); + const literal = /CANONICAL_JSON: String =\s*("(?:[^"\\]|\\.)*")/.exec(kotlin); + assert.ok(literal); + assert.equal(contractBytes.toString(), JSON.parse(literal[1])); + const contract = JSON.parse(contractBytes.toString()); + assert.equal(contract.contract_version, 3); + assert.equal(contract.package_contract.filename, 'HarvestCircle-1.0.0.dmg'); + assert.equal(contract.producer.source_task, 'packageDmg'); + assert.equal(contract.source_archive.format, 'ustar'); + assert.equal(contract.source_archive.content, 'exact_source_revision_tree'); + assert.equal(contract.source_archive.pax_headers, 'forbidden'); + assert.equal(contract.source_archive.trailer, 'two_zero_blocks'); + assert.equal(contract.delivery.publication, 'unauthorized'); + assert.equal(contract.sqlite.high_level_authority, 'sqlx_only'); +}); +await test('actual complete HEAD Git tree fixture preserves web, licenses, corpus, modes, epoch and blobs', () => { + decodeAndCompare(archive); + assert.ok(inputs.some((entry) => entry.name === 'web/package.json')); + assert.ok(inputs.some((entry) => entry.name === 'LICENSE')); + assert.ok(inputs.some((entry) => entry.name.startsWith('LICENSES/'))); + assert.ok( + inputs.some((entry) => + entry.name.startsWith('core/crates/harvestcircle_nostr/tests/fixtures/') + ) + ); + assert.ok(inputs.some((entry) => entry.mode === 0o755)); +}); +for (const kind of [ + 'filtered', + 'truncated', + 'trailer', + 'content', + 'mode', + 'epoch', + 'uid', + 'prefix', + 'pax', + 'directory', + 'symlink', + 'hardlink', + 'order', + 'duplicate', + 'padding' +]) { + await test(`independent full-tree archive comparison rejects ${kind}`, () => { + let candidate = Buffer.from(archive); + if (kind === 'filtered') + candidate = encodeFixture( + inputs.filter((entry) => !entry.name.startsWith('web/')) + ); + else if (kind === 'order') + candidate = encodeFixture([inputs[1], inputs[0], ...inputs.slice(2)]); + else if (kind === 'duplicate') + candidate = encodeFixture([inputs[0], ...inputs]); + else if (kind === 'truncated') candidate = candidate.subarray(0, 200); + else if (kind === 'trailer') + candidate = Buffer.concat([candidate, Buffer.alloc(512)]); + else if (kind === 'content') candidate[512] ^= 1; + else if (kind === 'mode') + octal(candidate, 100, 8, inputs[0].mode === 0o644 ? 0o755 : 0o644); + else if (kind === 'epoch') octal(candidate, 136, 12, epoch + 1); + else if (kind === 'uid') octal(candidate, 108, 8, 1); + else if (kind === 'prefix') candidate.write('runtime/', 0); + else if (kind === 'padding') candidate[512 + inputs[0].bytes.length] = 1; + else { + const type = { pax: 120, directory: 53, symlink: 50, hardlink: 49 }[kind]; + if (type === undefined) + throw new Error('Unknown negative archive fixture'); + candidate[156] = type; + } + assert.throws(() => decodeAndCompare(candidate)); + }); +} +await test('ordinary git archive PAX/directory output is not the native v3 canonical fixture', () => { + assert.throws(() => + decodeAndCompare(git('archive', '--format=tar', revision)) + ); +}); diff --git a/web/tools/build-info.mjs b/web/tools/build-info.mjs @@ -78,7 +78,7 @@ function selectedOracle(bytes) { } /** @param {string} webDirectory */ -export async function generateBuildInfo(webDirectory) { +export async function deriveBuildInfo(webDirectory) { const web = await realpath(webDirectory); if (path.basename(web) !== 'web') throw new Error('Build provenance requires the owned web directory'); @@ -172,6 +172,14 @@ export async function generateBuildInfo(webDirectory) { oracle_cargo_sha256: oracleLockDigest } }; + return metadata; +} + +/** @param {string} webDirectory */ +export async function generateBuildInfo(webDirectory) { + const metadata = await deriveBuildInfo(webDirectory); + const web = await realpath(webDirectory); + const root = await realpath(path.dirname(web)); const directory = path.join(web, 'static'); await mkdir(directory, { recursive: true }); if (!(await lstat(directory)).isDirectory()) diff --git a/web/tools/check-output.mjs b/web/tools/check-output.mjs @@ -0,0 +1,402 @@ +import { execFileSync } from 'node:child_process'; +import { isDeepStrictEqual } from 'node:util'; +import { constants } from 'node:fs'; +import { lstat, open, opendir, realpath } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { deriveBuildInfo } from './build-info.mjs'; + +/** @param {unknown} actual @param {unknown} expected @param {string} [message] */ +function equal(actual, expected, message = 'Invalid public output shape') { + if (!isDeepStrictEqual(actual, expected)) throw new Error(message); +} +/** @param {string} text */ +function parse(text) { + try { + return JSON.parse(text); + } catch { + throw new Error('Invalid static JSON'); + } +} +const maxBytes = 8 * 1024 * 1024; +/** @param {string} name */ +function safeName(name) { + if ( + !name || + name.includes('\\') || + name.split('/').some((part) => !part || part === '.' || part === '..') || + [...name].some( + (character) => + character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127 + ) + ) + throw new Error('Unsafe output path'); +} +/** @param {string} root @param {string} name */ +async function readOwned(root, name) { + safeName(name); + const parts = name.split('/'); + let file = root; + for (let i = 0; i < parts.length; i++) { + file = path.join(file, parts[i]); + const stat = await lstat(file); + if ( + i < parts.length - 1 + ? !stat.isDirectory() + : !stat.isFile() || + stat.nlink !== 1 || + Boolean(stat.mode & 0o111) || + stat.size > maxBytes + ) + throw new Error('Unsafe output entry'); + } + if ((await realpath(file)) !== file) + throw new Error('Unsafe output ancestry'); + const handle = await open( + file, + constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK + ); + try { + const stat = await handle.stat(); + if ( + !stat.isFile() || + stat.nlink !== 1 || + Boolean(stat.mode & 0o111) || + stat.size > maxBytes + ) + throw new Error('Unsafe output entry'); + const chunks = []; + let size = 0; + for (;;) { + const buffer = Buffer.alloc(64 * 1024); + const { bytesRead } = await handle.read(buffer); + if (!bytesRead) break; + size += bytesRead; + if (size > maxBytes) throw new Error('Oversized output entry'); + chunks.push(buffer.subarray(0, bytesRead)); + } + return Buffer.concat(chunks); + } finally { + await handle.close(); + } +} +/** @param {string} root */ +async function inventory(root) { + if (!(await lstat(root)).isDirectory() || (await realpath(root)) !== root) + throw new Error('Unsafe output root'); + const files = new Map(); + let entries = 0; + let total = 0; + /** @param {string} prefix */ + async function walk(prefix) { + let childCount = 0; + for await (const entry of await opendir(path.join(root, prefix))) { + childCount++; + if (++entries > 512) throw new Error('Unbounded output inventory'); + const name = prefix ? `${prefix}/${entry.name}` : entry.name; + safeName(name); + if (name.split('/').length > 8) throw new Error('Unbounded output depth'); + const stat = await lstat(path.join(root, name)); + if (stat.isDirectory()) { + await walk(name); + } else { + const bytes = await readOwned(root, name); + total += bytes.length; + if (total > 32 * 1024 * 1024) throw new Error('Unbounded output bytes'); + files.set(name, bytes); + } + } + if (prefix && !childCount) + throw new Error('Undeclared empty output directory'); + } + await walk(''); + return files; +} +/** @param {Uint8Array} bytes */ +function publicText(bytes) { + const buffer = Buffer.from(bytes); + // Controlled binary/container/database signatures, irrespective of filename. + const magic = [ + '7f454c46', + 'cffaedfe', + 'cefaedfe', + 'feedfacf', + 'feedface', + 'cafebabe', + 'bebafeca', + '504b0304', + '504b0506', + '1f8b', + '0061736d' + ]; + if ( + magic.some( + (hex) => buffer.subarray(0, hex.length / 2).toString('hex') === hex + ) || + buffer.subarray(0, 2).toString() === 'MZ' || + buffer.subarray(0, 16).toString() === 'SQLite format 3\0' + ) + throw new Error('Static binary contamination'); + const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes); + if ( + [...text].some((character) => { + const code = character.charCodeAt(0); + return code < 32 && ![9, 10, 13].includes(code); + }) + ) + throw new Error('Invalid static text'); + // These are bounded contamination controls, not an arbitrary-secret scanner. + if ( + /UniFFI|HC_TEST_ONLY_SECRET|nsec1[023456789acdefghjklmnpqrstuvwxyz]{20,}|-----BEGIN (?:[A-Z ]*PRIVATE KEY|OPENSSH)|private[_ -]transcript|TestSigner|test[_ -]provider|createRelayHarness|createStaticHarness|installControlledProvider|sourceMappingURL|radroots\.lib\.source-lock|core\/Cargo\.lock|docs\/oss\/harvestcircle|HARVESTCIRCLE_SECRET/i.test( + text + ) + ) + throw new Error('Static controlled contamination'); + return text; +} +/** @param {unknown} value @param {string[]} keys */ +function objectKeys(value, keys) { + if (!value || typeof value !== 'object' || Array.isArray(value)) + throw new Error('Invalid compiler object'); + if (Object.keys(value).some((key) => !keys.includes(key))) + throw new Error('Unknown compiler field'); +} +/** @param {string} webDirectory */ +export async function auditOutput(webDirectory) { + const web = path.resolve(webDirectory); + if (path.basename(web) !== 'web' || (await realpath(web)) !== web) + throw new Error('Output audit requires the owned web directory'); + // Pure derivation: auditing must not repair or overwrite poisoned metadata. + const gitEnv = Object.fromEntries( + Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')) + ); + try { + execFileSync( + 'git', + [ + '-C', + path.dirname(web), + 'check-ignore', + '-q', + '--', + 'web/static/build-info.json' + ], + { env: gitEnv, stdio: 'pipe' } + ); + } catch { + throw new Error('Generated public provenance must be ignored'); + } + const expectedMetadata = Buffer.from( + JSON.stringify(await deriveBuildInfo(web), null, 2) + '\n' + ); + const generated = await readOwned(web, 'static/build-info.json'); + equal(generated, expectedMetadata, 'Stale or nonpublic generated provenance'); + const client = await inventory(path.join(web, '.svelte-kit/output/client')); + const pages = await inventory( + path.join(web, '.svelte-kit/output/prerendered/pages') + ); + const output = await inventory(path.join(web, 'build')); + const manifestBytes = client.get('.vite/manifest.json'); + if (!manifestBytes) throw new Error('Missing compiler manifest'); + const manifest = parse(publicText(manifestBytes)); + objectKeys(manifest, Object.keys(manifest)); + // Admit compiler-owned module identities, not arbitrary extensions/copy roots. + const names = new Set([ + 'entry/app', + 'entry/start', + 'entry/payload', + 'nodes/0', + 'nodes/1', + 'nodes/2', + 'error-template', + 'preload-helper', + 'client', + 'client.svelte', + 'client-entry' + ]); + const admitted = new Map([['build-info.json', expectedMetadata]]); + const seen = new Set(); + for (const [key, record] of Object.entries(manifest)) { + objectKeys(record, [ + 'file', + 'name', + 'src', + 'isEntry', + 'isDynamicEntry', + 'imports', + 'dynamicImports', + 'css' + ]); + if (!names.has(record.name) || seen.has(record.name)) + throw new Error('Undeclared compiler module'); + seen.add(record.name); + const generatedModule = + /^\.svelte-kit\/generated\/build\/(?:client-optimized\/(?:app|nodes\/[012])|shared\/error-template)\.js$/; + const frameworkModule = + /^node_modules\/\.pnpm\/[^/]+\/node_modules\/@sveltejs\/kit\/src\/runtime\/client\/(?:client-entry|entry|payload)\.js$/; + if (!( + generatedModule.test(key) || + frameworkModule.test(key) || + /^_[A-Za-z0-9_-]+\.js$/.test(key) + )) + throw new Error('Undeclared compiler input'); + if ( + record.src !== undefined && + (record.src !== key || + !(generatedModule.test(key) || frameworkModule.test(key))) + ) + throw new Error('Invalid compiler source'); + if ( + !/^_app\/immutable\/(?:(?:entry|nodes)\/[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|chunks\/[A-Za-z0-9_-]+)\.js$/.test( + record.file + ) + ) + throw new Error('Undeclared compiler output'); + for (const flag of ['isEntry', 'isDynamicEntry']) + if (record[flag] !== undefined && typeof record[flag] !== 'boolean') + throw new Error('Invalid compiler flag'); + for (const field of ['imports', 'dynamicImports', 'css']) { + if ( + record[field] !== undefined && + (!Array.isArray(record[field]) || + record[field].some((item) => typeof item !== 'string') || + new Set(record[field]).size !== record[field].length) + ) + throw new Error('Invalid compiler references'); + } + for (const dependency of [ + ...(record.imports ?? []), + ...(record.dynamicImports ?? []) + ]) + if (!Object.hasOwn(manifest, dependency)) + throw new Error('Unresolved compiler import'); + for (const file of [record.file, ...(record.css ?? [])]) { + safeName(file); + if ( + file !== record.file && + !/^_app\/immutable\/assets\/[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.css$/.test( + file + ) + ) + throw new Error('Undeclared compiler resource'); + const bytes = client.get(file); + if (!bytes) throw new Error('Missing compiler output'); + publicText(bytes); + const previous = admitted.get(file); + if (previous && !previous.equals(bytes)) + throw new Error('Conflicting compiler output'); + admitted.set(file, bytes); + } + } + equal([...seen].sort(), [...names].sort(), 'Incomplete compiler modules'); + const versionBytes = client.get('_app/version.json'); + if (!versionBytes) throw new Error('Missing compiler version'); + const version = parse(publicText(versionBytes)); + equal(Object.keys(version), ['version']); + if ( + typeof version.version !== 'string' || + !/^[A-Za-z0-9._-]{1,128}$/.test(version.version) + ) + throw new Error('Invalid compiler version'); + admitted.set('_app/version.json', versionBytes); + equal( + [...client.keys()].sort(), + [...admitted.keys(), '.vite/manifest.json'].sort(), + 'Compiler inventory mismatch' + ); + equal( + client.get('build-info.json'), + expectedMetadata, + 'Compiler provenance mismatch' + ); + equal([...pages.keys()], ['index.html'], 'Undeclared prerendered pages'); + admitted.set('index.html', pages.get('index.html')); + // Adapter fallback has no separate disk input. Reconstruct the exact current + // compiler bootstrap shell from the actual index/manifest/version and source + // template; future framework/resource shapes require scoped tested admission. + const index = publicText(pages.get('index.html')); + const id = /(__sveltekit_[a-z0-9]+) = \{/.exec(index)?.[1]; + if (!id) throw new Error('Missing compiler bootstrap identity'); + const start = Object.values(manifest).find( + (record) => record.name === 'entry/start' + ).file; + const app = Object.values(manifest).find( + (record) => record.name === 'entry/app' + ).file; + const preload = [ + ...index.matchAll(/<link href="\.\/([^"<>]+)" rel="modulepreload">/g) + ] + .map((match) => match[1]) + .filter( + (file) => + !file.startsWith('_app/immutable/nodes/') || + file === + Object.values(manifest).find((record) => record.name === 'nodes/0') + .file + ); + if (!preload.length || preload.some((file) => !admitted.has(file))) + throw new Error('Invalid compiler preloads'); + const fallbackCSS = new Set(); + const cssVisited = new Set(); + /** @param {string} key */ + function collectCSS(key) { + if (cssVisited.has(key)) return; + cssVisited.add(key); + const record = manifest[key]; + for (const file of record.css ?? []) fallbackCSS.add(file); + for (const dependency of record.imports ?? []) collectCSS(dependency); + } + for (const [key, record] of Object.entries(manifest)) + if (['entry/start', 'entry/app', 'nodes/0'].includes(record.name)) + collectCSS(key); + const stylesheetLinks = [ + ...index.matchAll(/<link href="\.\/([^"<>]+)" rel="stylesheet">/g) + ] + .map((match) => match[1]) + .filter((file) => fallbackCSS.has(file)); + equal( + [...stylesheetLinks].sort(), + [...fallbackCSS].sort(), + 'Missing owned fallback stylesheet' + ); + const head = + preload + .map((file) => `<link href="/${file}" rel="modulepreload">`) + .join('\n\t\t') + + '\n\t\t' + + (stylesheetLinks.length ? '\n\t\t' : '') + + stylesheetLinks + .map((file) => `<link href="/${file}" rel="stylesheet">`) + .join('\n\t\t'); + const body = `\n\t\t\t<script>\n\t\t\t\t{\n\t\t\t\t\t${id} = {\n\t\t\t\t\t\tbase: "",\n\t\t\t\t\t\tversion: ${JSON.stringify(version.version)}\n\t\t\t\t\t};\n\n\t\t\t\t\tconst element = document.currentScript.parentElement;\n\n\t\t\t\t\timport("/${start}").then(async (kit) => {\n\t\t\t\t\t\tkit.init(${id});\n\t\t\t\t\t\tconst app = await import("/${app}");\n\t\t\t\t\t\tkit.start(app, element);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t</script>\n\t\t`; + const template = publicText(await readOwned(web, 'src/app.html')); + admitted.set( + '200.html', + Buffer.from( + template + .replace('%sveltekit.head%', head) + .replace('%sveltekit.body%', body) + ) + ); + equal( + [...output.keys()].sort(), + [...admitted.keys()].sort(), + 'Static output inventory mismatch' + ); + for (const [file, expected] of admitted) { + const bytes = output.get(file); + publicText(bytes); + equal(bytes, expected, 'Static output differs from owned compiler input'); + } + return [...output.keys()].sort(); +} +if ( + process.argv[1] && + path.resolve(process.argv[1]) === fileURLToPath(import.meta.url) +) { + const web = fileURLToPath(new URL('../', import.meta.url)); + if ((await realpath(process.cwd())) !== (await realpath(web))) + throw new Error('Run the output audit from web/'); + const files = await auditOutput(web); + console.log(`Static output boundary: ${files.length} owned files checked`); +}