Add encrypted build and save flow

build.js seals a JSON document into the single-file HTML (passphrase from
a hidden prompt or FW_PASSPHRASE, 12 character minimum). In the page,
Save writes via the File System Access API when available and downloads
otherwise; Backup copy downloads final-wishes-YYYYMMDD.html; Ctrl+S and
Change passphrase are supported. Build and Save share assemble() in
template.js, replacing src/index.html. Tests confirm the built file
holds no plaintext and that the payload round-trips.

Closes #6

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 08:17:20 -05:00
parent e291ee75b6
commit fec23c3e80
8 changed files with 293 additions and 32 deletions

76
test/build.test.js Normal file
View File

@@ -0,0 +1,76 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync, spawnSync } from 'node:child_process';
import { mkdtempSync, writeFileSync, readFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { decrypt, passphraseProblem } from '../src/crypto.js';
import { validate } from '../src/model.js';
import { backupName } from '../src/ops.js';
import { assemble } from '../src/template.js';
const SECRET = 'hunter2-SENTINEL-9431';
const doc = {
schemaVersion: 1,
meta: { owner: 'Test Owner SENTINEL', title: 'T', lastUpdated: '2022-04-14' },
sections: [{ id: 's', title: 'S', kind: 'records', records: [
{ id: 'r', title: 'Acct', status: 'ok', updated: '2022-04-14',
fields: [{ label: 'PW', value: SECRET, secret: true }] }] }],
};
const PASS = 'a long test passphrase';
function build(extraEnv = {}, args) {
const dir = mkdtempSync(join(tmpdir(), 'fw-'));
const data = join(dir, 'doc.json');
const out = join(dir, 'out.html');
writeFileSync(data, JSON.stringify(doc));
const r = spawnSync('node', ['build.js', '--data', data, '--out', out, ...(args || [])],
{ env: { ...process.env, FW_PASSPHRASE: PASS, ...extraEnv }, encoding: 'utf8' });
return { r, out };
}
test('built file embeds ciphertext only: no plaintext anywhere', () => {
const { r, out } = build();
assert.equal(r.status, 0, r.stderr);
const html = readFileSync(out, 'utf8');
assert.ok(!html.includes(SECRET), 'secret leaked');
assert.ok(!html.includes('Test Owner SENTINEL'), 'owner leaked');
assert.ok(!html.includes(PASS), 'passphrase leaked');
assert.doesNotMatch(html, /https?:\/\//, 'external URL present');
});
test('embedded payload decrypts back to the document', async () => {
const { out } = build();
const html = readFileSync(out, 'utf8');
const payload = html.match(/<script id="payload"[^>]*>([^<]*)<\/script>/)[1];
const back = JSON.parse(await decrypt(payload, PASS));
assert.deepEqual(back, doc);
assert.deepEqual(validate(back), []);
await assert.rejects(decrypt(payload, 'wrong passphrase!!'));
});
test('build refuses a weak passphrase and missing data', () => {
const weak = build({ FW_PASSPHRASE: 'short' });
assert.notEqual(weak.r.status, 0);
assert.match(weak.r.stderr, /at least 12/);
const bad = spawnSync('node', ['build.js', '--data', '/nonexistent.json'], { env: { ...process.env, FW_PASSPHRASE: PASS }, encoding: 'utf8' });
assert.notEqual(bad.status, 0);
});
test('assemble is reproducible from the pieces of a built file (what Save relies on)', () => {
const { out } = build();
const html = readFileSync(out, 'utf8');
const css = html.match(/<style id="fw-css">([\s\S]*?)<\/style>/)[1];
const js = html.match(/<script id="fw-js">([\s\S]*?)<\/script>\n<\/body>/)[1];
const payload = html.match(/<script id="payload"[^>]*>([^<]*)<\/script>/)[1];
assert.equal(assemble({ css, js, payload }), html);
assert.doesNotMatch(js, /<\/script/i, 'inline script would end early');
});
test('backupName and passphraseProblem', () => {
assert.equal(backupName(new Date(2026, 10, 9)), 'final-wishes-20261109.html');
assert.equal(backupName(new Date(2026, 0, 5)), 'final-wishes-20260105.html');
assert.ok(passphraseProblem(''));
assert.ok(passphraseProblem('elevenchars'));
assert.equal(passphraseProblem('twelve chars!'), '');
});