@eyeoverthink/sva-gate 2.1.0 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +221 -157
- package/bin/sva-gate.js +172 -172
- package/package.json +80 -79
- package/src/index.d.ts +142 -142
- package/src/index.js +49 -49
- package/src/lib/binder-induction.d.ts +17 -17
- package/src/lib/binder-induction.js +267 -267
- package/src/lib/membrane-core.js +156 -156
- package/src/lib/sentinel.js +103 -103
- package/src/lib/stream-guard.js +228 -228
- package/src/mcp-server.js +273 -273
- package/src/receipt.js +412 -412
- package/test/review-2026-12-04.test.js +84 -84
- package/test/sva-gate.test.js +268 -268
- package/test/trust.test.js +88 -88
- package/test/types.test.js +31 -31
package/test/trust.test.js
CHANGED
|
@@ -1,88 +1,88 @@
|
|
|
1
|
-
// 2.0 trust rules: whose signature counts, weak keys, unsigned AER-1, and RFC 6962 Merkle conformance.
|
|
2
|
-
import test from 'node:test';
|
|
3
|
-
import assert from 'node:assert/strict';
|
|
4
|
-
import crypto from 'node:crypto';
|
|
5
|
-
import { execFileSync } from 'node:child_process';
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
|
-
import { mintSvaEr1, verifyAnyReceipt, evaluateOpeg1, isSmallOrderKey, generateEd25519Keypair, canonical } from '../src/index.js';
|
|
8
|
-
|
|
9
|
-
const BIN = fileURLToPath(new URL('../bin/sva-gate.js', import.meta.url));
|
|
10
|
-
const sha = (b) => crypto.createHash('sha256').update(b).digest();
|
|
11
|
-
|
|
12
|
-
test('a receipt minted with someone else\'s key is rejected when the caller names the trusted key', () => {
|
|
13
|
-
const mine = mintSvaEr1({ rSan: { a: 1 } });
|
|
14
|
-
const theirs = mintSvaEr1({ rSan: { a: 1 }, seedHex: 'ab'.repeat(32) });
|
|
15
|
-
const r = verifyAnyReceipt(theirs.receipt, { deliverable: theirs.deliverable, expectedPublicKeys: [mine.publicKeyHex] });
|
|
16
|
-
assert.equal(r.ok, false);
|
|
17
|
-
assert.equal(r.outcome, 'untrusted_signer');
|
|
18
|
-
assert.equal(evaluateOpeg1(theirs.opegEnvelope, { expectedPublicKeys: [mine.publicKeyHex] }).ok, false);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('without a trusted key an intact receipt is integrity_only, and allowUnpinned is the explicit opt-out', () => {
|
|
22
|
-
const m = mintSvaEr1({ rSan: { a: 1 } });
|
|
23
|
-
const r = verifyAnyReceipt(m.receipt, { deliverable: m.deliverable });
|
|
24
|
-
assert.equal(r.ok, false); assert.equal(r.outcome, 'integrity_only'); assert.equal(r.signer, m.publicKeyHex);
|
|
25
|
-
const u = verifyAnyReceipt(m.receipt, { deliverable: m.deliverable, allowUnpinned: true });
|
|
26
|
-
assert.equal(u.ok, true); assert.equal(u.trust, 'unpinned');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('small-order keys are refused, even when the caller lists one as trusted', () => {
|
|
30
|
-
const smallOrder = ['01' + '00'.repeat(31), '00'.repeat(32), '00'.repeat(31) + '80', 'ec' + 'ff'.repeat(30) + '7f',
|
|
31
|
-
'26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05', 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa'];
|
|
32
|
-
for (const k of smallOrder) assert.equal(isSmallOrderKey(k), true, k);
|
|
33
|
-
for (let i = 0; i < 200; i++) assert.equal(isSmallOrderKey(generateEd25519Keypair().publicKeyHex), false);
|
|
34
|
-
// A receipt "signed" with the identity point and R = identity, S = 0 — no private key involved.
|
|
35
|
-
const m = mintSvaEr1({ rSan: { a: 1 } }), r = structuredClone(m.receipt), id = smallOrder[0], sig = id + '00'.repeat(32);
|
|
36
|
-
r.agency.publicKey = id; r.statementSig = sig; for (const e of r.entries) { e.signer = id; e.sig = sig; }
|
|
37
|
-
const v = verifyAnyReceipt(r, { deliverable: m.deliverable, expectedPublicKeys: [id], allowUnpinned: true });
|
|
38
|
-
assert.equal(v.ok, false);
|
|
39
|
-
assert.match(v.reason, /weak_key/);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
function makeAer(outputs) {
|
|
43
|
-
const prov = 'LOGGED BY AGENT', bytes = Buffer.from(JSON.stringify({ receipt_schema_version: '0.4', issuer_claims: { provenance_class: prov }, outputs }));
|
|
44
|
-
const r = { id: crypto.randomUUID(), receipt_schema_version: '0.4', created_at: '2026-12-03T12:00:00Z', tool: { name: 'a', version: '1', scope: 'x' },
|
|
45
|
-
provenance_class: prov, verification_status: 'verified', canonical_bytes: bytes.toString('base64'), output_hash: 'sha256:' + sha(bytes).toString('hex') };
|
|
46
|
-
r.metadata_hash = 'sha256:' + sha(Buffer.from(canonical({ created_at: r.created_at, id: r.id, output_hash: r.output_hash, provenance_class: prov,
|
|
47
|
-
receipt_schema_version: '0.4', tool: { name: 'a', scope: 'x', version: '1' } }))).toString('hex');
|
|
48
|
-
return r;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
test('an AER-1 receipt anyone can build never passes the gate, and its deliverable is checked', () => {
|
|
52
|
-
const rSan = { transfer: 'approved' }, h = sha(Buffer.from(canonical(rSan))).toString('hex');
|
|
53
|
-
const env = { spec: 'opeg/1', state: 'S4_COMMIT', mode: 'EXECUTE_AND_RECEIVE', receiptFormat: 'aer-1', sourcePriority: ['runtime_receipt'],
|
|
54
|
-
sanitizer: { status: 'SUCCESS', rSan, rSanSha256: h }, receipt: makeAer({ rSanSha256: h }) };
|
|
55
|
-
const g = evaluateOpeg1(env);
|
|
56
|
-
assert.equal(g.ok, false); assert.equal(g.gate, 'integrity_only');
|
|
57
|
-
const d = verifyAnyReceipt(makeAer({ x: 1 }), { deliverable: { x: 2 }, allowUnpinned: true });
|
|
58
|
-
assert.equal(d.ok, false); assert.equal(d.deliverable, 'verification_failed');
|
|
59
|
-
assert.equal(verifyAnyReceipt(makeAer({ x: 1 }), { deliverable: { x: 1 }, allowUnpinned: true }).deliverable, 'verified');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// RFC 6962 §2.1, written from the definition over raw digest bytes.
|
|
63
|
-
function mth(leaves) {
|
|
64
|
-
if (leaves.length === 1) return sha(Buffer.concat([Buffer.from([0]), leaves[0]]));
|
|
65
|
-
let k = 1; while (k * 2 < leaves.length) k *= 2;
|
|
66
|
-
return sha(Buffer.concat([Buffer.from([1]), mth(leaves.slice(0, k)), mth(leaves.slice(k))]));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
test('new receipts use a real RFC 6962 tree; receipts minted by 1.x still verify and say so', () => {
|
|
70
|
-
for (let n = 0; n < 12; n++) {
|
|
71
|
-
const events = Array.from({ length: n }, (_, i) => ({ kind: 'tool.call', data: { step: i } }));
|
|
72
|
-
const m = mintSvaEr1({ rSan: { n }, events });
|
|
73
|
-
const leaves = [...m.receipt.entries].sort((a, b) => a.seq - b.seq).map((e) => Buffer.from(e.hash, 'hex'));
|
|
74
|
-
assert.equal(m.receipt.statement.merkleRoot, mth(leaves).toString('hex'), `${leaves.length} leaves`);
|
|
75
|
-
assert.equal(verifyAnyReceipt(m.receipt, { expectedPublicKeys: [m.publicKeyHex] }).merkleEncoding, 'rfc6962');
|
|
76
|
-
}
|
|
77
|
-
const legacy = mintSvaEr1({ rSan: { a: 1 }, merkleEncoding: 'hex-text', events: [{ kind: 'x', data: {} }, { kind: 'y', data: {} }] });
|
|
78
|
-
const v = verifyAnyReceipt(legacy.receipt, { deliverable: legacy.deliverable, expectedPublicKeys: [legacy.publicKeyHex] });
|
|
79
|
-
assert.equal(v.ok, true); assert.equal(v.merkleEncoding, 'legacy-hex-text');
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test('CLI: verify exits 1 without --key and 0 with the right one', () => {
|
|
83
|
-
const m = mintSvaEr1({ rSan: { a: 1 } });
|
|
84
|
-
const run = (...extra) => { try { execFileSync(process.execPath, [BIN, 'verify', JSON.stringify(m.receipt), m.deliverable, ...extra], { stdio: 'pipe' }); return 0; } catch (e) { return e.status; } };
|
|
85
|
-
assert.equal(run(), 1);
|
|
86
|
-
assert.equal(run('--key', m.publicKeyHex), 0);
|
|
87
|
-
assert.equal(run('--key', generateEd25519Keypair().publicKeyHex), 1);
|
|
88
|
-
});
|
|
1
|
+
// 2.0 trust rules: whose signature counts, weak keys, unsigned AER-1, and RFC 6962 Merkle conformance.
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import { execFileSync } from 'node:child_process';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { mintSvaEr1, verifyAnyReceipt, evaluateOpeg1, isSmallOrderKey, generateEd25519Keypair, canonical } from '../src/index.js';
|
|
8
|
+
|
|
9
|
+
const BIN = fileURLToPath(new URL('../bin/sva-gate.js', import.meta.url));
|
|
10
|
+
const sha = (b) => crypto.createHash('sha256').update(b).digest();
|
|
11
|
+
|
|
12
|
+
test('a receipt minted with someone else\'s key is rejected when the caller names the trusted key', () => {
|
|
13
|
+
const mine = mintSvaEr1({ rSan: { a: 1 } });
|
|
14
|
+
const theirs = mintSvaEr1({ rSan: { a: 1 }, seedHex: 'ab'.repeat(32) });
|
|
15
|
+
const r = verifyAnyReceipt(theirs.receipt, { deliverable: theirs.deliverable, expectedPublicKeys: [mine.publicKeyHex] });
|
|
16
|
+
assert.equal(r.ok, false);
|
|
17
|
+
assert.equal(r.outcome, 'untrusted_signer');
|
|
18
|
+
assert.equal(evaluateOpeg1(theirs.opegEnvelope, { expectedPublicKeys: [mine.publicKeyHex] }).ok, false);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('without a trusted key an intact receipt is integrity_only, and allowUnpinned is the explicit opt-out', () => {
|
|
22
|
+
const m = mintSvaEr1({ rSan: { a: 1 } });
|
|
23
|
+
const r = verifyAnyReceipt(m.receipt, { deliverable: m.deliverable });
|
|
24
|
+
assert.equal(r.ok, false); assert.equal(r.outcome, 'integrity_only'); assert.equal(r.signer, m.publicKeyHex);
|
|
25
|
+
const u = verifyAnyReceipt(m.receipt, { deliverable: m.deliverable, allowUnpinned: true });
|
|
26
|
+
assert.equal(u.ok, true); assert.equal(u.trust, 'unpinned');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('small-order keys are refused, even when the caller lists one as trusted', () => {
|
|
30
|
+
const smallOrder = ['01' + '00'.repeat(31), '00'.repeat(32), '00'.repeat(31) + '80', 'ec' + 'ff'.repeat(30) + '7f',
|
|
31
|
+
'26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05', 'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa'];
|
|
32
|
+
for (const k of smallOrder) assert.equal(isSmallOrderKey(k), true, k);
|
|
33
|
+
for (let i = 0; i < 200; i++) assert.equal(isSmallOrderKey(generateEd25519Keypair().publicKeyHex), false);
|
|
34
|
+
// A receipt "signed" with the identity point and R = identity, S = 0 — no private key involved.
|
|
35
|
+
const m = mintSvaEr1({ rSan: { a: 1 } }), r = structuredClone(m.receipt), id = smallOrder[0], sig = id + '00'.repeat(32);
|
|
36
|
+
r.agency.publicKey = id; r.statementSig = sig; for (const e of r.entries) { e.signer = id; e.sig = sig; }
|
|
37
|
+
const v = verifyAnyReceipt(r, { deliverable: m.deliverable, expectedPublicKeys: [id], allowUnpinned: true });
|
|
38
|
+
assert.equal(v.ok, false);
|
|
39
|
+
assert.match(v.reason, /weak_key/);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function makeAer(outputs) {
|
|
43
|
+
const prov = 'LOGGED BY AGENT', bytes = Buffer.from(JSON.stringify({ receipt_schema_version: '0.4', issuer_claims: { provenance_class: prov }, outputs }));
|
|
44
|
+
const r = { id: crypto.randomUUID(), receipt_schema_version: '0.4', created_at: '2026-12-03T12:00:00Z', tool: { name: 'a', version: '1', scope: 'x' },
|
|
45
|
+
provenance_class: prov, verification_status: 'verified', canonical_bytes: bytes.toString('base64'), output_hash: 'sha256:' + sha(bytes).toString('hex') };
|
|
46
|
+
r.metadata_hash = 'sha256:' + sha(Buffer.from(canonical({ created_at: r.created_at, id: r.id, output_hash: r.output_hash, provenance_class: prov,
|
|
47
|
+
receipt_schema_version: '0.4', tool: { name: 'a', scope: 'x', version: '1' } }))).toString('hex');
|
|
48
|
+
return r;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
test('an AER-1 receipt anyone can build never passes the gate, and its deliverable is checked', () => {
|
|
52
|
+
const rSan = { transfer: 'approved' }, h = sha(Buffer.from(canonical(rSan))).toString('hex');
|
|
53
|
+
const env = { spec: 'opeg/1', state: 'S4_COMMIT', mode: 'EXECUTE_AND_RECEIVE', receiptFormat: 'aer-1', sourcePriority: ['runtime_receipt'],
|
|
54
|
+
sanitizer: { status: 'SUCCESS', rSan, rSanSha256: h }, receipt: makeAer({ rSanSha256: h }) };
|
|
55
|
+
const g = evaluateOpeg1(env);
|
|
56
|
+
assert.equal(g.ok, false); assert.equal(g.gate, 'integrity_only');
|
|
57
|
+
const d = verifyAnyReceipt(makeAer({ x: 1 }), { deliverable: { x: 2 }, allowUnpinned: true });
|
|
58
|
+
assert.equal(d.ok, false); assert.equal(d.deliverable, 'verification_failed');
|
|
59
|
+
assert.equal(verifyAnyReceipt(makeAer({ x: 1 }), { deliverable: { x: 1 }, allowUnpinned: true }).deliverable, 'verified');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// RFC 6962 §2.1, written from the definition over raw digest bytes.
|
|
63
|
+
function mth(leaves) {
|
|
64
|
+
if (leaves.length === 1) return sha(Buffer.concat([Buffer.from([0]), leaves[0]]));
|
|
65
|
+
let k = 1; while (k * 2 < leaves.length) k *= 2;
|
|
66
|
+
return sha(Buffer.concat([Buffer.from([1]), mth(leaves.slice(0, k)), mth(leaves.slice(k))]));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
test('new receipts use a real RFC 6962 tree; receipts minted by 1.x still verify and say so', () => {
|
|
70
|
+
for (let n = 0; n < 12; n++) {
|
|
71
|
+
const events = Array.from({ length: n }, (_, i) => ({ kind: 'tool.call', data: { step: i } }));
|
|
72
|
+
const m = mintSvaEr1({ rSan: { n }, events });
|
|
73
|
+
const leaves = [...m.receipt.entries].sort((a, b) => a.seq - b.seq).map((e) => Buffer.from(e.hash, 'hex'));
|
|
74
|
+
assert.equal(m.receipt.statement.merkleRoot, mth(leaves).toString('hex'), `${leaves.length} leaves`);
|
|
75
|
+
assert.equal(verifyAnyReceipt(m.receipt, { expectedPublicKeys: [m.publicKeyHex] }).merkleEncoding, 'rfc6962');
|
|
76
|
+
}
|
|
77
|
+
const legacy = mintSvaEr1({ rSan: { a: 1 }, merkleEncoding: 'hex-text', events: [{ kind: 'x', data: {} }, { kind: 'y', data: {} }] });
|
|
78
|
+
const v = verifyAnyReceipt(legacy.receipt, { deliverable: legacy.deliverable, expectedPublicKeys: [legacy.publicKeyHex] });
|
|
79
|
+
assert.equal(v.ok, true); assert.equal(v.merkleEncoding, 'legacy-hex-text');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('CLI: verify exits 1 without --key and 0 with the right one', () => {
|
|
83
|
+
const m = mintSvaEr1({ rSan: { a: 1 } });
|
|
84
|
+
const run = (...extra) => { try { execFileSync(process.execPath, [BIN, 'verify', JSON.stringify(m.receipt), m.deliverable, ...extra], { stdio: 'pipe' }); return 0; } catch (e) { return e.status; } };
|
|
85
|
+
assert.equal(run(), 1);
|
|
86
|
+
assert.equal(run('--key', m.publicKeyHex), 0);
|
|
87
|
+
assert.equal(run('--key', generateEd25519Keypair().publicKeyHex), 1);
|
|
88
|
+
});
|
package/test/types.test.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
6
|
-
import { SealSession, detect } from '../src/index.js';
|
|
7
|
-
|
|
8
|
-
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
9
|
-
|
|
10
|
-
test('package.json exports declare existing types and import targets for all subpaths', () => {
|
|
11
|
-
const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'));
|
|
12
|
-
for (const [subpath, target] of Object.entries(pkg.exports)) {
|
|
13
|
-
assert.equal(typeof target, 'object', `${subpath} export must be a conditional object`);
|
|
14
|
-
assert.ok(target.types, `${subpath} must declare types`);
|
|
15
|
-
assert.ok(target.import, `${subpath} must declare import`);
|
|
16
|
-
assert.ok(fs.existsSync(path.join(ROOT, target.types)), `missing types file for ${subpath}: ${target.types}`);
|
|
17
|
-
assert.ok(fs.existsSync(path.join(ROOT, target.import)), `missing import file for ${subpath}: ${target.import}`);
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('src/index.d.ts declares SealSession.unseal (not restore) and matches runtime', () => {
|
|
22
|
-
const dts = fs.readFileSync(path.join(ROOT, 'src', 'index.d.ts'), 'utf8');
|
|
23
|
-
assert.match(dts, /unseal\(text:\s*string,\s*opts\?:\s*\{\s*json\?:\s*boolean;\s*strict\?:\s*boolean\s*\}\):\s*string;/);
|
|
24
|
-
assert.doesNotMatch(dts, /\brestore\s*\(/);
|
|
25
|
-
|
|
26
|
-
const session = new SealSession();
|
|
27
|
-
const input = 'Contact alice@example.com with sk-ant-api03-secretkey1234567890abcdef';
|
|
28
|
-
const sealed = session.apply(input, detect(input));
|
|
29
|
-
assert.equal(session.unseal(sealed), input);
|
|
30
|
-
assert.equal(session.unseal(sealed, { json: false }), input);
|
|
31
|
-
});
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { SealSession, detect } from '../src/index.js';
|
|
7
|
+
|
|
8
|
+
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
9
|
+
|
|
10
|
+
test('package.json exports declare existing types and import targets for all subpaths', () => {
|
|
11
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'));
|
|
12
|
+
for (const [subpath, target] of Object.entries(pkg.exports)) {
|
|
13
|
+
assert.equal(typeof target, 'object', `${subpath} export must be a conditional object`);
|
|
14
|
+
assert.ok(target.types, `${subpath} must declare types`);
|
|
15
|
+
assert.ok(target.import, `${subpath} must declare import`);
|
|
16
|
+
assert.ok(fs.existsSync(path.join(ROOT, target.types)), `missing types file for ${subpath}: ${target.types}`);
|
|
17
|
+
assert.ok(fs.existsSync(path.join(ROOT, target.import)), `missing import file for ${subpath}: ${target.import}`);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('src/index.d.ts declares SealSession.unseal (not restore) and matches runtime', () => {
|
|
22
|
+
const dts = fs.readFileSync(path.join(ROOT, 'src', 'index.d.ts'), 'utf8');
|
|
23
|
+
assert.match(dts, /unseal\(text:\s*string,\s*opts\?:\s*\{\s*json\?:\s*boolean;\s*strict\?:\s*boolean\s*\}\):\s*string;/);
|
|
24
|
+
assert.doesNotMatch(dts, /\brestore\s*\(/);
|
|
25
|
+
|
|
26
|
+
const session = new SealSession();
|
|
27
|
+
const input = 'Contact alice@example.com with sk-ant-api03-secretkey1234567890abcdef';
|
|
28
|
+
const sealed = session.apply(input, detect(input));
|
|
29
|
+
assert.equal(session.unseal(sealed), input);
|
|
30
|
+
assert.equal(session.unseal(sealed, { json: false }), input);
|
|
31
|
+
});
|