@bom-pi/core 0.1.4 → 0.2.1
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 +8 -3
- package/src/cloud-connector.ts +4 -3
- package/src/cloud-wss-transport.ts +388 -33
- package/src/pairing-storage.ts +6 -1
- package/src/relay-boundary.ts +292 -3
- package/src/relay-mux-wire.ts +105 -0
- package/src/relay-remote-channel.ts +482 -0
- package/src/relay-remote-dispatch.ts +227 -0
- package/src/relay-secure-channel.ts +325 -0
- package/src/relay-secure-target.ts +592 -0
- package/src/session-gateway.ts +129 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bom-pi/core",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Core pairing machinery for Bom Pi: ceremonies, transports, durable stores",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,8 +15,13 @@
|
|
|
15
15
|
"./ingress-receiver.ts": "./src/ingress-receiver.ts",
|
|
16
16
|
"./lan-pairing.ts": "./src/lan-pairing.ts",
|
|
17
17
|
"./pairing-storage.ts": "./src/pairing-storage.ts",
|
|
18
|
+
"./relay-boundary.ts": "./src/relay-boundary.ts",
|
|
19
|
+
"./relay-mux-wire.ts": "./src/relay-mux-wire.ts",
|
|
20
|
+
"./relay-remote-channel.ts": "./src/relay-remote-channel.ts",
|
|
21
|
+
"./relay-remote-dispatch.ts": "./src/relay-remote-dispatch.ts",
|
|
22
|
+
"./relay-secure-channel.ts": "./src/relay-secure-channel.ts",
|
|
23
|
+
"./relay-secure-target.ts": "./src/relay-secure-target.ts",
|
|
18
24
|
"./secure-transport.ts": "./src/secure-transport.ts",
|
|
19
|
-
"./session-gateway.ts": "./src/session-gateway.ts"
|
|
20
|
-
"./relay-boundary.ts": "./src/relay-boundary.ts"
|
|
25
|
+
"./session-gateway.ts": "./src/session-gateway.ts"
|
|
21
26
|
}
|
|
22
27
|
}
|
package/src/cloud-connector.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
type KeyObject,
|
|
13
13
|
} from "node:crypto";
|
|
14
14
|
import { canonicalJson, parseCanonicalJson, type CanonicalValue } from "@bom-pi/protocol/codec.ts";
|
|
15
|
+
import { CLOUD_RECEIPT_DOMAIN } from "@bom-pi/protocol/receipt.ts";
|
|
15
16
|
import { DAEMON_PROOF_DOMAIN } from "@bom-pi/protocol/daemon-proof.ts";
|
|
16
17
|
import { fingerprintPublicKey } from "@bom-pi/protocol/identity.ts";
|
|
17
18
|
|
|
@@ -130,9 +131,9 @@ export type ConnectorProof = {
|
|
|
130
131
|
// (duplicated literal: packages must not import apps). Leader owns single-sourcing this constant.
|
|
131
132
|
const CLEANUP_REQUEST_DOMAIN = "bom-pi/connector/cleanup-request/v1";
|
|
132
133
|
|
|
133
|
-
//
|
|
134
|
-
// (
|
|
135
|
-
const RECEIPT_DOMAIN =
|
|
134
|
+
// REL-13: the X-receipt domain literal is single-sourced in @bom-pi/protocol/receipt.ts
|
|
135
|
+
// (imported above) and aliased here so the existing uses stay unchanged.
|
|
136
|
+
const RECEIPT_DOMAIN = CLOUD_RECEIPT_DOMAIN;
|
|
136
137
|
|
|
137
138
|
/** §16 candidate resource limit: durable cleanup capacity reserved for active grants/memberships.
|
|
138
139
|
* Admission (enroll) refuses when this reserve cannot be guaranteed; deletion/removal is NEVER
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
* `onRelayClosed`). The channel is memory-only: it dies with the connection and nothing about it
|
|
44
44
|
* persists. */
|
|
45
45
|
import { createPublicKey, randomBytes, verify, type KeyObject } from "node:crypto";
|
|
46
|
+
import { CLOUD_RECEIPT_DOMAIN } from "@bom-pi/protocol/receipt.ts";
|
|
46
47
|
import {
|
|
47
48
|
canonicalJson,
|
|
48
49
|
MAX_CONTROL_BYTES,
|
|
@@ -51,6 +52,7 @@ import {
|
|
|
51
52
|
type CanonicalValue,
|
|
52
53
|
} from "@bom-pi/protocol/codec.ts";
|
|
53
54
|
import { fingerprintPublicKey } from "@bom-pi/protocol/identity.ts";
|
|
55
|
+
import { encodeMuxFrame, MUX_CHANNEL_ID_HEX, parseMuxFrame } from "./relay-mux-wire.ts";
|
|
54
56
|
import type { IngressClass } from "./ingress-receiver.ts";
|
|
55
57
|
import {
|
|
56
58
|
ConnectorStateError,
|
|
@@ -69,9 +71,8 @@ import {
|
|
|
69
71
|
type StoredCleanupRequest,
|
|
70
72
|
} from "./cloud-connector.ts";
|
|
71
73
|
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
const CLOUD_RECEIPT_DOMAIN = "bom-pi/cloud-receipt/v1";
|
|
74
|
+
// REL-13: the cloud-receipt domain literal is single-sourced in
|
|
75
|
+
// @bom-pi/protocol/receipt.ts and imported above; this module only consumes it.
|
|
75
76
|
|
|
76
77
|
const WIRE_VERSION = 1;
|
|
77
78
|
const TYPE_CONTROL = "control";
|
|
@@ -408,6 +409,19 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
408
409
|
private relayDataListener: ((payload: Uint8Array) => void) | null = null;
|
|
409
410
|
private relayClosedCallbacks: ((notice: RelayClosedNotice) => void)[] = [];
|
|
410
411
|
|
|
412
|
+
/* ---------------- D2 SEC-13: the multichannel (mux) relay state ---------------- */
|
|
413
|
+
|
|
414
|
+
/** Cached {mux:1} capability probe for THIS connection; undefined = not probed yet,
|
|
415
|
+
* null = probed and the pinned cloud does not advertise mux (fail-closed to 1 channel). */
|
|
416
|
+
private muxCapsValue: { maxChannels: number } | null | undefined = undefined;
|
|
417
|
+
/** Requester-side open mux channel ids on THIS connection (X-minted, memory-only). */
|
|
418
|
+
private readonly relayMuxRequesterChannels = new Set<string>();
|
|
419
|
+
/** Target-side adopted mux channel ids (A's per-channel demux table, memory-only). */
|
|
420
|
+
private readonly relayTargetChannels = new Set<string>();
|
|
421
|
+
/** THE inbound mux-frame listener (one per connection; a second registration refuses). */
|
|
422
|
+
private relayMuxDataListener:
|
|
423
|
+
((frame: { channelId: string; payload: Uint8Array }) => void) | null = null;
|
|
424
|
+
|
|
411
425
|
constructor(input: { pinned: PinnedCloud; options?: CloudWssTransportOptions }) {
|
|
412
426
|
this.pinned = input.pinned;
|
|
413
427
|
this.options = input.options ?? {};
|
|
@@ -480,7 +494,13 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
480
494
|
async relayOpen(claim: RelayOpenClaim): Promise<string> {
|
|
481
495
|
this.requireOpenSocket();
|
|
482
496
|
|
|
483
|
-
|
|
497
|
+
// SEC-13 mode isolation: a transport holding mux channels never falls back to the
|
|
498
|
+
// legacy single-channel lane (and vice versa) — refuse locally, no frame leaves.
|
|
499
|
+
if (
|
|
500
|
+
this.relayChannelIdValue !== null ||
|
|
501
|
+
this.relayMuxRequesterChannels.size > 0 ||
|
|
502
|
+
this.relayTargetChannels.size > 0
|
|
503
|
+
) {
|
|
484
504
|
throw new ConnectorStateError("Relay channel already open on this transport");
|
|
485
505
|
}
|
|
486
506
|
|
|
@@ -564,10 +584,22 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
564
584
|
this.relayDataListener = listener;
|
|
565
585
|
}
|
|
566
586
|
|
|
567
|
-
/** Registers a relay-close callback; fires exactly once
|
|
568
|
-
*
|
|
569
|
-
|
|
570
|
-
|
|
587
|
+
/** Registers a relay-close callback; fires exactly once per torn-down channel (peer
|
|
588
|
+
* notification, local close, or connection loss). Default: refuses while no channel is
|
|
589
|
+
* open (the frozen §16 seam contract). `early: true` opts into connection-scoped
|
|
590
|
+
* registration BEFORE any mux channel opens — for wirings (RemoteChannel.open) whose
|
|
591
|
+
* channel may close between the relay-open reply and their listener attachment. */
|
|
592
|
+
onRelayClosed(
|
|
593
|
+
callback: (notice: RelayClosedNotice) => void,
|
|
594
|
+
options: { early?: boolean } = {},
|
|
595
|
+
): void {
|
|
596
|
+
if (
|
|
597
|
+
options.early !== true &&
|
|
598
|
+
this.relayChannelIdValue === null &&
|
|
599
|
+
!this.relayTargetActive &&
|
|
600
|
+
this.relayMuxRequesterChannels.size === 0 &&
|
|
601
|
+
this.relayTargetChannels.size === 0
|
|
602
|
+
) {
|
|
571
603
|
throw new ConnectorStateError("No relay channel is open");
|
|
572
604
|
}
|
|
573
605
|
|
|
@@ -593,14 +625,246 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
593
625
|
this.disposeRelayChannel("peer-closed");
|
|
594
626
|
}
|
|
595
627
|
|
|
628
|
+
/* ------------- D2 SEC-13: the multichannel (mux) relay surface (additive) ------------- */
|
|
629
|
+
|
|
630
|
+
/** The per-connection mux capability: probed once with the authenticated {mux:1}
|
|
631
|
+
* relay-caps; an old cloud (or any probe failure) reads null and every mux open fails
|
|
632
|
+
* closed with a static error — exactly the legacy fail-closed pattern, no relay-open
|
|
633
|
+
* frame ever leaves for a cloud that did not advertise mux. */
|
|
634
|
+
async relayMuxCaps(): Promise<{ maxChannels: number } | null> {
|
|
635
|
+
if (this.muxCapsValue !== undefined) {
|
|
636
|
+
return this.muxCapsValue;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
this.requireOpenSocket();
|
|
640
|
+
|
|
641
|
+
let caps: CanonicalValue;
|
|
642
|
+
|
|
643
|
+
try {
|
|
644
|
+
caps = await this.rpc(METHOD_RELAY_CAPS, { d: { mux: 1 } });
|
|
645
|
+
} catch {
|
|
646
|
+
this.muxCapsValue = null;
|
|
647
|
+
|
|
648
|
+
return null;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (
|
|
652
|
+
!isPlainObject(caps) ||
|
|
653
|
+
!exactKeys(caps, ["relay", "mux", "maxChannels"]) ||
|
|
654
|
+
caps.relay !== 1 ||
|
|
655
|
+
caps.mux !== 1 ||
|
|
656
|
+
typeof caps.maxChannels !== "number" ||
|
|
657
|
+
!Number.isSafeInteger(caps.maxChannels) ||
|
|
658
|
+
caps.maxChannels < 1 ||
|
|
659
|
+
caps.maxChannels > 4
|
|
660
|
+
) {
|
|
661
|
+
this.muxCapsValue = null;
|
|
662
|
+
|
|
663
|
+
return null;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
this.muxCapsValue = { maxChannels: caps.maxChannels };
|
|
667
|
+
|
|
668
|
+
return this.muxCapsValue;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/** SEC-13: opens one E2EE mux channel toward the relation target. The claim is the exact
|
|
672
|
+
* §11/§12 routing claim; `clientHello` is this channel's opaque §21 handshake first
|
|
673
|
+
* message, which X forwards inside the relay-open delivery — never parsed here or at X.
|
|
674
|
+
* Returns X's minted (unprivileged) channel id plus the target's opaque serverHello. A
|
|
675
|
+
* cloud without mux, a held legacy channel on this transport, or the per-endpoint cap
|
|
676
|
+
* refuse closed with static errors before any frame leaves. */
|
|
677
|
+
async relayOpenE2ee(
|
|
678
|
+
claim: RelayOpenClaim,
|
|
679
|
+
clientHello: Uint8Array,
|
|
680
|
+
): Promise<{ channelId: string; serverHello: Uint8Array }> {
|
|
681
|
+
this.requireOpenSocket();
|
|
682
|
+
|
|
683
|
+
if (this.relayChannelIdValue !== null || this.relayTargetActive) {
|
|
684
|
+
throw new ConnectorStateError("Relay channel already open on this transport");
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
const caps = await this.relayMuxCaps();
|
|
688
|
+
|
|
689
|
+
if (caps === null) {
|
|
690
|
+
throw new ConnectorStateError(
|
|
691
|
+
"Relay mux capability not advertised by the cloud (old cloud or relay lane absent)",
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (this.relayMuxRequesterChannels.size >= caps.maxChannels) {
|
|
696
|
+
throw new ConnectorStateError(
|
|
697
|
+
"Relay channel capacity reached on this transport (SEC-13 mux cap)",
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const hello =
|
|
702
|
+
clientHello instanceof Uint8Array
|
|
703
|
+
? Buffer.from(clientHello).toString("base64url")
|
|
704
|
+
: null;
|
|
705
|
+
|
|
706
|
+
if (hello === null || hello.length === 0 || hello.length > 12_000) {
|
|
707
|
+
throw new ConnectorStateError("Invalid relay handshake hello");
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const claimBody = relayClaimToWire(claim);
|
|
711
|
+
const result = await this.rpc(METHOD_RELAY_OPEN, {
|
|
712
|
+
d: { ...claimBody, e2ee: 1, hello },
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
if (
|
|
716
|
+
!isPlainObject(result) ||
|
|
717
|
+
!exactKeys(result, ["channel", "hello"]) ||
|
|
718
|
+
typeof result.channel !== "string" ||
|
|
719
|
+
!MUX_CHANNEL_ID_HEX.test(result.channel) ||
|
|
720
|
+
typeof result.hello !== "string" ||
|
|
721
|
+
result.hello.length === 0 ||
|
|
722
|
+
result.hello.length > 12_000
|
|
723
|
+
) {
|
|
724
|
+
throw new ConnectorStateError("Malformed mux relay-open result from cloud");
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const serverHello = decodeBase64urlStrict(result.hello);
|
|
728
|
+
|
|
729
|
+
if (serverHello === null || serverHello.length === 0) {
|
|
730
|
+
throw new ConnectorStateError("Malformed mux relay-open result from cloud");
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
this.relayMuxRequesterChannels.add(result.channel);
|
|
734
|
+
|
|
735
|
+
return { channelId: result.channel, serverHello };
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/** The requester-side open mux channel ids (memory-only, in open order). */
|
|
739
|
+
relayMuxChannelIds(): readonly string[] {
|
|
740
|
+
return [...this.relayMuxRequesterChannels];
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/** Sends one §16-bounded mux-framed binary body on an open mux channel; oversize or
|
|
744
|
+
* empty frames are refused locally BEFORE any write (the 17-byte mux header counts
|
|
745
|
+
* toward the whole-frame bound), and the outbound backpressure bound applies exactly
|
|
746
|
+
* like every other send. */
|
|
747
|
+
relaySendChannel(channelId: string, payload: Uint8Array): void {
|
|
748
|
+
const ws = this.requireOpenSocket();
|
|
749
|
+
|
|
750
|
+
if (
|
|
751
|
+
!MUX_CHANNEL_ID_HEX.test(channelId) ||
|
|
752
|
+
(!this.relayMuxRequesterChannels.has(channelId) &&
|
|
753
|
+
!this.relayTargetChannels.has(channelId))
|
|
754
|
+
) {
|
|
755
|
+
throw new ConnectorStateError("No such mux relay channel is open");
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const bytes = payload instanceof Uint8Array ? payload : null;
|
|
759
|
+
|
|
760
|
+
if (bytes === null || bytes.length === 0) {
|
|
761
|
+
throw new ConnectorStateError("Relay data frames must not be empty");
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
const frame = encodeMuxFrame(channelId, bytes);
|
|
765
|
+
|
|
766
|
+
if (frame.length > MAX_DATA_FRAME_BYTES) {
|
|
767
|
+
throw new ConnectorStateError(
|
|
768
|
+
`Relay data frame exceeds the ${MAX_DATA_FRAME_BYTES}-byte whole-frame wire bound; refused before send`,
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
if (ws.bufferedAmount + frame.length > this.maxBufferedBytes) {
|
|
773
|
+
throw new EndpointError(503, "Cloud WSS outbound backpressure bound reached");
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
try {
|
|
777
|
+
// A fresh ArrayBuffer-backed view (same as the legacy lane): WebSocket.send
|
|
778
|
+
// requires a BufferSource over a plain ArrayBuffer, with the one bounded copy.
|
|
779
|
+
ws.send(Uint8Array.from(frame));
|
|
780
|
+
} catch {
|
|
781
|
+
throw new EndpointError(503, "Cloud WSS send failed");
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/** Cooperative per-channel teardown (mux): best-effort RPC, local state clears either
|
|
786
|
+
* way, and the registered close callbacks fire exactly once with this channel id. */
|
|
787
|
+
async relayCloseChannel(channelId: string): Promise<void> {
|
|
788
|
+
if (!this.relayMuxRequesterChannels.has(channelId)) {
|
|
789
|
+
throw new ConnectorStateError("No such mux relay channel is open");
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
try {
|
|
793
|
+
await this.rpc(METHOD_RELAY_CLOSE, { d: { channel: channelId } });
|
|
794
|
+
} catch {
|
|
795
|
+
// X may already have disposed the channel; local teardown proceeds fail-closed.
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
this.dropMuxChannel(channelId, "peer-closed");
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/** Target-side (A) mux adoption: X delivered a relay-open(e2ee) for this channel id, so
|
|
802
|
+
* this connection now demultiplexes inbound mux frames for it. A duplicate or malformed
|
|
803
|
+
* id refuses; adoption carries no authorization (the boundary already decided at open). */
|
|
804
|
+
adoptRelayTargetChannel(channelId: string): void {
|
|
805
|
+
if (!MUX_CHANNEL_ID_HEX.test(channelId)) {
|
|
806
|
+
throw new ConnectorStateError("Malformed mux channel id");
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
if (this.relayTargetChannels.has(channelId)) {
|
|
810
|
+
throw new ConnectorStateError("Mux relay channel already adopted");
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
this.relayTargetChannels.add(channelId);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/** Target-side per-channel release; safe when nothing is adopted. */
|
|
817
|
+
releaseRelayTargetChannel(channelId: string): void {
|
|
818
|
+
if (this.relayTargetChannels.has(channelId)) {
|
|
819
|
+
this.dropMuxChannel(channelId, "peer-closed");
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/** Registers THE inbound mux-frame listener (one per connection; a second registration
|
|
824
|
+
* refuses — ambiguity about who consumes relayed frames is what fail-closed wiring must
|
|
825
|
+
* not allow). Frames arriving with no listener are refused and counted, never buffered. */
|
|
826
|
+
onRelayMuxData(listener: (frame: { channelId: string; payload: Uint8Array }) => void): void {
|
|
827
|
+
this.requireOpenSocket();
|
|
828
|
+
|
|
829
|
+
if (this.relayMuxDataListener !== null) {
|
|
830
|
+
throw new ConnectorStateError("Mux relay data listener already registered");
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// Connection-scoped registration: it may happen BEFORE any mux channel opens (the
|
|
834
|
+
// first channel's handshake reply can race the caller's wiring); frames arriving
|
|
835
|
+
// with no OPEN channel stay refused by the demux, never buffered.
|
|
836
|
+
this.relayMuxDataListener = listener;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
/** Clears one mux channel locally and fires the close callbacks exactly once for it. */
|
|
840
|
+
private dropMuxChannel(channelId: string, reason: RelayCloseReason): void {
|
|
841
|
+
const wasRequester = this.relayMuxRequesterChannels.delete(channelId);
|
|
842
|
+
const wasTarget = this.relayTargetChannels.delete(channelId);
|
|
843
|
+
|
|
844
|
+
if (!wasRequester && !wasTarget) {
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
for (const callback of [...this.relayClosedCallbacks]) {
|
|
849
|
+
try {
|
|
850
|
+
callback({ channelId, reason });
|
|
851
|
+
} catch {
|
|
852
|
+
// A close listener must not break teardown; the channel is gone either way.
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
596
857
|
/** Tears the channel down exactly once: both listener slots cleared, every registered close
|
|
597
858
|
* callback fired with the static reason. A no-op when no channel is open — which includes
|
|
598
859
|
* the TARGET-side adoption (A never holds a channel id; X routes raw frames), so the
|
|
599
|
-
* guard must consider BOTH the requester channel and the target flag.
|
|
860
|
+
* guard must consider BOTH the requester channel and the target flag. Connection loss
|
|
861
|
+
* also disposes every open mux channel, each with its own exactly-once notification. */
|
|
600
862
|
private disposeRelayChannel(reason: RelayCloseReason): void {
|
|
601
863
|
const channelId = this.relayChannelIdValue;
|
|
602
864
|
|
|
603
865
|
if (channelId === null && !this.relayTargetActive) {
|
|
866
|
+
this.disposeMuxChannels(reason);
|
|
867
|
+
|
|
604
868
|
return;
|
|
605
869
|
}
|
|
606
870
|
|
|
@@ -618,6 +882,28 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
618
882
|
// way, and the connection-level onClosed path is unaffected.
|
|
619
883
|
}
|
|
620
884
|
}
|
|
885
|
+
|
|
886
|
+
this.disposeMuxChannels(reason);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/** Connection-loss teardown of every open mux channel (requester and target side), each
|
|
890
|
+
* close callback firing exactly once with its own channel id. */
|
|
891
|
+
private disposeMuxChannels(reason: RelayCloseReason): void {
|
|
892
|
+
const ids = [...this.relayMuxRequesterChannels, ...this.relayTargetChannels];
|
|
893
|
+
|
|
894
|
+
this.relayMuxRequesterChannels.clear();
|
|
895
|
+
this.relayTargetChannels.clear();
|
|
896
|
+
this.relayMuxDataListener = null;
|
|
897
|
+
|
|
898
|
+
for (const id of ids) {
|
|
899
|
+
for (const callback of [...this.relayClosedCallbacks]) {
|
|
900
|
+
try {
|
|
901
|
+
callback({ channelId: id, reason });
|
|
902
|
+
} catch {
|
|
903
|
+
// See disposeRelayChannel: a listener must not break teardown.
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
}
|
|
621
907
|
}
|
|
622
908
|
|
|
623
909
|
/**
|
|
@@ -1201,8 +1487,10 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
1201
1487
|
* close settles close(), everything else is connection loss and fires onClosed exactly once. */
|
|
1202
1488
|
private adoptClosed(): void {
|
|
1203
1489
|
this.failPending(new EndpointError(503, "Cloud connection lost"));
|
|
1204
|
-
// The relay channel (if any) dies with the connection, before the loss callbacks fire
|
|
1490
|
+
// The relay channel (if any) dies with the connection, before the loss callbacks fire;
|
|
1491
|
+
// every open mux channel dies with it (SEC-13), each with its own notification.
|
|
1205
1492
|
this.disposeRelayChannel("session-ended");
|
|
1493
|
+
this.muxCapsValue = undefined;
|
|
1206
1494
|
this.state = "closed";
|
|
1207
1495
|
|
|
1208
1496
|
const ws = this.ws;
|
|
@@ -1277,11 +1565,24 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
1277
1565
|
envelope.m === METHOD_PAIR_DELIVER &&
|
|
1278
1566
|
isPlainObject(payload) &&
|
|
1279
1567
|
payload.type === "relay-close" &&
|
|
1280
|
-
this.relayChannelIdValue !== null &&
|
|
1281
|
-
payload.channel === this.relayChannelIdValue &&
|
|
1282
1568
|
isRelayCloseReason(payload.reason)
|
|
1283
1569
|
) {
|
|
1284
|
-
|
|
1570
|
+
// The legacy single channel closes through its own dispose; a MUX channel
|
|
1571
|
+
// (SEC-13) closes through the same bounded notice — dropped here so the
|
|
1572
|
+
// per-channel close event fires even with no delivery handler wired, and
|
|
1573
|
+
// the connection itself stays intact for the remaining channels.
|
|
1574
|
+
if (
|
|
1575
|
+
typeof payload.channel === "string" &&
|
|
1576
|
+
this.relayChannelIdValue !== null &&
|
|
1577
|
+
payload.channel === this.relayChannelIdValue
|
|
1578
|
+
) {
|
|
1579
|
+
this.disposeRelayChannel(payload.reason);
|
|
1580
|
+
} else if (
|
|
1581
|
+
typeof payload.channel === "string" &&
|
|
1582
|
+
this.relayMuxRequesterChannels.has(payload.channel)
|
|
1583
|
+
) {
|
|
1584
|
+
this.dropMuxChannel(payload.channel, payload.reason);
|
|
1585
|
+
}
|
|
1285
1586
|
}
|
|
1286
1587
|
|
|
1287
1588
|
if (
|
|
@@ -1318,7 +1619,19 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
1318
1619
|
}
|
|
1319
1620
|
|
|
1320
1621
|
const successShape = exactKeys(envelope, ["v", "k", "id", "r"]);
|
|
1321
|
-
const failureShape = exactKeys(envelope, ["v", "k", "id", "s"], ["e", "rc"]);
|
|
1622
|
+
const failureShape = exactKeys(envelope, ["v", "k", "id", "s"], ["e", "rc", "retryable"]);
|
|
1623
|
+
|
|
1624
|
+
// SEC-13: the one failure extra the relay may send is the retryable flag — but only
|
|
1625
|
+
// as canonical 1 on exactly a capped-channel-open 409; anything else is malformed.
|
|
1626
|
+
if (
|
|
1627
|
+
failureShape &&
|
|
1628
|
+
envelope.retryable !== undefined &&
|
|
1629
|
+
(envelope.s !== 409 || envelope.retryable !== 1)
|
|
1630
|
+
) {
|
|
1631
|
+
this.refuse();
|
|
1632
|
+
|
|
1633
|
+
return;
|
|
1634
|
+
}
|
|
1322
1635
|
|
|
1323
1636
|
if (
|
|
1324
1637
|
// Exactly one response shape must match: a success carries its result, a failure its
|
|
@@ -1470,6 +1783,9 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
1470
1783
|
}
|
|
1471
1784
|
|
|
1472
1785
|
private attach(ws: WebSocket): void {
|
|
1786
|
+
// A fresh connection re-probes the mux capability (SEC-13): caps are per-connection.
|
|
1787
|
+
this.muxCapsValue = undefined;
|
|
1788
|
+
|
|
1473
1789
|
ws.addEventListener("message", (event) => {
|
|
1474
1790
|
if (this.ws !== ws || this.state !== "open") {
|
|
1475
1791
|
return;
|
|
@@ -1478,35 +1794,74 @@ export class CloudWssTransport implements CloudTransport, CloudEndpoint, RelayCa
|
|
|
1478
1794
|
const data: unknown = event.data;
|
|
1479
1795
|
|
|
1480
1796
|
if (typeof data !== "string") {
|
|
1481
|
-
// M4 B3: binary belongs to an OPEN relay channel only — bounded §16
|
|
1482
|
-
// the registered data listener; anything else (no channel,
|
|
1483
|
-
// refused locally exactly like the pre-relay profile:
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
) {
|
|
1797
|
+
// M4 B3 + SEC-13: binary belongs to an OPEN relay channel only — bounded §16
|
|
1798
|
+
// frames go to the registered data listener; anything else (no channel,
|
|
1799
|
+
// oversize, empty) is refused locally exactly like the pre-relay profile:
|
|
1800
|
+
// counted, never forwarded. A connection holding mux channels demultiplexes
|
|
1801
|
+
// mux-framed bodies per channel id (unknown id or missing listener refuses).
|
|
1802
|
+
if (data instanceof ArrayBuffer) {
|
|
1488
1803
|
const payload = Buffer.from(data);
|
|
1489
1804
|
|
|
1490
1805
|
if (
|
|
1491
|
-
payload.length
|
|
1492
|
-
payload.length
|
|
1493
|
-
this.
|
|
1806
|
+
payload.length > 0 &&
|
|
1807
|
+
payload.length <= MAX_DATA_FRAME_BYTES &&
|
|
1808
|
+
(this.relayMuxRequesterChannels.size > 0 ||
|
|
1809
|
+
this.relayTargetChannels.size > 0)
|
|
1494
1810
|
) {
|
|
1495
|
-
|
|
1811
|
+
const mux = parseMuxFrame(payload);
|
|
1496
1812
|
|
|
1497
|
-
|
|
1813
|
+
if (mux !== null) {
|
|
1814
|
+
const known =
|
|
1815
|
+
this.relayMuxRequesterChannels.has(mux.channelId) ||
|
|
1816
|
+
this.relayTargetChannels.has(mux.channelId);
|
|
1817
|
+
|
|
1818
|
+
if (!known || this.relayMuxDataListener === null) {
|
|
1819
|
+
this.refuse();
|
|
1820
|
+
|
|
1821
|
+
return;
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
const listener = this.relayMuxDataListener;
|
|
1825
|
+
|
|
1826
|
+
try {
|
|
1827
|
+
listener({ channelId: mux.channelId, payload: mux.body });
|
|
1828
|
+
} catch {
|
|
1829
|
+
// The relay consumer owns its session-fatal path; a throwing
|
|
1830
|
+
// listener must neither crash the event emitter nor corrupt
|
|
1831
|
+
// transport state. Counted.
|
|
1832
|
+
this.seamErrors += 1;
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
return;
|
|
1836
|
+
}
|
|
1498
1837
|
}
|
|
1499
1838
|
|
|
1500
|
-
|
|
1839
|
+
if (this.relayChannelIdValue !== null || this.relayTargetActive) {
|
|
1840
|
+
if (
|
|
1841
|
+
payload.length === 0 ||
|
|
1842
|
+
payload.length > MAX_DATA_FRAME_BYTES ||
|
|
1843
|
+
this.relayDataListener === null
|
|
1844
|
+
) {
|
|
1845
|
+
this.refuse();
|
|
1846
|
+
|
|
1847
|
+
return;
|
|
1848
|
+
}
|
|
1501
1849
|
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1850
|
+
const listener = this.relayDataListener;
|
|
1851
|
+
|
|
1852
|
+
try {
|
|
1853
|
+
listener(payload);
|
|
1854
|
+
} catch {
|
|
1855
|
+
// The relay consumer owns its session-fatal path; a throwing listener must
|
|
1856
|
+
// neither crash the event emitter nor corrupt transport state. Counted.
|
|
1857
|
+
this.seamErrors += 1;
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
return;
|
|
1508
1861
|
}
|
|
1509
1862
|
|
|
1863
|
+
this.refuse();
|
|
1864
|
+
|
|
1510
1865
|
return;
|
|
1511
1866
|
}
|
|
1512
1867
|
|
package/src/pairing-storage.ts
CHANGED
|
@@ -83,7 +83,12 @@ function parse(text: string, limit: number): StorageSnapshot {
|
|
|
83
83
|
async function checkDirectory(directory: string): Promise<void> {
|
|
84
84
|
const stat = await lstat(directory);
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
// POSIX-only privacy proof: Windows has no POSIX mode bits (Node reports 0666 for every
|
|
87
|
+
// directory), so on win32 the check is the user-profile directory's own ACL and the mode
|
|
88
|
+
// assertion is skipped — same ADR-13 §4 spirit as the endpoint's platform gate.
|
|
89
|
+
const modeOk = process.platform === "win32" || (stat.mode & 0o777) === 0o700;
|
|
90
|
+
|
|
91
|
+
if (!stat.isDirectory() || !modeOk) {
|
|
87
92
|
fail("directory must be private (0700) and not a symlink");
|
|
88
93
|
}
|
|
89
94
|
}
|