63 lines
3.2 KiB
JavaScript
63 lines
3.2 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { mkdtempSync, writeFileSync, readFileSync, existsSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { encrypt, decrypt } from '../src/crypto.js';
|
|
import { assemble } from '../src/template.js';
|
|
import { sanitize, extractPayload } from '../sanitize.js';
|
|
|
|
const SECRET = 'hunter2-SENTINEL-9431';
|
|
const OLD = 'the real long passphrase';
|
|
const NEW = 'a throwaway passphrase';
|
|
const doc = {
|
|
schemaVersion: 1,
|
|
meta: { owner: 'O', 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 }, { label: 'Bank', value: 'First Bank' }] }] }],
|
|
};
|
|
const fast = 1000; // PBKDF2 iterations, tests only
|
|
async function realFile() {
|
|
return assemble({ css: 'x', js: 'y', payload: await encrypt(JSON.stringify(doc), OLD, fast) });
|
|
}
|
|
|
|
test('erases secrets, keeps the rest, re-seals under the new passphrase', async () => {
|
|
const html = await realFile();
|
|
const r = await sanitize({ html, oldPass: OLD, newPass: NEW, css: 'newcss', js: 'newjs', iterations: fast });
|
|
assert.equal(r.erased, 1);
|
|
assert.ok(!r.html.includes(SECRET));
|
|
assert.ok(r.html.includes('newcss') && r.html.includes('newjs'));
|
|
const out = JSON.parse(await decrypt(extractPayload(r.html), NEW));
|
|
assert.equal(out.sections[0].records[0].fields[0].value, '');
|
|
assert.equal(out.sections[0].records[0].fields[0].secret, true);
|
|
assert.equal(out.sections[0].records[0].fields[1].value, 'First Bank');
|
|
assert.deepEqual(r.left.map((s) => [s.title, s.text]), [['S', 1]]);
|
|
await assert.rejects(decrypt(extractPayload(r.html), OLD));
|
|
});
|
|
|
|
test('refuses the same passphrase, a weak one, or a wrong old one', async () => {
|
|
const html = await realFile();
|
|
await assert.rejects(sanitize({ html, oldPass: OLD, newPass: OLD, css: '', js: '', iterations: fast }), /different passphrase/);
|
|
await assert.rejects(sanitize({ html, oldPass: OLD, newPass: 'short', css: '', js: '', iterations: fast }), /New passphrase/);
|
|
await assert.rejects(sanitize({ html, oldPass: 'wrong wrong wrong', newPass: NEW, css: '', js: '', iterations: fast }), /Wrong passphrase/);
|
|
});
|
|
|
|
test('CLI: writes a new file, leaves the input alone, refuses in == out', async () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'fw-san-'));
|
|
const inp = join(dir, 'real.html'), out = join(dir, 'copy.html');
|
|
const html = await realFile();
|
|
writeFileSync(inp, html);
|
|
const env = { ...process.env, FW_PASSPHRASE: OLD, FW_NEW_PASSPHRASE: NEW };
|
|
const bad = spawnSync('node', ['sanitize.js', '--in', inp, '--out', inp], { env, encoding: 'utf8' });
|
|
assert.notEqual(bad.status, 0);
|
|
assert.match(bad.stderr, /different file/);
|
|
const ok = spawnSync('node', ['sanitize.js', '--in', inp, '--out', out], { env, encoding: 'utf8' });
|
|
assert.equal(ok.status, 0, ok.stderr);
|
|
assert.match(ok.stdout, /Erased 1 secret value/);
|
|
assert.ok(existsSync(out));
|
|
assert.equal(readFileSync(inp, 'utf8'), html);
|
|
assert.ok(!readFileSync(out, 'utf8').includes(SECRET));
|
|
});
|