app

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

build-info.test.mjs (8833B)


      1 import assert from 'node:assert/strict';
      2 import { execFileSync } from 'node:child_process';
      3 import { createHash } from 'node:crypto';
      4 import {
      5   mkdtemp,
      6   mkdir,
      7   readFile,
      8   writeFile,
      9   rm,
     10   symlink,
     11   link
     12 } from 'node:fs/promises';
     13 import os from 'node:os';
     14 import path from 'node:path';
     15 import { test } from 'node:test';
     16 import { generateBuildInfo } from '../../tools/build-info.mjs';
     17 
     18 /** @param {Uint8Array} bytes */
     19 const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex');
     20 /** @param {string} root @param {string[]} args */
     21 function git(root, ...args) {
     22   return execFileSync('git', ['-C', root, ...args], {
     23     encoding: 'utf8'
     24   }).trim();
     25 }
     26 /** @param {import('node:test').TestContext} t */
     27 async function fixture(t) {
     28   // Actual immutable selection/lock bytes, never a native tool invocation.
     29   const root = await mkdtemp(path.join(os.tmpdir(), 'hc provenance '));
     30   t.after(() => rm(root, { recursive: true, force: true }));
     31   await mkdir(path.join(root, 'web/src'), { recursive: true });
     32   await mkdir(path.join(root, 'core'));
     33   await writeFile(
     34     path.join(root, 'web/src/source.ts'),
     35     'export const value = 1;\n'
     36   );
     37   await writeFile(
     38     path.join(root, 'web/.gitignore'),
     39     '/static/build-info.json\n/build/\n'
     40   );
     41   for (const name of [
     42     'radroots.lib.source-lock.v1.toml',
     43     'core/Cargo.lock',
     44     'web/pnpm-lock.yaml'
     45   ]) {
     46     await writeFile(
     47       path.join(root, name),
     48       await readFile(new URL('../../../' + name, import.meta.url))
     49     );
     50   }
     51   git(root, 'init', '-q');
     52   git(root, 'add', '.');
     53   git(
     54     root,
     55     '-c',
     56     'user.name=Provenance Test',
     57     '-c',
     58     'user.email=provenance@example.invalid',
     59     'commit',
     60     '-qm',
     61     'source fixture'
     62   );
     63   return {
     64     root,
     65     web: path.join(root, 'web'),
     66     generate: () => generateBuildInfo(path.join(root, 'web'))
     67   };
     68 }
     69 
     70 await test('allowlist, actual selected oracle/locks, clean identity and deterministic generated output', async (t) => {
     71   const f = await fixture(t);
     72   const first = await f.generate();
     73   const bytes = await readFile(path.join(f.web, 'static/build-info.json'));
     74   assert.deepEqual(Object.keys(first), [
     75     'web_source',
     76     'radroots_oracle',
     77     'dependency_locks'
     78   ]);
     79   assert.deepEqual(Object.keys(first.web_source), [
     80     'revision',
     81     'dirty',
     82     'input_sha256'
     83   ]);
     84   assert.deepEqual(Object.keys(first.radroots_oracle), [
     85     'repository',
     86     'revision',
     87     'source_lock_sha256'
     88   ]);
     89   assert.deepEqual(Object.keys(first.dependency_locks), [
     90     'web_pnpm_sha256',
     91     'oracle_cargo_sha256'
     92   ]);
     93   assert.equal(first.web_source.revision, git(f.root, 'rev-parse', 'HEAD'));
     94   assert.equal(first.web_source.dirty, false);
     95   assert.equal(
     96     first.radroots_oracle.revision,
     97     '189c49b74b4bafc142b00b76b296477931139e72'
     98   );
     99   /** @type {Array<[keyof typeof first.dependency_locks, string]>} */
    100   const locks = [
    101     ['web_pnpm_sha256', 'web/pnpm-lock.yaml'],
    102     ['oracle_cargo_sha256', 'core/Cargo.lock']
    103   ];
    104   for (const [field, name] of locks) {
    105     assert.equal(
    106       first.dependency_locks[field],
    107       sha256(await readFile(path.join(f.root, name)))
    108     );
    109   }
    110   await mkdir(path.join(f.web, 'build'));
    111   await writeFile(path.join(f.web, 'build/generated.txt'), 'generated output');
    112   await writeFile(path.join(f.root, 'native-unrelated.txt'), 'not a web input');
    113   assert.deepEqual(await f.generate(), first);
    114   assert.deepEqual(
    115     await readFile(path.join(f.web, 'static/build-info.json')),
    116     bytes
    117   );
    118   assert.equal(git(f.root, 'status', '--short', '--', 'web'), '');
    119   assert.doesNotMatch(
    120     bytes.toString(),
    121     /example.invalid|qualified|GIT_|\/Users\/|hc provenance/
    122   );
    123 });
    124 
    125 for (const mutation of [
    126   'modified',
    127   'staged',
    128   'deleted',
    129   'untracked',
    130   'lock',
    131   'oracle'
    132 ]) {
    133   await test(`actual ${mutation} input dirtiness changes web identity`, async (t) => {
    134     const f = await fixture(t);
    135     const before = await f.generate();
    136     const file = path.join(f.web, 'src/source.ts');
    137     if (mutation === 'deleted') await rm(file);
    138     else if (mutation === 'untracked')
    139       await writeFile(path.join(f.web, 'src/new file.ts'), 'new source');
    140     else if (mutation === 'lock')
    141       await writeFile(path.join(f.web, 'pnpm-lock.yaml'), 'changed web lock');
    142     else if (mutation === 'oracle') {
    143       const selected = path.join(f.root, 'radroots.lib.source-lock.v1.toml');
    144       await writeFile(
    145         selected,
    146         (await readFile(selected, 'utf8')).replace(
    147           '189c49b74b4bafc142b00b76b296477931139e72',
    148           'a'.repeat(40)
    149         )
    150       );
    151     } else {
    152       await writeFile(file, 'export const value = 2;\n');
    153       if (mutation === 'staged') git(f.root, 'add', 'web/src/source.ts');
    154     }
    155     const after = await f.generate();
    156     assert.equal(after.web_source.dirty, true);
    157     assert.notEqual(
    158       after.web_source.input_sha256,
    159       before.web_source.input_sha256
    160     );
    161     assert.equal(after.web_source.revision, before.web_source.revision);
    162     if (mutation === 'oracle')
    163       assert.equal(after.radroots_oracle.revision, 'a'.repeat(40));
    164   });
    165 }
    166 
    167 await test('reject unavailable/malformed selection, mismatched lock, symlink and tracked generated metadata', async (t) => {
    168   const f = await fixture(t);
    169   const selected = path.join(f.root, 'radroots.lib.source-lock.v1.toml');
    170   const original = await readFile(selected);
    171   await writeFile(selected, 'revision = "bogus"\n');
    172   await assert.rejects(f.generate, /invalid public Radroots/);
    173   await writeFile(
    174     selected,
    175     Buffer.concat([original, Buffer.from('revision = "duplicate"\n')])
    176   );
    177   await assert.rejects(f.generate, /Malformed/);
    178   await rm(selected);
    179   await assert.rejects(f.generate, /ENOENT/);
    180   await writeFile(selected, original);
    181   const cargoLock = path.join(f.root, 'core/Cargo.lock');
    182   const lockBytes = await readFile(cargoLock);
    183   await writeFile(cargoLock, 'tampered lock');
    184   await assert.rejects(f.generate, /digest mismatch/);
    185   await writeFile(cargoLock, lockBytes);
    186   await symlink('source.ts', path.join(f.web, 'src/link.ts'));
    187   await assert.rejects(f.generate, /Symlinked/);
    188   await rm(path.join(f.web, 'src/link.ts'));
    189   await f.generate();
    190   git(f.root, 'add', '-f', 'web/static/build-info.json');
    191   await assert.rejects(f.generate, /must not be tracked/);
    192 });
    193 
    194 await test('no Git and enclosing parent Git cannot masquerade as capsule source', async (t) => {
    195   const f = await fixture(t);
    196   await rm(path.join(f.root, '.git'), { recursive: true });
    197   await assert.rejects(f.generate);
    198   const parent = path.dirname(f.root);
    199   const outer = await mkdtemp(path.join(parent, 'hc outer '));
    200   t.after(() => rm(outer, { recursive: true, force: true }));
    201   git(outer, 'init', '-q');
    202   const web = path.join(outer, 'capsule/web');
    203   await mkdir(web, { recursive: true });
    204   await assert.rejects(() => generateBuildInfo(web), /HarvestCircle-owned Git/);
    205 });
    206 
    207 await test('ambient sibling Git redirect and secrets cannot affect metadata', async (t) => {
    208   const f = await fixture(t);
    209   const before = await f.generate();
    210   const script = new URL('../../tools/build-info.mjs', import.meta.url);
    211   const output = execFileSync(
    212     process.execPath,
    213     [
    214       '--input-type=module',
    215       '-e',
    216       `import {generateBuildInfo} from ${JSON.stringify(script.href)}; console.log(JSON.stringify(await generateBuildInfo(${JSON.stringify(f.web)})));`
    217     ],
    218     {
    219       env: {
    220         ...process.env,
    221         GIT_DIR: '/unavailable/sibling',
    222         GIT_WORK_TREE: '/private/parent',
    223         HARVESTCIRCLE_SECRET: 'HC_TEST_ONLY_SECRET'
    224       },
    225       encoding: 'utf8'
    226     }
    227   );
    228   assert.deepEqual(JSON.parse(output), before);
    229   assert.doesNotMatch(
    230     output,
    231     /HC_TEST_ONLY_SECRET|private\/parent|unavailable/
    232   );
    233 });
    234 
    235 await test('hostile output entries fail promptly without altering source', async (t) => {
    236   const f = await fixture(t);
    237   await f.generate();
    238   const directory = path.join(f.web, 'static');
    239   const output = path.join(directory, 'build-info.json');
    240   const source = path.join(f.web, 'src/source.ts');
    241   const original = await readFile(source);
    242   for (const kind of ['fifo', 'symlink', 'hardlink', 'directory']) {
    243     await rm(output, { recursive: true, force: true });
    244     if (kind === 'fifo') execFileSync('mkfifo', [output]);
    245     else if (kind === 'symlink') await symlink(source, output);
    246     else if (kind === 'hardlink') await link(source, output);
    247     else await mkdir(output);
    248     await assert.rejects(f.generate, /Unsafe generated metadata target/);
    249     assert.deepEqual(await readFile(source), original);
    250   }
    251   await rm(directory, { recursive: true });
    252   await symlink(path.dirname(source), directory);
    253   await assert.rejects(
    254     f.generate,
    255     /Unsafe generated metadata directory|Symlinked build provenance input/
    256   );
    257   assert.deepEqual(await readFile(source), original);
    258 });