@johnhenry/browsermesh-core 0.2.2 → 0.2.4
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/package.json +1 -1
- package/src/group-keys.mjs +59 -4
- package/src/handshake.mjs +10 -0
- package/src/hardening.mjs +34 -4
package/package.json
CHANGED
package/src/group-keys.mjs
CHANGED
|
@@ -386,18 +386,73 @@ export class GroupKeyManager {
|
|
|
386
386
|
* Used when adding a member (existing key is fine, but new epoch
|
|
387
387
|
* ensures clean key lifecycle) or periodically for key freshness.
|
|
388
388
|
*
|
|
389
|
+
* By default this self-generates a fresh random key for the new epoch
|
|
390
|
+
* (unchanged, original behavior). Pass `{ epoch, key }` to instead
|
|
391
|
+
* *adopt* a specific, externally-agreed key rather than generating one —
|
|
392
|
+
* see the "Adopt-path calling convention" note below.
|
|
393
|
+
*
|
|
394
|
+
* ## Adopt-path calling convention
|
|
395
|
+
*
|
|
396
|
+
* This exists for callers that run a distributed agreement protocol
|
|
397
|
+
* (e.g. PBFT/consensus) over *when* to rotate and *who* the members are,
|
|
398
|
+
* without letting the actual key bytes anywhere near that agreement
|
|
399
|
+
* payload — broadcasting raw key material to every participant of a
|
|
400
|
+
* consensus round (rather than just the intended group members) would be
|
|
401
|
+
* a security regression relative to the per-recipient envelope
|
|
402
|
+
* mechanism this package already provides (`wrapKeyForMember` /
|
|
403
|
+
* `unwrapKeyForMember`, used by `broadcastDistribute()` /
|
|
404
|
+
* `wireTransport()`'s `GROUP_KEY_DISTRIBUTE` handler).
|
|
405
|
+
*
|
|
406
|
+
* The intended shape of that split:
|
|
407
|
+
* 1. Consensus agrees ONLY on `{ epoch, members }` — never key bytes.
|
|
408
|
+
* 2. Exactly one designated party (the proposer/leader) generates the
|
|
409
|
+
* real key and distributes it to each member individually via the
|
|
410
|
+
* existing secure envelope mechanism (`wrapKeyForMember` on the
|
|
411
|
+
* sender, `unwrapKeyForMember` — or the `GROUP_KEY_DISTRIBUTE`
|
|
412
|
+
* handler wired by `wireTransport()`, which calls
|
|
413
|
+
* `unwrapKeyForMember` internally — on each recipient).
|
|
414
|
+
* 3. Once a caller has BOTH the consensus-agreed epoch number AND the
|
|
415
|
+
* real `CryptoKey` obtained locally from that envelope decrypt (not
|
|
416
|
+
* exported/wire bytes — `unwrapKeyForMember` already hands back a
|
|
417
|
+
* live `CryptoKey`, the same shape `acceptEpoch()` accepts), it
|
|
418
|
+
* calls `rotate(members, { epoch, key })` to install that exact key
|
|
419
|
+
* as this manager's active state for that epoch, instead of calling
|
|
420
|
+
* plain `rotate(members)` (which would self-generate a different,
|
|
421
|
+
* non-agreed key).
|
|
422
|
+
*
|
|
423
|
+
* `key` is deliberately a `CryptoKey`, not exported/serialized bytes:
|
|
424
|
+
* this path is for in-process callers on the same JS runtime (e.g. a
|
|
425
|
+
* PBFT `applyBlock()` dispatch handing off to this manager) that already
|
|
426
|
+
* hold a real key locally — it is not a wire format and must never be
|
|
427
|
+
* put on the wire itself.
|
|
428
|
+
*
|
|
389
429
|
* @param {string[]} [newMembers] - Updated member list; defaults to current members
|
|
430
|
+
* @param {object} [adopt] - Omit entirely for today's self-generating behavior.
|
|
431
|
+
* @param {number} [adopt.epoch] - Externally-agreed epoch number to adopt. Must be
|
|
432
|
+
* provided together with `adopt.key`, never alone.
|
|
433
|
+
* @param {CryptoKey} [adopt.key] - Externally-agreed key to adopt in-process (e.g. from
|
|
434
|
+
* `unwrapKeyForMember`/an envelope decrypt), rather than generating a new one. Must be
|
|
435
|
+
* provided together with `adopt.epoch`, never alone.
|
|
390
436
|
* @returns {Promise<GroupState>}
|
|
391
437
|
*/
|
|
392
|
-
async rotate(newMembers) {
|
|
438
|
+
async rotate(newMembers, { epoch, key } = {}) {
|
|
439
|
+
const hasEpoch = epoch !== undefined
|
|
440
|
+
const hasKey = key !== undefined
|
|
441
|
+
if (hasEpoch !== hasKey) {
|
|
442
|
+
throw new Error('rotate: epoch and key must be provided together (both or neither)')
|
|
443
|
+
}
|
|
444
|
+
if (hasKey && !(typeof CryptoKey !== 'undefined' && key instanceof CryptoKey)) {
|
|
445
|
+
throw new Error('rotate: key must be a CryptoKey (e.g. from unwrapKeyForMember/an envelope decrypt), not exported bytes')
|
|
446
|
+
}
|
|
447
|
+
|
|
393
448
|
const current = this.getCurrentState()
|
|
394
449
|
const members = newMembers || (current ? current.members : [this.#localPodId])
|
|
395
450
|
|
|
396
|
-
const nextEpoch = this.#currentEpoch + 1
|
|
397
|
-
const
|
|
451
|
+
const nextEpoch = hasEpoch ? epoch : this.#currentEpoch + 1
|
|
452
|
+
const nextKey = hasKey ? key : await this.#generateKey()
|
|
398
453
|
const state = new GroupState({
|
|
399
454
|
epoch: nextEpoch,
|
|
400
|
-
key,
|
|
455
|
+
key: nextKey,
|
|
401
456
|
members,
|
|
402
457
|
})
|
|
403
458
|
|
package/src/handshake.mjs
CHANGED
|
@@ -816,6 +816,16 @@ export class HandshakeCoordinator {
|
|
|
816
816
|
])
|
|
817
817
|
clearTimeout(timeoutHandle)
|
|
818
818
|
|
|
819
|
+
// TransportFactory.negotiate() returns a transport that has been
|
|
820
|
+
// created but not yet connected (see its docstring) -- no offer,
|
|
821
|
+
// answer or ICE candidate has crossed the wire yet. Mirror
|
|
822
|
+
// acceptConnection()'s `await transport.handleOffer(offer)` on the
|
|
823
|
+
// answerer side by actually driving the handshake here before this
|
|
824
|
+
// session is adopted as 'connected'.
|
|
825
|
+
if (typeof transport.connect === 'function') {
|
|
826
|
+
await transport.connect()
|
|
827
|
+
}
|
|
828
|
+
|
|
819
829
|
const sessionInfo = {
|
|
820
830
|
localPodId: this.#localPodId,
|
|
821
831
|
remotePodId,
|
package/src/hardening.mjs
CHANGED
|
@@ -540,9 +540,16 @@ export class ConnectionPool {
|
|
|
540
540
|
*
|
|
541
541
|
* @param {string} peerId
|
|
542
542
|
* @param {object} transport
|
|
543
|
+
* @param {object} [opts]
|
|
544
|
+
* @param {*} [opts.purpose] - Opaque tag (issue #116) recorded alongside
|
|
545
|
+
* this transport so a later `acquire(peerId, { purpose })` can request
|
|
546
|
+
* "the connection for purpose X" specifically, instead of getting
|
|
547
|
+
* whichever idle entry happens to be first. Untagged (the default) if
|
|
548
|
+
* omitted -- only matches an `acquire()` call that also omits `purpose`/
|
|
549
|
+
* `select`, same as every call before this option existed.
|
|
543
550
|
* @returns {boolean} true if added successfully
|
|
544
551
|
*/
|
|
545
|
-
add(peerId, transport) {
|
|
552
|
+
add(peerId, transport, { purpose } = {}) {
|
|
546
553
|
if (!this.#pools.has(peerId)) {
|
|
547
554
|
this.#pools.set(peerId, []);
|
|
548
555
|
}
|
|
@@ -564,6 +571,7 @@ export class ConnectionPool {
|
|
|
564
571
|
transport,
|
|
565
572
|
lastUsed: this.#nowFn(),
|
|
566
573
|
acquired: false,
|
|
574
|
+
purpose: purpose ?? null,
|
|
567
575
|
});
|
|
568
576
|
|
|
569
577
|
return true;
|
|
@@ -571,16 +579,37 @@ export class ConnectionPool {
|
|
|
571
579
|
|
|
572
580
|
/**
|
|
573
581
|
* Acquire an idle connection for a peer. Marks it as acquired.
|
|
574
|
-
* Returns null if no idle connections
|
|
582
|
+
* Returns null if no idle (matching, if a selector is given) connections
|
|
583
|
+
* exist.
|
|
584
|
+
*
|
|
585
|
+
* Without a selector, returns the first idle entry -- opaque round-robin
|
|
586
|
+
* reuse, exactly the pre-#116 behavior. With one, only an idle entry that
|
|
587
|
+
* matches is eligible, which is what makes `add()`'s `purpose` tag (or any
|
|
588
|
+
* property of the transport itself) actually useful for more than that:
|
|
589
|
+
* a caller that pooled e.g. a "bulk" and a "control" connection can now
|
|
590
|
+
* `acquire(peerId, { purpose: 'control' })` and reliably get that one
|
|
591
|
+
* back rather than whichever of the two happened to be idle first.
|
|
575
592
|
*
|
|
576
593
|
* @param {string} peerId
|
|
594
|
+
* @param {object} [opts]
|
|
595
|
+
* @param {*} [opts.purpose] - Only consider idle entries `add()` tagged
|
|
596
|
+
* with this exact `purpose` (`Object.is` comparison).
|
|
597
|
+
* @param {(transport: object, purpose: *) => boolean} [opts.select] -
|
|
598
|
+
* Only consider idle entries for which this returns true. Combined with
|
|
599
|
+
* `purpose` (both must pass) when both are given, so a caller can filter
|
|
600
|
+
* by purpose and by the transport's own state/shape at once.
|
|
577
601
|
* @returns {object|null} The transport, or null
|
|
578
602
|
*/
|
|
579
|
-
acquire(peerId) {
|
|
603
|
+
acquire(peerId, { purpose, select } = {}) {
|
|
580
604
|
const entries = this.#pools.get(peerId);
|
|
581
605
|
if (!entries) return null;
|
|
582
606
|
|
|
583
|
-
const entry = entries.find(e =>
|
|
607
|
+
const entry = entries.find((e) => {
|
|
608
|
+
if (e.acquired) return false;
|
|
609
|
+
if (purpose !== undefined && !Object.is(e.purpose, purpose)) return false;
|
|
610
|
+
if (typeof select === 'function' && !select(e.transport, e.purpose)) return false;
|
|
611
|
+
return true;
|
|
612
|
+
});
|
|
584
613
|
if (!entry) return null;
|
|
585
614
|
|
|
586
615
|
entry.acquired = true;
|
|
@@ -724,6 +753,7 @@ export class ConnectionPool {
|
|
|
724
753
|
acquired: e.acquired,
|
|
725
754
|
lastUsed: e.lastUsed,
|
|
726
755
|
transportType: e.transport.type ?? 'unknown',
|
|
756
|
+
purpose: e.purpose ?? null,
|
|
727
757
|
}));
|
|
728
758
|
}
|
|
729
759
|
return {
|