@full-self-browsing/phantom-stream 0.1.0 → 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.
Files changed (46) hide show
  1. package/dist/types/adapters/extension.d.ts.map +1 -1
  2. package/dist/types/adapters/playwright.d.ts.map +1 -1
  3. package/dist/types/capture/index.d.ts +112 -0
  4. package/dist/types/capture/index.d.ts.map +1 -1
  5. package/dist/types/protocol/constants.d.ts +22 -0
  6. package/dist/types/protocol/constants.d.ts.map +1 -1
  7. package/dist/types/protocol/index.d.ts +1 -0
  8. package/dist/types/protocol/media-reconcile.d.ts +53 -0
  9. package/dist/types/protocol/media-reconcile.d.ts.map +1 -0
  10. package/dist/types/protocol/messages.d.ts +266 -2
  11. package/dist/types/protocol/messages.d.ts.map +1 -1
  12. package/dist/types/renderer/asset-policy.d.ts +72 -0
  13. package/dist/types/renderer/asset-policy.d.ts.map +1 -0
  14. package/dist/types/renderer/diff.d.ts.map +1 -1
  15. package/dist/types/renderer/index.d.ts +13 -0
  16. package/dist/types/renderer/index.d.ts.map +1 -1
  17. package/dist/types/renderer/media-player.d.ts +114 -0
  18. package/dist/types/renderer/media-player.d.ts.map +1 -0
  19. package/dist/types/renderer/overlays.d.ts +12 -0
  20. package/dist/types/renderer/overlays.d.ts.map +1 -1
  21. package/dist/types/renderer/sanitize.d.ts +22 -0
  22. package/dist/types/renderer/sanitize.d.ts.map +1 -1
  23. package/dist/types/renderer/snapshot.d.ts +38 -0
  24. package/dist/types/renderer/snapshot.d.ts.map +1 -1
  25. package/dist/types/transport/websocket.d.ts.map +1 -1
  26. package/docs/ARCHITECTURE.md +13 -4
  27. package/docs/SECURITY.md +233 -2
  28. package/examples/bookmarklet-demo/viewer.html +12 -3
  29. package/examples/extension-mv3/viewer.html +10 -3
  30. package/package.json +9 -1
  31. package/src/adapters/extension.js +202 -7
  32. package/src/adapters/playwright-inject.js +50 -0
  33. package/src/adapters/playwright.js +159 -0
  34. package/src/capture/index.js +1071 -11
  35. package/src/protocol/constants.js +24 -0
  36. package/src/protocol/index.js +1 -0
  37. package/src/protocol/media-reconcile.js +188 -0
  38. package/src/protocol/messages.js +145 -2
  39. package/src/renderer/asset-policy.js +172 -0
  40. package/src/renderer/diff.js +106 -2
  41. package/src/renderer/index.js +849 -9
  42. package/src/renderer/media-player.js +367 -0
  43. package/src/renderer/overlays.js +324 -0
  44. package/src/renderer/sanitize.js +25 -3
  45. package/src/renderer/snapshot.js +534 -3
  46. package/src/transport/websocket.js +36 -0
@@ -37,7 +37,32 @@
37
37
  // injected identity hooks owned by createViewer.
38
38
 
39
39
  import { DIFF_OP } from '../protocol/messages.js';
40
- import { sanitizeFragment, sanitizeAttrValue } from './sanitize.js';
40
+ import { sanitizeFragment, sanitizeAttrValue, parseSrcsetCandidates } from './sanitize.js';
41
+
42
+ /**
43
+ * True when any candidate in a srcset value is blocked by the injected fetch
44
+ * gate. Reuses the shared per-candidate parser so the ATTR branch gates srcset
45
+ * with the same vocabulary as the snapshot/fragment gates (review WR-03).
46
+ * Unparseable input fails closed (treated as blocked).
47
+ * @param {string} srcset
48
+ * @param {(url: string, kind: string) => { allow: boolean }} gateAssetUrl
49
+ * @returns {boolean}
50
+ */
51
+ function srcsetHasBlockedCandidate(srcset, gateAssetUrl) {
52
+ if (!srcset) return false;
53
+ try {
54
+ var candidates = parseSrcsetCandidates(srcset);
55
+ for (var i = 0; i < candidates.length; i++) {
56
+ var url = candidates[i].url;
57
+ if (!url) continue;
58
+ var verdict = gateAssetUrl(url, 'image');
59
+ if (!verdict || !verdict.allow) return true;
60
+ }
61
+ return false;
62
+ } catch (e) {
63
+ return true; // unparseable srcset: fail closed
64
+ }
65
+ }
41
66
 
