@le-space/orbitdb-identity-provider-webauthn-did 0.12.0 → 0.12.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/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # Changes
2
2
 
3
+ ## 0.12.2
4
+
5
+ A request that names a passkey asks that passkey. Until now this entry stood under 0.12.1, but
6
+ 0.12.1 was tagged before it merged and does not have it.
7
+
8
+ ### Fixed
9
+
10
+ - **A PRF output came from whichever passkey answered.** With discoverable credentials on (the
11
+ default), `buildCredentialRequestOptions` dropped the credential id it was given, so the browser
12
+ let any passkey of the relying party answer: two passkeys of one site in one keychain, or two
13
+ security keys plugged in. Every request that names a credential now carries it in
14
+ `allowCredentials`; discoverable credentials decide only what a request without one does.
15
+ `readPrfOutput` also checks which credential answered and refuses another one
16
+ (`WebAuthnAuthenticationError`), and the provider's own read for the signing key derives nothing
17
+ from it. Found in simple-todo's invoice01: a second passkey's vault slot was sealed under the
18
+ first one's PRF output, and the second passkey could not open it. Le-Space/belege has asked with
19
+ `discoverableCredentials: false` since its #429 for this reason; Le-Space/invoice and
20
+ simple-todo's archived01 ask without it, and are fixed by updating to 0.12.2.
21
+
22
+ Tested in `tests/prf-keys.test.js` with discoverable credentials on: a request names the credential
23
+ it asks, and another credential's answer is refused.
24
+
25
+ ## 0.12.1
26
+
27
+ A peer loads another peer's varsig identity from IPFS again, with Helia 7.
28
+
29
+ ### Fixed
30
+
31
+ - **`createIpfsIdentityStorage(ipfs).get` returns the block's bytes.** It returned whatever
32
+ `blockstore.get` gave, and Helia 7's blockstore (interface-blockstore 7) gives a generator of
33
+ chunks. Both callers decoded that as bytes, `getIdentity` in `createWebAuthnVarsigIdentities` and
34
+ the IPFS fallback in `wrapWithVarsigVerification`. The decode threw, their catch swallowed it, and
35
+ a peer that had not cached a varsig identity never loaded it from IPFS. `get` now joins the
36
+ chunks, still takes a blockstore that resolves to bytes, and waits at most 30 seconds for a block
37
+ the node does not have, as OrbitDB's own block storage does. The declaration says
38
+ `Promise<Uint8Array | undefined>` again.
39
+
40
+ Tested with two Helia 7 peers over libp2p and bitswap: the one without the identity loads it from
41
+ the other and verifies it, through either caller.
42
+
3
43
  ## 0.12.0
4
44
 
5
45
  The vault's keeper, once for every application, and the vault on its own entry.
package/README.md CHANGED
@@ -74,9 +74,14 @@ Behavior:
74
74
 
75
75
  - `discoverableCredentials: true`
76
76
  - registration requests resident/discoverable credentials
77
- - assertion requests omit `allowCredentials`
77
+ - an assertion that names no credential omits `allowCredentials`, and the
78
+ browser offers the passkeys it has
78
79
  - `discoverableCredentials: false`
79
- - assertion requests target a specific credential ID via `allowCredentials`
80
+ - every assertion names its credential
81
+ - Either way, an assertion that names a credential asks that one, in
82
+ `allowCredentials` (since 0.12.2). Before, discoverable mode dropped the
83
+ name, and another passkey of the site could answer — with its own PRF
84
+ output, which looks as random as the right one.
80
85
 
81
86
  ## Memory Keystore Quick Start
82
87
 
package/docs/API.md CHANGED
@@ -283,9 +283,14 @@ bytes that never come back.
283
283
  - `prfInput` — overrides the credential's. Leave it out when the output also
284
284
  goes to the identity provider, which asks with the credential's.
285
285
 
286
+ It asks this credential only (`allowCredentials`), and checks that this
287
+ credential answered: another passkey of the same site would answer with its own
288
+ PRF output, and every key derived from it would quietly be the wrong one.
289
+
286
290
  Throws `PrfUnavailableError` when the authenticator answers without a PRF
