@full-self-browsing/phantom-stream 0.1.0
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/LICENSE +21 -0
- package/README.md +263 -0
- package/bin/phantom-stream.js +193 -0
- package/dist/types/adapters/bookmarklet.d.ts +22 -0
- package/dist/types/adapters/bookmarklet.d.ts.map +1 -0
- package/dist/types/adapters/browser-inject.d.ts +7 -0
- package/dist/types/adapters/browser-inject.d.ts.map +1 -0
- package/dist/types/adapters/extension.d.ts +43 -0
- package/dist/types/adapters/extension.d.ts.map +1 -0
- package/dist/types/adapters/playwright-inject.d.ts +2 -0
- package/dist/types/adapters/playwright-inject.d.ts.map +1 -0
- package/dist/types/adapters/playwright.d.ts +14 -0
- package/dist/types/adapters/playwright.d.ts.map +1 -0
- package/dist/types/capture/index.d.ts +241 -0
- package/dist/types/capture/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/protocol/constants.d.ts +35 -0
- package/dist/types/protocol/constants.d.ts.map +1 -0
- package/dist/types/protocol/envelope.d.ts +41 -0
- package/dist/types/protocol/envelope.d.ts.map +1 -0
- package/dist/types/protocol/index.d.ts +5 -0
- package/dist/types/protocol/index.d.ts.map +1 -0
- package/dist/types/protocol/messages.d.ts +520 -0
- package/dist/types/protocol/messages.d.ts.map +1 -0
- package/dist/types/protocol/remote-control.d.ts +70 -0
- package/dist/types/protocol/remote-control.d.ts.map +1 -0
- package/dist/types/relay/backends/ws.d.ts +39 -0
- package/dist/types/relay/backends/ws.d.ts.map +1 -0
- package/dist/types/relay/index.d.ts +4 -0
- package/dist/types/relay/index.d.ts.map +1 -0
- package/dist/types/relay/limits.d.ts +54 -0
- package/dist/types/relay/limits.d.ts.map +1 -0
- package/dist/types/relay/relay.d.ts +73 -0
- package/dist/types/relay/relay.d.ts.map +1 -0
- package/dist/types/renderer/diff.d.ts +62 -0
- package/dist/types/renderer/diff.d.ts.map +1 -0
- package/dist/types/renderer/index.d.ts +211 -0
- package/dist/types/renderer/index.d.ts.map +1 -0
- package/dist/types/renderer/overlays.d.ts +166 -0
- package/dist/types/renderer/overlays.d.ts.map +1 -0
- package/dist/types/renderer/sanitize.d.ts +73 -0
- package/dist/types/renderer/sanitize.d.ts.map +1 -0
- package/dist/types/renderer/snapshot.d.ts +41 -0
- package/dist/types/renderer/snapshot.d.ts.map +1 -0
- package/dist/types/transport/websocket.d.ts +44 -0
- package/dist/types/transport/websocket.d.ts.map +1 -0
- package/docs/ARCHITECTURE.md +273 -0
- package/docs/DESIGN-HISTORY.md +120 -0
- package/docs/QUICKSTARTS.md +289 -0
- package/docs/RELEASE.md +60 -0
- package/docs/SECURITY.md +156 -0
- package/docs/paper/OUTLINE.md +100 -0
- package/examples/bookmarklet-demo/demo.css +102 -0
- package/examples/bookmarklet-demo/server.js +297 -0
- package/examples/bookmarklet-demo/source.html +52 -0
- package/examples/bookmarklet-demo/viewer.html +48 -0
- package/examples/extension-mv3/demo.css +102 -0
- package/examples/extension-mv3/server.js +350 -0
- package/examples/extension-mv3/source.html +48 -0
- package/examples/extension-mv3/viewer.html +48 -0
- package/examples/loopback-mirror.html +369 -0
- package/examples/loopback-transport.js +65 -0
- package/examples/playwright-demo/demo.css +462 -0
- package/examples/playwright-demo/fixture.html +56 -0
- package/examples/playwright-demo/fixture.js +57 -0
- package/examples/playwright-demo/server.js +517 -0
- package/examples/playwright-demo/viewer.html +112 -0
- package/examples/playwright-demo/viewer.js +414 -0
- package/examples/serve.js +97 -0
- package/examples/two-tab-demo/demo.css +266 -0
- package/examples/two-tab-demo/server.js +233 -0
- package/examples/two-tab-demo/source.html +70 -0
- package/examples/two-tab-demo/source.js +213 -0
- package/examples/two-tab-demo/viewer.html +72 -0
- package/examples/two-tab-demo/viewer.js +121 -0
- package/package.json +107 -0
- package/src/adapters/bookmarklet.js +98 -0
- package/src/adapters/browser-inject.js +17 -0
- package/src/adapters/extension.js +322 -0
- package/src/adapters/playwright-inject.js +4401 -0
- package/src/adapters/playwright.js +540 -0
- package/src/capture/README.md +302 -0
- package/src/capture/index.js +4361 -0
- package/src/index.js +5 -0
- package/src/protocol/constants.js +49 -0
- package/src/protocol/envelope.js +75 -0
- package/src/protocol/index.js +4 -0
- package/src/protocol/messages.js +268 -0
- package/src/protocol/remote-control.js +300 -0
- package/src/relay/README.md +22 -0
- package/src/relay/backends/ws.js +186 -0
- package/src/relay/index.js +3 -0
- package/src/relay/limits.js +105 -0
- package/src/relay/relay.js +397 -0
- package/src/renderer/README.md +443 -0
- package/src/renderer/diff.js +389 -0
- package/src/renderer/index.js +1401 -0
- package/src/renderer/overlays.js +528 -0
- package/src/renderer/sanitize.js +366 -0
- package/src/renderer/snapshot.js +202 -0
- package/src/transport/websocket.js +553 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// PhantomStream renderer: Document-parameterized diff applier.
|
|
2
|
+
//
|
|
3
|
+
// Port of the FSB reference viewer's mutation handler
|
|
4
|
+
// (reference/dashboard/dashboard.js:3209-3356, handleDOMMutations) with the
|
|
5
|
+
// iframe chrome factored out so the op loop runs against ANY injected
|
|
6
|
+
// Document -- jsdom 29 never parses srcdoc (02-RESEARCH.md Pattern 3,
|
|
7
|
+
// verified), so this seam is what makes the applier unit-testable.
|
|
8
|
+
//
|
|
9
|
+
// Division of responsibility (plan 02-01 contract, extended by 03-02):
|
|
10
|
+
// - This module owns: the per-op switch (add/rm/attr/text), per-op
|
|
11
|
+
// try/catch containment (one bad op never aborts the rest of the batch),
|
|
12
|
+
// miss/failure counter increments, the parity thresholds (>= 3 stale
|
|
13
|
+
// misses or >= 2 apply failures fire the injected resync callback; a
|
|
14
|
+
// whole-batch failure fires it immediately), and the whole-batch catch.
|
|
15
|
+
// - createViewer (plan 02-03) owns: the resync latch (requestResync here
|
|
16
|
+
// is a plain injected callback -- dedup is the caller's job), staleness
|
|
17
|
+
// gating, the post-batch scroll re-apply (needs contentWindow), and all
|
|
18
|
+
// iframe state reads.
|
|
19
|
+
//
|
|
20
|
+
// Phase 3 upgrade (plan 03-02, SEC-02): the ADD branch parses wire html in
|
|
21
|
+
// TEMPLATE context (the queued Phase-3+ fix -- context-dependent elements
|
|
22
|
+
// like tr/td/col/option no longer drop; 03-RESEARCH Pattern 2, verified
|
|
23
|
+
// against jsdom 29) and every reconstructed-DOM insertion runs through the
|
|
24
|
+
// render-side chokepoint: sanitizeFragment scrubs the parsed template
|
|
25
|
+
// content before importNode, and sanitizeAttrValue interposes before every
|
|
26
|
+
// setAttribute in the ATTR branch (Pitfall 5, render side). Strip counts
|
|
27
|
+
// land in the caller-owned hooks.sanitizeCounters (per-session lifecycle
|
|
28
|
+
// in createViewer -- Pitfall 3).
|
|
29
|
+
//
|
|
30
|
+
// Dropped from the reference: every recordDashboardTransportEvent /
|
|
31
|
+
// recordDashboardTransportError call (FSB dashboard diagnostics ring
|
|
32
|
+
// buffers) -- replaced with counter increments plus hooks.logger.warn lines
|
|
33
|
+
// prefixed '[Renderer]'. The reference's two stale-miss event labels (a
|
|
34
|
+
// parent variant for add, a target variant for rm/attr/text) feed ONE
|
|
35
|
+
// counter there and one here; the resync reason collapses to the parent
|
|
36
|
+
// variant per the plan contract. Phase 7 routes node addressing through
|
|
37
|
+
// injected identity hooks owned by createViewer.
|
|
38
|
+
|
|
39
|
+
import { DIFF_OP } from '../protocol/messages.js';
|
|
40
|
+
import { sanitizeFragment, sanitizeAttrValue } from './sanitize.js';
|
|
41
|
+
|
|
42
|
+
function installShadowRootDirect(doc, host, payload, sanitizeCounters, logger, indexSubtree, removeSubtree) {
|
|
43
|
+
var p = payload || {};
|
|
44
|
+
if (!doc || !host) return false;
|
|
45
|
+
if (p.mode && p.mode !== 'open') return false;
|
|
46
|
+
var shadowRoot = host.shadowRoot || null;
|
|
47
|
+
if (!shadowRoot) {
|
|
48
|
+
if (typeof host.attachShadow !== 'function') {
|
|
49
|
+
logger.warn('[Renderer] shadow root unsupported', { hostNid: p.hostNid || '' });
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
shadowRoot = host.attachShadow({ mode: 'open' });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
removeSubtree(shadowRoot);
|
|
56
|
+
while (shadowRoot.firstChild) shadowRoot.removeChild(shadowRoot.firstChild);
|
|
57
|
+
|
|
58
|
+
var tpl = doc.createElement('template');
|
|
59
|
+
tpl.innerHTML = p.html || '';
|
|
60
|
+
sanitizeFragment(tpl.content, sanitizeCounters, logger);
|
|
61
|
+
shadowRoot.appendChild(doc.importNode(tpl.content, true));
|
|
62
|
+
indexSubtree(shadowRoot, p.nodeIds || []);
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @typedef {Object} DiffCounters
|
|
68
|
+
* @property {number} staleMisses Nid lookups that found no element (stale ops)
|
|
69
|
+
* @property {number} applyFailures Ops (or whole batches) whose apply threw
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @typedef {Object} DiffHooks
|
|
74
|
+
* @property {{warn: function(...*): void}} logger
|
|
75
|
+
* Diagnostics sink for misses and failures (console-shaped).
|
|
76
|
+
* @property {function(string, Object): void} requestResync
|
|
77
|
+
* Resync escalation callback. Fired at the parity thresholds; latching
|
|
78
|
+
* and the CONTROL.START send live in createViewer, not here.
|
|
79
|
+
* @property {import('./sanitize.js').SanitizeCounters} [sanitizeCounters]
|
|
80
|
+
* OPTIONAL caller-owned sanitization strip counters (plan 03-02).
|
|
81
|
+
* Defaults to a local throwaway object so the public 4-arg signature is
|
|
82
|
+
* unchanged; createViewer injects its per-session object (Pitfall 3).
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Apply a batch of capture diff ops against an injected Document.
|
|
87
|
+
* Behavioral port of dashboard.js:3209-3356: each op addresses its target
|
|
88
|
+
* by the injected identity resolver; a lookup miss is counted and warned, never thrown; a
|
|
89
|
+
* throwing op is contained per-op so the rest of the batch still applies;
|
|
90
|
+
* a failure of the batch machinery itself is caught once and escalated
|
|
91
|
+
* immediately. Missing or empty batches return without side effects.
|
|
92
|
+
*
|
|
93
|
+
* @param {Document} doc Target mirror Document (any Document works)
|
|
94
|
+
* @param {Array<Object>} mutations Diff ops ({op, nid|parentNid, ...})
|
|
95
|
+
* @param {DiffCounters} counters Mutated in place; caller resets per snapshot
|
|
96
|
+
* @param {DiffHooks} hooks Injected logger + resync callback
|
|
97
|
+
* @returns {void}
|
|
98
|
+
*/
|
|
99
|
+
export function applyMutations(doc, mutations, counters, hooks) {
|
|
100
|
+
var opts = hooks || {};
|
|
101
|
+
var logger = opts.logger && typeof opts.logger.warn === 'function'
|
|
102
|
+
? opts.logger
|
|
103
|
+
: { warn: function () {} };
|
|
104
|
+
var requestResync = typeof opts.requestResync === 'function'
|
|
105
|
+
? opts.requestResync
|
|
106
|
+
: function () {};
|
|
107
|
+
var tallies = counters || { staleMisses: 0, applyFailures: 0 };
|
|
108
|
+
var sanitizeCounters = opts.sanitizeCounters || {
|
|
109
|
+
strippedHandlers: 0, blockedUrls: 0, droppedSubtrees: 0, cssScrubs: 0
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
if (!doc || !doc.body) return;
|
|
113
|
+
if (!mutations) return; // missing batch: not an error (reference defaults to [])
|
|
114
|
+
|
|
115
|
+
// hooks.identity is normalized through opts so omitted hooks stay safe.
|
|
116
|
+
var identity = opts.identity || {};
|
|
117
|
+
var resolve = typeof identity.resolve === 'function'
|
|
118
|
+
? function (nid) { return identity.resolve(nid); }
|
|
119
|
+
: function () { return null; };
|
|
120
|
+
var indexSubtree = typeof identity.indexSubtree === 'function'
|
|
121
|
+
? function (root, nodeIds) { identity.indexSubtree(root, nodeIds || []); }
|
|
122
|
+
: function () {};
|
|
123
|
+
var removeSubtree = typeof identity.removeSubtree === 'function'
|
|
124
|
+
? function (root) { identity.removeSubtree(root); }
|
|
125
|
+
: function () {};
|
|
126
|
+
var installShadowRoot = typeof identity.installShadowRoot === 'function'
|
|
127
|
+
? function (hostNid, payload) { identity.installShadowRoot(hostNid, payload); }
|
|
128
|
+
: null;
|
|
129
|
+
var installFrames = typeof identity.installFrames === 'function'
|
|
130
|
+
? function (frames) { identity.installFrames(frames || []); }
|
|
131
|
+
: null;
|
|
132
|
+
var applyStyleSource = typeof identity.applyStyleSource === 'function'
|
|
133
|
+
? function (action, sourceId, scope, source) { return identity.applyStyleSource(action, sourceId, scope, source); }
|
|
134
|
+
: null;
|
|
135
|
+
var removeStyleSource = typeof identity.removeStyleSource === 'function'
|
|
136
|
+
? function (sourceId, scope) { return identity.removeStyleSource(sourceId, scope); }
|
|
137
|
+
: null;
|
|
138
|
+
|
|
139
|
+
// Shared miss path: count, warn, and escalate at the parity threshold.
|
|
140
|
+
function recordStaleMiss(op, nid) {
|
|
141
|
+
tallies.staleMisses += 1;
|
|
142
|
+
logger.warn('[Renderer] stale mutation: no element for nid', {
|
|
143
|
+
op: op,
|
|
144
|
+
nid: nid || '',
|
|
145
|
+
staleMisses: tallies.staleMisses
|
|
146
|
+
});
|
|
147
|
+
if (tallies.staleMisses >= 3) {
|
|
148
|
+
requestResync('stale-mutation-parent', { op: op, nid: nid || '' });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function applyShadowRoot(payload) {
|
|
153
|
+
var p = payload || {};
|
|
154
|
+
var hostNid = p.hostNid;
|
|
155
|
+
var host = resolve(hostNid);
|
|
156
|
+
if (!host) {
|
|
157
|
+
recordStaleMiss(DIFF_OP.SHADOW_ROOT, hostNid);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (installShadowRoot) {
|
|
161
|
+
installShadowRoot(hostNid, p);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
installShadowRootDirect(doc, host, p, sanitizeCounters, logger, indexSubtree, removeSubtree);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function applyStyleSourceOp(m) {
|
|
168
|
+
var sourceId = m && m.sourceId ? String(m.sourceId) : '';
|
|
169
|
+
var scope = m && m.scope ? m.scope : null;
|
|
170
|
+
var scopeKind = scope && scope.kind ? String(scope.kind) : '';
|
|
171
|
+
if (!sourceId || !scopeKind) {
|
|
172
|
+
requestResync('stale-style-scope', { sourceId: sourceId, scopeKind: scopeKind });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
var ok = false;
|
|
176
|
+
if (m.action === 'remove') {
|
|
177
|
+
ok = removeStyleSource ? removeStyleSource(sourceId, scope) : false;
|
|
178
|
+
} else if (m.action === 'upsert' || m.action === 'replace') {
|
|
179
|
+
ok = applyStyleSource ? applyStyleSource(m.action, sourceId, scope, m.source || null) : false;
|
|
180
|
+
}
|
|
181
|
+
if (!ok) {
|
|
182
|
+
tallies.staleMisses += 1;
|
|
183
|
+
logger.warn('[Renderer] stale style source scope', {
|
|
184
|
+
sourceId: sourceId,
|
|
185
|
+
scopeKind: scopeKind,
|
|
186
|
+
staleMisses: tallies.staleMisses
|
|
187
|
+
});
|
|
188
|
+
requestResync('stale-style-scope', { sourceId: sourceId, scopeKind: scopeKind });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
mutations.forEach(function (m) {
|
|
194
|
+
try {
|
|
195
|
+
switch (m.op) {
|
|
196
|
+
case DIFF_OP.ADD: {
|
|
197
|
+
var parent = resolve(m.parentNid);
|
|
198
|
+
if (!parent) {
|
|
199
|
+
recordStaleMiss(DIFF_OP.ADD, m.parentNid);
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
// TEMPLATE-context parse (03-RESEARCH Pattern 2, jsdom-29-
|
|
203
|
+
// verified): template content accepts any HTML, so context-
|
|
204
|
+
// dependent elements (<tr>, <td>, <col>, <option>, ...) no
|
|
205
|
+
// longer drop. This is one of the renderer's two sanctioned
|
|
206
|
+
// innerHTML sinks for wire content; the other is the Phase 8
|
|
207
|
+
// shadow-root replacement parser above. Both are followed by
|
|
208
|
+
// sanitizeFragment and pinned by the purity scan.
|
|
209
|
+
var tpl = doc.createElement('template');
|
|
210
|
+
tpl.innerHTML = m.html;
|
|
211
|
+
// RENDER-SIDE CHOKEPOINT (SEC-02): scrub the PARSED fragment
|
|
212
|
+
// before it gets anywhere near the mirror document -- DOM-
|
|
213
|
+
// fragment based, never string-scrub-then-reparse (mXSS).
|
|
214
|
+
sanitizeFragment(tpl.content, sanitizeCounters, logger);
|
|
215
|
+
var newNode = tpl.content.firstElementChild;
|
|
216
|
+
if (!newNode) {
|
|
217
|
+
// Still possible: empty/whitespace-only m.html, or html whose
|
|
218
|
+
// only element was a dropped hostile subtree. Never silent
|
|
219
|
+
// (review WR-02): warn with the real cause, then count the
|
|
220
|
+
// drop through the stale-miss path so the >= 3 resync
|
|
221
|
+
// threshold self-heals via a fresh snapshot.
|
|
222
|
+
logger.warn('[Renderer] add op dropped: html parsed to no element', {
|
|
223
|
+
parentNid: m.parentNid || ''
|
|
224
|
+
});
|
|
225
|
+
recordStaleMiss(DIFF_OP.ADD, m.parentNid);
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
// importNode is REQUIRED: the parsed node lives in the
|
|
229
|
+
// template's parser document; importNode adopts a deep clone
|
|
230
|
+
// into the mirror doc (cross-doc insertBefore historically
|
|
231
|
+
// varies across real browsers for some element types).
|
|
232
|
+
var imported = doc.importNode(newNode, true);
|
|
233
|
+
if (m.beforeNid) {
|
|
234
|
+
var before = resolve(m.beforeNid);
|
|
235
|
+
parent.insertBefore(imported, before); // null before == appendChild
|
|
236
|
+
} else {
|
|
237
|
+
parent.appendChild(imported);
|
|
238
|
+
}
|
|
239
|
+
indexSubtree(imported, m.nodeIds || []);
|
|
240
|
+
if (Array.isArray(m.shadowRoots)) {
|
|
241
|
+
for (var s = 0; s < m.shadowRoots.length; s++) {
|
|
242
|
+
applyShadowRoot(m.shadowRoots[s]);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (installFrames && Array.isArray(m.frames)) {
|
|
246
|
+
installFrames(m.frames);
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
case DIFF_OP.SHADOW_ROOT: {
|
|
251
|
+
applyShadowRoot(m);
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
case DIFF_OP.FRAME: {
|
|
255
|
+
if (installFrames && m.frame) {
|
|
256
|
+
installFrames([m.frame]);
|
|
257
|
+
}
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
case DIFF_OP.STYLE_SOURCE: {
|
|
261
|
+
applyStyleSourceOp(m);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
case DIFF_OP.REMOVE: {
|
|
265
|
+
var el = resolve(m.nid);
|
|
266
|
+
if (!el) {
|
|
267
|
+
recordStaleMiss(DIFF_OP.REMOVE, m.nid);
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
removeSubtree(el);
|
|
271
|
+
if (el.parentNode) el.parentNode.removeChild(el);
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
case DIFF_OP.ATTR: {
|
|
275
|
+
var target = resolve(m.nid);
|
|
276
|
+
if (!target) {
|
|
277
|
+
recordStaleMiss(DIFF_OP.ATTR, m.nid);
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
var attrName = String(m.attr || '').toLowerCase();
|
|
281
|
+
var targetTag = target.tagName ? String(target.tagName).toLowerCase() : '';
|
|
282
|
+
if (targetTag === 'iframe' && attrName === 'src') {
|
|
283
|
+
target.removeAttribute('src');
|
|
284
|
+
logger.warn('[Renderer] iframe src attr op ignored', {
|
|
285
|
+
nid: m.nid || ''
|
|
286
|
+
});
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
if (m.val === null) {
|
|
290
|
+
// null is a removal, not a value -- it precedes the scrub
|
|
291
|
+
// by design (removing an attribute is always safe).
|
|
292
|
+
target.removeAttribute(m.attr);
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
// RENDER-SIDE CHOKEPOINT (SEC-02, Pitfall 5): scrub before
|
|
296
|
+
// every setAttribute -- defense-in-depth behind the capture
|
|
297
|
+
// chokepoint. Drops and neutralizations are counted, never
|
|
298
|
+
// silent; they are NOT stale misses (no resync churn).
|
|
299
|
+
var scrubbed = sanitizeAttrValue(m.attr, m.val);
|
|
300
|
+
if (scrubbed.drop) {
|
|
301
|
+
sanitizeCounters.strippedHandlers += 1;
|
|
302
|
+
logger.warn('[Renderer] attr op dropped by sanitizer', {
|
|
303
|
+
nid: m.nid || '', attr: m.attr || ''
|
|
304
|
+
});
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
if (scrubbed.value !== m.val) {
|
|
308
|
+
if (String(m.attr).toLowerCase() === 'style') {
|
|
309
|
+
sanitizeCounters.cssScrubs += 1;
|
|
310
|
+
} else {
|
|
311
|
+
sanitizeCounters.blockedUrls += 1;
|
|
312
|
+
}
|
|
313
|
+
logger.warn('[Renderer] attr op value scrubbed by sanitizer', {
|
|
314
|
+
nid: m.nid || '', attr: m.attr || ''
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
if (scrubbed.value === null) {
|
|
318
|
+
target.removeAttribute(m.attr);
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
target.setAttribute(m.attr, scrubbed.value);
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
case DIFF_OP.VALUE: {
|
|
325
|
+
var valueTarget = resolve(m.nid);
|
|
326
|
+
if (!valueTarget) {
|
|
327
|
+
recordStaleMiss(DIFF_OP.VALUE, m.nid);
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
if (Object.prototype.hasOwnProperty.call(m, 'value')) {
|
|
331
|
+
valueTarget.value = String(m.value ?? '');
|
|
332
|
+
}
|
|
333
|
+
if (Object.prototype.hasOwnProperty.call(m, 'checked')) {
|
|
334
|
+
valueTarget.checked = !!m.checked;
|
|
335
|
+
}
|
|
336
|
+
if (Array.isArray(m.selectedValues) && valueTarget.options) {
|
|
337
|
+
var selectedValues = new Set();
|
|
338
|
+
for (var sv = 0; sv < m.selectedValues.length; sv++) {
|
|
339
|
+
selectedValues.add(String(m.selectedValues[sv]));
|
|
340
|
+
}
|
|
341
|
+
for (var opt = 0; opt < valueTarget.options.length; opt++) {
|
|
342
|
+
var option = valueTarget.options[opt];
|
|
343
|
+
option.selected = selectedValues.has(String(option.value));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case DIFF_OP.TEXT: {
|
|
349
|
+
var textTarget = resolve(m.nid);
|
|
350
|
+
if (!textTarget) {
|
|
351
|
+
recordStaleMiss(DIFF_OP.TEXT, m.nid);
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
// No scrub here by design: textContent assignment has no HTML
|
|
355
|
+
// parse path, so markup in m.text stays literal text.
|
|
356
|
+
textTarget.textContent = m.text;
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
} catch (e) {
|
|
361
|
+
tallies.applyFailures += 1;
|
|
362
|
+
logger.warn('[Renderer] mutation apply failed', {
|
|
363
|
+
op: m && m.op ? m.op : '',
|
|
364
|
+
nid: m && (m.nid || m.parentNid || m.beforeNid || '') ? (m.nid || m.parentNid || m.beforeNid || '') : '',
|
|
365
|
+
error: e && e.message ? e.message : String(e),
|
|
366
|
+
applyFailures: tallies.applyFailures
|
|
367
|
+
});
|
|
368
|
+
// Skip individual mutation errors -- don't break the whole batch
|
|
369
|
+
if (tallies.applyFailures >= 2) {
|
|
370
|
+
requestResync('dom-mutation-apply-failed', {
|
|
371
|
+
op: m && m.op ? m.op : '',
|
|
372
|
+
nid: m && (m.nid || m.parentNid || m.beforeNid || '') ? (m.nid || m.parentNid || m.beforeNid || '') : ''
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
} catch (e) {
|
|
378
|
+
tallies.applyFailures += 1;
|
|
379
|
+
logger.warn('[Renderer] mutation batch failed', {
|
|
380
|
+
error: e && e.message ? e.message : String(e),
|
|
381
|
+
applyFailures: tallies.applyFailures
|
|
382
|
+
});
|
|
383
|
+
// Whole-batch failure escalates immediately; the caller keeps showing
|
|
384
|
+
// the last good frame (no state change here -- reference parity).
|
|
385
|
+
requestResync('dom-mutation-batch-failed', {
|
|
386
|
+
error: String(e && e.message ? e.message : e)
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
}
|