@dharma-ai-labs/agent-fabric-relay-client 0.2.32 → 0.2.34
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/README.md +47 -0
- package/dist/deviceSigningControl.d.ts +21 -0
- package/dist/deviceSigningControl.d.ts.map +1 -0
- package/dist/deviceSigningControl.js +68 -0
- package/dist/deviceSigningControl.js.map +1 -0
- package/dist/deviceSigningControl.test.d.ts +2 -0
- package/dist/deviceSigningControl.test.d.ts.map +1 -0
- package/dist/deviceSigningControl.test.js +55 -0
- package/dist/deviceSigningControl.test.js.map +1 -0
- package/dist/enrollmentAnchorLock.d.ts +5 -1
- package/dist/enrollmentAnchorLock.d.ts.map +1 -1
- package/dist/enrollmentAnchorLock.js +8 -1
- package/dist/enrollmentAnchorLock.js.map +1 -1
- package/dist/enrollmentAnchorLock.test.js +12 -3
- package/dist/enrollmentAnchorLock.test.js.map +1 -1
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -0
- package/dist/index.js.map +1 -1
- package/dist/signingRecoveryClient.d.ts +50 -0
- package/dist/signingRecoveryClient.d.ts.map +1 -0
- package/dist/signingRecoveryClient.js +201 -0
- package/dist/signingRecoveryClient.js.map +1 -0
- package/dist/signingRecoveryClient.test.d.ts +2 -0
- package/dist/signingRecoveryClient.test.d.ts.map +1 -0
- package/dist/signingRecoveryClient.test.js +219 -0
- package/dist/signingRecoveryClient.test.js.map +1 -0
- package/dist/signingRecoveryHost.d.ts +101 -0
- package/dist/signingRecoveryHost.d.ts.map +1 -0
- package/dist/signingRecoveryHost.js +342 -0
- package/dist/signingRecoveryHost.js.map +1 -0
- package/dist/signingRecoveryHost.test.d.ts +2 -0
- package/dist/signingRecoveryHost.test.d.ts.map +1 -0
- package/dist/signingRecoveryHost.test.js +682 -0
- package/dist/signingRecoveryHost.test.js.map +1 -0
- package/dist/signingRecoveryJournal.d.ts +35 -0
- package/dist/signingRecoveryJournal.d.ts.map +1 -0
- package/dist/signingRecoveryJournal.js +178 -0
- package/dist/signingRecoveryJournal.js.map +1 -0
- package/dist/signingRecoveryJournal.test.d.ts +2 -0
- package/dist/signingRecoveryJournal.test.d.ts.map +1 -0
- package/dist/signingRecoveryJournal.test.js +278 -0
- package/dist/signingRecoveryJournal.test.js.map +1 -0
- package/dist/signingRecoveryOfferClient.d.ts +50 -0
- package/dist/signingRecoveryOfferClient.d.ts.map +1 -0
- package/dist/signingRecoveryOfferClient.js +204 -0
- package/dist/signingRecoveryOfferClient.js.map +1 -0
- package/dist/signingRecoveryOfferClient.test.d.ts +2 -0
- package/dist/signingRecoveryOfferClient.test.d.ts.map +1 -0
- package/dist/signingRecoveryOfferClient.test.js +428 -0
- package/dist/signingRecoveryOfferClient.test.js.map +1 -0
- package/dist/signingRecoveryPlan.d.ts +67 -0
- package/dist/signingRecoveryPlan.d.ts.map +1 -0
- package/dist/signingRecoveryPlan.js +194 -0
- package/dist/signingRecoveryPlan.js.map +1 -0
- package/dist/signingRecoveryPlan.test.d.ts +2 -0
- package/dist/signingRecoveryPlan.test.d.ts.map +1 -0
- package/dist/signingRecoveryPlan.test.js +317 -0
- package/dist/signingRecoveryPlan.test.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { createHash, generateKeyPairSync, verify } from 'node:crypto';
|
|
3
|
+
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { basename, dirname, resolve } from 'node:path';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { createServer } from 'node:http';
|
|
7
|
+
import test from 'node:test';
|
|
8
|
+
import { canonicalize, deviceSigningRecoveryPreparationSigningPayload, deviceSigningRecoverySigningPayload, parseDeviceSigningRecoveryPreparation, sealDeviceSigningRecoveryDelivery, signCanonicalObject, validateContract } from '@dharma-ai-labs/agent-fabric-contracts';
|
|
9
|
+
import * as relay from './index.js';
|
|
10
|
+
const NOW = Date.parse('2030-01-01T00:00:00.000Z');
|
|
11
|
+
const id = (n) => `c1000000-0000-4000-8000-${String(n).padStart(12, '0')}`;
|
|
12
|
+
const hash = (b) => 'sha256:' + createHash('sha256').update(b).digest('hex');
|
|
13
|
+
const digest = (c) => 'sha256:' + c.repeat(64);
|
|
14
|
+
function api() {
|
|
15
|
+
const names = ['prepareOwnedSigningRecovery', 'submitOwnedSigningRecoveryPreparation', 'progressOwnedSigningRecoveryOffer',
|
|
16
|
+
'completeOwnedSigningRecovery', 'inspectOwnedSigningRecovery', 'disposeOwnedSigningRecovery'];
|
|
17
|
+
const out = {};
|
|
18
|
+
for (const name of names) {
|
|
19
|
+
const value = relay[name];
|
|
20
|
+
assert.equal(typeof value, 'function', name);
|
|
21
|
+
out[name] = value;
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
async function fixture() {
|
|
26
|
+
const old = generateKeyPairSync('ed25519'), next = generateKeyPairSync('ed25519'), device = generateKeyPairSync('ed25519');
|
|
27
|
+
const pub = (p) => p.publicKey.export({ format: 'jwk' }).x;
|
|
28
|
+
const keyset = (p, generation, issued, expiry) => {
|
|
29
|
+
const body = { schema: 'dharma.server-signing-keyset/v1', organizationId: 'org_fixture', generation,
|
|
30
|
+
keys: [{ keyVersion: 'fixture-version-' + generation, publicKeyEd25519: pub(p), status: 'active',
|
|
31
|
+
notBefore: new Date(issued).toISOString(), notAfter: new Date(expiry).toISOString() }], signedByKeyVersion: 'fixture-version-' + generation,
|
|
32
|
+
issuedAt: new Date(issued).toISOString(), expiresAt: new Date(expiry).toISOString() };
|
|
33
|
+
return { ...body, signature: signCanonicalObject(body, p.privateKey) };
|
|
34
|
+
};
|
|
35
|
+
const parent = resolve(tmpdir()), root = await mkdtemp(resolve(parent, 'fabric-owned-recovery-')), configPath = resolve(root, 'device.json');
|
|
36
|
+
const config = { schema: 'dharma.device-config/v1', hqUrl: 'https://www.dharma-ai.io', organizationId: 'org_fixture',
|
|
37
|
+
installationId: id(1), deviceId: id(2), deviceName: 'Synthetic owned host', platform: 'linux', publicKeyEd25519: pub(device),
|
|
38
|
+
serverPublicKeyEd25519: pub(old), serverSigningKeyset: keyset(old, 2, NOW - 86400000, NOW - 1000),
|
|
39
|
+
relayUrl: 'wss://relay.example.invalid', enrolledAt: '2029-12-01T00:00:00.000Z', connectionMode: 'manual',
|
|
40
|
+
setupClaimReference: id(3), setupClaimRepositoryFingerprint: digest('a') };
|
|
41
|
+
const anchor = { schema: 'dharma.device-enrollment-anchor/v1', hqUrl: config.hqUrl, organizationId: config.organizationId,
|
|
42
|
+
deviceId: config.deviceId, devicePublicKeyEd25519: config.publicKeyEd25519, serverPublicKeyEd25519: config.serverPublicKeyEd25519,
|
|
43
|
+
serverSigningKeyset: config.serverSigningKeyset, enrolledAt: config.enrolledAt };
|
|
44
|
+
const anchorAccount = 'device-enrollment-' + hash(`${config.hqUrl}:${config.organizationId}:${config.deviceId}`).slice(7, 39);
|
|
45
|
+
const identityAccount = 'device-key-' + hash(`${config.hqUrl}:${config.organizationId}:${config.installationId}`).slice(7, 39);
|
|
46
|
+
const privateIdentity = JSON.stringify(device.privateKey.export({ format: 'jwk' }));
|
|
47
|
+
const values = new Map([[anchorAccount, JSON.stringify(anchor)], [identityAccount, privateIdentity]]), reads = [], writes = [];
|
|
48
|
+
const store = { backend: 'linux-secret-service', get: async (name) => { reads.push(name); return values.get(name) ?? null; },
|
|
49
|
+
getFresh: async (name) => { reads.push(name); return values.get(name) ?? null; }, put: async (name, value) => { writes.push(name); values.set(name, value); },
|
|
50
|
+
delete: async () => { throw Error('FORBIDDEN_STORE_DELETE'); } };
|
|
51
|
+
const source = { organizationId: config.organizationId, workspaceId: id(4), repositoryBindingId: id(5), repositoryFingerprint: digest('a'),
|
|
52
|
+
policyRevision: 'agent-fabric-policy-v1', scopeDigest: digest('b'), contractDigest: digest('c'), sourceGenerationId: id(6),
|
|
53
|
+
sourceReceiptId: 'repo_consent_' + id(6), sourceRevision: 1, sourcePolicyRevision: 'repository-source-' + id(6), sourcePolicyHash: digest('d') };
|
|
54
|
+
const session = { organizationId: config.organizationId, workspaceId: id(4), repositoryBindingId: id(5), endpointId: id(7), membershipId: id(8), deviceId: id(2),
|
|
55
|
+
bindingId: id(9), provider: 'codex', sessionId: 'fixture-owned-session', owner: 'dharma_bridge',
|
|
56
|
+
createdAt: new Date(NOW - 1000).toISOString(), expiresAt: new Date(NOW + 900000).toISOString(), maximumProviderCostCents: 4000 };
|
|
57
|
+
let clock = NOW, allowed = true, offered = false, registered = false, published = false, receiptLifetime = 900000;
|
|
58
|
+
let preparation, challenge;
|
|
59
|
+
let handler;
|
|
60
|
+
let offerTransform;
|
|
61
|
+
const controller = new AbortController(), requests = [];
|
|
62
|
+
const nextKeyset = keyset(next, 3, NOW, NOW + 86400000);
|
|
63
|
+
const fetcher = (async (url, init) => {
|
|
64
|
+
assert.equal(typeof url, 'string');
|
|
65
|
+
assert.ok(init);
|
|
66
|
+
const address = new URL(String(url)), body = JSON.parse(String(init.body));
|
|
67
|
+
assert.equal(address.origin, config.hqUrl);
|
|
68
|
+
assert.equal(address.search, '');
|
|
69
|
+
assert.equal(init.redirect, 'error');
|
|
70
|
+
assert.equal(init.credentials, 'omit');
|
|
71
|
+
const headers = new Headers(init.headers);
|
|
72
|
+
assert.equal(headers.has('authorization'), false);
|
|
73
|
+
assert.equal(headers.has('cookie'), false);
|
|
74
|
+
assert.equal(init.method, 'POST');
|
|
75
|
+
assert.equal(headers.get('content-type'), 'application/json');
|
|
76
|
+
assert.equal(headers.get('cache-control'), 'no-store');
|
|
77
|
+
assert.equal(headers.get('x-dharma-device-id'), config.deviceId);
|
|
78
|
+
assert.equal(headers.get('x-dharma-sequence'), '1');
|
|
79
|
+
assert.equal(Number.isFinite(Date.parse(headers.get('x-dharma-timestamp') ?? '')), true);
|
|
80
|
+
const transport = Buffer.from(JSON.stringify({ bodyHash: hash(String(init.body)), deviceId: config.deviceId,
|
|
81
|
+
messageId: headers.get('x-dharma-message-id'), method: 'POST', nonce: headers.get('x-dharma-nonce'), organizationId: config.organizationId,
|
|
82
|
+
pathname: address.pathname, sequence: 1, sessionId: headers.get('x-dharma-session-id'), timestamp: headers.get('x-dharma-timestamp') }));
|
|
83
|
+
assert.equal(verify(null, transport, device.publicKey, Buffer.from(headers.get('x-dharma-signature') ?? '', 'base64url')), true);
|
|
84
|
+
if (body.schema === 'dharma.device-signing-maintenance-context-request/v1') {
|
|
85
|
+
assert.equal(address.pathname, '/api/v1/agent-fabric/signing-recovery/preparations');
|
|
86
|
+
assert.equal(headers.get('x-dharma-message-id'), body.operationId);
|
|
87
|
+
assert.equal(headers.get('x-dharma-session-id'), body.operationId);
|
|
88
|
+
assert.equal(headers.get('x-dharma-nonce'), body.nonce);
|
|
89
|
+
}
|
|
90
|
+
else if (address.pathname.endsWith('/preparations')) {
|
|
91
|
+
assert.equal(headers.get('x-dharma-message-id'), body.preparation.plan.planId);
|
|
92
|
+
assert.equal(headers.get('x-dharma-session-id'), body.preparation.plan.planId);
|
|
93
|
+
assert.equal(headers.get('x-dharma-nonce'), body.preparation.nonce);
|
|
94
|
+
}
|
|
95
|
+
else if (address.pathname.includes('/offers/')) {
|
|
96
|
+
assert.equal(headers.get('x-dharma-session-id'), body.preparationId);
|
|
97
|
+
assert.equal(headers.get('x-dharma-message-id'), body.requestId);
|
|
98
|
+
assert.equal(headers.get('x-dharma-nonce'), body.nonce);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
assert.equal(headers.get('x-dharma-session-id'), body.recoveryId);
|
|
102
|
+
assert.equal(headers.get('x-dharma-message-id'), body.approvalId);
|
|
103
|
+
assert.equal(headers.get('x-dharma-nonce'), challenge?.nonce);
|
|
104
|
+
}
|
|
105
|
+
requests.push({ path: address.pathname, body });
|
|
106
|
+
if (handler)
|
|
107
|
+
return handler(address.pathname, init);
|
|
108
|
+
if (address.pathname === '/api/v1/agent-fabric/signing-recovery/preparations') {
|
|
109
|
+
preparation = parseDeviceSigningRecoveryPreparation(body.preparation, body.preparation.plan, clock);
|
|
110
|
+
assert.equal(verify(null, deviceSigningRecoveryPreparationSigningPayload(preparation), device.publicKey, Buffer.from(body.signature, 'base64url')), true);
|
|
111
|
+
const { schema, planId, authority, preparedAt, expiresAt, ...bound } = preparation.plan;
|
|
112
|
+
void [schema, planId, authority, preparedAt, expiresAt];
|
|
113
|
+
const maintenance = preparation.plan.schema === 'dharma.device-signing-maintenance-plan/v1';
|
|
114
|
+
assert.equal(body.schema, maintenance ? 'dharma.device-signing-maintenance-preparation-request/v1' : 'dharma.device-signing-recovery-preparation-request/v1');
|
|
115
|
+
challenge = { ...bound, schema: maintenance ? 'dharma.device-signing-maintenance-challenge/v1' : 'dharma.device-signing-recovery-challenge/v1', purpose: 'expired_signing_trust_reapproval',
|
|
116
|
+
recoveryId: id(12), approvalId: id(13), credentialEncryptionPublicKey: preparation.credentialEncryptionPublicKey, candidateGeneration: 3,
|
|
117
|
+
candidateKeysetHash: hash(canonicalize(nextKeyset)), candidateRootFingerprint: hash(Buffer.from(pub(next), 'base64url')),
|
|
118
|
+
candidateKeyVersionFingerprint: hash(nextKeyset.signedByKeyVersion), method: 'POST', path: '/api/v1/agent-fabric/signing-recovery',
|
|
119
|
+
nonce: Buffer.alloc(32, 7).toString('base64url'), authenticator: Buffer.alloc(32, 8).toString('base64url'),
|
|
120
|
+
issuedAt: new Date(clock).toISOString(), expiresAt: new Date(Math.min(clock + 600000, NOW + receiptLifetime)).toISOString() };
|
|
121
|
+
return Response.json({ schema: 'dharma.signing-recovery-preparation-receipt/v1', preparationId: planId, organizationId: config.organizationId,
|
|
122
|
+
recipientMembershipId: session.membershipId, deviceId: config.deviceId, expiresAt: new Date(Math.min(Date.parse(String(expiresAt)), NOW + receiptLifetime)).toISOString(), state: 'pending_browser_review',
|
|
123
|
+
clientTrustInstalled: false, ...(maintenance ? { controllerBinding: 'device_attested_not_native_authority', nativeContinuationAuthorized: false }
|
|
124
|
+
: { nativeSessionBinding: 'device_attested_not_verified' }) });
|
|
125
|
+
}
|
|
126
|
+
assert.ok(preparation && challenge);
|
|
127
|
+
const common = { schema: 'dharma.device-signing-recovery-offer-response/v1', organizationId: config.organizationId, deviceId: config.deviceId,
|
|
128
|
+
preparationId: preparation.plan.planId, ready: false, clientTrustInstalled: false };
|
|
129
|
+
if (address.pathname.endsWith('/offers/read')) {
|
|
130
|
+
const value = !offered ? { ...common, state: 'awaiting_browser_offer', deviceProofRegistered: false,
|
|
131
|
+
expiresAt: new Date(Math.min(Date.parse(preparation.plan.expiresAt), NOW + receiptLifetime)).toISOString() } : { ...common, state: registered ? 'pending_approval' : 'awaiting_device_proof', deviceProofRegistered: registered,
|
|
132
|
+
expiresAt: challenge.expiresAt, challenge };
|
|
133
|
+
return Response.json(offerTransform ? offerTransform(value) : value);
|
|
134
|
+
}
|
|
135
|
+
assert.equal(verify(null, deviceSigningRecoverySigningPayload(challenge), device.publicKey, Buffer.from(String(body.signature), 'base64url')), true);
|
|
136
|
+
if (address.pathname.endsWith('/offers/register')) {
|
|
137
|
+
registered = true;
|
|
138
|
+
return Response.json({ ...common, schema: 'dharma.device-signing-recovery-offer-registration-receipt/v1',
|
|
139
|
+
recoveryId: challenge.recoveryId, approvalId: challenge.approvalId, state: 'pending_approval', deviceProofRegistered: true, expiresAt: challenge.expiresAt });
|
|
140
|
+
}
|
|
141
|
+
assert.equal(address.pathname, '/api/v1/agent-fabric/signing-recovery');
|
|
142
|
+
if (!published)
|
|
143
|
+
return new Response(null, { status: 409 });
|
|
144
|
+
const maintenance = preparation.plan.schema === 'dharma.device-signing-maintenance-plan/v1';
|
|
145
|
+
assert.equal(body.schema, maintenance ? 'dharma.device-signing-maintenance-delivery-request/v1' : 'dharma.device-signing-recovery-delivery-request/v1');
|
|
146
|
+
const candidate = { schema: 'dharma.device-signing-recovery-candidate/v1', organizationId: config.organizationId,
|
|
147
|
+
recoveryId: challenge.recoveryId, approvalId: challenge.approvalId, keysetCanonical: canonicalize(nextKeyset), rootPublicKey: pub(next),
|
|
148
|
+
publication: { schema: 'dharma.signing-recovery-publication/v1', organizationId: config.organizationId, recoveryId: challenge.recoveryId,
|
|
149
|
+
generation: 3, artifactHash: digest('e'), publishedAt: new Date(clock).toISOString() } };
|
|
150
|
+
return Response.json({ ok: true, delivery: sealDeviceSigningRecoveryDelivery(challenge, candidate, clock), clientTrustInstalled: false,
|
|
151
|
+
...(maintenance ? { controllerBinding: 'device_attested_not_native_authority', nativeContinuationAuthorized: false }
|
|
152
|
+
: { nativeSessionBinding: 'device_attested_not_independently_observed' }) });
|
|
153
|
+
});
|
|
154
|
+
const input = { configPath, source, session, hostBinding: { connectionId: id(10), hostContextId: id(11), threadId: session.sessionId, turnId: 'fixture-original-turn' },
|
|
155
|
+
hostScope: { signal: controller.signal, current: async () => allowed }, store, now: () => clock, fetcher };
|
|
156
|
+
await writeFile(configPath, JSON.stringify(config));
|
|
157
|
+
return { root, input, config, configPath, privateIdentity, identityAccount, anchorAccount, values, reads, writes, requests, controller,
|
|
158
|
+
offer: () => { offered = true; }, publish: () => { published = true; }, revoke: () => { allowed = false; }, clock: (at) => { clock = at; },
|
|
159
|
+
setHandler: (fn) => { handler = fn; }, cleanup: async () => {
|
|
160
|
+
assert.equal(dirname(resolve(root)), parent);
|
|
161
|
+
assert.ok(basename(root).startsWith('fabric-owned-recovery-'));
|
|
162
|
+
await rm(root, { recursive: true, force: true });
|
|
163
|
+
}, setOfferTransform: (fn) => { offerTransform = fn; }, shortenReceipt: (ms) => { receiptLifetime = ms; } };
|
|
164
|
+
}
|
|
165
|
+
const safe = (value) => assert.doesNotMatch(JSON.stringify(value), /signature|authenticator|keysetCanonical|credentialEncryption|privateJwk|PRIVATE_CANARY/);
|
|
166
|
+
test('official maintenance context read signs only the fixed endpoint and returns exact current source without writes', async () => {
|
|
167
|
+
const f = await fixture();
|
|
168
|
+
try {
|
|
169
|
+
f.setHandler(async (_path, init) => {
|
|
170
|
+
const { schema, issuedAt, repositoryFingerprint, ...selection } = JSON.parse(String(init.body));
|
|
171
|
+
void [schema, issuedAt, repositoryFingerprint];
|
|
172
|
+
return Response.json({ ...selection, schema: 'dharma.device-signing-maintenance-context/v1',
|
|
173
|
+
recipientMembershipId: f.input.session.membershipId, source: f.input.source, authority: 'current_source_context_only_not_consent' });
|
|
174
|
+
});
|
|
175
|
+
const result = await relay.readOwnedSigningMaintenanceContext({ ...f.input, workspaceId: f.input.source.workspaceId,
|
|
176
|
+
repositoryBindingId: f.input.source.repositoryBindingId, membershipId: f.input.session.membershipId, operationId: id(21) });
|
|
177
|
+
assert.deepEqual(result.source, f.input.source);
|
|
178
|
+
assert.equal(f.requests.length, 1);
|
|
179
|
+
assert.equal(f.reads.filter(name => name === f.identityAccount).length, 1);
|
|
180
|
+
assert.deepEqual(f.writes, []);
|
|
181
|
+
assert.equal((await validateContract(resolve(import.meta.dirname, '../../../schemas'), 'https://schemas.dharma-ai.io/device-signing-maintenance-context-request/v1', f.requests[0].body)).ok, true);
|
|
182
|
+
assert.equal((await validateContract(resolve(import.meta.dirname, '../../../schemas'), 'https://schemas.dharma-ai.io/device-signing-maintenance-context/v1', result)).ok, true);
|
|
183
|
+
safe(result);
|
|
184
|
+
}
|
|
185
|
+
finally {
|
|
186
|
+
await f.cleanup();
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
for (const fault of ['device', 'member', 'source', 'nonce', 'expiry', 'extra', 'config', 'scope', 'http']) {
|
|
190
|
+
test('maintenance context rejects ' + fault + ' without replacing credentials or retrying', async () => {
|
|
191
|
+
const f = await fixture();
|
|
192
|
+
try {
|
|
193
|
+
f.setHandler(async (_path, init) => {
|
|
194
|
+
const { schema, issuedAt, repositoryFingerprint, ...selection } = JSON.parse(String(init.body));
|
|
195
|
+
void [schema, issuedAt, repositoryFingerprint];
|
|
196
|
+
const body = { ...selection, schema: 'dharma.device-signing-maintenance-context/v1', recipientMembershipId: f.input.session.membershipId,
|
|
197
|
+
source: f.input.source, authority: 'current_source_context_only_not_consent' };
|
|
198
|
+
if (fault === 'device')
|
|
199
|
+
body.deviceId = id(99);
|
|
200
|
+
if (fault === 'member')
|
|
201
|
+
body.recipientMembershipId = id(99);
|
|
202
|
+
if (fault === 'source')
|
|
203
|
+
body.source = { ...body.source, repositoryBindingId: id(99) };
|
|
204
|
+
if (fault === 'nonce')
|
|
205
|
+
body.nonce = Buffer.alloc(32, 3).toString('base64url');
|
|
206
|
+
if (fault === 'expiry')
|
|
207
|
+
body.expiresAt = new Date(NOW).toISOString();
|
|
208
|
+
if (fault === 'extra')
|
|
209
|
+
body.token = 'SYNTHETIC_PRIVATE_CANARY';
|
|
210
|
+
if (fault === 'scope')
|
|
211
|
+
f.revoke();
|
|
212
|
+
if (fault === 'config')
|
|
213
|
+
await writeFile(f.configPath, JSON.stringify({ ...f.config, deviceName: 'Changed current config' }));
|
|
214
|
+
return fault === 'http' ? new Response(null, { status: 503 }) : Response.json(body);
|
|
215
|
+
});
|
|
216
|
+
await assert.rejects(relay.readOwnedSigningMaintenanceContext({ ...f.input, workspaceId: f.input.source.workspaceId,
|
|
217
|
+
repositoryBindingId: f.input.source.repositoryBindingId, membershipId: f.input.session.membershipId, operationId: id(21) }), /owned_signing_recovery_failed/);
|
|
218
|
+
assert.equal(f.requests.length, 1);
|
|
219
|
+
assert.deepEqual(f.writes, []);
|
|
220
|
+
assert.equal(f.values.get(f.identityAccount), f.privateIdentity);
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
await f.cleanup();
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
test('maintenance host carries a distinct origin through preparation, offer, encrypted delivery and protected journal', async () => {
|
|
228
|
+
const fn = api(), f = await fixture();
|
|
229
|
+
try {
|
|
230
|
+
const { session, hostBinding, ...base } = f.input;
|
|
231
|
+
void hostBinding;
|
|
232
|
+
const controller = { schema: 'dharma.device-trust-maintenance-owner/v1', operationId: id(21), controllerInstanceId: id(22),
|
|
233
|
+
processIdentityDigest: digest('e'), leaseDigest: digest('f'), registrationDigest: digest('a'),
|
|
234
|
+
deviceId: session.deviceId, recipientMembershipId: session.membershipId, originalBindingId: session.bindingId,
|
|
235
|
+
originalSessionId: session.sessionId, expiresAt: new Date(NOW + 900000).toISOString() };
|
|
236
|
+
const handle = await relay.prepareOwnedSigningMaintenance({ ...base, controller });
|
|
237
|
+
const planned = await fn.inspectOwnedSigningRecovery(handle);
|
|
238
|
+
assert.equal(planned.authorityKind, 'device_trust_maintenance');
|
|
239
|
+
assert.equal(planned.nativeContinuationAuthorized, false);
|
|
240
|
+
assert.equal((await validateContract(resolve(import.meta.dirname, '../../../schemas'), 'https://schemas.dharma-ai.io/device-signing-maintenance-host-progress/v1', planned)).ok, true);
|
|
241
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, 0);
|
|
242
|
+
assert.deepEqual(f.writes, []);
|
|
243
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
244
|
+
const p = f.requests[0].body.preparation;
|
|
245
|
+
assert.equal(p.plan.schema, 'dharma.device-signing-maintenance-plan/v1');
|
|
246
|
+
assert.equal('nativeSessionBindingDigest' in p.plan, false);
|
|
247
|
+
f.offer();
|
|
248
|
+
await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
249
|
+
assert.deepEqual(f.writes, []);
|
|
250
|
+
f.publish();
|
|
251
|
+
const result = await fn.completeOwnedSigningRecovery(handle);
|
|
252
|
+
assert.equal(result.state, 'trust_installed');
|
|
253
|
+
assert.equal(result.ready, false);
|
|
254
|
+
assert.equal(result.nativeContinuationAuthorized, false);
|
|
255
|
+
const installed = JSON.parse(await readFile(f.configPath, 'utf8'));
|
|
256
|
+
assert.equal(installed.serverSigningKeyset.generation, 3);
|
|
257
|
+
assert.equal(installed.deviceId, f.config.deviceId);
|
|
258
|
+
assert.equal(f.values.get(f.identityAccount), f.privateIdentity);
|
|
259
|
+
assert.ok(f.writes.every(key => key === f.anchorAccount));
|
|
260
|
+
safe(result);
|
|
261
|
+
const count = f.requests.length;
|
|
262
|
+
await assert.rejects(fn.completeOwnedSigningRecovery(handle));
|
|
263
|
+
assert.equal(f.requests.length, count);
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
await f.cleanup();
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
test('host plan returns only an opaque public handle, without private-key reads, network or writes', async () => {
|
|
270
|
+
const fn = api(), f = await fixture();
|
|
271
|
+
try {
|
|
272
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input), summary = await fn.inspectOwnedSigningRecovery(handle);
|
|
273
|
+
assert.equal(f.requests.length, 0);
|
|
274
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, 0);
|
|
275
|
+
assert.deepEqual(f.writes, []);
|
|
276
|
+
assert.equal(summary.state, 'prepared');
|
|
277
|
+
assert.equal(summary.ready, false);
|
|
278
|
+
safe(handle);
|
|
279
|
+
safe(summary);
|
|
280
|
+
assert.equal((await validateContract(resolve(import.meta.dirname, '../../../schemas'), 'https://schemas.dharma-ai.io/device-signing-recovery-host-progress/v1', summary)).ok, true);
|
|
281
|
+
}
|
|
282
|
+
finally {
|
|
283
|
+
await f.cleanup();
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
test('same owning host submits once, retains the private handshake, progresses proof and installs only explicitly', async () => {
|
|
287
|
+
const fn = api(), f = await fixture();
|
|
288
|
+
try {
|
|
289
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
290
|
+
const admitted = await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
291
|
+
assert.equal(admitted.state, 'awaiting_browser_offer');
|
|
292
|
+
safe(admitted);
|
|
293
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, 1);
|
|
294
|
+
assert.deepEqual(f.writes, []);
|
|
295
|
+
const waiting = await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
296
|
+
assert.equal(waiting.state, 'awaiting_browser_offer');
|
|
297
|
+
f.offer();
|
|
298
|
+
const pending = await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
299
|
+
assert.equal(pending.state, 'pending_approval');
|
|
300
|
+
safe(pending);
|
|
301
|
+
assert.deepEqual(f.writes, []);
|
|
302
|
+
assert.equal(JSON.parse(await readFile(f.configPath, 'utf8')).serverSigningKeyset.generation, 2);
|
|
303
|
+
f.publish();
|
|
304
|
+
const completed = await fn.completeOwnedSigningRecovery(handle);
|
|
305
|
+
safe(completed);
|
|
306
|
+
assert.equal(completed.ready, false);
|
|
307
|
+
const installed = JSON.parse(await readFile(f.configPath, 'utf8'));
|
|
308
|
+
assert.equal(installed.serverSigningKeyset.generation, 3);
|
|
309
|
+
assert.equal(installed.deviceId, f.config.deviceId);
|
|
310
|
+
assert.equal(f.values.get(f.identityAccount), f.privateIdentity);
|
|
311
|
+
assert.ok(f.writes.every(key => key === f.anchorAccount));
|
|
312
|
+
const before = f.requests.length;
|
|
313
|
+
await assert.rejects(fn.completeOwnedSigningRecovery(handle));
|
|
314
|
+
assert.equal(f.requests.length, before);
|
|
315
|
+
assert.equal((await fn.inspectOwnedSigningRecovery(handle)).state, 'trust_installed');
|
|
316
|
+
}
|
|
317
|
+
finally {
|
|
318
|
+
await f.cleanup();
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
test('serialized, copied, proxied or disposed handles carry no execution authority', async () => {
|
|
322
|
+
const fn = api(), f = await fixture();
|
|
323
|
+
try {
|
|
324
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
325
|
+
for (const forged of [JSON.parse(JSON.stringify(handle)), { ...handle }, new Proxy(handle, {})])
|
|
326
|
+
await assert.rejects(fn.submitOwnedSigningRecoveryPreparation(forged));
|
|
327
|
+
fn.disposeOwnedSigningRecovery(handle);
|
|
328
|
+
await assert.rejects(fn.submitOwnedSigningRecoveryPreparation(handle));
|
|
329
|
+
assert.equal(f.requests.length, 0);
|
|
330
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, 0);
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
await f.cleanup();
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
test('changed original source, named session or host scope is rejected before a preparation signature', async () => {
|
|
337
|
+
const fn = api();
|
|
338
|
+
for (const fault of ['source', 'session', 'scope', 'expiry']) {
|
|
339
|
+
const f = await fixture();
|
|
340
|
+
try {
|
|
341
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
342
|
+
if (fault === 'source')
|
|
343
|
+
f.input.source.sourceRevision++;
|
|
344
|
+
if (fault === 'session')
|
|
345
|
+
f.input.session.bindingId = id(99);
|
|
346
|
+
if (fault === 'scope')
|
|
347
|
+
f.revoke();
|
|
348
|
+
if (fault === 'expiry')
|
|
349
|
+
f.clock(NOW + 900000);
|
|
350
|
+
await assert.rejects(fn.submitOwnedSigningRecoveryPreparation(handle));
|
|
351
|
+
assert.equal(f.requests.length, 0);
|
|
352
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, 0);
|
|
353
|
+
}
|
|
354
|
+
finally {
|
|
355
|
+
await f.cleanup();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
test('unconfirmed submission preserves its handle but cannot repeat the key read or authority request', async () => {
|
|
360
|
+
const fn = api(), f = await fixture();
|
|
361
|
+
try {
|
|
362
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
363
|
+
f.setHandler(async () => new Response('PRIVATE_CANARY', { status: 503 }));
|
|
364
|
+
await assert.rejects(fn.submitOwnedSigningRecoveryPreparation(handle), error => error instanceof Error && !error.message.includes('PRIVATE_CANARY'));
|
|
365
|
+
assert.equal((await fn.inspectOwnedSigningRecovery(handle)).state, 'submission_unconfirmed');
|
|
366
|
+
const count = f.reads.filter(x => x === f.identityAccount).length;
|
|
367
|
+
await assert.rejects(fn.submitOwnedSigningRecoveryPreparation(handle));
|
|
368
|
+
assert.equal(f.requests.length, 1);
|
|
369
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, count);
|
|
370
|
+
}
|
|
371
|
+
finally {
|
|
372
|
+
await f.cleanup();
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
test('concurrent submit is refused, and disposal bounds a noncooperative response without another request', async () => {
|
|
376
|
+
const fn = api(), f = await fixture();
|
|
377
|
+
try {
|
|
378
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
379
|
+
let entered;
|
|
380
|
+
const reading = new Promise(resolve => { entered = resolve; });
|
|
381
|
+
f.setHandler(async () => new Response(new ReadableStream({ pull() { entered(); return new Promise(() => { }); }, cancel() { return new Promise(() => { }); } }), { headers: { 'content-type': 'application/json' } }));
|
|
382
|
+
const pending = fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
383
|
+
await reading;
|
|
384
|
+
await assert.rejects(fn.submitOwnedSigningRecoveryPreparation(handle));
|
|
385
|
+
fn.disposeOwnedSigningRecovery(handle);
|
|
386
|
+
await Promise.race([assert.rejects(pending), new Promise((_, reject) => setTimeout(() => reject(Error('UNBOUNDED_HOST_CANCEL')), 1000))]);
|
|
387
|
+
assert.equal(f.requests.length, 1);
|
|
388
|
+
assert.deepEqual(f.writes, []);
|
|
389
|
+
}
|
|
390
|
+
finally {
|
|
391
|
+
await f.cleanup();
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
test('receipt context, lifetime, shape and encoding failures stay unconfirmed and never progress', async () => {
|
|
395
|
+
const fn = api();
|
|
396
|
+
for (const fault of ['context', 'expiry', 'extra', 'type', 'size', 'utf8', 'redirect', 'missing', 'installed', 'native']) {
|
|
397
|
+
const f = await fixture();
|
|
398
|
+
try {
|
|
399
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
400
|
+
f.setHandler(async (_path, init) => {
|
|
401
|
+
const p = JSON.parse(String(init.body)).preparation.plan;
|
|
402
|
+
const value = { schema: 'dharma.signing-recovery-preparation-receipt/v1', preparationId: p.planId,
|
|
403
|
+
organizationId: p.organizationId, recipientMembershipId: p.recipientMembershipId, deviceId: p.deviceId, expiresAt: p.expiresAt, state: 'pending_browser_review',
|
|
404
|
+
clientTrustInstalled: false, nativeSessionBinding: 'device_attested_not_verified' };
|
|
405
|
+
if (fault === 'missing')
|
|
406
|
+
Reflect.deleteProperty(value, 'nativeSessionBinding');
|
|
407
|
+
if (fault === 'installed')
|
|
408
|
+
value.clientTrustInstalled = true;
|
|
409
|
+
if (fault === 'native')
|
|
410
|
+
value.nativeSessionBinding = 'verified';
|
|
411
|
+
if (fault === 'context')
|
|
412
|
+
value.deviceId = id(99);
|
|
413
|
+
if (fault === 'expiry')
|
|
414
|
+
value.expiresAt = new Date(NOW + 1800000).toISOString();
|
|
415
|
+
if (fault === 'extra')
|
|
416
|
+
Object.assign(value, { secret: 'PRIVATE_CANARY' });
|
|
417
|
+
if (fault === 'type')
|
|
418
|
+
return new Response(JSON.stringify(value), { headers: { 'content-type': 'application/jsonp' } });
|
|
419
|
+
if (fault === 'size')
|
|
420
|
+
return new Response('x'.repeat(8193), { headers: { 'content-type': 'application/json' } });
|
|
421
|
+
if (fault === 'utf8')
|
|
422
|
+
return new Response(new Uint8Array([255]), { headers: { 'content-type': 'application/json' } });
|
|
423
|
+
const result = Response.json(value);
|
|
424
|
+
if (fault === 'redirect')
|
|
425
|
+
Object.defineProperty(result, 'redirected', { value: true });
|
|
426
|
+
return result;
|
|
427
|
+
});
|
|
428
|
+
await assert.rejects(fn.submitOwnedSigningRecoveryPreparation(handle));
|
|
429
|
+
assert.equal((await fn.inspectOwnedSigningRecovery(handle)).state, 'submission_unconfirmed');
|
|
430
|
+
await assert.rejects(fn.progressOwnedSigningRecoveryOffer(handle));
|
|
431
|
+
assert.equal(f.requests.length, 1);
|
|
432
|
+
assert.deepEqual(f.writes, []);
|
|
433
|
+
}
|
|
434
|
+
finally {
|
|
435
|
+
await f.cleanup();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
test('progress and installation cannot precede their explicit separate phases', async () => {
|
|
440
|
+
const fn = api(), f = await fixture();
|
|
441
|
+
try {
|
|
442
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
443
|
+
await assert.rejects(fn.progressOwnedSigningRecoveryOffer(handle));
|
|
444
|
+
await assert.rejects(fn.completeOwnedSigningRecovery(handle));
|
|
445
|
+
assert.equal(f.requests.length, 0);
|
|
446
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
447
|
+
await assert.rejects(fn.completeOwnedSigningRecovery(handle));
|
|
448
|
+
assert.equal(f.requests.length, 1);
|
|
449
|
+
assert.deepEqual(f.writes, []);
|
|
450
|
+
}
|
|
451
|
+
finally {
|
|
452
|
+
await f.cleanup();
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
test('preparation consumes the actual server receipt envelope, not a fixture-only partial shape', async () => {
|
|
456
|
+
const fn = api(), f = await fixture();
|
|
457
|
+
try {
|
|
458
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
459
|
+
f.setHandler(async (_path, init) => {
|
|
460
|
+
const p = JSON.parse(String(init.body)).preparation.plan;
|
|
461
|
+
return Response.json({ schema: 'dharma.signing-recovery-preparation-receipt/v1', preparationId: p.planId, organizationId: p.organizationId,
|
|
462
|
+
recipientMembershipId: p.recipientMembershipId, deviceId: p.deviceId, expiresAt: p.expiresAt, state: 'pending_browser_review',
|
|
463
|
+
clientTrustInstalled: false, nativeSessionBinding: 'device_attested_not_verified' });
|
|
464
|
+
});
|
|
465
|
+
assert.equal((await fn.submitOwnedSigningRecoveryPreparation(handle)).state, 'awaiting_browser_offer');
|
|
466
|
+
assert.deepEqual(f.writes, []);
|
|
467
|
+
}
|
|
468
|
+
finally {
|
|
469
|
+
await f.cleanup();
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
test('a current narrower server deadline is retained and expiry stops before another private read or request', async () => {
|
|
473
|
+
const fn = api(), f = await fixture();
|
|
474
|
+
try {
|
|
475
|
+
f.shortenReceipt(120000);
|
|
476
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
477
|
+
const admitted = await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
478
|
+
assert.equal(admitted.expiresAt, new Date(NOW + 120000).toISOString());
|
|
479
|
+
const count = f.requests.length, reads = f.reads.filter(x => x === f.identityAccount).length;
|
|
480
|
+
f.clock(NOW + 120000);
|
|
481
|
+
await assert.rejects(fn.progressOwnedSigningRecoveryOffer(handle));
|
|
482
|
+
assert.equal(f.requests.length, count);
|
|
483
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, reads);
|
|
484
|
+
assert.deepEqual(f.writes, []);
|
|
485
|
+
}
|
|
486
|
+
finally {
|
|
487
|
+
await f.cleanup();
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
test('a later offer cannot extend the admitted server deadline before registering a proof', async () => {
|
|
491
|
+
const fn = api(), f = await fixture();
|
|
492
|
+
try {
|
|
493
|
+
f.shortenReceipt(120000);
|
|
494
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
495
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
496
|
+
f.offer();
|
|
497
|
+
f.setOfferTransform(value => ({ ...value, expiresAt: new Date(NOW + 900000).toISOString() }));
|
|
498
|
+
await assert.rejects(fn.progressOwnedSigningRecoveryOffer(handle));
|
|
499
|
+
assert.equal(f.requests.some(x => x.path.endsWith('/register')), false);
|
|
500
|
+
assert.deepEqual(f.writes, []);
|
|
501
|
+
}
|
|
502
|
+
finally {
|
|
503
|
+
await f.cleanup();
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
test('shortened receipt lifetime remains usable for proof and explicit confirmed installation', async () => {
|
|
507
|
+
const fn = api(), f = await fixture();
|
|
508
|
+
try {
|
|
509
|
+
f.shortenReceipt(120000);
|
|
510
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
511
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
512
|
+
f.offer();
|
|
513
|
+
const pending = await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
514
|
+
assert.equal(pending.expiresAt, new Date(NOW + 120000).toISOString());
|
|
515
|
+
f.publish();
|
|
516
|
+
const result = await fn.completeOwnedSigningRecovery(handle);
|
|
517
|
+
assert.equal(result.state, 'trust_installed');
|
|
518
|
+
assert.equal(result.clientTrustInstalled, true);
|
|
519
|
+
assert.equal(result.ready, false);
|
|
520
|
+
assert.equal(result.expiresAt, new Date(NOW + 120000).toISOString());
|
|
521
|
+
safe(result);
|
|
522
|
+
}
|
|
523
|
+
finally {
|
|
524
|
+
await f.cleanup();
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
test('unpublished delivery is not retried or converted to consent and leaves original trust intact', async () => {
|
|
528
|
+
const fn = api(), f = await fixture();
|
|
529
|
+
try {
|
|
530
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
531
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
532
|
+
f.offer();
|
|
533
|
+
await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
534
|
+
await assert.rejects(fn.completeOwnedSigningRecovery(handle));
|
|
535
|
+
assert.equal((await fn.inspectOwnedSigningRecovery(handle)).state, 'delivery_unconfirmed');
|
|
536
|
+
const before = f.requests.length;
|
|
537
|
+
f.publish();
|
|
538
|
+
await assert.rejects(fn.completeOwnedSigningRecovery(handle));
|
|
539
|
+
assert.equal(f.requests.length, before);
|
|
540
|
+
assert.deepEqual(f.writes, []);
|
|
541
|
+
assert.equal(JSON.parse(await readFile(f.configPath, 'utf8')).serverSigningKeyset.generation, 2);
|
|
542
|
+
}
|
|
543
|
+
finally {
|
|
544
|
+
await f.cleanup();
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
test('disposing delivery bounds noncooperative fetch and response reads without protected effects', async () => {
|
|
548
|
+
const fn = api();
|
|
549
|
+
for (const fault of ['fetch', 'body']) {
|
|
550
|
+
const f = await fixture();
|
|
551
|
+
try {
|
|
552
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
553
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
554
|
+
f.offer();
|
|
555
|
+
await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
556
|
+
let entered;
|
|
557
|
+
const started = new Promise(resolve => { entered = resolve; });
|
|
558
|
+
f.setHandler(async () => {
|
|
559
|
+
if (fault === 'fetch') {
|
|
560
|
+
entered();
|
|
561
|
+
return new Promise(() => { });
|
|
562
|
+
}
|
|
563
|
+
return new Response(new ReadableStream({ pull() { entered(); return new Promise(() => { }); }, cancel() { return new Promise(() => { }); } }), { headers: { 'content-type': 'application/json' } });
|
|
564
|
+
});
|
|
565
|
+
const pending = fn.completeOwnedSigningRecovery(handle);
|
|
566
|
+
await started;
|
|
567
|
+
fn.disposeOwnedSigningRecovery(handle);
|
|
568
|
+
await Promise.race([assert.rejects(pending), new Promise((_, reject) => setTimeout(() => reject(Error('UNBOUNDED_DELIVERY_CANCEL')), 1000))]);
|
|
569
|
+
assert.deepEqual(f.writes, []);
|
|
570
|
+
assert.equal(JSON.parse(await readFile(f.configPath, 'utf8')).serverSigningKeyset.generation, 2);
|
|
571
|
+
}
|
|
572
|
+
finally {
|
|
573
|
+
await f.cleanup();
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
test('changed challenge context cannot reach proof registration or delivery and cannot retry implicitly', async () => {
|
|
578
|
+
const fn = api();
|
|
579
|
+
for (const field of ['deviceId', 'nativeSessionBindingDigest', 'credentialEncryptionPublicKey', 'previousKeysetHash']) {
|
|
580
|
+
const f = await fixture();
|
|
581
|
+
try {
|
|
582
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
583
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
584
|
+
f.offer();
|
|
585
|
+
f.setOfferTransform(value => ({ ...value, challenge: { ...value.challenge, [field]: field === 'deviceId' ? id(99) : field === 'credentialEncryptionPublicKey' ?
|
|
586
|
+
generateKeyPairSync('x25519').publicKey.export({ format: 'jwk' }).x : digest('f') } }));
|
|
587
|
+
await assert.rejects(fn.progressOwnedSigningRecoveryOffer(handle));
|
|
588
|
+
assert.equal((await fn.inspectOwnedSigningRecovery(handle)).state, 'offer_unconfirmed');
|
|
589
|
+
const count = f.requests.length;
|
|
590
|
+
await assert.rejects(fn.progressOwnedSigningRecoveryOffer(handle));
|
|
591
|
+
await assert.rejects(fn.completeOwnedSigningRecovery(handle));
|
|
592
|
+
assert.equal(f.requests.length, count);
|
|
593
|
+
assert.equal(f.requests.some(x => x.path.endsWith('/register')), false);
|
|
594
|
+
assert.deepEqual(f.writes, []);
|
|
595
|
+
}
|
|
596
|
+
finally {
|
|
597
|
+
await f.cleanup();
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
test('source and session withdrawal after preparation or proof prevents the next private-key read and request', async () => {
|
|
602
|
+
const fn = api();
|
|
603
|
+
for (const phase of ['offer', 'delivery']) {
|
|
604
|
+
const f = await fixture();
|
|
605
|
+
try {
|
|
606
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
607
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
608
|
+
if (phase === 'delivery') {
|
|
609
|
+
f.offer();
|
|
610
|
+
await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
611
|
+
f.publish();
|
|
612
|
+
}
|
|
613
|
+
const requests = f.requests.length, reads = f.reads.filter(x => x === f.identityAccount).length;
|
|
614
|
+
f.input.source.repositoryBindingId = id(99);
|
|
615
|
+
await assert.rejects(phase === 'offer' ? fn.progressOwnedSigningRecoveryOffer(handle) : fn.completeOwnedSigningRecovery(handle));
|
|
616
|
+
assert.equal(f.requests.length, requests);
|
|
617
|
+
assert.equal(f.reads.filter(x => x === f.identityAccount).length, reads);
|
|
618
|
+
assert.deepEqual(f.writes, []);
|
|
619
|
+
}
|
|
620
|
+
finally {
|
|
621
|
+
await f.cleanup();
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
test('only the owning-host opaque operations are public, not the raw challenge handoff', () => {
|
|
626
|
+
api();
|
|
627
|
+
assert.equal('progressPreparedSigningRecoveryOfferForHost' in relay, false);
|
|
628
|
+
});
|
|
629
|
+
test('actual loopback HTTP carries signed preparation, proof and encrypted delivery in explicit separate phases', async () => {
|
|
630
|
+
const fn = api(), f = await fixture();
|
|
631
|
+
let server;
|
|
632
|
+
try {
|
|
633
|
+
const protocol = f.input.fetcher;
|
|
634
|
+
server = createServer(async (request, response) => {
|
|
635
|
+
try {
|
|
636
|
+
const chunks = [];
|
|
637
|
+
for await (const chunk of request)
|
|
638
|
+
chunks.push(Buffer.from(chunk));
|
|
639
|
+
const output = await protocol(f.config.hqUrl + request.url, { method: request.method, headers: request.headers,
|
|
640
|
+
body: Buffer.concat(chunks).toString('utf8'), redirect: 'error', credentials: 'omit' });
|
|
641
|
+
response.writeHead(output.status, Object.fromEntries(output.headers));
|
|
642
|
+
response.end(Buffer.from(await output.arrayBuffer()));
|
|
643
|
+
}
|
|
644
|
+
catch {
|
|
645
|
+
response.writeHead(500);
|
|
646
|
+
response.end('fixture_failed');
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
650
|
+
const base = 'http://127.0.0.1:' + server.address().port;
|
|
651
|
+
// Only this synthetic transport fixture maps the pinned HTTPS target to a
|
|
652
|
+
// private loopback HTTP server. It does not qualify production TLS/auth.
|
|
653
|
+
f.input.fetcher = async (url, init) => {
|
|
654
|
+
const target = new URL(String(url));
|
|
655
|
+
assert.equal(target.origin, f.config.hqUrl);
|
|
656
|
+
const wire = await fetch(base + target.pathname, init);
|
|
657
|
+
assert.equal(wire.url, base + target.pathname);
|
|
658
|
+
assert.equal(wire.redirected, false);
|
|
659
|
+
return new Response(wire.body, { status: wire.status, headers: wire.headers });
|
|
660
|
+
};
|
|
661
|
+
const handle = await fn.prepareOwnedSigningRecovery(f.input);
|
|
662
|
+
await fn.submitOwnedSigningRecoveryPreparation(handle);
|
|
663
|
+
f.offer();
|
|
664
|
+
await fn.progressOwnedSigningRecoveryOffer(handle);
|
|
665
|
+
f.publish();
|
|
666
|
+
const complete = await fn.completeOwnedSigningRecovery(handle);
|
|
667
|
+
assert.equal(complete.state, 'trust_installed');
|
|
668
|
+
assert.equal(complete.ready, false);
|
|
669
|
+
assert.equal(f.requests.length, 4);
|
|
670
|
+
safe(complete);
|
|
671
|
+
assert.equal(f.values.get(f.identityAccount), f.privateIdentity);
|
|
672
|
+
fn.disposeOwnedSigningRecovery(handle);
|
|
673
|
+
}
|
|
674
|
+
finally {
|
|
675
|
+
if (server) {
|
|
676
|
+
server.closeAllConnections();
|
|
677
|
+
await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve()));
|
|
678
|
+
}
|
|
679
|
+
await f.cleanup();
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
//# sourceMappingURL=signingRecoveryHost.test.js.map
|