42
67
  function installShadowRootDirect(doc, host, payload, sanitizeCounters, logger, indexSubtree, removeSubtree) {
43
68
  var p = payload || {};
@@ -135,6 +160,19 @@ export function applyMutations(doc, mutations, counters, hooks) {
135
160
  var removeStyleSource = typeof identity.removeStyleSource === 'function'
136
161
  ? function (sourceId, scope) { return identity.removeStyleSource(sourceId, scope); }
137
162
  : null;
163
+ // Phase 12 (MSEC-01) injected pre-write asset gate. createViewer injects the
164
+ // posture-bound closures; omitted hooks default to no-ops so the public
165
+ // applyMutations signature and every existing caller stay unchanged.
166
+ // gateFragmentAssets(node): pre-write gate over inert ADD template content
167
+ // (currentSrc pin + blocked-origin -> placeholder), run before importNode.
168
+ // gateAssetUrl(url, kind): per-URL verdict for the ATTR branch's src/poster,
169
+ // run before setAttribute (live-element mutation -> must gate pre-write).
170
+ var gateFragmentAssets = typeof identity.gateFragmentAssets === 'function'
171
+ ? function (node) { identity.gateFragmentAssets(node); }
172
+ : function () {};
173
+ var gateAssetUrl = typeof identity.gateAssetUrl === 'function'
174
+ ? function (url, kind) { return identity.gateAssetUrl(url, kind); }
175
+ : null;
138
176
 
139
177
  // Shared miss path: count, warn, and escalate at the parity threshold.
140
178
  function recordStaleMiss(op, nid) {
@@ -212,6 +250,11 @@ export function applyMutations(doc, mutations, counters, hooks) {
212
250
  // before it gets anywhere near the mirror document -- DOM-
213
251
  // fragment based, never string-scrub-then-reparse (mXSS).
214
252
  sanitizeFragment(tpl.content, sanitizeCounters, logger);
253
+ // PRE-WRITE FETCH GATE (MSEC-01): the parsed template content is
254
+ // inert (fires no fetch), so gating it here -- before importNode --
255
+ // is pre-write. Applies the currentSrc pin and replaces blocked
256
+ // origins with the dimensioned placeholder.
257
+ gateFragmentAssets(tpl.content);
215
258
  var newNode = tpl.content.firstElementChild;
216
259
  if (!newNode) {
217
260
  // Still possible: empty/whitespace-only m.html, or html whose
@@ -318,6 +361,55 @@ export function applyMutations(doc, mutations, counters, hooks) {
318
361
  target.removeAttribute(m.attr);
319
362
  break;
320
363
  }
364
+ // PRE-WRITE FETCH GATE (MSEC-01): for fetchable attrs (src/poster)
365
+ // on a LIVE mirror element, gate before setAttribute so a blocked
366
+ // origin never reaches the live DOM (the browser would GET it
367
+ // immediately). Blocked -> drop the attribute (no fetchable src);
368
+ // the dimensioned placeholder is the snapshot/ADD path's job.
369
+ if (gateAssetUrl && (attrName === 'src' || attrName === 'poster')) {
370
+ // Kind must match the live element so the gate applies mode-aware
371
+ // policy (poster mode withholds playable media but keeps images):
372
+ // <video>/<audio> src -> 'media', <source> src -> 'source', any
373
+ // other src -> 'image'; a poster attr stays 'poster'. Mirrors
374
+ // gateOneMediaTag / gateFragmentMedia so a poster-mode <video src>
375
+ // (or <source src>) mutation is denied 'poster-mode-media' instead
376
+ // of slipping through gated as an image and fetching media bytes.
377
+ var assetKind;
378
+ if (attrName === 'poster') {
379
+ assetKind = 'poster';
380
+ } else if (targetTag === 'video' || targetTag === 'audio') {
381
+ assetKind = 'media';
382
+ } else if (targetTag === 'source') {
383
+ assetKind = 'source';
384
+ } else {
385
+ assetKind = 'image';
386
+ }
387
+ var assetVerdict = gateAssetUrl(scrubbed.value, assetKind);
388
+ if (!assetVerdict || !assetVerdict.allow) {
389
+ sanitizeCounters.blockedUrls += 1;
390
+ logger.warn('[Renderer] attr op asset blocked by origin gate', {
391
+ nid: m.nid || '', attr: m.attr || ''
392
+ });
393
+ target.removeAttribute(m.attr);
394
+ break;
395
+ }
396
+ }
397
+ // PRE-WRITE FETCH GATE (MSEC-01, review WR-03): srcset is a
398
+ // first-class multi-candidate fetchable attribute -- on a live
399
+ // <img> the browser may select and GET a candidate. Gate every
400
+ // candidate; if ANY is blocked, drop the whole srcset (the
401
+ // remaining src/placeholder path covers display) so no blocked
402
+ // origin reaches the live DOM.
403
+ if (gateAssetUrl && attrName === 'srcset') {
404
+ if (srcsetHasBlockedCandidate(scrubbed.value, gateAssetUrl)) {
405
+ sanitizeCounters.blockedUrls += 1;
406
+ logger.warn('[Renderer] attr op srcset candidate blocked by origin gate', {
407
+ nid: m.nid || '', attr: m.attr || ''
408
+ });
409
+ target.removeAttribute(m.attr);
410
+ break;
411
+ }
412
+ }
321
413
  target.setAttribute(m.attr, scrubbed.value);
322
414
  break;
323
415
  }
@@ -333,7 +425,19 @@ export function applyMutations(doc, mutations, counters, hooks) {
333
425
  if (Object.prototype.hasOwnProperty.call(m, 'checked')) {
334
426
  valueTarget.checked = !!m.checked;
335
427
  }
336
- if (Array.isArray(m.selectedValues) && valueTarget.options) {
428
+ if (Array.isArray(m.selectedIndexes) && valueTarget.options) {
429
+ // Index-addressed selection is unambiguous even when masking
430
+ // collapses distinct option values to the same masked string
431
+ // (capture omits the raw values from the wire), so it wins over
432
+ // selectedValues whenever the capture side provides it.
433
+ var selectedIndexes = new Set();
434
+ for (var si = 0; si < m.selectedIndexes.length; si++) {
435
+ selectedIndexes.add(Number(m.selectedIndexes[si]));
436
+ }
437
+ for (var ix = 0; ix < valueTarget.options.length; ix++) {
438
+ valueTarget.options[ix].selected = selectedIndexes.has(ix);
439
+ }
440
+ } else if (Array.isArray(m.selectedValues) && valueTarget.options) {
337
441
  var selectedValues = new Set();
338
442
  for (var sv = 0; sv < m.selectedValues.length; sv++) {
339
443
  selectedValues.add(String(m.selectedValues[sv]));