287
- output. A refused prompt stays the browser's `NotAllowedError`, so "the user
288
- said no" and "this authenticator has no PRF" can be told apart.
291
+ output, and `WebAuthnAuthenticationError` when another credential answers. A
292
+ refused prompt stays the browser's `NotAllowedError`, so "the user said no" and
293
+ "this authenticator has no PRF" can be told apart.
289
294
 
290
295
  #### `deriveSubkey(prfOutput, info, { length = 32 })`
291
296
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@le-space/orbitdb-identity-provider-webauthn-did",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "WebAuthn-based DID identity provider for OrbitDB for hardware-secured wallets and biometric Passkey authentication",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -36,7 +36,7 @@ import {
36
36
  import { CRYPTO_ALGORITHMS, KEY_TYPES } from '../constants.js';
37
37
  import { buildCredentialRequestOptions } from '../webauthn/config.js';
38
38
  import { prfInputForRelyingParty } from '../webauthn/prf-input.js';
39
- import { hkdfSha256 } from './prf-keys.js';
39
+ import { hkdfSha256, sameBytes } from './prf-keys.js';
40
40
 
41
41
  const log = logger('orbitdb-identity-provider-webauthn-did:derived-key');
42
42
 
@@ -145,6 +145,13 @@ export async function getPrfOutput(credential, { rpId } = {}) {
145
145
  })
146
146
  );
147
147
 
148
+ // Another passkey's PRF output would derive another passkey's key, under
149
+ // this DID: refused like an answer without PRF.
150
+ const answered = assertion?.rawId ? new Uint8Array(assertion.rawId) : null;
151
+ if (answered && !sameBytes(answered, rawCredentialId)) {
152
+ log('another credential answered; no key derived from it');
153
+ return null;
154
+ }
148
155
  const results = assertion?.getClientExtensionResults?.()?.prf?.results;
