Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(seal): authenticate format header [H15,H17]

Corey Olsonc9202bdaa3d7
diff --git a/src/seal.js b/src/seal.js
index b49f4a36..f528f759 100644
--- a/src/seal.js
+++ b/src/seal.js
@@ -36,15 +36,15 @@ function keyBytes(value) {
return key;
}
-/** Canonical, domain-separated associated data binds every blob to its public position. */
-function associatedData(binding) {
+/** Canonical, domain-separated associated data binds the header and public position. */
+function associatedData(binding, version = SEAL_VERSION) {
if (!binding || typeof binding.boardId !== 'string' || !binding.boardId.length ||
binding.boardId.length > 256 || !['snapshot', 'move', 'request'].includes(binding.kind) ||
- !Number.isSafeInteger(binding.sequence) || binding.sequence < 0) {
+ !Number.isSafeInteger(binding.sequence) || binding.sequence < 0 || !Number.isInteger(version) || version < 0 || version > 255) {
throw new Refused('SEAL_BINDING', 'Supply the board id, snapshot/move/request kind and nonnegative safe sequence.');
}
return new TextEncoder().encode(JSON.stringify([
- 'pullboard-sealed', SEAL_VERSION, binding.boardId, binding.kind, binding.sequence,
+ 'pullboard-sealed', version, binding.boardId, binding.sequence, binding.kind,
]));
}
@@ -83,7 +83,7 @@ export function decodeBoardKey(encoded) {
export async function seal(key, plaintext, binding) {
const raw = keyBytes(key);
const plain = bytes(plaintext, 'SEAL_DATA', 'Encode the payload as bytes before sealing it.');
- const additionalData = associatedData(binding);
+ const additionalData = associatedData(binding, SEAL_VERSION);
const platform = crypto();
const iv = platform.getRandomValues(new Uint8Array(NONCE_BYTES));
const imported = await platform.subtle.importKey('raw', raw, 'AES-GCM', false, ['encrypt']);
@@ -108,9 +108,10 @@ export async function seal(key, plaintext, binding) {
export async function unseal(key, sealed, binding) {
const raw = keyBytes(key);
const blob = bytes(sealed, 'SEAL_FORMAT', 'The sealed payload must be bytes. Fetch the board again.');
- const additionalData = associatedData(binding);
if (!blob.length) throw new Refused('SEAL_FORMAT', 'The sealed payload is truncated. Fetch the board again.');
- if (blob[0] !== SEAL_VERSION) throw new Refused('SEAL_VERSION', 'This sealed format is unsupported. Upgrade this device.');
+ const version = blob[0];
+ const additionalData = associatedData(binding, version);
+ if (version !== SEAL_VERSION) throw new Refused('SEAL_VERSION', 'This sealed format is unsupported. Upgrade this device.');
if (blob.length < 1 + NONCE_BYTES + TAG_BYTES) {
throw new Refused('SEAL_FORMAT', 'The sealed payload is truncated. Fetch the board again.');
}
diff --git a/test/seal.test.js b/test/seal.test.js
index 3b3c5484..eb3b0e5d 100644
--- a/test/seal.test.js
+++ b/test/seal.test.js
@@ -1,10 +1,11 @@
/** Device-side sealing is the same bytes and WebCrypto code in Node and Chrome (H15, H17). */
import assert from 'node:assert/strict';
-import { spawn } from 'node:child_process';
-import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { spawn, spawnSync } from 'node:child_process';
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
-import { join } from 'node:path';
+import { join, resolve } from 'node:path';
+import { pathToFileURL } from 'node:url';
import { test } from 'node:test';
import { decodeBoardKey, encodeBoardKey, generateBoardKey, seal, SEAL_VERSION, unseal } from '../src/seal.js';
@@ -76,6 +77,74 @@ test('unsealing refuses wrong keys, every changed payload byte and every wrong b
}
});
+test('adjacent board-id and sequence fields cannot collide at their boundary [H15,H17]', async () => {
+ const key = await generateBoardKey();
+ const blob = await seal(key, TEXT, binding({ boardId: 'a1', sequence: 0, kind: 'move' }));
+ await assert.rejects(unseal(key, blob, binding({ boardId: 'a', sequence: 10, kind: 'move' })), refused('SEAL_AUTH_FAILED'));
+});
+
+test('the header byte remains authenticated when the version refusal guard is removed [H15,H17]', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'pullboard-seal-version-mutant-'));
+ try {
+ const source = readFileSync(new URL('../src/seal.js', import.meta.url), 'utf8');
+ const guard = " if (version !== SEAL_VERSION) throw new Refused('SEAL_VERSION', 'This sealed format is unsupported. Upgrade this device.');";
+ assert.equal(source.split(guard).length - 1, 1, 'the private mutant removes exactly the version guard');
+ writeFileSync(join(dir, 'seal.mjs'), source.replace(guard, ''));
+ writeFileSync(join(dir, 'refused.js'), readFileSync(new URL('../src/refused.js', import.meta.url)));
+ const mutant = await import(pathToFileURL(join(dir, 'seal.mjs')).href);
+ const key = await generateBoardKey();
+ const bindingA = binding({ boardId: 'a1', sequence: 0, kind: 'move' });
+ const blob = await mutant.seal(key, TEXT, bindingA);
+ const changedHeader = new Uint8Array(blob);
+ changedHeader[0] = 9;
+ await assert.rejects(mutant.unseal(key, changedHeader, bindingA), refused('SEAL_AUTH_FAILED'));
+ await assert.rejects(mutant.unseal(key, blob, binding({ boardId: 'a', sequence: 10, kind: 'move' })), refused('SEAL_AUTH_FAILED'));
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+});
+
+test('generate, encode, decode, seal and unseal never write to console or output streams [H15,H17]', () => {
+ const moduleUrl = process.env.PULLBOARD_SEAL_MODULE ?? pathToFileURL(resolve(import.meta.dirname, '../src/seal.js')).href;
+ const harness = `
+ import { generateBoardKey, encodeBoardKey, decodeBoardKey, seal, unseal } from ${JSON.stringify(moduleUrl)};
+ const methods = ['log','info','warn','error','debug','dir','dirxml','table','trace','group','groupCollapsed','groupEnd','assert','count','countReset','time','timeEnd','timeLog','clear','profile','profileEnd','timeStamp'];
+ const originals = new Map(methods.map((name) => [name, console[name]]));
+ const stdout = process.stdout.write;
+ const stderr = process.stderr.write;
+ let outputCalls = 0;
+ let failed = false;
+ /** Suppress output without retaining attempted bytes or strings. */
+ function suppressOutput() { outputCalls += 1; return true; }
+ try {
+ for (const name of methods) console[name] = suppressOutput;
+ process.stdout.write = suppressOutput;
+ process.stderr.write = suppressOutput;
+ const binding = { boardId: 'private-board', kind: 'move', sequence: 0 };
+ const key = await generateBoardKey();
+ const encoded = encodeBoardKey(key);
+ const decoded = decodeBoardKey(encoded);
+ const blob = await seal(decoded, new TextEncoder().encode('payload'), binding);
+ await unseal(decoded, blob, binding);
+ try { decodeBoardKey('invalid'); failed = true; } catch (error) { failed ||= error.code !== 'SEAL_KEY'; }
+ try { await seal(decoded, new Uint8Array(), { ...binding, kind: 'invalid' }); failed = true; } catch (error) { failed ||= error.code !== 'SEAL_BINDING'; }
+ try { await unseal(decoded, new Uint8Array(), binding); failed = true; } catch (error) { failed ||= error.code !== 'SEAL_FORMAT'; }
+ try { await unseal(await generateBoardKey(), blob, binding); failed = true; } catch (error) { failed ||= error.code !== 'SEAL_AUTH_FAILED'; }
+ } catch { failed = true; }
+ finally {
+ for (const [name, method] of originals) console[name] = method;
+ process.stdout.write = stdout;
+ process.stderr.write = stderr;
+ }
+ process.stdout.write(String(outputCalls));
+ process.exitCode = failed ? 1 : 0;
+ `;
+ const result = spawnSync(process.execPath, ['--input-type=module', '-e', harness], { encoding: 'utf8', timeout: 10_000 });
+ assert.equal(result.status, 0, 'the isolated output-capture probe completed its operations');
+ assert.equal(result.stderr.length, 0, 'the probe wrote nothing to stderr');
+ assert.equal(Number(result.stdout), 0, 'no console, stdout or stderr output was attempted');
+});
+
test('sealing validates inputs and copies mutable buffers and binding before awaiting [H15,H17]', async () => {
const key = await generateBoardKey();
for (const where of [null, binding({ boardId: '' }), binding({ kind: 'other' }),