@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,366 @@
|
|
|
1
|
+
// PhantomStream renderer: render-side sanitization chokepoint (SEC-02,
|
|
2
|
+
// plan 03-02).
|
|
3
|
+
//
|
|
4
|
+
// sanitizeFragment is the named chokepoint every reconstructed-DOM
|
|
5
|
+
// insertion runs through: the diff applier's add-op fragment (template-
|
|
6
|
+
// context parse, src/renderer/diff.js) and the post-parse scrub of the
|
|
7
|
+
// loaded mirror document (src/renderer/index.js). It is DOM-FRAGMENT
|
|
8
|
+
// based by design: it walks PARSED nodes and mutates them in place --
|
|
9
|
+
// it never serializes back to a string and never re-parses. The
|
|
10
|
+
// serialize-reparse round-trip is the canonical mXSS amplifier
|
|
11
|
+
// (03-RESEARCH.md "Anti-Patterns"), which is why string-scrub-then-parse
|
|
12
|
+
// is structurally absent from this module.
|
|
13
|
+
//
|
|
14
|
+
// Policy (blocklist, fidelity-first -- 03-CONTEXT decision; defense-in-
|
|
15
|
+
// depth behind the capture-side chokepoint, threat T-03-07/T-03-08):
|
|
16
|
+
// - drop <script>/<noscript>/<object>/<embed> subtrees entirely
|
|
17
|
+
// (noscript content IS DOM in no-allow-scripts sandboxes --
|
|
18
|
+
// 03-RESEARCH Pitfall 9; the mirror iframe never has allow-scripts)
|
|
19
|
+
// - strip on* event-handler attributes, namespace-aware: attributes are
|
|
20
|
+
// enumerated on every element regardless of namespace (Pitfall 4:
|
|
21
|
+
// namespace-confusion mXSS relocates elements across HTML/SVG/MathML)
|
|
22
|
+
// - remove the srcdoc attribute (nested attacker iframe)
|
|
23
|
+
// - neutralize dangerous URL schemes (javascript:, vbscript:,
|
|
24
|
+
// data:text/html -- data:image/* stays allowed) in
|
|
25
|
+
// href/src/action/formaction/poster/data/xlink:href, and
|
|
26
|
+
// per-candidate in srcset
|
|
27
|
+
// - scrub style attribute values via scrubCssText
|
|
28
|
+
//
|
|
29
|
+
// The CSS policy in scrubCssText deliberately DUPLICATES the capture-side
|
|
30
|
+
// scrub (src/capture/index.js, 03-RESEARCH.md assumption A4): zero
|
|
31
|
+
// shared-module coupling between capture and renderer is the project
|
|
32
|
+
// shape, and the same regex policy on both sides is the documented
|
|
33
|
+
// defense-in-depth recipe.
|
|
34
|
+
//
|
|
35
|
+
// Strips are counted + logged, never silent (03-CONTEXT observability
|
|
36
|
+
// decision): one aggregated logger.warn('[Renderer] sanitization strips',
|
|
37
|
+
// ...) per sanitizeFragment call that strips anything. The counters object
|
|
38
|
+
// is caller-owned and mutated in place -- createViewer keeps it on a
|
|
39
|
+
// PER-SESSION lifecycle (03-RESEARCH Pitfall 3).
|
|
40
|
+
//
|
|
41
|
+
// Cross-runtime style per the renderer precedent: var declarations,
|
|
42
|
+
// || inline defaulting, named exports, no module-level side effects;
|
|
43
|
+
// DOM access only through the nodes passed in (the walker is created via
|
|
44
|
+
// root.ownerDocument, never an ambient document).
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {Object} SanitizeCounters
|
|
48
|
+
* @property {number} strippedHandlers on* attributes + srcdoc attributes removed
|
|
49
|
+
* @property {number} blockedUrls dangerous URL scheme values neutralized
|
|
50
|
+
* @property {number} droppedSubtrees script/noscript/object/embed subtrees removed
|
|
51
|
+
* @property {number} cssScrubs style values changed by scrubCssText
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
// Tags whose whole subtree is dropped (03-CONTEXT discretion resolved:
|
|
55
|
+
// object/embed neutralization renders as full removal -- script/noscript
|
|
56
|
+
// parity; plugin shells would not render under the sandbox anyway).
|
|
57
|
+
var DROP_TAGS = { script: true, noscript: true, object: true, embed: true };
|
|
58
|
+
|
|
59
|
+
// URL-carrying attributes whose values get the dangerous-scheme test.
|
|
60
|
+
// xlink:href is matched by qualified name -- that is how the attribute
|
|
61
|
+
// enumerates (verified against jsdom 29, 03-RESEARCH).
|
|
62
|
+
var URL_ATTRS = {
|
|
63
|
+
href: true,
|
|
64
|
+
src: true,
|
|
65
|
+
action: true,
|
|
66
|
+
formaction: true,
|
|
67
|
+
poster: true,
|
|
68
|
+
data: true,
|
|
69
|
+
'xlink:href': true
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Dangerous-scheme prefix test, control-char tolerant: HTML parsers strip
|
|
74
|
+
* control characters and whitespace inside scheme names, so the probe does
|
|
75
|
+
* too before the prefix check. Only the probe is transformed -- the
|
|
76
|
+
* original value is never rewritten here. data:image/* stays allowed;
|
|
77
|
+
* data:text/html is blocked (03-CONTEXT policy). Module-internal duplicate
|
|
78
|
+
* of the capture-side helper by design (zero shared-module coupling).
|
|
79
|
+
* @param {*} value
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
function hasDangerousScheme(value) {
|
|
83
|
+
var probe;
|
|
84
|
+
try {
|
|
85
|
+
probe = String(value == null ? '' : value)
|
|
86
|
+
.replace(/[\u0000-\u0020]+/g, '')
|
|
87
|
+
.toLowerCase();
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return true; // unstringifiable input: treat as dangerous
|
|
90
|
+
}
|
|
91
|
+
return probe.indexOf('javascript:') === 0
|
|
92
|
+
|| probe.indexOf('vbscript:') === 0
|
|
93
|
+
|| probe.indexOf('data:text/html') === 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function parseSrcsetCandidates(srcset) {
|
|
97
|
+
var raw = String(srcset == null ? '' : srcset);
|
|
98
|
+
var out = [];
|
|
99
|
+
var i = 0;
|
|
100
|
+
while (i < raw.length) {
|
|
101
|
+
while (i < raw.length && /[\s,]/.test(raw.charAt(i))) i++;
|
|
102
|
+
var urlStart = i;
|
|
103
|
+
var isData = raw.slice(i, i + 5).toLowerCase() === 'data:';
|
|
104
|
+
while (i < raw.length
|
|
105
|
+
&& !/\s/.test(raw.charAt(i))
|
|
106
|
+
&& (isData || raw.charAt(i) !== ',')) {
|
|
107
|
+
i++;
|
|
108
|
+
}
|
|
109
|
+
var url = raw.slice(urlStart, i);
|
|
110
|
+
while (i < raw.length && /\s/.test(raw.charAt(i))) i++;
|
|
111
|
+
var descriptorStart = i;
|
|
112
|
+
while (i < raw.length && raw.charAt(i) !== ',') i++;
|
|
113
|
+
var descriptor = raw.slice(descriptorStart, i).trim();
|
|
114
|
+
if (url) out.push({ url: url, descriptor: descriptor });
|
|
115
|
+
if (raw.charAt(i) === ',') i++;
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function formatSrcsetCandidate(candidate) {
|
|
121
|
+
return candidate.descriptor ? candidate.url + ' ' + candidate.descriptor : candidate.url;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Neutralize dangerous srcset candidates per-candidate; benign candidates
|
|
126
|
+
* pass through. Returns the rebuilt value plus the blocked count so the
|
|
127
|
+
* caller only rewrites the attribute when something was actually blocked
|
|
128
|
+
* (fidelity: benign srcset values stay byte-identical -- a naive split on
|
|
129
|
+
* commas would otherwise mangle data: URLs in legitimate candidates).
|
|
130
|
+
* @param {*} value
|
|
131
|
+
* @returns {{value: string, blocked: number}}
|
|
132
|
+
*/
|
|
133
|
+
function neutralizeSrcset(value) {
|
|
134
|
+
var raw = String(value == null ? '' : value);
|
|
135
|
+
var kept = [];
|
|
136
|
+
var blocked = 0;
|
|
137
|
+
var candidates = parseSrcsetCandidates(raw);
|
|
138
|
+
for (var i = 0; i < candidates.length; i++) {
|
|
139
|
+
if (hasDangerousScheme(candidates[i].url)) {
|
|
140
|
+
blocked += 1;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
kept.push(formatSrcsetCandidate(candidates[i]));
|
|
144
|
+
}
|
|
145
|
+
return { value: kept.join(', '), blocked: blocked };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Targeted CSS value scrub -- the only string-operation pass in the render
|
|
150
|
+
* pipeline, and it is a VALUE scrub (regex), never a parser. Same policy
|
|
151
|
+
* as the capture-side scrub (03-RESEARCH.md A4, src/capture/index.js):
|
|
152
|
+
* - url() with an explicit non-http(s)/non-data:image scheme has its
|
|
153
|
+
* contents replaced with about:blank; scheme-less (relative) url()
|
|
154
|
+
* values pass unchanged
|
|
155
|
+
* - expression() and -moz-binding (legacy script-execution vectors) are
|
|
156
|
+
* neutralized
|
|
157
|
+
* - non-http(s) CSS import targets are neutralized (string and url() forms)
|
|
158
|
+
* - a literal </style sequence is rewritten so captured CSS can never
|
|
159
|
+
* break out of the style tag it is emitted into at the string-assembly
|
|
160
|
+
* layer (src/renderer/snapshot.js)
|
|
161
|
+
* Containment: a scrub failure returns the input unchanged (absolutifyUrl
|
|
162
|
+
* discipline) -- the sandbox + CSP layers backstop CSS regardless.
|
|
163
|
+
* @param {*} css
|
|
164
|
+
* @returns {string}
|
|
165
|
+
*/
|
|
166
|
+
export function scrubCssText(css) {
|
|
167
|
+
var input = String(css == null ? '' : css);
|
|
168
|
+
try {
|
|
169
|
+
var out = input;
|
|
170
|
+
out = out.replace(/url\(\s*(?:"([^"]*)"|'([^']*)'|([^)"'][^)]*))?\s*\)/gi,
|
|
171
|
+
function (match, dq, sq, bare) {
|
|
172
|
+
var inner = dq !== undefined ? dq : (sq !== undefined ? sq : (bare || ''));
|
|
173
|
+
var probe = String(inner || '').replace(/[\u0000-\u0020]+/g, '').toLowerCase();
|
|
174
|
+
var scheme = /^([a-z][a-z0-9+.-]*):/.exec(probe);
|
|
175
|
+
if (!scheme) return match; // relative URL: allowed
|
|
176
|
+
if (scheme[1] === 'http' || scheme[1] === 'https') return match;
|
|
177
|
+
if (scheme[1] === 'data' && probe.indexOf('data:image/') === 0) return match;
|
|
178
|
+
return 'url(about:blank)';
|
|
179
|
+
});
|
|
180
|
+
out = out.replace(/expression\s*\(/gi, 'blocked(');
|
|
181
|
+
out = out.replace(/-moz-binding/gi, 'blocked-binding');
|
|
182
|
+
out = out.replace(/@import\b(\s*(?:url\(\s*)?['"]?\s*)([^'");\s]*)/gi, function (match, lead, target) {
|
|
183
|
+
var probe = String(target || '').replace(/[\u0000-\u0020]+/g, '').toLowerCase();
|
|
184
|
+
if (probe.indexOf('http:') === 0 || probe.indexOf('https:') === 0) return match;
|
|
185
|
+
return '@import-blocked' + lead + 'about:blank';
|
|
186
|
+
});
|
|
187
|
+
out = out.replace(/<\/style/gi, '<\\/style');
|
|
188
|
+
return out;
|
|
189
|
+
} catch (e) {
|
|
190
|
+
return input;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Attr-op-shaped scrub dispatch for the diff applier's ATTR branch
|
|
196
|
+
* (defense-in-depth render side of 03-RESEARCH Pitfall 5): the caller
|
|
197
|
+
* applies the returned value via setAttribute, or skips the op entirely
|
|
198
|
+
* when drop is true. Pure value transform -- no counters, no DOM; the
|
|
199
|
+
* caller owns counting (its counters, its lifecycle).
|
|
200
|
+
* @param {*} name Attribute name from the wire (untrusted)
|
|
201
|
+
* @param {*} value Attribute value from the wire (untrusted)
|
|
202
|
+
* @returns {{drop: boolean, value: string|null}}
|
|
203
|
+
*/
|
|
204
|
+
export function sanitizeAttrValue(name, value) {
|
|
205
|
+
var n;
|
|
206
|
+
var v;
|
|
207
|
+
try {
|
|
208
|
+
n = String(name == null ? '' : name).toLowerCase();
|
|
209
|
+
v = String(value == null ? '' : value); // identity for wire strings
|
|
210
|
+
} catch (e) {
|
|
211
|
+
return { drop: true, value: '' };
|
|
212
|
+
}
|
|
213
|
+
if (n.indexOf('on') === 0) return { drop: true, value: '' };
|
|
214
|
+
if (n === 'srcdoc') return { drop: true, value: '' };
|
|
215
|
+
if (URL_ATTRS[n] === true) {
|
|
216
|
+
if (hasDangerousScheme(v)) return { drop: false, value: null };
|
|
217
|
+
return { drop: false, value: v };
|
|
218
|
+
}
|
|
219
|
+
if (n === 'srcset') {
|
|
220
|
+
var rebuilt = neutralizeSrcset(v);
|
|
221
|
+
return { drop: false, value: rebuilt.blocked > 0 ? rebuilt.value : v };
|
|
222
|
+
}
|
|
223
|
+
if (n === 'style') {
|
|
224
|
+
return { drop: false, value: scrubCssText(v) };
|
|
225
|
+
}
|
|
226
|
+
return { drop: false, value: v };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The render-side chokepoint: walk root (DocumentFragment | Element) plus
|
|
231
|
+
* every descendant element and scrub in place per the module-top policy.
|
|
232
|
+
* Collect-then-mutate (the capture serializer's TreeWalker precedent,
|
|
233
|
+
* src/capture/index.js:596-615): the walker is live, so every element is
|
|
234
|
+
* gathered into an array BEFORE any removal. Elements detached by an
|
|
235
|
+
* earlier subtree drop are skipped (never double-counted). Mutates in
|
|
236
|
+
* place -- never serializes.
|
|
237
|
+
*
|
|
238
|
+
* Containment: never throws. A failed walk warns and returns; a single
|
|
239
|
+
* hostile element's scrub failure warns and the walk continues.
|
|
240
|
+
*
|
|
241
|
+
* @param {DocumentFragment|Element|null} root
|
|
242
|
+
* @param {SanitizeCounters} [counters] Mutated in place; caller-owned
|
|
243
|
+
* lifecycle (per-session in createViewer -- 03-RESEARCH Pitfall 3)
|
|
244
|
+
* @param {{warn: function(...*): void}} [logger]
|
|
245
|
+
* @returns {void}
|
|
246
|
+
*/
|
|
247
|
+
export function sanitizeFragment(root, counters, logger) {
|
|
248
|
+
if (!root || !root.ownerDocument) return;
|
|
249
|
+
var tallies = counters || {
|
|
250
|
+
strippedHandlers: 0, blockedUrls: 0, droppedSubtrees: 0, cssScrubs: 0
|
|
251
|
+
};
|
|
252
|
+
var log = logger && typeof logger.warn === 'function'
|
|
253
|
+
? logger
|
|
254
|
+
: { warn: function () {} };
|
|
255
|
+
var before = {
|
|
256
|
+
strippedHandlers: tallies.strippedHandlers,
|
|
257
|
+
blockedUrls: tallies.blockedUrls,
|
|
258
|
+
droppedSubtrees: tallies.droppedSubtrees,
|
|
259
|
+
cssScrubs: tallies.cssScrubs
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
var elements = [];
|
|
263
|
+
try {
|
|
264
|
+
if (root.nodeType === 1) elements.push(root); // Element roots scrub themselves too
|
|
265
|
+
var walker = root.ownerDocument.createTreeWalker(root, 1 /* NodeFilter.SHOW_ELEMENT */, null);
|
|
266
|
+
var node = walker.nextNode();
|
|
267
|
+
while (node) {
|
|
268
|
+
elements.push(node);
|
|
269
|
+
node = walker.nextNode();
|
|
270
|
+
}
|
|
271
|
+
} catch (e) {
|
|
272
|
+
log.warn('[Renderer] sanitization walk failed', {
|
|
273
|
+
error: e && e.message ? e.message : String(e)
|
|
274
|
+
});
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
for (var i = 0; i < elements.length; i++) {
|
|
279
|
+
var el = elements[i];
|
|
280
|
+
try {
|
|
281
|
+
// Skip elements detached by an earlier subtree drop: their dropped
|
|
282
|
+
// ancestor already left the tree, so they are unreachable from root.
|
|
283
|
+
if (el !== root && !root.contains(el)) continue;
|
|
284
|
+
|
|
285
|
+
var tag = el.tagName ? String(el.tagName).toLowerCase() : '';
|
|
286
|
+
if (DROP_TAGS[tag] === true) {
|
|
287
|
+
if (el.parentNode) el.parentNode.removeChild(el);
|
|
288
|
+
tallies.droppedSubtrees += 1;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (tag === 'style') {
|
|
292
|
+
var styleText = el.textContent || '';
|
|
293
|
+
var scrubbedStyleText = scrubCssText(styleText);
|
|
294
|
+
if (scrubbedStyleText !== styleText) {
|
|
295
|
+
el.textContent = scrubbedStyleText;
|
|
296
|
+
tallies.cssScrubs += 1;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Enumerate attribute names FIRST (removal mutates the live
|
|
301
|
+
// NamedNodeMap), on every element regardless of namespace.
|
|
302
|
+
var names = [];
|
|
303
|
+
var attrs = el.attributes;
|
|
304
|
+
for (var a = 0; a < attrs.length; a++) names.push(attrs[a].name);
|
|
305
|
+
|
|
306
|
+
for (var x = 0; x < names.length; x++) {
|
|
307
|
+
var name = names[x];
|
|
308
|
+
var lower = String(name).toLowerCase();
|
|
309
|
+
if (lower.indexOf('on') === 0) {
|
|
310
|
+
el.removeAttribute(name);
|
|
311
|
+
tallies.strippedHandlers += 1;
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (lower === 'srcdoc') {
|
|
315
|
+
el.removeAttribute(name);
|
|
316
|
+
tallies.strippedHandlers += 1;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (URL_ATTRS[lower] === true) {
|
|
320
|
+
var attrNode = el.getAttributeNode(name);
|
|
321
|
+
if (attrNode && hasDangerousScheme(attrNode.value)) {
|
|
322
|
+
el.removeAttributeNode(attrNode);
|
|
323
|
+
tallies.blockedUrls += 1;
|
|
324
|
+
}
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (lower === 'srcset') {
|
|
328
|
+
var rebuilt = neutralizeSrcset(el.getAttribute(name));
|
|
329
|
+
if (rebuilt.blocked > 0) {
|
|
330
|
+
el.setAttribute(name, rebuilt.value);
|
|
331
|
+
tallies.blockedUrls += rebuilt.blocked;
|
|
332
|
+
}
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (lower === 'style') {
|
|
336
|
+
var styleVal = el.getAttribute(name);
|
|
337
|
+
var scrubbed = scrubCssText(styleVal);
|
|
338
|
+
if (scrubbed !== styleVal) {
|
|
339
|
+
el.setAttribute(name, scrubbed);
|
|
340
|
+
tallies.cssScrubs += 1;
|
|
341
|
+
}
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
} catch (e) {
|
|
346
|
+
// Per-element containment: one hostile element never aborts the walk.
|
|
347
|
+
log.warn('[Renderer] sanitization element scrub failed', {
|
|
348
|
+
error: e && e.message ? e.message : String(e)
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
var strippedHandlers = tallies.strippedHandlers - before.strippedHandlers;
|
|
354
|
+
var blockedUrls = tallies.blockedUrls - before.blockedUrls;
|
|
355
|
+
var droppedSubtrees = tallies.droppedSubtrees - before.droppedSubtrees;
|
|
356
|
+
var cssScrubs = tallies.cssScrubs - before.cssScrubs;
|
|
357
|
+
if (strippedHandlers || blockedUrls || droppedSubtrees || cssScrubs) {
|
|
358
|
+
// ONE aggregated warn per call -- counted + logged, never silent.
|
|
359
|
+
log.warn('[Renderer] sanitization strips', {
|
|
360
|
+
strippedHandlers: strippedHandlers,
|
|
361
|
+
blockedUrls: blockedUrls,
|
|
362
|
+
droppedSubtrees: droppedSubtrees,
|
|
363
|
+
cssScrubs: cssScrubs
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// PhantomStream renderer: pure snapshot HTML builder.
|
|
2
|
+
//
|
|
3
|
+
// Verbatim port of the FSB reference viewer's srcdoc assembly
|
|
4
|
+
// (reference/dashboard/dashboard.js:2671-2694 escapePreviewAttribute +
|
|
5
|
+
// buildShellAttributeString, :2785-2800 full-page assembly inside
|
|
6
|
+
// handleDOMSnapshot), extracted as a pure transform: SnapshotPayload in,
|
|
7
|
+
// srcdoc string out. No DOM access, no module-level side effects -- the
|
|
8
|
+
// caller (createViewer, plan 02-03) owns the iframe write.
|
|
9
|
+
//
|
|
10
|
+
// Phase 3 (plan 03-02, SEC-02): the adopted CSP meta is injected as the
|
|
11
|
+
// first element after <head>, and inlineStyles entries are CSS-scrubbed at
|
|
12
|
+
// assembly via scrubCssText (a pure string pass -- this module stays
|
|
13
|
+
// DOM-free per the contract above; the DOM-fragment chokepoint lives in
|
|
14
|
+
// sanitize.js and may never land here). payload.html stays raw AT THE
|
|
15
|
+
// STRING LAYER by design: string-level scrubbing is the mXSS anti-pattern
|
|
16
|
+
// (scrub-then-reparse), so that insertion point is protected by the
|
|
17
|
+
// layered chain instead -- capture chokepoint upstream, the viewer's
|
|
18
|
+
// post-parse sanitizeFragment scrub on load (src/renderer/index.js), the
|
|
19
|
+
// CSP meta below, and the allow-same-origin-only sandbox.
|
|
20
|
+
//
|
|
21
|
+
// Wire-value insertion-point inventory (keep ACCURATE -- this list is what
|
|
22
|
+
// the sanitization chokepoint audits; review WR-03):
|
|
23
|
+
// 1. inlineStyles entries -- CSS-scrubbed via scrubCssText
|
|
24
|
+
// (03-02; was RAW)
|
|
25
|
+
// 2. payload.html -- intentionally RAW at the string
|
|
26
|
+
// layer; defense chain: capture
|
|
27
|
+
// chokepoint + post-parse scrub +
|
|
28
|
+
// CSP meta + sandbox (03-02)
|
|
29
|
+
// 3. stylesheet hrefs -- dangerous schemes filtered, then
|
|
30
|
+
// double quotes escaped
|
|
31
|
+
// 4. html/body shell attrs + styles -- attrs escapeAttribute; styleText
|
|
32
|
+
// scrubCssText + escapeAttribute
|
|
33
|
+
// 5. viewportWidth -- numerically coerced
|
|
34
|
+
// (parseInt(_, 10) || 1920), never
|
|
35
|
+
// interpolated raw: the typed
|
|
36
|
+
// contract says number, but wire
|
|
37
|
+
// values are not trusted
|
|
38
|
+
|
|
39
|
+
import { scrubCssText } from './sanitize.js';
|
|
40
|
+
|
|
41
|
+
/** @typedef {import('../protocol/messages.js').SnapshotPayload} SnapshotPayload */
|
|
42
|
+
|
|
43
|
+
// The ADOPTED srcdoc CSP (backstop behind both sanitization chokepoints,
|
|
44
|
+
// delivered via meta because the mirror is never a fetched URL). Baseline
|
|
45
|
+
// from 03-CONTEXT with ONE documented adjustment under the decision's own
|
|
46
|
+
// clause ("adjust only with documented rationale if mirror fidelity
|
|
47
|
+
// requires, never weaker than script-blocking"): style-src gains
|
|
48
|
+
// http: https: because the capture deliberately emits external stylesheet
|
|
49
|
+
// links (stylesheets[] collection, src/capture/index.js) and
|
|
50
|
+
// 'unsafe-inline' alone would block every link-rel-stylesheet load in the
|
|
51
|
+
// mirror, breaking real-world fidelity from Phase 4 on. Script-blocking is
|
|
52
|
+
// untouched: default-src 'none' still governs scripts (no script-src is
|
|
53
|
+
// introduced). Meta-unsupported directives are deliberately absent
|
|
54
|
+
// (03-RESEARCH Pitfall 8); the iframe-level sandbox attribute asserted in
|
|
55
|
+
// createViewer is the analogous control. Documented in docs/SECURITY.md
|
|
56
|
+
// (plan 03-05).
|
|
57
|
+
var CSP_META = '<meta http-equiv="Content-Security-Policy" content="'
|
|
58
|
+
+ "default-src 'none'; "
|
|
59
|
+
+ 'img-src http: https: data:; '
|
|
60
|
+
+ "style-src http: https: 'unsafe-inline'; "
|
|
61
|
+
+ 'font-src http: https: data:'
|
|
62
|
+
+ '">';
|
|
63
|
+
|
|
64
|
+
function hasDangerousStylesheetUrl(value) {
|
|
65
|
+
if (!value || typeof value !== 'string') return false;
|
|
66
|
+
var compact = value.replace(/[\u0000-\u0020]+/g, '').toLowerCase();
|
|
67
|
+
return compact.indexOf('javascript:') === 0
|
|
68
|
+
|| compact.indexOf('vbscript:') === 0
|
|
69
|
+
|| compact.indexOf('data:text/html') === 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function escapeStyleSourceId(value) {
|
|
73
|
+
return escapeAttribute(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function styleSourceTagsForDocument(styleSources) {
|
|
77
|
+
var sources = Array.isArray(styleSources) ? styleSources.slice() : [];
|
|
78
|
+
sources.sort(function (a, b) {
|
|
79
|
+
return (a && typeof a.order === 'number' ? a.order : 0)
|
|
80
|
+
- (b && typeof b.order === 'number' ? b.order : 0);
|
|
81
|
+
});
|
|
82
|
+
return sources.filter(function (source) {
|
|
83
|
+
return source && source.scope && source.scope.kind === 'document';
|
|
84
|
+
}).map(function (source) {
|
|
85
|
+
var sourceId = escapeStyleSourceId(source.sourceId || '');
|
|
86
|
+
if (source.href && !hasDangerousStylesheetUrl(source.href)) {
|
|
87
|
+
return '<link rel="stylesheet" data-ps-style-source-id="' + sourceId +
|
|
88
|
+
'" href="' + String(source.href).replace(/"/g, '"') + '">';
|
|
89
|
+
}
|
|
90
|
+
return '<style data-ps-style-source-id="' + sourceId + '">' +
|
|
91
|
+
scrubCssText(source.cssText || '') +
|
|
92
|
+
'</style>';
|
|
93
|
+
}).join('\n');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Escape a value for inclusion inside a double-quoted HTML attribute.
|
|
98
|
+
* Renamed port of escapePreviewAttribute (dashboard.js:2671-2677): null and
|
|
99
|
+
* undefined map to '', then &, ", <, > are escaped in that exact order
|
|
100
|
+
* (&-first so the other replacements are never themselves re-escaped).
|
|
101
|
+
* @param {*} value
|
|
102
|
+
* @returns {string}
|
|
103
|
+
*/
|
|
104
|
+
export function escapeAttribute(value) {
|
|
105
|
+
return String(value == null ? '' : value)
|
|
106
|
+
.replace(/&/g, '&')
|
|
107
|
+
.replace(/"/g, '"')
|
|
108
|
+
.replace(/</g, '<')
|
|
109
|
+
.replace(/>/g, '>');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Build the attribute string for an html/body shell tag. Verbatim port of
|
|
114
|
+
* dashboard.js:2679-2694: names are lowercased and must match
|
|
115
|
+
* /^[a-z][a-z0-9_:.~-]*$/; 'style' and any on*-prefixed name are dropped
|
|
116
|
+
* (shell event-handler attributes never reach the mirror); null/undefined
|
|
117
|
+
* values are dropped; trimmed styleText is scrubbed and appended last as
|
|
118
|
+
* style="...". Values are escaped via escapeAttribute.
|
|
119
|
+
* @param {Object|null|undefined} attrs Captured shell attributes (name -> value)
|
|
120
|
+
* @param {string|null|undefined} styleText Shell computed style text
|
|
121
|
+
* @returns {string} ' name="value" ...' (leading-space-prefixed) or ''
|
|
122
|
+
*/
|
|
123
|
+
export function buildShellAttributeString(attrs, styleText) {
|
|
124
|
+
var parts = [];
|
|
125
|
+
if (attrs && typeof attrs === 'object') {
|
|
126
|
+
Object.keys(attrs).forEach(function (rawName) {
|
|
127
|
+
var name = String(rawName || '').toLowerCase();
|
|
128
|
+
if (!/^[a-z][a-z0-9_:.~-]*$/.test(name)) return;
|
|
129
|
+
if (name === 'style' || name.indexOf('on') === 0) return;
|
|
130
|
+
var value = attrs[rawName];
|
|
131
|
+
if (value === undefined || value === null) return;
|
|
132
|
+
parts.push(name + '="' + escapeAttribute(value) + '"');
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
var style = scrubCssText(String(styleText || '')).trim();
|
|
136
|
+
if (style) parts.push('style="' + escapeAttribute(style) + '"');
|
|
137
|
+
return parts.length ? ' ' + parts.join(' ') : '';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Build the full mirror srcdoc string for a snapshot payload. Assembly per
|
|
142
|
+
* dashboard.js:2785-2800 extended by 03-02, in order: doctype + html shell
|
|
143
|
+
* attrs (from payload.htmlAttrs/payload.htmlStyle) + head with the adopted
|
|
144
|
+
* CSP meta FIRST, charset meta, viewport meta
|
|
145
|
+
* width=(parseInt(payload.viewportWidth, 10) || 1920) -- numerically
|
|
146
|
+
* coerced, never raw (review WR-03), stylesheet links (each with
|
|
147
|
+
* ONLY double quotes escaped to "), inline style tags CSS-scrubbed
|
|
148
|
+
* via scrubCssText, the exact parity reset-CSS style tag (02-UI-SPEC
|
|
149
|
+
* "Mirror page reset CSS") + body shell attrs
|
|
150
|
+
* (payload.bodyAttrs/payload.bodyStyle) + payload.html raw at the string
|
|
151
|
+
* layer (see the insertion-point inventory above) + closing tags.
|
|
152
|
+
* Pure transform: no DOM access anywhere.
|
|
153
|
+
* @param {SnapshotPayload} payload
|
|
154
|
+
* @returns {string}
|
|
155
|
+
*/
|
|
156
|
+
export function buildSnapshotHtml(payload) {
|
|
157
|
+
var p = payload || {};
|
|
158
|
+
|
|
159
|
+
var stylesheetLinks = (p.stylesheets || [])
|
|
160
|
+
.filter(function (url) { return !hasDangerousStylesheetUrl(url); })
|
|
161
|
+
.map(function (url) {
|
|
162
|
+
return '<link rel="stylesheet" href="' + url.replace(/"/g, '"') + '">';
|
|
163
|
+
}).join('\n');
|
|
164
|
+
|
|
165
|
+
var inlineStyleTags = (p.inlineStyles || []).map(function (css) {
|
|
166
|
+
// CSS scrub at assembly (03-02): pure string pass; the scrub also
|
|
167
|
+
// rewrites any literal style-closing sequence so captured CSS cannot
|
|
168
|
+
// break out of this tag.
|
|
169
|
+
return '<style>' + scrubCssText(css) + '</style>';
|
|
170
|
+
}).join('\n');
|
|
171
|
+
var cssomStyleTags = styleSourceTagsForDocument(p.styleSources || []);
|
|
172
|
+
|
|
173
|
+
var htmlAttrs = buildShellAttributeString(p.htmlAttrs, p.htmlStyle);
|
|
174
|
+
var bodyAttrs = buildShellAttributeString(p.bodyAttrs, p.bodyStyle);
|
|
175
|
+
|
|
176
|
+
// CSP meta FIRST after <head> so the policy applies before any
|
|
177
|
+
// parser-initiated fetch (03-RESEARCH "CSP meta injection").
|
|
178
|
+
return '<!DOCTYPE html><html' + htmlAttrs + '><head>' + CSP_META + '<meta charset="UTF-8">' +
|
|
179
|
+
'<meta name="viewport" content="width=' + (parseInt(p.viewportWidth, 10) || 1920) + '">' +
|
|
180
|
+
stylesheetLinks +
|
|
181
|
+
inlineStyleTags +
|
|
182
|
+
cssomStyleTags +
|
|
183
|
+
'<style>body { margin: 0; overflow: hidden; } *::selection { background: transparent; } ::-webkit-scrollbar { display: none; }</style>' +
|
|
184
|
+
'</head><body' + bodyAttrs + '>' + (p.html || '') + '</body></html>';
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function buildFramePlaceholderHtml(frame) {
|
|
188
|
+
var f = frame || {};
|
|
189
|
+
var label = escapeAttribute(f.label || 'Cross-origin iframe');
|
|
190
|
+
var origin = escapeAttribute(f.origin || '');
|
|
191
|
+
var src = escapeAttribute(f.src || '');
|
|
192
|
+
var meta = '';
|
|
193
|
+
if (origin) meta += '<p>Origin: ' + origin + '</p>';
|
|
194
|
+
if (src) meta += '<p>Source: ' + src + '</p>';
|
|
195
|
+
return '<!DOCTYPE html><html><head>' + CSP_META + '<meta charset="UTF-8">' +
|
|
196
|
+
'<style>body{margin:0;font:13px system-ui,sans-serif;color:#30333a;background:#f6f7f9;}' +
|
|
197
|
+
'.ps-frame-placeholder{box-sizing:border-box;min-height:100vh;display:flex;flex-direction:column;gap:6px;' +
|
|
198
|
+
'justify-content:center;align-items:center;text-align:center;border:1px dashed #9aa3af;padding:16px;}' +
|
|
199
|
+
'.ps-frame-placeholder strong{font-size:14px;} .ps-frame-placeholder p{margin:0;color:#5f6673;word-break:break-word;}</style>' +
|
|
200
|
+
'</head><body><div class="ps-frame-placeholder" role="note"><strong>' + label + '</strong>' +
|
|
201
|
+
meta + '</div></body></html>';
|
|
202
|
+
}
|