149
156
  if (!results?.first) {
150
157
  log('authenticator returned no PRF output');
@@ -20,7 +20,7 @@
20
20
  * the identity asks the passkey once.
21
21
  */
22
22
  import { CRYPTO_ALGORITHMS } from '../constants.js';
23
- import { PrfUnavailableError } from '../errors.js';
23
+ import { PrfUnavailableError, WebAuthnAuthenticationError } from '../errors.js';
24
24
  import { buildCredentialRequestOptions } from '../webauthn/config.js';
25
25
  import { prfInputForRelyingParty } from '../webauthn/prf-input.js';
26
26
 
@@ -39,6 +39,11 @@ function toBytes(value) {
39
39
  return undefined;
40
40
  }
41
41
 
42
+ /** @param {Uint8Array} a @param {Uint8Array} b */
43
+ export function sameBytes(a, b) {
44
+ return a.length === b.length && a.every((byte, i) => byte === b[i]);
45
+ }
46
+
42
47
  /**
43
48
  * HKDF-SHA-256 with an empty salt.
44
49
  *
@@ -113,8 +118,13 @@ function assertInfo(info, caller) {
113
118
  * @param {string} [options.rpId] - Relying party id; defaults to this page's
114
119
  * hostname.
115
120
  * @param {Uint8Array} [options.prfInput] - Overrides the credential's input.
121
+ * The request names the credential, and the answer is checked against it:
122
+ * another passkey of the same relying party would answer with its own PRF
123
+ * output, and every key derived from that would belong to the wrong one.
124
+ *
116
125
  * @returns {Promise<Uint8Array>} The PRF output, 32 bytes.
117
126
  * @throws {PrfUnavailableError} When the authenticator answers without one.
127
+ * @throws {WebAuthnAuthenticationError} When another credential answers.
118
128
  */
119
129
  export async function readPrfOutput(credential, { rpId, prfInput } = {}) {
120
130
  if (!credential || typeof credential !== 'object') {
@@ -140,6 +150,12 @@ export async function readPrfOutput(credential, { rpId, prfInput } = {}) {
140
150
  })
141
151
  );
142
152
 
153
+ const answered = toBytes(assertion?.rawId);
154
+ if (answered && !sameBytes(answered, rawCredentialId)) {
155
+ throw new WebAuthnAuthenticationError(
156
+ 'Another passkey answered than the one asked for its PRF output'
157
+ );
158
+ }
143
159
  const first = assertion?.getClientExtensionResults?.()?.prf?.results?.first;
144
160
  if (!first) {
145
161
  throw new PrfUnavailableError(
@@ -356,6 +356,32 @@ export async function verifyVarsigIdentity(identity, domainLabels = {}) {
356
356
  );
357
357
  }
358
358
 
359
+ // How long a lookup waits for an identity block this node does not hold:
360
+ // as long as OrbitDB's own block storage waits for any block.
361
+ const IDENTITY_BLOCK_TIMEOUT = 30000;
362
+
363
+ /**
364
+ * The bytes of a block, from whatever a blockstore's `get` gives. Helia 7's
365
+ * blockstore (interface-blockstore 7) yields them in chunks from a generator,
366
+ * and only iterating it fetches a block the node does not have; earlier ones
367
+ * resolve to the bytes.
368
+ * @param {*} result - What `blockstore.get` returned.
369
+ * @returns {Promise<Uint8Array|undefined>}
370
+ */
371
+ async function readBlockBytes(result) {
372
+ const value = await result;
373
+ if (value instanceof Uint8Array) return value;
374
+ if (
375
+ typeof value?.[Symbol.asyncIterator] === 'function' ||
376
+ typeof value?.[Symbol.iterator] === 'function'
377
+ ) {
378
+ const chunks = [];
379
+ for await (const chunk of value) chunks.push(chunk);
380
+ if (chunks.length > 0) return concat(chunks);
381
+ }
382
+ return undefined;
383
+ }
384
+
359
385
  /**
360
386
  * Create an IPFS blockstore adapter for identity storage.
361
387
  * @param {Object} ipfs - Helia/IPFS instance with a blockstore.
@@ -364,10 +390,18 @@ export async function verifyVarsigIdentity(identity, domainLabels = {}) {
364
390
  export function createIpfsIdentityStorage(ipfs) {
365
391
  if (!ipfs || !ipfs.blockstore) return null;
366
392
  return {
393
+ // Until 0.12.1 this returned what `blockstore.get` gave. With Helia 7
394
+ // that is a generator, which both callers then decoded as bytes: the
395
+ // decode threw, their catch swallowed it, and no varsig identity of
396
+ // another peer was ever loaded from IPFS.
367
397
  get: async (hash) => {
368
398
  try {
369
399
  const cid = CID.parse(hash);
370
- return await ipfs.blockstore.get(cid);
400
+ return await readBlockBytes(
401
+ ipfs.blockstore.get(cid, {
402
+ signal: AbortSignal.timeout(IDENTITY_BLOCK_TIMEOUT),
403
+ })
404
+ );
371
405
  } catch {
372
406
  return undefined;
373
407
  }
@@ -138,7 +138,14 @@ export function buildCredentialRequestOptions(options = {}) {
138
138
  ...(rpId ? { rpId } : {}),
139
139
  userVerification,
140
140
  ...(extensions ? { extensions } : {}),
141
- ...(!discoverableCredentials && credentialId
141
+ // A request that names a credential asks that credential. Discoverable
142
+ // credentials decide only what a request without one does: the browser
143
+ // offers the passkeys it has. Left to the browser, a request that named
144
+ // one was answered by whichever passkey the person picked or the
145
+ // authenticator chose — with that passkey's PRF output, which nothing can
146
+ // tell from the right one: a vault slot sealed under another key, a
147
+ // signing key derived from another passkey.
148
+ ...(credentialId
142
149
  ? {
143
150
  allowCredentials: [
144
151
  {
package/types/index.d.ts CHANGED
@@ -487,8 +487,11 @@ export function verifyVarsigIdentity(
487
487
  ): Promise<boolean>;
488
488
  /** `get` and `put` on the instance's blockstore; `null` without one. */
489
489
  export function createIpfsIdentityStorage(ipfs?: unknown): {
490
- /** Whatever the blockstore's `get` resolves to, `undefined` on failure. */
491
- get(hash: string): Promise<unknown>;
490
+ /**
491
+ * The block's bytes, from Helia 7's blockstore or an older one; `undefined`
492
+ * when it is not found within 30 seconds.
493
+ */
494
+ get(hash: string): Promise<Uint8Array | undefined>;
492
495
  put(hash: string, bytes: Uint8Array): Promise<void>;
493
496
  } | null;
494
497
  /** OrbitDB's identities, made to verify varsig entries as well. */