@idos-network/enclave 1.5.0 → 2.2.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/dist/{base-D5Ywnfua.d.mts → base-CjIdohef.d.mts} +4 -4
- package/dist/{base-D5Ywnfua.d.mts.map → base-CjIdohef.d.mts.map} +1 -1
- package/dist/client-Dqb0xbdb.mjs +2 -0
- package/dist/{client-4H2-Ewlq.mjs.map → client-Dqb0xbdb.mjs.map} +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -2
- package/dist/keys-P2XLU3qP.mjs +2 -0
- package/dist/keys-P2XLU3qP.mjs.map +1 -0
- package/dist/local.d.mts +3 -4
- package/dist/local.d.mts.map +1 -1
- package/dist/local.mjs +1 -252
- package/dist/local.mjs.map +1 -1
- package/dist/mpc/index.mjs +1 -2
- package/package.json +10 -9
- package/src/base.test.ts +1 -1
- package/src/base.ts +5 -1
- package/src/local.test.ts +197 -1
- package/src/local.ts +46 -15
- package/dist/client-4H2-Ewlq.mjs +0 -1197
- package/dist/keys-DLzoEkkD.mjs +0 -187
- package/dist/keys-DLzoEkkD.mjs.map +0 -1
package/src/local.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { idOSCredential } from "@idos-network/credentials/types";
|
|
1
|
+
import type { idOSCredentialRecord as idOSCredential } from "@idos-network/credentials/types";
|
|
2
2
|
|
|
3
3
|
import { base64Decode, base64Encode, utf8Encode } from "@idos-network/utils/codecs";
|
|
4
4
|
import { encryptContent } from "@idos-network/utils/cryptography";
|
|
@@ -6,8 +6,24 @@ import { MemoryStore } from "@idos-network/utils/store";
|
|
|
6
6
|
import tweetnacl from "tweetnacl";
|
|
7
7
|
import { describe, expect, it, vi } from "vitest";
|
|
8
8
|
|
|
9
|
+
import { STORAGE_KEYS } from "./keys.js";
|
|
9
10
|
import { LocalEnclave, type LocalEnclaveOptions } from "./local.js";
|
|
10
11
|
|
|
12
|
+
// ponytail: real scrypt (N=16384) runs on every obfuscated store read/write, which is
|
|
13
|
+
// several hundred ms per test and times out on CI. The codec only needs a deterministic
|
|
14
|
+
// key of the right length, not a real KDF.
|
|
15
|
+
vi.mock("scrypt-js", () => ({
|
|
16
|
+
syncScrypt: (
|
|
17
|
+
_password: Uint8Array,
|
|
18
|
+
salt: Uint8Array,
|
|
19
|
+
_n: number,
|
|
20
|
+
_r: number,
|
|
21
|
+
_p: number,
|
|
22
|
+
dkLen: number,
|
|
23
|
+
) =>
|
|
24
|
+
new Uint8Array(Array.from({ length: dkLen }, (_, index) => salt[index % salt.length] ^ index)),
|
|
25
|
+
}));
|
|
26
|
+
|
|
11
27
|
vi.mock("@idos-network/utils/encryption", async (importOriginal) => {
|
|
12
28
|
const actual = await importOriginal<typeof import("@idos-network/utils/encryption")>();
|
|
13
29
|
|
|
@@ -19,8 +35,20 @@ vi.mock("@idos-network/utils/encryption", async (importOriginal) => {
|
|
|
19
35
|
};
|
|
20
36
|
});
|
|
21
37
|
|
|
38
|
+
// Obfuscated store uses syncScrypt(N=16384); keep tests under vitest's default 5s timeout on CI.
|
|
39
|
+
vi.mock("scrypt-js", () => ({
|
|
40
|
+
syncScrypt: vi.fn(
|
|
41
|
+
(_password: Uint8Array, _salt: Uint8Array, _N: number, _r: number, _p: number, dkLen: number) =>
|
|
42
|
+
new Uint8Array(dkLen).fill(7),
|
|
43
|
+
),
|
|
44
|
+
}));
|
|
45
|
+
|
|
22
46
|
class TestEnclave extends LocalEnclave {
|
|
47
|
+
userIdPresentOnPrompt: boolean[] = [];
|
|
48
|
+
|
|
23
49
|
async getPasswordContext() {
|
|
50
|
+
this.userIdPresentOnPrompt.push((await this.store.get(STORAGE_KEYS.USER_ID)) !== undefined);
|
|
51
|
+
|
|
24
52
|
return {
|
|
25
53
|
encryptionPasswordStore: "user",
|
|
26
54
|
password: "super-secret",
|
|
@@ -29,9 +57,116 @@ class TestEnclave extends LocalEnclave {
|
|
|
29
57
|
}
|
|
30
58
|
}
|
|
31
59
|
|
|
60
|
+
class MPCTestEnclave extends LocalEnclave {
|
|
61
|
+
mpcPassword = "super-secret";
|
|
62
|
+
|
|
63
|
+
async getPasswordContext() {
|
|
64
|
+
return {
|
|
65
|
+
encryptionPasswordStore: "mpc",
|
|
66
|
+
} as const;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
protected async ensureMPCPassword() {
|
|
70
|
+
return this.mpcPassword;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
32
74
|
describe("LocalEnclave", () => {
|
|
33
75
|
const userId = "9f51b3b2-4cbe-4c2b-8ea3-0b0c1b2f1a11";
|
|
34
76
|
|
|
77
|
+
it("rejects passwords whose derived public key does not match expectedUserEncryptionPublicKey", async () => {
|
|
78
|
+
const store = new MemoryStore();
|
|
79
|
+
const enclave = new TestEnclave({
|
|
80
|
+
userId,
|
|
81
|
+
store,
|
|
82
|
+
expectedUserEncryptionPublicKey: base64Encode(new Uint8Array(32).fill(1)),
|
|
83
|
+
} as LocalEnclaveOptions);
|
|
84
|
+
|
|
85
|
+
await expect(
|
|
86
|
+
enclave.createEncryptionProfileFromPassword("super-secret", userId, "user"),
|
|
87
|
+
).rejects.toThrow(
|
|
88
|
+
"Derived encryption public key does not match expectedUserEncryptionPublicKey",
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("accepts passwords whose derived public key matches expectedUserEncryptionPublicKey", async () => {
|
|
93
|
+
const store = new MemoryStore();
|
|
94
|
+
const enclave = new TestEnclave({ userId, store } as LocalEnclaveOptions);
|
|
95
|
+
const { keyPair } = await enclave.createEncryptionProfileFromPassword(
|
|
96
|
+
"super-secret",
|
|
97
|
+
userId,
|
|
98
|
+
"user",
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const matchingEnclave = new TestEnclave({
|
|
102
|
+
userId,
|
|
103
|
+
store: new MemoryStore(),
|
|
104
|
+
expectedUserEncryptionPublicKey: base64Encode(keyPair.publicKey),
|
|
105
|
+
} as LocalEnclaveOptions);
|
|
106
|
+
|
|
107
|
+
await expect(
|
|
108
|
+
matchingEnclave.createEncryptionProfileFromPassword("super-secret", userId, "user"),
|
|
109
|
+
).resolves.toMatchObject({ userId, encryptionPasswordStore: "user" });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("rejects MPC unlock when the downloaded password derives a mismatched public key", async () => {
|
|
113
|
+
const store = new MemoryStore();
|
|
114
|
+
const enclave = new MPCTestEnclave({
|
|
115
|
+
userId,
|
|
116
|
+
store,
|
|
117
|
+
mode: "existing",
|
|
118
|
+
expectedUserEncryptionPublicKey: base64Encode(new Uint8Array(32).fill(1)),
|
|
119
|
+
} as LocalEnclaveOptions);
|
|
120
|
+
|
|
121
|
+
await expect(enclave.getPrivateEncryptionProfile()).rejects.toThrow(
|
|
122
|
+
"Derived encryption public key does not match expectedUserEncryptionPublicKey",
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("requires origin authorization after deriving a key and keeps the profile if denied", async () => {
|
|
127
|
+
const store = new MemoryStore();
|
|
128
|
+
const enclave = new TestEnclave({ userId, store } as LocalEnclaveOptions);
|
|
129
|
+
let allowed = false;
|
|
130
|
+
const guard = vi.spyOn(enclave, "guardKeys").mockImplementation(async () => allowed);
|
|
131
|
+
const passwordContextSpy = vi.spyOn(enclave, "getPasswordContext");
|
|
132
|
+
|
|
133
|
+
await expect(enclave.getPrivateEncryptionProfile()).rejects.toThrow(
|
|
134
|
+
"Origin is not authorized to use the keys",
|
|
135
|
+
);
|
|
136
|
+
expect(passwordContextSpy).toHaveBeenCalledTimes(1);
|
|
137
|
+
expect(guard).toHaveBeenCalledTimes(1);
|
|
138
|
+
|
|
139
|
+
allowed = true;
|
|
140
|
+
const profile = await enclave.getPrivateEncryptionProfile();
|
|
141
|
+
|
|
142
|
+
expect(profile.userId).toBe(userId);
|
|
143
|
+
expect(passwordContextSpy).toHaveBeenCalledTimes(1);
|
|
144
|
+
expect(guard).toHaveBeenCalledTimes(2);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("does not check origin authorization when the guard is skipped", async () => {
|
|
148
|
+
const enclave = new TestEnclave({ userId, store: new MemoryStore() } as LocalEnclaveOptions);
|
|
149
|
+
const guard = vi.spyOn(enclave, "guardKeys");
|
|
150
|
+
|
|
151
|
+
await enclave.getPrivateEncryptionProfile(true);
|
|
152
|
+
|
|
153
|
+
expect(guard).not.toHaveBeenCalled();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("does not request a password context for an MM profile without a stored key", async () => {
|
|
157
|
+
const enclave = new TestEnclave({
|
|
158
|
+
userId,
|
|
159
|
+
store: new MemoryStore(),
|
|
160
|
+
encryptionPasswordStore: "mm",
|
|
161
|
+
});
|
|
162
|
+
const passwordContextSpy = vi.spyOn(enclave, "getPasswordContext");
|
|
163
|
+
|
|
164
|
+
await expect(enclave.getPrivateEncryptionProfile()).rejects.toThrow(
|
|
165
|
+
"MM encryption profiles require an externally supplied encryption private key",
|
|
166
|
+
);
|
|
167
|
+
expect(passwordContextSpy).not.toHaveBeenCalled();
|
|
168
|
+
});
|
|
169
|
+
|
|
35
170
|
it("creates and exposes a public encryption profile", async () => {
|
|
36
171
|
const store = new MemoryStore();
|
|
37
172
|
const enclave = new TestEnclave({ userId, store } as LocalEnclaveOptions);
|
|
@@ -121,4 +256,65 @@ describe("LocalEnclave", () => {
|
|
|
121
256
|
expect(filtered[0]?.content).toBe("");
|
|
122
257
|
expect(passwordContextSpy).toHaveBeenCalledTimes(1);
|
|
123
258
|
});
|
|
259
|
+
|
|
260
|
+
it("wipes a live cached profile once the canonical public key rotates", async () => {
|
|
261
|
+
const store = new MemoryStore();
|
|
262
|
+
const enclave = new TestEnclave({ userId, store } as LocalEnclaveOptions);
|
|
263
|
+
const { keyPair } = await enclave.getPrivateEncryptionProfile();
|
|
264
|
+
|
|
265
|
+
await enclave.reconfigure({ expectedUserEncryptionPublicKey: base64Encode(keyPair.publicKey) });
|
|
266
|
+
expect((await enclave.getPrivateEncryptionProfile()).keyPair).toBe(keyPair);
|
|
267
|
+
|
|
268
|
+
await enclave.reconfigure({
|
|
269
|
+
expectedUserEncryptionPublicKey: base64Encode(new Uint8Array(32).fill(1)),
|
|
270
|
+
});
|
|
271
|
+
await expect(enclave.getPrivateEncryptionProfile()).rejects.toThrow(
|
|
272
|
+
"Derived encryption public key does not match expectedUserEncryptionPublicKey",
|
|
273
|
+
);
|
|
274
|
+
// Rotation reset before re-entry: the second prompt found an empty store.
|
|
275
|
+
expect(enclave.userIdPresentOnPrompt).toEqual([false, false]);
|
|
276
|
+
expect(await store.get(STORAGE_KEYS.OBFUSCATED_BASE64_ENCRYPTION_SECRET_KEY)).toBeUndefined();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("wipes a reloaded cached profile whose key no longer matches the canonical key", async () => {
|
|
280
|
+
const store = new MemoryStore();
|
|
281
|
+
await new TestEnclave({ userId, store } as LocalEnclaveOptions).getPrivateEncryptionProfile();
|
|
282
|
+
|
|
283
|
+
const reloaded = new TestEnclave({
|
|
284
|
+
userId,
|
|
285
|
+
store,
|
|
286
|
+
expectedUserEncryptionPublicKey: base64Encode(new Uint8Array(32).fill(1)),
|
|
287
|
+
} as LocalEnclaveOptions);
|
|
288
|
+
await reloaded.load();
|
|
289
|
+
|
|
290
|
+
await expect(reloaded.encrypt(new Uint8Array([1]), new Uint8Array(32))).rejects.toThrow(
|
|
291
|
+
"Derived encryption public key does not match expectedUserEncryptionPublicKey",
|
|
292
|
+
);
|
|
293
|
+
expect(reloaded.userIdPresentOnPrompt).toEqual([false]);
|
|
294
|
+
expect(await store.get(STORAGE_KEYS.OBFUSCATED_BASE64_ENCRYPTION_SECRET_KEY)).toBeUndefined();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("forgets a cached profile after the remember duration elapses", async () => {
|
|
298
|
+
vi.useFakeTimers({ toFake: ["Date"] });
|
|
299
|
+
|
|
300
|
+
try {
|
|
301
|
+
vi.setSystemTime(new Date("2026-01-01T00:00:00Z"));
|
|
302
|
+
|
|
303
|
+
const store = new MemoryStore();
|
|
304
|
+
const enclave = new TestEnclave({ userId, store } as LocalEnclaveOptions);
|
|
305
|
+
const first = await enclave.getPrivateEncryptionProfile();
|
|
306
|
+
|
|
307
|
+
expect(await enclave.getPrivateEncryptionProfile()).toBe(first);
|
|
308
|
+
|
|
309
|
+
vi.setSystemTime(new Date("2026-01-03T00:00:00Z"));
|
|
310
|
+
const after = await enclave.getPrivateEncryptionProfile();
|
|
311
|
+
|
|
312
|
+
expect(after).not.toBe(first);
|
|
313
|
+
expect(after.userId).toBe(userId);
|
|
314
|
+
// First prompt finds an empty store. The second must too: expiry resets before re-entry.
|
|
315
|
+
expect(enclave.userIdPresentOnPrompt).toEqual([false, false]);
|
|
316
|
+
} finally {
|
|
317
|
+
vi.useRealTimers();
|
|
318
|
+
}
|
|
319
|
+
});
|
|
124
320
|
});
|
package/src/local.ts
CHANGED
|
@@ -96,7 +96,7 @@ export class LocalEnclave<
|
|
|
96
96
|
async reset(): Promise<void> {
|
|
97
97
|
await super.reset();
|
|
98
98
|
this.storedEncryptionProfile = undefined;
|
|
99
|
-
this.store.reset();
|
|
99
|
+
await this.store.reset();
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
/** @override parent method to reconfigure the enclave */
|
|
@@ -266,26 +266,39 @@ export class LocalEnclave<
|
|
|
266
266
|
* @see BaseProvider#getPrivateEncryptionProfile
|
|
267
267
|
*/
|
|
268
268
|
async getPrivateEncryptionProfile(skipGuard = false): Promise<PrivateEncryptionProfile> {
|
|
269
|
-
//
|
|
270
|
-
|
|
271
|
-
|
|
269
|
+
// Remember duration is a key-use check. A long-lived iframe keeps this profile
|
|
270
|
+
// in memory, so construction-time store expiry is not enough.
|
|
271
|
+
if (this.storedEncryptionProfile && (await this.store.hasRememberDurationElapsed())) {
|
|
272
|
+
await this.reset();
|
|
273
|
+
}
|
|
272
274
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
275
|
+
// A cached key is only reusable while it still matches the canonical public key;
|
|
276
|
+
// after a rotation it falls through to the reset below and gets wiped.
|
|
277
|
+
const expectedPublicKey = this.options.expectedUserEncryptionPublicKey;
|
|
278
|
+
if (
|
|
279
|
+
this.storedEncryptionProfile?.userId === this.userId &&
|
|
280
|
+
(!expectedPublicKey ||
|
|
281
|
+
base64Encode(this.storedEncryptionProfile.keyPair.publicKey) === expectedPublicKey)
|
|
282
|
+
) {
|
|
283
|
+
// A matching cached profile still needs origin authorization before key use.
|
|
284
|
+
// Denying it must keep the profile and the allowlist; only a different user resets.
|
|
285
|
+
if (!skipGuard && !(await this.guardKeys())) {
|
|
286
|
+
throw new Error("Origin is not authorized to use the keys");
|
|
277
287
|
}
|
|
278
288
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
289
|
+
return this.storedEncryptionProfile;
|
|
290
|
+
}
|
|
282
291
|
|
|
283
|
-
|
|
284
|
-
// and reset the enclave.
|
|
292
|
+
if (this.storedEncryptionProfile) {
|
|
285
293
|
await this.reset();
|
|
286
294
|
}
|
|
287
295
|
|
|
288
|
-
|
|
296
|
+
if (this.options.encryptionPasswordStore === "mm") {
|
|
297
|
+
throw new Error(
|
|
298
|
+
"MM encryption profiles require an externally supplied encryption private key",
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
289
302
|
let password: string | undefined;
|
|
290
303
|
let encryptionPasswordStore: EncryptionPasswordStore | undefined;
|
|
291
304
|
|
|
@@ -310,7 +323,18 @@ export class LocalEnclave<
|
|
|
310
323
|
throw new Error("Password or encryption password store is not found");
|
|
311
324
|
}
|
|
312
325
|
|
|
313
|
-
|
|
326
|
+
const profile = await this.createEncryptionProfileFromPassword(
|
|
327
|
+
password,
|
|
328
|
+
this.userId,
|
|
329
|
+
encryptionPasswordStore,
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
// Same origin check as the cached path. Password or MPC success is not authorization.
|
|
333
|
+
if (!skipGuard && !(await this.guardKeys())) {
|
|
334
|
+
throw new Error("Origin is not authorized to use the keys");
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return profile;
|
|
314
338
|
}
|
|
315
339
|
|
|
316
340
|
/** @see BaseProvider#ensureUserEncryptionProfile */
|
|
@@ -351,6 +375,13 @@ export class LocalEnclave<
|
|
|
351
375
|
|
|
352
376
|
const keyPair = nacl.box.keyPair.fromSecretKey(secretKey);
|
|
353
377
|
|
|
378
|
+
const expectedPublicKey = this.options.expectedUserEncryptionPublicKey;
|
|
379
|
+
if (expectedPublicKey && base64Encode(keyPair.publicKey) !== expectedPublicKey) {
|
|
380
|
+
throw new Error(
|
|
381
|
+
"Derived encryption public key does not match expectedUserEncryptionPublicKey",
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
354
385
|
await this.store.set(STORAGE_KEYS.USER_ID, userId);
|
|
355
386
|
await this.storeObfuscated.set(STORAGE_KEYS.OBFUSCATED_PASSWORD, password);
|
|
356
387
|
await this.store.delete(STORAGE_KEYS.DEPRECATED___PASSWORD);
|