@nac3/runtime 2.9.0 → 2.9.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.
@@ -84,6 +84,48 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
84
84
  }
85
85
  }
86
86
 
87
+ /* 2.9.1 (PND-161) -- WHERE THE HOST'S CREDENTIAL IS ALLOWED TO GO.
88
+
89
+ import_remote_tree takes two arguments with very different
90
+ standing: `conn`, which the HOST wrote, and `payload`, which is
91
+ DATA THE PEER SENT. The ack subscription was built from
92
+ payload.ack_endpoint and carried conn.bearer on the query string,
93
+ so the other side of the channel chose the destination of the
94
+ host's credential: a peer that is malicious, or merely
95
+ compromised, pointed it at its own collector and the browser
96
+ delivered it.
97
+
98
+ The rule now is the one the host can actually reason about: the
99
+ credential travels only to the origin the HOST already trusted
100
+ with it -- the origin of conn.endpoint -- or to an endpoint the
101
+ host named itself in conn.ack_endpoint. A same-origin
102
+ payload.ack_endpoint still works, which is the shape every real
103
+ bridge has (the SSE route lives beside the RPC route), so no
104
+ legitimate deployment changes.
105
+
106
+ What this does NOT fix, and is accepted and documented: the bearer
107
+ still rides the query string, because EventSource cannot carry
108
+ headers. That is why pinning the origin matters -- a query string
109
+ ends up in proxy logs and Referer headers, and now at least it is
110
+ the logs of the host's own bridge. */
111
+ function _originOf(url, base) {
112
+ try {
113
+ if (typeof URL !== 'function') return null;
114
+ var b = base || (typeof location !== 'undefined' ? location.href : undefined);
115
+ return new URL(String(url), b).origin;
116
+ } catch (e) {
117
+ return null;
118
+ }
119
+ }
120
+ function _ackEndpointAllowed(ackEndpoint, connEndpoint) {
121
+ var want = _originOf(connEndpoint);
122
+ var got = _originOf(ackEndpoint, connEndpoint);
123
+ /* Fail closed: an origin that cannot be resolved is not an origin
124
+ that was checked. */
125
+ if (!want || !got) return false;
126
+ return want === got;
127
+ }
128
+
87
129
  function _parseRemote(nac_id) {
88
130
  if (typeof nac_id !== 'string') return null;
89
131
  if (nac_id.indexOf(REMOTE_PREFIX) !== 0) return null;
@@ -214,6 +256,20 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
214
256
  var transport = conn.transport || 'http';
215
257
  var namespace = conn.namespace || payload.app_id;
216
258
  if (!namespace) throw _err('invalid', 'import_remote_tree: namespace required (no app_id in payload)');
259
+ /* 2.9.1 (PND-166) -- the namespace is the first segment of
260
+ "remote:<ns>:<peer_id>" and _parseRemote splits at the FIRST
261
+ colon, so a namespace containing one makes every id under it
262
+ ambiguous. It defaults to payload.app_id, which the PEER chose:
263
+ measured, a peer declaring app_id "bank:widget" routed
264
+ NAC.click('remote:bank:widget:pay') to the endpoint and bearer of
265
+ an unrelated peer named "bank". Refused by name instead of
266
+ imported into an ambiguity. */
267
+ if (String(namespace).indexOf(':') >= 0) {
268
+ throw _err('invalid',
269
+ 'import_remote_tree: namespace "' + namespace + '" contains ":", which is the '
270
+ + 'separator of remote:<namespace>:<peer_id> -- every id under it would be '
271
+ + 'ambiguous. Pass conn.namespace with no colon.');
272
+ }
217
273
  if (_remotes[namespace]) {
218
274
  throw _err('conflict', 'import_remote_tree: namespace "' + namespace + '" already imported; disconnect first');
219
275
  }
@@ -264,9 +320,33 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
264
320
  };
265
321
  _remotes[namespace] = handle;
266
322
 
267
- /* Auto-subscribe to ack events. */
268
- if (conn.auto_subscribe !== false && payload.ack_endpoint && transport === 'http') {
269
- _subscribeRemote(handle, payload.ack_endpoint);
323
+ /* Auto-subscribe to ack events -- see _ackEndpointAllowed for why
324
+ the host's endpoint wins over the peer's. */
325
+ if (conn.auto_subscribe !== false && transport === 'http') {
326
+ var hostAck = conn.ack_endpoint || null;
327
+ var peerAck = payload.ack_endpoint || null;
328
+ if (hostAck) {
329
+ _subscribeRemote(handle, hostAck);
330
+ } else if (peerAck && _ackEndpointAllowed(peerAck, conn.endpoint)) {
331
+ _subscribeRemote(handle, peerAck);
332
+ } else if (peerAck) {
333
+ /* Refused by name, never in silence: the import stands (the
334
+ tree is data and it is already registered) but the channel
335
+ that would have carried the bearer does not open, and the
336
+ host can see exactly that on the handle and on the event. */
337
+ handle.ack_refused = {
338
+ reason: 'ack_endpoint_cross_origin',
339
+ ack_endpoint: String(peerAck),
340
+ conn_origin: _originOf(conn.endpoint),
341
+ ack_origin: _originOf(peerAck, conn.endpoint),
342
+ };
343
+ console.warn('[nac-interop] refused the peer ack_endpoint: it is not the origin of '
344
+ + 'conn.endpoint, and the bearer would have travelled to it. Pass conn.ack_endpoint '
345
+ + 'to name one yourself.', handle.ack_refused);
346
+ document.dispatchEvent(new CustomEvent('nac:interop:ack_refused', {
347
+ detail: Object.assign({ namespace: namespace }, handle.ack_refused)
348
+ }));
349
+ }
270
350
  }
271
351
 
272
352
  document.dispatchEvent(new CustomEvent('nac:interop:imported', {
@@ -1,5 +1,5 @@
1
1
  // Generated by build.mjs -- do not edit. Source: js/nac-v2-extensions.js
2
- // Brownfield + data-table extensions for @nac3/runtime v2.9.0.
2
+ // Brownfield + data-table extensions for @nac3/runtime v2.9.1.
3
3
  // This module MUST load AFTER the core runtime; it attaches every
4
4
  // member below onto the same window.NAC object. Import as:
5
5
  // import '@nac3/runtime';