@xbrowser/cli 0.14.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.
Files changed (55) hide show
  1. package/README.md +858 -0
  2. package/dist/admin-6UTU2RZ2.js +281 -0
  3. package/dist/admin-MDGF4CET.js +285 -0
  4. package/dist/admin-RPJJ5CAF.js +282 -0
  5. package/dist/browser-GWBH6OJK.js +46 -0
  6. package/dist/browser-I2HJZ7IP.js +48 -0
  7. package/dist/browser-R7B255ML.js +46 -0
  8. package/dist/chunk-2ONMTDLK.js +2050 -0
  9. package/dist/chunk-3RG5ZIWI.js +10 -0
  10. package/dist/chunk-43VX3TYN.js +83 -0
  11. package/dist/chunk-ATFTAKMN.js +267 -0
  12. package/dist/chunk-DESA2KMG.js +77 -0
  13. package/dist/chunk-DTJRVA76.js +206 -0
  14. package/dist/chunk-F3ZWFCJJ.js +2051 -0
  15. package/dist/chunk-FF5WHQHN.js +135 -0
  16. package/dist/chunk-HINTG75P.js +77 -0
  17. package/dist/chunk-KDYXFLAC.js +1503 -0
  18. package/dist/chunk-KTSQU4QT.js +29 -0
  19. package/dist/chunk-L53IDAWK.js +68 -0
  20. package/dist/chunk-M7CMBPCA.js +100 -0
  21. package/dist/chunk-NFGO7J2I.js +29 -0
  22. package/dist/chunk-OLB6UJ25.js +438 -0
  23. package/dist/chunk-OPRXFZVE.js +52 -0
  24. package/dist/chunk-RS6YYWTK.js +685 -0
  25. package/dist/chunk-VEDJ5XSQ.js +196 -0
  26. package/dist/chunk-VEKPHQBR.js +47 -0
  27. package/dist/chunk-VUJDJCIN.js +437 -0
  28. package/dist/chunk-YEN2ODUI.js +14 -0
  29. package/dist/chunk-ZZ2TFWIV.js +1382 -0
  30. package/dist/cli.js +11012 -0
  31. package/dist/convert-4DUWZIKH.js +205 -0
  32. package/dist/convert-EKQVHKB4.js +11 -0
  33. package/dist/daemon-client-3IJD6X4B.js +59 -0
  34. package/dist/daemon-client-GX2UYIW4.js +241 -0
  35. package/dist/daemon-client-XWSSQBEA.js +58 -0
  36. package/dist/daemon-main.js +9910 -0
  37. package/dist/extract-EGRXZSSK.js +67 -0
  38. package/dist/extract-JUOQQX4V.js +11 -0
  39. package/dist/filter-OLAE26HN.js +51 -0
  40. package/dist/filter-VID2GGZ7.js +9 -0
  41. package/dist/human-interaction-QPHNDD76.js +8 -0
  42. package/dist/index.d.ts +2313 -0
  43. package/dist/index.js +13839 -0
  44. package/dist/marketplace-FCVN5OTZ.js +706 -0
  45. package/dist/marketplace-FPT5YLKB.js +351 -0
  46. package/dist/marketplace-W545W4FR.js +706 -0
  47. package/dist/network-store-2S5HATEV.js +194 -0
  48. package/dist/network-store-BN6QEZ7R.js +196 -0
  49. package/dist/network-store-YAF5OIBH.js +12 -0
  50. package/dist/parse-action-dsl-DRSPBALP.js +72 -0
  51. package/dist/parse-action-dsl-T3DYC33D.js +74 -0
  52. package/dist/proxy-WKGUCH2C.js +7 -0
  53. package/dist/session-recorder-ILSSV2UC.js +6 -0
  54. package/dist/session-recorder-XET3DNML.js +7 -0
  55. package/package.json +111 -0
@@ -0,0 +1,1382 @@
1
+ // src/cdp-interceptor/proxy.ts
2
+ import { WebSocketServer, WebSocket } from "ws";
3
+
4
+ // src/cdp-interceptor/rules/shared.ts
5
+ var PLAYWRIGHT_INTERNAL_MARKERS = [
6
+ "__commonJS",
7
+ "module.exports",
8
+ "__require",
9
+ "__toESM",
10
+ "inject_utils"
11
+ ];
12
+ function isPlaywrightInternal(code) {
13
+ return PLAYWRIGHT_INTERNAL_MARKERS.some((marker) => code.includes(marker));
14
+ }
15
+ function extractUserCode(ctx) {
16
+ if (ctx.method === "Runtime.evaluate") {
17
+ const expr = ctx.params.expression;
18
+ if (typeof expr === "string") {
19
+ if (isPlaywrightInternal(expr)) return null;
20
+ return expr;
21
+ }
22
+ }
23
+ if (ctx.method === "Runtime.callFunctionOn") {
24
+ const decl = ctx.params.functionDeclaration;
25
+ if (typeof decl === "string" && decl.includes("utilityScript.evaluate")) {
26
+ return extractAllStrings(ctx.params.arguments);
27
+ }
28
+ if (typeof decl === "string") return decl;
29
+ }
30
+ return null;
31
+ }
32
+ function extractAllStrings(rawArgs) {
33
+ if (!Array.isArray(rawArgs)) return null;
34
+ const strings = [];
35
+ for (const arg of rawArgs) {
36
+ if (arg && typeof arg === "object" && "value" in arg) {
37
+ const val = arg.value;
38
+ if (typeof val === "string" && val.length > 5 && !["true", "false"].includes(val)) {
39
+ strings.push(val);
40
+ }
41
+ }
42
+ }
43
+ return strings.length > 0 ? strings.join("\n") : null;
44
+ }
45
+
46
+ // src/cdp-interceptor/rules/dom-mutation.ts
47
+ var DOM_PATTERNS = [
48
+ // ── P0: Value/Checked (bypasses React onChange) ────────────
49
+ { pattern: /\.value\s*=\s*(?!["'\s]*$)/, name: ".value =", severity: "danger", action: "block", errorCode: -32001, suggestion: "Use page.fill(selector, value) which dispatches proper input/change events." },
50
+ { pattern: /\.checked\s*=\s*(?:true|false)/, name: ".checked =", severity: "danger", action: "block", errorCode: -32001, suggestion: "Use page.check(selector) or page.uncheck(selector)." },
51
+ { pattern: /\.indeterminate\s*=\s*true/, name: ".indeterminate =", severity: "warn", action: "block", errorCode: -32021, suggestion: "No human can set indeterminate state \u2014 remove this call." },
52
+ { pattern: /\.valueAsDate\s*=/, name: ".valueAsDate =", severity: "warn", action: "block", errorCode: -32022, suggestion: "Use page.fill() with formatted date string instead." },
53
+ { pattern: /\.valueAsNumber\s*=/, name: ".valueAsNumber =", severity: "warn", action: "block", errorCode: -32022, suggestion: "Use page.fill() with numeric string instead." },
54
+ // ── P1: Select/Option (bypasses React onChange on select) ─
55
+ { pattern: /\.selectedIndex\s*=\s*\d+/, name: ".selectedIndex =", severity: "danger", action: "block", errorCode: -32003, suggestion: "Use page.selectOption(selector, value)." },
56
+ { pattern: /(?:options|children)\s*\[[^\]]*\]\s*\.\s*selected\s*=\s*(?:true|false)/, name: "options[N].selected =", severity: "danger", action: "block", errorCode: -32003, suggestion: "Use page.selectOption(selector, value)." },
57
+ { pattern: /\.value\s*=\s*["'][^"']*["']\s*[;)]?\s*$/, name: "selectElement.value =", severity: "warn", action: "block", errorCode: -32023, suggestion: "For select elements, use page.selectOption(selector, value)." },
58
+ // ── P2: Content properties (bypasses virtual DOM diffing) ──
59
+ { pattern: /\.innerHTML\s*=/, name: ".innerHTML =", severity: "info", action: "pass", errorCode: -32007, suggestion: ".innerHTML bypasses React/Vue diffing. Use component state or page.setContent()." },
60
+ { pattern: /\.outerHTML\s*=/, name: ".outerHTML =", severity: "info", action: "pass", errorCode: -32007, suggestion: "outerHTML replacement destroys React fiber tree. Use page.setContent()." },
61
+ { pattern: /\.innerText\s*=/, name: ".innerText =", severity: "warn", action: "block", errorCode: -32024, suggestion: "Framework components should be updated via state, not innerText." },
62
+ { pattern: /\.textContent\s*=/, name: ".textContent =", severity: "warn", action: "block", errorCode: -32024, suggestion: "textContent bypasses React/Vue diffing. Use component state instead." },
63
+ { pattern: /\.outerText\s*=/, name: ".outerText =", severity: "warn", action: "block", errorCode: -32024, suggestion: "Non-standard property \u2014 use proper framework update methods." },
64
+ { pattern: /nodeValue\s*=/, name: "node.nodeValue =", severity: "info", action: "block", errorCode: -32025, suggestion: "Direct text node mutation. Use textContent instead if needed." },
65
+ // ── P3: Style properties ──────────────────────────────────
66
+ { pattern: /\.style\s*=\s*["']/, name: ".style = (string override)", severity: "info", action: "pass", errorCode: -32008, suggestion: 'Setting style as a string overwrites CSSStyleDeclaration. Use element.style.prop = "value".' },
67
+ { pattern: /\.style\.cssText\s*=/, name: ".style.cssText =", severity: "info", action: "pass", errorCode: -32008, suggestion: "style.cssText replacement destroys inline styles \u2014 use individual property set." },
68
+ { pattern: /\.style\.setProperty\s*\(/, name: ".style.setProperty()", severity: "info", action: "pass", errorCode: -32008, suggestion: "Direct style property set bypasses CSS transitions." },
69
+ { pattern: /\.style\.removeProperty\s*\(/, name: ".style.removeProperty()", severity: "info", action: "pass", errorCode: -32026, suggestion: "Style removal without user interaction is suspicious." },
70
+ { pattern: /\.style\.animation\s*=/, name: ".style.animation =", severity: "info", action: "pass", errorCode: -32027, suggestion: "Forcing animation state via CDP is detectable." },
71
+ { pattern: /\.style\.transition\s*=/, name: ".style.transition =", severity: "info", action: "pass", errorCode: -32027, suggestion: "Manipulating CSS transitions is rare in normal browsing." },
72
+ // ── P4: Class/Attribute properties ────────────────────────
73
+ { pattern: /\.className\s*=/, name: ".className =", severity: "info", action: "pass", errorCode: -32009, suggestion: "Use component state or a Playwright locator instead." },
74
+ { pattern: /\.classList\.add\s*\(/, name: ".classList.add()", severity: "info", action: "pass", errorCode: -32009, suggestion: "classList manipulation via CDP bypasses framework state tracking." },
75
+ { pattern: /\.classList\.remove\s*\(/, name: ".classList.remove()", severity: "info", action: "pass", errorCode: -32009, suggestion: "Use component state or attribute selectors instead." },
76
+ { pattern: /\.classList\.toggle\s*\(/, name: ".classList.toggle()", severity: "info", action: "pass", errorCode: -32009, suggestion: "Toggle without user interaction is detectable." },
77
+ { pattern: /\.classList\.replace\s*\(/, name: ".classList.replace()", severity: "info", action: "pass", errorCode: -32028, suggestion: "Rare operation \u2014 likely automated." },
78
+ // ── P5: Attribute manipulation ────────────────────────────
79
+ { pattern: /\.setAttribute\s*\(/, name: ".setAttribute()", severity: "info", action: "pass", errorCode: -32010, suggestion: "setAttribute bypasses framework attribute tracking. Use component state." },
80
+ { pattern: /\.removeAttribute\s*\(/, name: ".removeAttribute()", severity: "info", action: "pass", errorCode: -32010, suggestion: "Attribute removal without user interaction is suspicious." },
81
+ { pattern: /\.toggleAttribute\s*\(/, name: ".toggleAttribute()", severity: "info", action: "pass", errorCode: -32029, suggestion: "Attribute toggling via CDP is detectable." },
82
+ { pattern: /\.dataset\.\w+\s*=/, name: ".dataset.* =", severity: "info", action: "pass", errorCode: -32030, suggestion: "dataset mutations via evaluate bypass native mutation observers." },
83
+ // ── P6: Focus/Selection properties ────────────────────────
84
+ { pattern: /\.selectionStart\s*=/, name: ".selectionStart =", severity: "info", action: "pass", errorCode: -32011, suggestion: "Setting cursor position without focus is detectable." },
85
+ { pattern: /\.selectionEnd\s*=/, name: ".selectionEnd =", severity: "info", action: "pass", errorCode: -32011, suggestion: "Selection range manipulation without user input is suspicious." },
86
+ { pattern: /\.selectionDirection\s*=/, name: ".selectionDirection =", severity: "info", action: "pass", errorCode: -32031, suggestion: "Selection direction changes normally via mouse drag." },
87
+ // ── P7: Boolean properties ────────────────────────────────
88
+ { pattern: /\.disabled\s*=/, name: ".disabled =", severity: "info", action: "pass", errorCode: -32012, suggestion: "Disabling elements via CDP mid-interaction is detectable." },
89
+ { pattern: /\.readOnly\s*=/, name: ".readOnly =", severity: "info", action: "pass", errorCode: -32032, suggestion: "readOnly changes without user action." },
90
+ { pattern: /\.hidden\s*=/, name: ".hidden =", severity: "info", action: "pass", errorCode: -32012, suggestion: "Hiding elements is a common scraper tactic \u2014 detectable." },
91
+ { pattern: /\.required\s*=/, name: ".required =", severity: "info", action: "pass", errorCode: -32032, suggestion: "Validation constraint changes mid-session." },
92
+ { pattern: /\.multiple\s*=/, name: ".multiple =", severity: "info", action: "pass", errorCode: -32032, suggestion: "Multiple attribute toggle is rare in normal browsing." },
93
+ { pattern: /\.autofocus\s*=/, name: ".autofocus =", severity: "info", action: "pass", errorCode: -32032, suggestion: "autofocus changes mid-session are suspicious." },
94
+ // ── P8: Frame/Navigation properties ───────────────────────
95
+ { pattern: /\.src\s*=/, name: ".src = (on img/iframe/script)", severity: "info", action: "pass", errorCode: -32013, suggestion: "Changing src via CDP bypasses user interaction. Use page.click() on the element." },
96
+ { pattern: /\.href\s*=/, name: ".href =", severity: "info", action: "pass", errorCode: -32033, suggestion: "Changing anchor href via evaluate \u2014 use page.click() instead." },
97
+ { pattern: /\.action\s*=/, name: "form.action =", severity: "info", action: "pass", errorCode: -32034, suggestion: "Changing form action URL is highly suspicious." },
98
+ { pattern: /\.method\s*=/, name: "form.method =", severity: "info", action: "pass", errorCode: -32034, suggestion: "Form method changes without user interaction." },
99
+ { pattern: /\.target\s*=/, name: "link/area.target =", severity: "info", action: "pass", errorCode: -32034, suggestion: "Link target manipulation via CDP." },
100
+ // ── P9: Media properties ──────────────────────────────────
101
+ { pattern: /\.currentTime\s*=/, name: "media.currentTime = (seeking)", severity: "info", action: "pass", errorCode: -32014, suggestion: "Video seeking without user interaction \u2014 common scraper pattern." },
102
+ { pattern: /\.playbackRate\s*=/, name: "media.playbackRate =", severity: "info", action: "pass", errorCode: -32014, suggestion: "Changing playback speed is detectable automation signal." },
103
+ { pattern: /\.volume\s*=/, name: "media.volume =", severity: "info", action: "pass", errorCode: -32035, suggestion: "Volume setting via evaluate is suspicious." },
104
+ { pattern: /\.muted\s*=/, name: "media.muted =", severity: "info", action: "pass", errorCode: -32035, suggestion: "Muting media without user click is suspicious." },
105
+ // ── P10: Element geometry ─────────────────────────────────
106
+ { pattern: /\.scrollTop\s*=/, name: ".scrollTop =", severity: "info", action: "pass", errorCode: -32015, suggestion: "Programmatic scroll without user gesture. Use page.mouse.wheel()." },
107
+ { pattern: /\.scrollLeft\s*=/, name: ".scrollLeft =", severity: "info", action: "pass", errorCode: -32015, suggestion: "Horizontal scroll without user gesture." },
108
+ { pattern: /\.scrollTo\s*\(/, name: ".scrollTo()", severity: "info", action: "pass", errorCode: -32015, suggestion: "ScrollTo bypasses user scroll detection." },
109
+ { pattern: /\.scrollBy\s*\(/, name: ".scrollBy()", severity: "info", action: "pass", errorCode: -32015, suggestion: "ScrollBy without user gesture." },
110
+ { pattern: /\.scrollIntoView\s*\(/, name: ".scrollIntoView()", severity: "info", action: "pass", errorCode: -32015, suggestion: "scrollIntoView is a common bot pattern. Let Playwright handle scrolling." },
111
+ // ── P11: Shadow DOM ───────────────────────────────────────
112
+ { pattern: /\.shadowRoot\s*=/, name: ".shadowRoot = (override)", severity: "info", action: "block", errorCode: -32036, suggestion: "ShadowRoot is read-only \u2014 this set attempt is detectable." },
113
+ // ── P12: Force reflow / layout thrashing ──────────────────
114
+ { pattern: /\.offsetHeight\b(?!\s*===?\s*)/, name: ".offsetHeight read (forced reflow)", severity: "warn", action: "pass", errorCode: -32016, suggestion: "Reading offsetHeight triggers forced reflow \u2014 anti-crawlers detect this as layout probing." },
115
+ { pattern: /\.offsetWidth\b(?!\s*===?\s*)/, name: ".offsetWidth read (forced reflow)", severity: "warn", action: "pass", errorCode: -32016, suggestion: "Reading offsetWidth triggers forced reflow \u2014 detectable." },
116
+ { pattern: /getBoundingClientRect\s*\(/, name: "getBoundingClientRect()", severity: "warn", action: "pass", errorCode: -32037, suggestion: "getBoundingClientRect triggers reflow. Minimize calls." },
117
+ { pattern: /getComputedStyle\s*\(/, name: "getComputedStyle()", severity: "info", action: "pass", errorCode: -32038, suggestion: "getComputedStyle can trigger style recalculation." },
118
+ { pattern: /\.clientHeight\b/, name: ".clientHeight read", severity: "info", action: "pass", errorCode: -32038, suggestion: "clientHeight read triggers layout." },
119
+ { pattern: /\.clientWidth\b/, name: ".clientWidth read", severity: "info", action: "pass", errorCode: -32038, suggestion: "clientWidth read triggers layout." }
120
+ ];
121
+ var domMutationRule = {
122
+ id: "dom-mutation",
123
+ name: "DOM Property Mutation Detection (50+ setters)",
124
+ priority: 10,
125
+ canHandle(ctx) {
126
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn";
127
+ },
128
+ evaluate(ctx) {
129
+ const userCode = extractUserCode(ctx);
130
+ if (!userCode) return null;
131
+ for (const p of DOM_PATTERNS) {
132
+ if (p.pattern.test(userCode)) {
133
+ return {
134
+ ruleId: "dom-mutation",
135
+ action: p.action,
136
+ severity: p.severity,
137
+ reason: `Direct DOM property setter: "${p.name}". This bypasses framework event systems and is detectable as automation.`,
138
+ suggestion: p.suggestion,
139
+ errorCode: p.errorCode,
140
+ errorMessage: p.severity === "danger" ? `[CDP Firewall] ${p.name} blocked \u2014 bypasses framework reactivity` : `[CDP Firewall] ${p.name} detected \u2014 use proper interaction API`
141
+ };
142
+ }
143
+ }
144
+ return null;
145
+ }
146
+ };
147
+
148
+ // src/cdp-interceptor/rules/mouse-trajectory.ts
149
+ var TRACKER_KEY = "mouse-trajectory-tracker";
150
+ var MAX_SAMPLES = 200;
151
+ var MIN_SAMPLES_FOR_ANALYSIS = 5;
152
+ var mouseTrajectoryRule = {
153
+ id: "mouse-trajectory",
154
+ name: "Mouse Trajectory Analysis",
155
+ priority: 20,
156
+ canHandle(ctx) {
157
+ return ctx.method === "Input.dispatchMouseEvent";
158
+ },
159
+ evaluate(ctx) {
160
+ const type = ctx.params.type;
161
+ const x = ctx.params.x;
162
+ const y = ctx.params.y;
163
+ if (typeof x !== "number" || typeof y !== "number") return null;
164
+ let tracker = ctx.sessionState.get(TRACKER_KEY);
165
+ if (!tracker) {
166
+ tracker = { samples: [], lastMouseDownAt: 0, isDragging: false };
167
+ ctx.sessionState.set(TRACKER_KEY, tracker);
168
+ }
169
+ if (type === "mousePressed") {
170
+ const priorResult = analyzeTrajectory(tracker.samples);
171
+ tracker.lastMouseDownAt = Date.now();
172
+ tracker.isDragging = true;
173
+ tracker.samples = [];
174
+ return priorResult;
175
+ }
176
+ if (type === "mouseReleased") {
177
+ tracker.isDragging = false;
178
+ const result = analyzeTrajectory(tracker.samples);
179
+ tracker.samples = [];
180
+ return result;
181
+ }
182
+ if (type === "mouseMoved") {
183
+ const now = Date.now();
184
+ const prevSample = tracker.samples[tracker.samples.length - 1];
185
+ const distanceTraveled = prevSample ? prevSample.distanceTraveled + distance(prevSample.x, prevSample.y, x, y) : 0;
186
+ const sample = { x, y, timestamp: now, distanceTraveled };
187
+ tracker.samples.push(sample);
188
+ if (tracker.samples.length > MAX_SAMPLES) {
189
+ tracker.samples.shift();
190
+ }
191
+ return null;
192
+ }
193
+ if (type === "mouseReleased" && tracker.samples.length < MIN_SAMPLES_FOR_ANALYSIS) {
194
+ tracker.samples = [];
195
+ return null;
196
+ }
197
+ return null;
198
+ }
199
+ };
200
+ function analyzeTrajectory(samples) {
201
+ if (samples.length < MIN_SAMPLES_FOR_ANALYSIS) return null;
202
+ const issues = [];
203
+ const startX = samples[0].x;
204
+ const startY = samples[0].y;
205
+ const endX = samples[samples.length - 1].x;
206
+ const endY = samples[samples.length - 1].y;
207
+ const lineLength = distance(startX, startY, endX, endY);
208
+ const collinearityResult = checkCollinearity(samples, startX, startY, endX, endY, lineLength);
209
+ if (collinearityResult) {
210
+ issues.push(collinearityResult);
211
+ }
212
+ const velocityResult = checkConstantVelocity(samples);
213
+ if (velocityResult) {
214
+ issues.push(velocityResult);
215
+ }
216
+ const jitterResult = checkJitter(samples);
217
+ if (jitterResult) {
218
+ issues.push(jitterResult);
219
+ }
220
+ if (issues.length === 0) return null;
221
+ const stopX = samples[samples.length - 1].x;
222
+ const stopY = samples[samples.length - 1].y;
223
+ return {
224
+ ruleId: "mouse-trajectory",
225
+ action: "block",
226
+ severity: "danger",
227
+ reason: `Suspicious mouse trajectory: ${issues.join("; ")}`,
228
+ suggestion: `This mouse movement appears automated (straight line A\u2192B, no natural variation).
229
+ Use a humanized mouse API that generates:
230
+ - Bezier curves instead of straight lines
231
+ - Random acceleration/deceleration
232
+ - 1-3px micro-jitter per sample
233
+
234
+ Example: The 'faker' or 'ghost-cursor' libraries generate realistic mouse paths.
235
+ Target was: (${Math.round(startX)}, ${Math.round(startY)}) \u2192 (${Math.round(stopX)}, ${Math.round(stopY)})`,
236
+ errorCode: -32002,
237
+ errorMessage: "[CDP Firewall] Automated mouse trajectory blocked \u2014 appears non-human"
238
+ };
239
+ }
240
+ function distance(x1, y1, x2, y2) {
241
+ return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
242
+ }
243
+ function checkCollinearity(samples, x1, y1, x2, y2, lineLength) {
244
+ if (lineLength < 5) return null;
245
+ let maxDeviation = 0;
246
+ const dx = x2 - x1;
247
+ const dy = y2 - y1;
248
+ for (let i = 1; i < samples.length - 1; i++) {
249
+ const { x, y } = samples[i];
250
+ const cross = Math.abs(dy * x - dx * y + x2 * y1 - y2 * x1);
251
+ const dev = cross / lineLength;
252
+ if (dev > maxDeviation) maxDeviation = dev;
253
+ }
254
+ if (maxDeviation < 1.5 && lineLength > 20) {
255
+ return `Perfectly straight line (max deviation ${maxDeviation.toFixed(1)}px over ${lineLength.toFixed(0)}px)`;
256
+ }
257
+ if (maxDeviation < 0.5 && lineLength > 10) {
258
+ return `Near-perfect straight line (max deviation ${maxDeviation.toFixed(1)}px)`;
259
+ }
260
+ return null;
261
+ }
262
+ function checkConstantVelocity(samples) {
263
+ if (samples.length < 3) return null;
264
+ const speeds = [];
265
+ for (let i = 1; i < samples.length; i++) {
266
+ const d = distance(
267
+ samples[i - 1].x,
268
+ samples[i - 1].y,
269
+ samples[i].x,
270
+ samples[i].y
271
+ );
272
+ const dt = samples[i].timestamp - samples[i - 1].timestamp;
273
+ if (dt > 0) speeds.push(d / dt);
274
+ }
275
+ if (speeds.length < 2) return null;
276
+ const mean = speeds.reduce((a, b) => a + b, 0) / speeds.length;
277
+ if (mean === 0) return null;
278
+ const variance = speeds.reduce((sum, v) => sum + (v - mean) ** 2, 0) / speeds.length;
279
+ const stddev = Math.sqrt(variance);
280
+ const cv = stddev / mean;
281
+ if (cv < 0.05) {
282
+ return `Constant velocity (CV=${cv.toFixed(3)}, mean=${mean.toFixed(1)} px/ms)`;
283
+ }
284
+ return null;
285
+ }
286
+ function checkJitter(samples) {
287
+ if (samples.length < 4) return null;
288
+ let totalLateralChange = 0;
289
+ for (let i = 1; i < samples.length; i++) {
290
+ const d = distance(
291
+ samples[i - 1].x,
292
+ samples[i - 1].y,
293
+ samples[i].x,
294
+ samples[i].y
295
+ );
296
+ totalLateralChange += d;
297
+ }
298
+ const avgLateral = totalLateralChange / (samples.length - 1);
299
+ if (avgLateral < 0.3) {
300
+ return "No micro-jitter (sub-pixel precision, impossible for human)";
301
+ }
302
+ return null;
303
+ }
304
+
305
+ // src/cdp-interceptor/rules/input-keystroke.ts
306
+ var TRACKER_KEY2 = "keystroke-tracker";
307
+ var MIN_KEYS_FOR_ANALYSIS = 4;
308
+ var inputKeystrokeRule = {
309
+ id: "input-keystroke",
310
+ name: "Input Keystroke Timing Analysis",
311
+ priority: 40,
312
+ canHandle(ctx) {
313
+ return ctx.method === "Input.dispatchKeyEvent" || ctx.method === "Input.insertText";
314
+ },
315
+ evaluate(ctx) {
316
+ if (ctx.method === "Input.insertText") {
317
+ return {
318
+ ruleId: "input-keystroke",
319
+ action: "pass",
320
+ severity: "info",
321
+ reason: "Input.insertText bypasses native keyboard events. Playwright uses this internally for page.fill().",
322
+ suggestion: "Prefer page.type() with variable delay for human-like input.",
323
+ errorCode: -32004,
324
+ errorMessage: "[CDP Firewall] Input.insertText detected \u2014 note: Playwright uses this for fill()"
325
+ };
326
+ }
327
+ let tracker = ctx.sessionState.get(TRACKER_KEY2);
328
+ if (!tracker) {
329
+ tracker = { samples: [] };
330
+ ctx.sessionState.set(TRACKER_KEY2, tracker);
331
+ }
332
+ const type = ctx.params.type;
333
+ const code = ctx.params.code;
334
+ const key = ctx.params.key;
335
+ if (type === "keyDown" && key && key.length === 1) {
336
+ tracker.samples.push({
337
+ code,
338
+ key,
339
+ timestamp: Date.now(),
340
+ type
341
+ });
342
+ }
343
+ if (tracker.samples.length >= MIN_KEYS_FOR_ANALYSIS && (type === "keyUp" || type === "keyDown")) {
344
+ return analyzeKeyTiming(tracker.samples);
345
+ }
346
+ return null;
347
+ }
348
+ };
349
+ function analyzeKeyTiming(samples) {
350
+ if (samples.length < MIN_KEYS_FOR_ANALYSIS) return null;
351
+ const intervals = [];
352
+ for (let i = 1; i < samples.length; i++) {
353
+ const dt = samples[i].timestamp - samples[i - 1].timestamp;
354
+ if (dt > 0) intervals.push(dt);
355
+ }
356
+ if (intervals.length < 3) return null;
357
+ const mean = intervals.reduce((a, b) => a + b, 0) / intervals.length;
358
+ if (mean === 0) return null;
359
+ const variance = intervals.reduce((sum, v) => sum + (v - mean) ** 2, 0) / intervals.length;
360
+ const stddev = Math.sqrt(variance);
361
+ const cv = stddev / mean;
362
+ const uniqueIntervals = new Set(intervals);
363
+ const allIdentical = uniqueIntervals.size === 1;
364
+ if (allIdentical) {
365
+ return {
366
+ ruleId: "input-keystroke",
367
+ action: "block",
368
+ severity: "danger",
369
+ reason: `All ${intervals.length} keystroke intervals are exactly ${intervals[0]}ms \u2014 impossible for human typing.`,
370
+ suggestion: `Use page.fill(selector, text) instead of page.type() with delay.
371
+ Or add random variation: page.type(selector, text, {delay: 50 + Math.random() * 80}).`,
372
+ errorCode: -32004,
373
+ errorMessage: "[CDP Firewall] Constant keystroke timing detected \u2014 automated typing pattern"
374
+ };
375
+ }
376
+ if (cv < 0.08) {
377
+ return {
378
+ ruleId: "input-keystroke",
379
+ action: "block",
380
+ severity: "warn",
381
+ reason: `Unnatural keystroke timing (CV=${cv.toFixed(3)}). Human typing has CV > 0.2 on average.`,
382
+ suggestion: `Add random variation to your typing delay: page.type(selector, text, {delay: 50 + Math.random() * 80}).`,
383
+ errorCode: -32004,
384
+ errorMessage: "[CDP Firewall] Suspicious keystroke timing \u2014 likely automated"
385
+ };
386
+ }
387
+ return null;
388
+ }
389
+
390
+ // src/cdp-interceptor/rules/automation-signals.ts
391
+ var AUTOMATION_PATTERNS = [
392
+ // ── Playwright markers ─────────────────────────
393
+ { pattern: /window\s*\.\s*__playwright/, name: "window.__playwright", severity: "danger", errorCode: -32040 },
394
+ { pattern: /window\s*\.\s*__pw_[a-zA-Z]/, name: "window.__pw_*", severity: "danger", errorCode: -32040 },
395
+ { pattern: /window\s*\.\s*__pw_paused/, name: "window.__pw_paused", severity: "danger", errorCode: -32040 },
396
+ { pattern: /window\s*\.\s*playwright\b/, name: "window.playwright", severity: "danger", errorCode: -32040 },
397
+ { pattern: /window\s*\.\s*__pw_recorder/, name: "window.__pw_recorder", severity: "warn", errorCode: -32041 },
398
+ { pattern: /window\s*\.\s*__pw_trace/, name: "window.__pw_trace", severity: "warn", errorCode: -32041 },
399
+ // ── Puppeteer markers ──────────────────────────
400
+ { pattern: /window\s*\.\s*__puppeteer\b/, name: "window.__puppeteer", severity: "danger", errorCode: -32040 },
401
+ { pattern: /window\s*\.\s*__puppeteer_evaluation_script/, name: "window.__puppeteer_evaluation_script", severity: "danger", errorCode: -32040 },
402
+ { pattern: /window\s*\.\s*__puppeteer_testId/, name: "window.__puppeteer_testId", severity: "warn", errorCode: -32041 },
403
+ { pattern: /window\s*\.\s*__puppeteer_/, name: "window.__puppeteer_*", severity: "danger", errorCode: -32040 },
404
+ { pattern: /window\s*\.\s*_puppeteer\b/, name: "window._puppeteer", severity: "warn", errorCode: -32041 },
405
+ // ── Selenium / WebDriver markers ───────────────
406
+ { pattern: /window\s*\.\s*__webdriver_script_fn/, name: "window.__webdriver_script_fn", severity: "danger", errorCode: -32040 },
407
+ { pattern: /window\s*\.\s*__selenium\b/, name: "window.__selenium", severity: "danger", errorCode: -32040 },
408
+ { pattern: /window\s*\.\s*__selenium_evaluate/, name: "window.__selenium_evaluate", severity: "danger", errorCode: -32040 },
409
+ { pattern: /window\s*\.\s*__driver_evaluate/, name: "window.__driver_evaluate", severity: "warn", errorCode: -32041 },
410
+ { pattern: /window\s*\.\s*__webdriver_evaluate/, name: "window.__webdriver_evaluate", severity: "danger", errorCode: -32040 },
411
+ { pattern: /document\.\$cdc_/, name: "document.$cdc_* (Selenium marker)", severity: "danger", errorCode: -32040 },
412
+ { pattern: /document\.\$chrome_asyncScriptInfo/, name: "document.$chrome_asyncScriptInfo", severity: "danger", errorCode: -32040 },
413
+ // ── navigator.webdriver detection ─────────────
414
+ { pattern: /navigator\s*\.\s*webdriver/, name: "navigator.webdriver read", severity: "danger", errorCode: -32042 },
415
+ { pattern: /navigator\[["']webdriver["']\]/, name: 'navigator["webdriver"]', severity: "danger", errorCode: -32042 },
416
+ // ── Chrome headless API surface checks ────────
417
+ { pattern: /chrome\s*\.\s*app/, name: "chrome.app detection", severity: "danger", errorCode: -32043 },
418
+ { pattern: /chrome\s*\.\s*runtime/, name: "chrome.runtime detection", severity: "danger", errorCode: -32043 },
419
+ { pattern: /chrome\s*\.\s*loadTimes/, name: "chrome.loadTimes detection", severity: "warn", errorCode: -32044 },
420
+ { pattern: /chrome\s*\.\s*csi\b/, name: "chrome.csi detection", severity: "warn", errorCode: -32044 },
421
+ { pattern: /window\s*\.\s*chrome\b/, name: "window.chrome object probe", severity: "warn", errorCode: -32044 },
422
+ { pattern: /navigator\s*\.\s*plugins\b/, name: "navigator.plugins enumeration", severity: "warn", errorCode: -32044 },
423
+ { pattern: /navigator\s*\.\s*mimeTypes/, name: "navigator.mimeTypes enumeration", severity: "warn", errorCode: -32044 },
424
+ { pattern: /navigator\s*\.\s*hardwareConcurrency/, name: "navigator.hardwareConcurrency", severity: "warn", errorCode: -32044 },
425
+ { pattern: /navigator\s*\.\s*deviceMemory/, name: "navigator.deviceMemory", severity: "info", errorCode: -32045 },
426
+ { pattern: /navigator\s*\.\s*maxTouchPoints/, name: "navigator.maxTouchPoints", severity: "warn", errorCode: -32044 },
427
+ { pattern: /navigator\s*\.\s*languages/, name: "navigator.languages", severity: "info", errorCode: -32045 },
428
+ { pattern: /navigator\s*\.\s*platform/, name: "navigator.platform", severity: "info", errorCode: -32045 },
429
+ // ── Anti-detection injection attempts ─────────
430
+ { pattern: /navigator\.webdriver\s*=\s*(false|undefined|null)/, name: "navigator.webdriver override attempt", severity: "danger", errorCode: -32046, suggestionOverride: "Overriding navigator.webdriver is detectable. Use CDP Page.addScriptToEvaluateOnNewDocument instead." },
431
+ { pattern: /Object\.defineProperty\s*\([^)]*webdriver/, name: "Object.defineProperty navigator.webdriver", severity: "danger", errorCode: -32046, suggestionOverride: "Object.defineProperty patches are detectable via Function.prototype.toString checks." },
432
+ { pattern: /delete\s+navigator\s*\.\s*webdriver/, name: "delete navigator.webdriver", severity: "danger", errorCode: -32046, suggestionOverride: "Deleting webdriver flag is detectable \u2014 the delete itself is visible." },
433
+ // ── PhantomJS / Headless markers ──────────────
434
+ { pattern: /window\s*\.\s*callPhantom/, name: "window.callPhantom", severity: "danger", errorCode: -32040 },
435
+ { pattern: /window\s*\.\s*_phantom/, name: "window._phantom", severity: "danger", errorCode: -32040 },
436
+ { pattern: /window\s*\.\s*phantom\b/, name: "window.phantom", severity: "danger", errorCode: -32040 },
437
+ { pattern: /window\s*\.\s*Buffer\b/, name: "window.Buffer (Node.js leak)", severity: "danger", errorCode: -32040 },
438
+ { pattern: /window\s*\.\s*process\b/, name: "window.process (Node.js leak)", severity: "danger", errorCode: -32040 },
439
+ { pattern: /window\s*\.\s*global\b/, name: "window.global (Node.js leak)", severity: "danger", errorCode: -32040 },
440
+ { pattern: /window\s*\.\s*__dirname/, name: "window.__dirname (Node.js leak)", severity: "danger", errorCode: -32040 }
441
+ ];
442
+ var automationSignalsRule = {
443
+ id: "automation-signals",
444
+ name: "Browser Automation Signal Detection (35+ markers)",
445
+ priority: 20,
446
+ canHandle(ctx) {
447
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn" || ctx.method === "Page.addScriptToEvaluateOnNewDocument";
448
+ },
449
+ evaluate(ctx) {
450
+ if (ctx.method === "Page.addScriptToEvaluateOnNewDocument") {
451
+ const source = ctx.params.source;
452
+ if (typeof source === "string") {
453
+ for (const p of AUTOMATION_PATTERNS) {
454
+ if (p.pattern.test(source)) {
455
+ return makeDecision(p, source.substring(0, 60));
456
+ }
457
+ }
458
+ }
459
+ return null;
460
+ }
461
+ const userCode = extractUserCode(ctx);
462
+ if (!userCode) return null;
463
+ for (const p of AUTOMATION_PATTERNS) {
464
+ if (p.pattern.test(userCode)) {
465
+ return makeDecision(p, userCode.substring(0, 60));
466
+ }
467
+ }
468
+ return null;
469
+ }
470
+ };
471
+ function makeDecision(p, context) {
472
+ const suggestion = p.suggestionOverride ?? `Detected: "${p.name}" \u2014 an automation tool marker that anti-crawler systems immediately flag. Remove this pattern from your code.`;
473
+ return {
474
+ ruleId: "automation-signals",
475
+ action: "block",
476
+ severity: p.severity,
477
+ reason: `Automation marker detected: "${p.name}". Context: "${context}..."`,
478
+ suggestion,
479
+ errorCode: p.errorCode,
480
+ errorMessage: p.severity === "danger" ? `[CDP Firewall] ${p.name} blocked \u2014 automation tool marker detected` : `[CDP Firewall] ${p.name} detected \u2014 potential automation signal`
481
+ };
482
+ }
483
+
484
+ // src/cdp-interceptor/rules/fingerprinting.ts
485
+ var FP_PATTERNS = [
486
+ // ── Canvas Rendering fingerprinting ────────────────────
487
+ { pattern: /\.toDataURL\s*\(/, name: "canvas.toDataURL()", severity: "danger", errorCode: -32050, suggestion: "canvas.toDataURL() returns a unique hash that identifies the browser engine. Minimize calls." },
488
+ { pattern: /\.toBlob\s*\(/, name: "canvas.toBlob()", severity: "danger", errorCode: -32050, suggestion: "canvas.toBlob() is used for canvas fingerprinting. Avoid if possible." },
489
+ { pattern: /getImageData\s*\(/, name: "CanvasRenderingContext2D.getImageData()", severity: "danger", errorCode: -32050, suggestion: "getImageData reads pixel-level data used for fingerprinting." },
490
+ { pattern: /measureText\s*\(/, name: "CanvasRenderingContext2D.measureText()", severity: "warn", errorCode: -32051, suggestion: "Font metrics reveal installed fonts \u2014 a fingerprinting vector." },
491
+ { pattern: /OffscreenCanvas\s*\(/, name: "new OffscreenCanvas()", severity: "warn", errorCode: -32051, suggestion: "OffscreenCanvas is sometimes used to avoid visibility detection." },
492
+ { pattern: /convertToBlob\s*\(/, name: "OffscreenCanvas.convertToBlob()", severity: "warn", errorCode: -32051, suggestion: "Headless OffscreenCanvas rendering differs from real browser." },
493
+ // ── WebGL Fingerprinting ──────────────────────────────
494
+ { pattern: /getParameter\s*\([^)]*(?:VENDOR|RENDERER|VERSION)/, name: "WebGL getParameter(VENDOR/RENDERER)", severity: "danger", errorCode: -32052, suggestion: "WebGL VENDOR/RENDERER returns emulated values in headless. Cannot be reliably spoofed." },
495
+ { pattern: /getSupportedExtensions\s*\(/, name: "WebGL getSupportedExtensions()", severity: "warn", errorCode: -32053, suggestion: "WebGL extension list differs in headless mode." },
496
+ { pattern: /getShaderPrecisionFormat\s*\(/, name: "WebGL getShaderPrecisionFormat()", severity: "info", errorCode: -32054, suggestion: "Shader precision differs between headless and real GPU." },
497
+ { pattern: /UNMASKED_VENDOR_WEBGL/, name: "WEBGL_debug_renderer_info UNMASKED_VENDOR", severity: "warn", errorCode: -32053, suggestion: "Unmasked vendor info reveals the real GPU \u2014 blocked in many envs." },
498
+ { pattern: /UNMASKED_RENDERER_WEBGL/, name: "WEBGL_debug_renderer_info UNMASKED_RENDERER", severity: "warn", errorCode: -32053, suggestion: "Unmasked renderer string reveals the real GPU." },
499
+ // ── AudioContext Fingerprinting ────────────────────────
500
+ { pattern: /AnalyserNode\s*\(/, name: "new AnalyserNode()", severity: "warn", errorCode: -32055, suggestion: "Audio fingerprinting via AnalyserNode \u2014 produces silence in headless." },
501
+ { pattern: /getFloatFrequencyData\s*\(/, name: "AnalyserNode.getFloatFrequencyData()", severity: "danger", errorCode: -32050, suggestion: "Audio frequency data in headless returns silence (all zeros) \u2014 detectable." },
502
+ { pattern: /getByteFrequencyData\s*\(/, name: "AnalyserNode.getByteFrequencyData()", severity: "danger", errorCode: -32050, suggestion: "Audio byte frequency data in headless returns zeros." },
503
+ { pattern: /getByteTimeDomainData\s*\(/, name: "AnalyserNode.getByteTimeDomainData()", severity: "danger", errorCode: -32050, suggestion: "Time domain audio data in headless is a flat line \u2014 detectable." },
504
+ { pattern: /OfflineAudioContext\s*\(/, name: "new OfflineAudioContext()", severity: "warn", errorCode: -32055, suggestion: "Offline audio rendering is a known fingerprinting method." },
505
+ { pattern: /OscillatorNode\s*\(/, name: "new OscillatorNode()", severity: "info", errorCode: -32056, suggestion: "Audio oscillator used in fingerprinting probes." },
506
+ // ── Navigator property probing ───────────────────────
507
+ { pattern: /navigator\s*\.\s*connection\b/, name: "navigator.connection", severity: "info", errorCode: -32057, suggestion: 'Network connection info is used for fingerprinting (always "4g" in bots).' },
508
+ { pattern: /navigator\s*\.\s*getBattery\s*\(/, name: "navigator.getBattery()", severity: "warn", errorCode: -32058, suggestion: "Battery API is a fingerprinting vector. Returns fixed values in headless." },
509
+ { pattern: /navigator\s*\.\s*mediaDevices\s*\.\s*enumerateDevices/, name: "navigator.mediaDevices.enumerateDevices()", severity: "warn", errorCode: -32058, suggestion: "Media device enumeration returns empty/no devices in headless." },
510
+ { pattern: /navigator\s*\.\s*permissions\s*\.\s*query/, name: "navigator.permissions.query()", severity: "info", errorCode: -32059, suggestion: "Permission queries can reveal automation environment." },
511
+ // ── Screen / Window geometry probing ──────────────────
512
+ { pattern: /screen\.avail(Width|Height|Left|Top)/, name: "screen.avail*", severity: "warn", errorCode: -32060, suggestion: "screen.avail* values differ in headless (no OS chrome)." },
513
+ { pattern: /window\.outerWidth\s*-?\s*window\.innerWidth/, name: "window.outerWidth - window.innerWidth", severity: "danger", errorCode: -32050, suggestion: "This difference is 0 in headless (no browser chrome) \u2014 100% detection rate." },
514
+ { pattern: /window\.outerHeight\s*-?\s*window\.innerHeight/, name: "window.outerHeight - window.innerHeight", severity: "danger", errorCode: -32050, suggestion: "This difference is 0 in headless \u2014 immediate automation detection." },
515
+ { pattern: /screen\.width\b/, name: "screen.width", severity: "info", errorCode: -32061, suggestion: "Screen dimensions can be spoofed but inconsistencies with viewport are detectable." },
516
+ { pattern: /screen\.height\b/, name: "screen.height", severity: "info", errorCode: -32061, suggestion: "Screen dimension probes for viewport inconsistency detection." },
517
+ { pattern: /window\.devicePixelRatio/, name: "window.devicePixelRatio", severity: "warn", errorCode: -32060, suggestion: "devicePixelRatio is always 1 in headless \u2014 differs from real displays." },
518
+ { pattern: /matchMedia\s*\(/, name: "window.matchMedia()", severity: "warn", errorCode: -32060, suggestion: "matchMedia can detect CDP overridden viewport dimensions." },
519
+ // ── Performance / Timing API ──────────────────────────
520
+ { pattern: /performance\s*\.\s*now\s*\(/, name: "performance.now()", severity: "info", errorCode: -32062, suggestion: "High-resolution timer used for timing attacks and bot detection." },
521
+ { pattern: /performance\s*\.\s*memory/, name: "performance.memory", severity: "warn", errorCode: -32063, suggestion: "performance.memory shows VM memory limits in containers." },
522
+ { pattern: /performance\.getEntriesByType\s*\(\s*["']navigation["']\s*\)/, name: 'performance.getEntriesByType("navigation")', severity: "info", errorCode: -32062, suggestion: "Navigation timing reveals request pattern inconsistencies." },
523
+ { pattern: /performance\.getEntriesByType\s*\(\s*["']resource["']\s*\)/, name: 'performance.getEntriesByType("resource")', severity: "info", errorCode: -32062, suggestion: "Resource loading timing analysis for bot detection." },
524
+ // ── Font Detection ────────────────────────────────────
525
+ { pattern: /document\.fonts\.check\s*\(/, name: "document.fonts.check()", severity: "warn", errorCode: -32064, suggestion: "Font availability checks are used for fingerprinting. Installed font list is unique per user." },
526
+ { pattern: /document\.fonts\.ready/, name: "document.fonts.ready", severity: "info", errorCode: -32065, suggestion: "Font loading state probe for fingerprinting." },
527
+ // ── WebRTC / Connectivity ────────────────────────────
528
+ { pattern: /RTCPeerConnection\s*\(/, name: "new RTCPeerConnection()", severity: "warn", errorCode: -32066, suggestion: "WebRTC can leak internal IP and is used for connectivity fingerprinting." },
529
+ { pattern: /navigator\.mediaDevices\.getUserMedia/, name: "navigator.mediaDevices.getUserMedia()", severity: "info", errorCode: -32067, suggestion: "getUserMedia always fails in headless (no camera)." },
530
+ // ── Feature Consistency Checks ─────────────────────────
531
+ { pattern: /Intl\.DateTimeFormat.*resolvedOptions.*timeZone/, name: "Intl.DateTimeFormat timezone check", severity: "warn", errorCode: -32068, suggestion: "Timezone from Intl API vs Emulation.setTimezoneOverride will be inconsistent when mocked." },
532
+ { pattern: /new\s+Date\s*\(\s*\)\s*\.\s*getTimezoneOffset/, name: "Date.getTimezoneOffset()", severity: "info", errorCode: -32069, suggestion: "Timezone offset used for timing consistency cross-checks." },
533
+ { pattern: /Error\s*\(\s*\)\s*\.\s*stack/, name: "Error().stack format check", severity: "info", errorCode: -32070, suggestion: "Stack trace format differs between headless and full Chrome." },
534
+ { pattern: /Function\.prototype\.toString/, name: "Function.prototype.toString on native fn", severity: "info", errorCode: -32070, suggestion: "Native function toString() format can reveal patched APIs." }
535
+ ];
536
+ var fingerprintingRule = {
537
+ id: "fingerprinting",
538
+ name: "Browser Fingerprinting Access Detection (35+ APIs)",
539
+ priority: 30,
540
+ canHandle(ctx) {
541
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn";
542
+ },
543
+ evaluate(ctx) {
544
+ const userCode = extractUserCode(ctx);
545
+ if (!userCode) return null;
546
+ for (const p of FP_PATTERNS) {
547
+ if (p.pattern.test(userCode)) {
548
+ return {
549
+ ruleId: "fingerprinting",
550
+ action: "block",
551
+ severity: p.severity,
552
+ reason: `Browser fingerprinting API accessed: "${p.name}". Anti-crawler systems use this to identify your browser.`,
553
+ suggestion: p.suggestion,
554
+ errorCode: p.errorCode,
555
+ errorMessage: `[CDP Firewall] ${p.name} blocked \u2014 fingerprinting API access detected`
556
+ };
557
+ }
558
+ }
559
+ return null;
560
+ }
561
+ };
562
+
563
+ // src/cdp-interceptor/rules/event-simulation.ts
564
+ var EVENT_PATTERNS = [
565
+ // ── Direct method calls (all isTrusted=false) ─────────
566
+ { pattern: /\.click\s*\(\s*\)/, name: "el.click()", severity: "danger", errorCode: -32070, suggestion: "el.click() fires isTrusted=false events. Use page.click(selector) which uses Input.dispatchMouseEvent (isTrusted=true)." },
567
+ { pattern: /\.focus\s*\(\s*\)/, name: "el.focus()", severity: "danger", errorCode: -32071, suggestion: "el.focus() without user interaction is detectable. Use page.click(selector) which naturally focuses." },
568
+ { pattern: /\.blur\s*\(\s*\)/, name: "el.blur()", severity: "danger", errorCode: -32071, suggestion: "el.blur() without user interaction. Avoid in automation scripts." },
569
+ { pattern: /\.submit\s*\(\s*\)/, name: "el.submit()", severity: "danger", errorCode: -32072, suggestion: `form.submit() bypasses onSubmit handlers. Click the submit button: page.click('button[type="submit"]').` },
570
+ { pattern: /\.reset\s*\(\s*\)/, name: "el.reset()", severity: "danger", errorCode: -32072, suggestion: "form.reset() without user action. Let the user clear fields manually." },
571
+ { pattern: /\.select\s*\(\s*\)/, name: "el.select()", severity: "warn", errorCode: -32073, suggestion: "input.select() selects text without user interaction. Use page.click(selector) instead." },
572
+ { pattern: /dialog\.close\s*\(\s*\)|showModal\(\)/, name: "dialog.showModal()/close()", severity: "danger", errorCode: -32074, suggestion: "dialog.showModal() requires user gesture. Let the user open the dialog naturally." },
573
+ { pattern: /\.showPopover\s*\(\s*\)/, name: "el.showPopover()", severity: "warn", errorCode: -32073, suggestion: "Popover.show() requires user gesture. Simulate a click on the popover trigger." },
574
+ { pattern: /\.hidePopover\s*\(\s*\)/, name: "el.hidePopover()", severity: "warn", errorCode: -32073, suggestion: "Hiding popovers via CDP is detectable." },
575
+ { pattern: /\.requestFullscreen\s*\(\s*\)/, name: "el.requestFullscreen()", severity: "danger", errorCode: -32074, suggestion: "Fullscreen requests require user gesture. Cannot be triggered by automation." },
576
+ { pattern: /\.requestPointerLock\s*\(\s*\)/, name: "el.requestPointerLock()", severity: "danger", errorCode: -32074, suggestion: "Pointer lock requires user gesture. Blocked in automation." },
577
+ { pattern: /\.setSelectionRange\s*\(/, name: "el.setSelectionRange()", severity: "warn", errorCode: -32073, suggestion: "Setting selection range without user interaction is suspicious." },
578
+ { pattern: /\.setRangeText\s*\(/, name: "el.setRangeText()", severity: "info", errorCode: -32076, suggestion: "Range text replacement without user input is detectable." },
579
+ { pattern: /\.showPicker\s*\(\s*\)/, name: "HTMLInputElement.showPicker()", severity: "info", errorCode: -32076, suggestion: "Date/color picker shown without click \u2014 detectable." },
580
+ { pattern: /\.reportValidity\s*\(\s*\)/, name: "el.reportValidity()", severity: "info", errorCode: -32076, suggestion: "Validity reporting without form submission attempt." },
581
+ // ── dispatchEvent with synthetic events (isTrusted=false) ──
582
+ { pattern: /dispatchEvent\s*\(\s*new\s+(?:Event|CustomEvent)\s*\(/, name: "dispatchEvent(new Event/CustomEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic events have isTrusted=false. Use Input.dispatch* CDP methods for trusted events." },
583
+ { pattern: /dispatchEvent\s*\(\s*new\s+MouseEvent\s*\(/, name: "dispatchEvent(new MouseEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic mouse events (isTrusted=false). Use Input.dispatchMouseEvent CDP method." },
584
+ { pattern: /dispatchEvent\s*\(\s*new\s+KeyboardEvent\s*\(/, name: "dispatchEvent(new KeyboardEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic keyboard events (isTrusted=false). Use Input.dispatchKeyEvent CDP method." },
585
+ { pattern: /dispatchEvent\s*\(\s*new\s+FocusEvent\s*\(/, name: "dispatchEvent(new FocusEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic focus events bypass user interaction." },
586
+ { pattern: /dispatchEvent\s*\(\s*new\s+InputEvent\s*\(/, name: "dispatchEvent(new InputEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic input events (isTrusted=false). Use page.fill() or page.type()." },
587
+ { pattern: /dispatchEvent\s*\(\s*new\s+(?:Event)\s*\(\s*["'](?:input|change|submit|reset)/, name: 'dispatchEvent("input"/"change"/"submit")', severity: "danger", errorCode: -32077, suggestion: "Synthetic input/change/submit events are trusted=false. Always detectable." },
588
+ { pattern: /dispatchEvent\s*\(\s*new\s+PointerEvent\s*\(/, name: "dispatchEvent(new PointerEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic pointer events bypass the trusted input pipeline." },
589
+ { pattern: /dispatchEvent\s*\(\s*new\s+TouchEvent\s*\(/, name: "dispatchEvent(new TouchEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic touch events on mobile are detectable." },
590
+ { pattern: /dispatchEvent\s*\(\s*new\s+WheelEvent\s*\(/, name: "dispatchEvent(new WheelEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic scroll events (isTrusted=false). Use Input.dispatchMouseEvent with wheel type." },
591
+ { pattern: /dispatchEvent\s*\(\s*new\s+ClipboardEvent\s*\(/, name: "dispatchEvent(new ClipboardEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic clipboard events are detectable." },
592
+ { pattern: /dispatchEvent\s*\(\s*new\s+DragEvent\s*\(/, name: "dispatchEvent(new DragEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic drag events bypass real user interaction." },
593
+ { pattern: /dispatchEvent\s*\(\s*new\s+CompositionEvent\s*\(/, name: "dispatchEvent(new CompositionEvent)", severity: "info", errorCode: -32079, suggestion: "Synthetic IME composition events \u2014 detectable pattern." },
594
+ { pattern: /dispatchEvent\s*\(\s*new\s+AnimationEvent\s*\(/, name: "dispatchEvent(new AnimationEvent)", severity: "info", errorCode: -32079, suggestion: "Forcing animation state transitions via fake events." },
595
+ { pattern: /dispatchEvent\s*\(\s*new\s+TransitionEvent\s*\(/, name: "dispatchEvent(new TransitionEvent)", severity: "info", errorCode: -32079, suggestion: "Forcing CSS transition end via fake events." }
596
+ ];
597
+ var eventSimulationRule = {
598
+ id: "event-simulation",
599
+ name: "Event Simulation Detection (30+ patterns)",
600
+ priority: 40,
601
+ canHandle(ctx) {
602
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn";
603
+ },
604
+ evaluate(ctx) {
605
+ const userCode = extractUserCode(ctx);
606
+ if (!userCode) return null;
607
+ for (const p of EVENT_PATTERNS) {
608
+ if (p.pattern.test(userCode)) {
609
+ return {
610
+ ruleId: "event-simulation",
611
+ action: "block",
612
+ severity: p.severity,
613
+ reason: `Event simulation detected: "${p.name}". Synthetic events have isTrusted=false and are 100% detectable.`,
614
+ suggestion: p.suggestion,
615
+ errorCode: p.errorCode,
616
+ errorMessage: p.severity === "danger" ? `[CDP Firewall] ${p.name} blocked \u2014 synthetic event (isTrusted=false)` : `[CDP Firewall] ${p.name} detected \u2014 event simulation`
617
+ };
618
+ }
619
+ }
620
+ return null;
621
+ }
622
+ };
623
+
624
+ // src/cdp-interceptor/rules/emulation-override.ts
625
+ var OVERRIDE_PATTERNS = [
626
+ // ── Emulation overrides ──────────────────────────────
627
+ // NOTE: Emulation.setDeviceMetricsOverride is NOT included here because
628
+ // Playwright calls it internally for every new page (viewport setup).
629
+ // Blocking it would break page creation.
630
+ { method: "Emulation.setUserAgentOverride", name: "Emulation.setUserAgentOverride", severity: "danger", errorCode: -32080, suggestion: "navigator.userAgent override can be detected by checking consistency with navigator.plugins, WebGL vendor, etc." },
631
+ { method: "Emulation.setTouchEmulationEnabled", name: "Emulation.setTouchEmulationEnabled", severity: "warn", errorCode: -32081, suggestion: "Touch emulation creates inconsistent touch/mouse state. Windows touch events without real touch hardware." },
632
+ { method: "Emulation.setGeolocationOverride", name: "Emulation.setGeolocationOverride", severity: "danger", errorCode: -32082, suggestion: "Geolocation changing mid-session without user travel is impossible. Detectable via IP geo vs overridden geo." },
633
+ { method: "Emulation.setLocaleOverride", name: "Emulation.setLocaleOverride", severity: "warn", errorCode: -32081, suggestion: "Locale change without browser restart detectable via navigator.languages vs Accept-Language consistency." },
634
+ { method: "Emulation.setTimezoneOverride", name: "Emulation.setTimezoneOverride", severity: "danger", errorCode: -32082, suggestion: "Timezone mismatch vs IP geolocation + Date() is 100% detectable." },
635
+ { method: "Emulation.setDisabledImageTypes", name: "Emulation.setDisabledImageTypes", severity: "info", errorCode: -32083, suggestion: "Disabling image types prevents normal resource loading \u2014 visible to performance API." },
636
+ { method: "Emulation.setScriptExecutionDisabled", name: "Emulation.setScriptExecutionDisabled", severity: "info", errorCode: -32083, suggestion: "Disabling JS mid-session kills page interactivity \u2014 immediately obvious." },
637
+ { method: "Emulation.setCPUThrottlingRate", name: "Emulation.setCPUThrottlingRate", severity: "info", errorCode: -32083, suggestion: "CPU throttling creates unrealistic performance.now() profiles." },
638
+ { method: "Emulation.setVirtualTimePolicy", name: "Emulation.setVirtualTimePolicy", severity: "info", errorCode: -32083, suggestion: "Virtual time breaks Date.now() and performance.now() based detections \u2014 detectable via timer drift." },
639
+ // ── Network overrides ────────────────────────────────
640
+ { method: "Network.setUserAgentOverride", name: "Network.setUserAgentOverride (HTTP layer)", severity: "danger", errorCode: -32080, suggestion: "HTTP User-Agent vs navigator.userAgent inconsistency = immediate detection." },
641
+ { method: "Network.setExtraHTTPHeaders", name: "Network.setExtraHTTPHeaders", severity: "danger", errorCode: -32084, suggestion: "Custom headers can conflict with browser-generated headers. Missing client hints (Sec-CH-UA) are also detectable." },
642
+ { method: "Network.emulateNetworkConditions", name: "Network.emulateNetworkConditions", severity: "warn", errorCode: -32081, suggestion: "Network throttling creates unrealistic load timing patterns." },
643
+ { method: "Network.setCookie", name: "Network.setCookie", severity: "warn", errorCode: -32085, suggestion: "CDP-injected cookies are detectable via document.cookie vs Network.getCookies inconsistency." },
644
+ { method: "Network.deleteCookies", name: "Network.deleteCookies", severity: "warn", errorCode: -32085, suggestion: "Cookie deletion via CDP bypasses HTTP cookie expiration \u2014 detectable." },
645
+ // ── Security overrides ──────────────────────────────
646
+ { method: "Security.setIgnoreCertificateErrors", name: "Security.setIgnoreCertificateErrors", severity: "info", errorCode: -32086, suggestion: "Ignoring certificate errors creates unusual TLS behavior visible at network level." },
647
+ // ── Page overrides ──────────────────────────────────
648
+ { method: "Page.setDownloadBehavior", name: "Page.setDownloadBehavior", severity: "info", errorCode: -32087, suggestion: "Bypassing download dialogs \u2014 detectable via download event flow." },
649
+ { method: "Page.setWebLifecycleState", name: "Page.setWebLifecycleState", severity: "info", errorCode: -32087, suggestion: "Forcing page lifecycle transitions is unnatural." },
650
+ // ── Storage / Permissions ───────────────────────────
651
+ { method: "Storage.clearDataForOrigin", name: "Storage.clearDataForOrigin", severity: "info", errorCode: -32089, suggestion: "Clearing storage mid-session is unnatural for real users." },
652
+ { method: "Browser.grantPermissions", name: "Browser.grantPermissions", severity: "warn", errorCode: -32090, suggestion: "Granting permissions via CDP is detectable as the permission flow skips the user prompt." },
653
+ { method: "Browser.resetPermissions", name: "Browser.resetPermissions", severity: "info", errorCode: -32089, suggestion: "Permission resets without user action are unnatural." }
654
+ ];
655
+ var emulationOverrideRule = {
656
+ id: "emulation-override",
657
+ name: "CDP Emulation / Override Detection (20+ methods)",
658
+ priority: 60,
659
+ canHandle(ctx) {
660
+ for (const p of OVERRIDE_PATTERNS) {
661
+ if (ctx.method === p.method) return true;
662
+ }
663
+ return false;
664
+ },
665
+ evaluate(ctx) {
666
+ for (const p of OVERRIDE_PATTERNS) {
667
+ if (ctx.method !== p.method) continue;
668
+ return {
669
+ ruleId: "emulation-override",
670
+ action: "block",
671
+ severity: p.severity,
672
+ reason: `CDP emulation/override detected: "${p.name}". This creates detectable inconsistencies between the JS environment and real browser state.`,
673
+ suggestion: p.suggestion,
674
+ errorCode: p.errorCode,
675
+ errorMessage: `[CDP Firewall] ${p.name} blocked \u2014 creates detectable browser state inconsistency`
676
+ };
677
+ }
678
+ return null;
679
+ }
680
+ };
681
+
682
+ // src/cdp-interceptor/rules/network-anomaly.ts
683
+ var networkAnomalyRule = {
684
+ id: "network-anomaly",
685
+ name: "Network Anomaly Detection (8+ patterns)",
686
+ priority: 70,
687
+ canHandle(ctx) {
688
+ const m = ctx.method;
689
+ return m === "Network.setExtraHTTPHeaders" || m === "Network.clearBrowserCache" || m === "Network.clearBrowserCookies" || m === "Network.setBlockedURLs" || m === "Network.setBypassServiceWorker" || m === "Fetch.enable" || m === "Network.enable";
690
+ },
691
+ evaluate(ctx) {
692
+ switch (ctx.method) {
693
+ case "Network.setExtraHTTPHeaders": {
694
+ const headers = ctx.params.headers;
695
+ if (headers) {
696
+ const headerStr = JSON.stringify(headers).toLowerCase();
697
+ if (!headerStr.includes("sec-ch-ua")) {
698
+ return {
699
+ ruleId: "network-anomaly",
700
+ action: "block",
701
+ severity: "warn",
702
+ reason: "Network.setExtraHTTPHeaders called without Sec-CH-UA client hints \u2014 browser normally sends these.",
703
+ suggestion: "Modern browsers send Sec-CH-UA headers automatically. Adding custom headers without them creates detectable inconsistency.",
704
+ errorCode: -32110,
705
+ errorMessage: "[CDP Firewall] Missing client hints in custom headers"
706
+ };
707
+ }
708
+ }
709
+ return null;
710
+ }
711
+ case "Network.clearBrowserCache": {
712
+ return {
713
+ ruleId: "network-anomaly",
714
+ action: "block",
715
+ severity: "warn",
716
+ reason: "Network.clearBrowserCache called \u2014 cache clearing mid-session is unnatural for real users.",
717
+ suggestion: "Avoid cache clearing during sessions. Start with a fresh profile if needed.",
718
+ errorCode: -32111,
719
+ errorMessage: "[CDP Firewall] Cache clearing detected"
720
+ };
721
+ }
722
+ case "Network.clearBrowserCookies": {
723
+ return {
724
+ ruleId: "network-anomaly",
725
+ action: "block",
726
+ severity: "warn",
727
+ reason: "Network.clearBrowserCookies called \u2014 wiping cookies mid-session is a scraper optimization.",
728
+ suggestion: "Cookies should only be cleared via normal browser flow (expiration, user action).",
729
+ errorCode: -32112,
730
+ errorMessage: "[CDP Firewall] Cookie clearing detected"
731
+ };
732
+ }
733
+ case "Network.setBlockedURLs": {
734
+ return {
735
+ ruleId: "network-anomaly",
736
+ action: "block",
737
+ severity: "warn",
738
+ reason: "Network.setBlockedURLs blocks resource loading \u2014 this changes the page behavior and is detectable.",
739
+ suggestion: "Blocking images/fonts/etc creates measurable differences in performance and page rendering.",
740
+ errorCode: -32113,
741
+ errorMessage: "[CDP Firewall] URL blocking detected"
742
+ };
743
+ }
744
+ case "Network.setBypassServiceWorker": {
745
+ return {
746
+ ruleId: "network-anomaly",
747
+ action: "block",
748
+ severity: "info",
749
+ reason: "Network.setBypassServiceWorker called \u2014 bypassing service workers for content extraction.",
750
+ suggestion: "Service worker bypass changes fetch behavior and is detectable server-side.",
751
+ errorCode: -32114,
752
+ errorMessage: "[CDP Firewall] Service worker bypass detected"
753
+ };
754
+ }
755
+ case "Fetch.enable": {
756
+ return {
757
+ ruleId: "network-anomaly",
758
+ action: "block",
759
+ severity: "warn",
760
+ reason: "Fetch.enable intercepts all network requests \u2014 a man-in-the-middle approach used by scrapers.",
761
+ suggestion: "Do not use Fetch domain for network interception if avoiding detection.",
762
+ errorCode: -32115,
763
+ errorMessage: "[CDP Firewall] Fetch interception detected"
764
+ };
765
+ }
766
+ default:
767
+ return null;
768
+ }
769
+ }
770
+ };
771
+
772
+ // src/cdp-interceptor/rules/page-lifecycle.ts
773
+ var STAT_KEY = "lifecycle-tracker";
774
+ var pageLifecycleRule = {
775
+ id: "page-lifecycle",
776
+ name: "Page Lifecycle Anomaly Detection (10+ patterns)",
777
+ priority: 80,
778
+ canHandle(ctx) {
779
+ const m = ctx.method;
780
+ return m === "Page.navigate" || m === "Page.captureScreenshot" || m === "Page.printToPDF" || m === "Page.reload" || m === "Page.close" || m === "Runtime.evaluate" || m === "Runtime.callFunctionOn";
781
+ },
782
+ evaluate(ctx) {
783
+ let state = ctx.sessionState.get(STAT_KEY);
784
+ if (!state) {
785
+ state = { navigations: [], screenshots: 0, printToPDF: false, evaluateCount: 0, lastNavTime: 0, lastEvalTime: 0 };
786
+ ctx.sessionState.set(STAT_KEY, state);
787
+ }
788
+ const now = Date.now();
789
+ switch (ctx.method) {
790
+ case "Page.navigate": {
791
+ state.navigations.push(now);
792
+ if (state.navigations.length >= 2) {
793
+ const prev = state.navigations[state.navigations.length - 2];
794
+ const interval = now - prev;
795
+ if (interval < 100) {
796
+ return {
797
+ ruleId: "page-lifecycle",
798
+ action: "block",
799
+ severity: "warn",
800
+ reason: `Multiple Page.navigate calls within ${interval}ms of each other \u2014 unnatural rapid navigation.`,
801
+ suggestion: "Add proper waits between navigations: wait for page load before navigating again.",
802
+ errorCode: -32100,
803
+ errorMessage: "[CDP Firewall] Rapid navigation sequence detected"
804
+ };
805
+ }
806
+ }
807
+ state.lastNavTime = now;
808
+ return null;
809
+ }
810
+ case "Page.captureScreenshot": {
811
+ state.screenshots++;
812
+ if (state.lastNavTime > 0 && now - state.lastNavTime < 500) {
813
+ return {
814
+ ruleId: "page-lifecycle",
815
+ action: "block",
816
+ severity: "danger",
817
+ reason: "Page.captureScreenshot called within 500ms of navigation \u2014 content extraction pattern.",
818
+ suggestion: "Wait for the page to fully render before taking screenshots: wait for load/networkidle.",
819
+ errorCode: -32101,
820
+ errorMessage: "[CDP Firewall] Pre-render screenshot blocked \u2014 content extraction pattern"
821
+ };
822
+ }
823
+ if (state.screenshots > 3 && state.navigations.length < 2) {
824
+ return {
825
+ ruleId: "page-lifecycle",
826
+ action: "block",
827
+ severity: "warn",
828
+ reason: "Multiple screenshots on a single page without navigation \u2014 suspicious extraction behavior.",
829
+ suggestion: "Consider if all screenshots are necessary.",
830
+ errorCode: -32102,
831
+ errorMessage: "[CDP Firewall] Excessive screenshot detection"
832
+ };
833
+ }
834
+ return null;
835
+ }
836
+ case "Page.printToPDF": {
837
+ return {
838
+ ruleId: "page-lifecycle",
839
+ action: "block",
840
+ severity: "danger",
841
+ reason: "Page.printToPDF called \u2014 this is a telltale scraper pattern that gives away automation intent.",
842
+ suggestion: "Avoid PDF generation. If you must, add significant delays and user-like interaction first.",
843
+ errorCode: -32103,
844
+ errorMessage: "[CDP Firewall] printToPDF blocked \u2014 scraper intent detected"
845
+ };
846
+ }
847
+ case "Page.reload": {
848
+ if (state.lastNavTime > 0 && now - state.lastNavTime < 1e3) {
849
+ return {
850
+ ruleId: "page-lifecycle",
851
+ action: "block",
852
+ severity: "warn",
853
+ reason: "Page.reload called immediately after navigate \u2014 unnatural fast-reload pattern.",
854
+ suggestion: "Introduce delays between navigation and reload to simulate human behavior.",
855
+ errorCode: -32104,
856
+ errorMessage: "[CDP Firewall] Rapid reload detected"
857
+ };
858
+ }
859
+ return null;
860
+ }
861
+ case "Page.close": {
862
+ if (state.navigations.length < 2) {
863
+ return {
864
+ ruleId: "page-lifecycle",
865
+ action: "block",
866
+ severity: "info",
867
+ reason: "Page.close called after minimal interaction \u2014 zombie pages common in automation.",
868
+ suggestion: "Ensure meaningful interaction before closing pages.",
869
+ errorCode: -32105,
870
+ errorMessage: "[CDP Firewall] Page close after minimal interaction"
871
+ };
872
+ }
873
+ return null;
874
+ }
875
+ case "Runtime.evaluate":
876
+ case "Runtime.callFunctionOn": {
877
+ state.evaluateCount++;
878
+ if (state.evaluateCount > 50 && state.navigations.length === 0) {
879
+ return {
880
+ ruleId: "page-lifecycle",
881
+ action: "block",
882
+ severity: "info",
883
+ reason: "50+ evaluate calls without any Page.navigate \u2014 data extraction without real browsing.",
884
+ suggestion: "Navigate to a real page first. Evaluate on about:blank is suspicious.",
885
+ errorCode: -32106,
886
+ errorMessage: "[CDP Firewall] Excessive evaluate without navigation"
887
+ };
888
+ }
889
+ return null;
890
+ }
891
+ default:
892
+ return null;
893
+ }
894
+ }
895
+ };
896
+
897
+ // src/cdp-interceptor/rules-engine.ts
898
+ var BUILTIN_RULES = [
899
+ domMutationRule,
900
+ automationSignalsRule,
901
+ fingerprintingRule,
902
+ eventSimulationRule,
903
+ mouseTrajectoryRule,
904
+ inputKeystrokeRule,
905
+ emulationOverrideRule,
906
+ networkAnomalyRule,
907
+ pageLifecycleRule
908
+ ];
909
+ function createRuleEngine(customRules) {
910
+ const rules = [...BUILTIN_RULES, ...customRules ?? []].sort((a, b) => a.priority - b.priority);
911
+ const sessionStates = /* @__PURE__ */ new Map();
912
+ function getSessionState(sessionId) {
913
+ let state = sessionStates.get(sessionId);
914
+ if (!state) {
915
+ state = /* @__PURE__ */ new Map();
916
+ sessionStates.set(sessionId, state);
917
+ }
918
+ return state;
919
+ }
920
+ return {
921
+ start() {
922
+ sessionStates.clear();
923
+ },
924
+ stop() {
925
+ sessionStates.clear();
926
+ },
927
+ evaluate(ctx) {
928
+ const fullCtx = {
929
+ ...ctx,
930
+ sessionState: getSessionState(ctx.sessionId)
931
+ };
932
+ for (const rule of rules) {
933
+ if (rule.canHandle && !rule.canHandle(fullCtx)) continue;
934
+ const decision = rule.evaluate(fullCtx);
935
+ if (!decision) continue;
936
+ if (decision.action !== "pass") return decision;
937
+ }
938
+ return null;
939
+ }
940
+ };
941
+ }
942
+
943
+ // src/cdp-interceptor/logger.ts
944
+ function createLogger(config) {
945
+ const buffer = [];
946
+ const MAX_BUFFER = 2e3;
947
+ return {
948
+ info(message, meta) {
949
+ if (!config.enableLogging) return;
950
+ const ts = (/* @__PURE__ */ new Date()).toISOString();
951
+ if (meta) {
952
+ console.log(`[CDPInterceptor ${ts}] ${message}`, JSON.stringify(meta));
953
+ } else {
954
+ console.log(`[CDPInterceptor ${ts}] ${message}`);
955
+ }
956
+ },
957
+ log(method, direction, sessionId, payload, decision) {
958
+ const entry = {
959
+ timestamp: Date.now(),
960
+ direction,
961
+ sessionId,
962
+ method,
963
+ payload: sanitizePayload(payload),
964
+ decision: decision ?? void 0
965
+ };
966
+ if (config.enableLogging) {
967
+ buffer.push(entry);
968
+ if (buffer.length > MAX_BUFFER) buffer.shift();
969
+ const tag = decision ? decision.action === "block" ? "\u{1F6AB}BLOCK" : decision.action === "transform" ? "\u{1F504}XFMR" : "\u2705" : " ";
970
+ const reason = decision ? ` [${decision.severity}] ${decision.reason}` : "";
971
+ console.log(`[CDP] ${tag} ${direction} ${method}${reason}`);
972
+ }
973
+ return entry;
974
+ },
975
+ getRecent(count) {
976
+ return buffer.slice(-count);
977
+ },
978
+ flush() {
979
+ buffer.length = 0;
980
+ }
981
+ };
982
+ }
983
+ function sanitizePayload(payload) {
984
+ if (typeof payload !== "object" || payload === null) return { raw: String(payload) };
985
+ const obj = payload;
986
+ const cleaned = {};
987
+ for (const [key, value] of Object.entries(obj)) {
988
+ if (key === "data" && typeof value === "string" && value.length > 200) {
989
+ cleaned[key] = `<binary: ${value.length} chars>`;
990
+ } else if (key === "expression" && typeof value === "string" && value.length > 500) {
991
+ cleaned[key] = value.substring(0, 500) + "...";
992
+ } else {
993
+ cleaned[key] = value;
994
+ }
995
+ }
996
+ return cleaned;
997
+ }
998
+
999
+ // src/cdp-interceptor/advisor.ts
1000
+ function formatBlockMessage(decision, method) {
1001
+ const adv = advise(decision, method);
1002
+ const lines = [
1003
+ `[CDP-FIREWALL-BLOCK] rule=${decision.ruleId}`,
1004
+ `method=${method}`,
1005
+ `reason=${decision.reason}`,
1006
+ `suggestion=${decision.suggestion ?? adv.detail}`
1007
+ ];
1008
+ if (adv.codeExample) {
1009
+ lines.push(`code-example=`);
1010
+ lines.push(adv.codeExample);
1011
+ }
1012
+ return lines.join("\n");
1013
+ }
1014
+ function advise(decision, originalMethod) {
1015
+ const baseAdvice = getBaseAdvice(decision, originalMethod);
1016
+ return {
1017
+ ruleId: decision.ruleId,
1018
+ title: baseAdvice.title,
1019
+ detail: decision.suggestion ?? baseAdvice.detail,
1020
+ codeExample: baseAdvice.codeExample
1021
+ };
1022
+ }
1023
+ function getBaseAdvice(decision, method) {
1024
+ switch (decision.ruleId) {
1025
+ case "dom-mutation":
1026
+ return {
1027
+ title: "Direct DOM property mutation blocked",
1028
+ detail: `Your ${method} call tried to set a DOM property directly. In React/Vue/Angular, this bypasses the framework's virtual DOM completely meaning onChange/onInput never fires. The website CAN detect this mismatch as automation.`,
1029
+ codeExample: [
1030
+ "# \u274C BLOCKED \u2014 what you tried to do:",
1031
+ `page.evaluate(\`el.value = 'hello'\`) # triggers isTrusted=false`,
1032
+ "",
1033
+ "# \u2705 USE INSTEAD \u2014 proper CDP input dispatch:",
1034
+ "page.fill('#selector', 'hello') # Playwright: dispatches input+change events",
1035
+ "page.type('#selector', 'hello', {delay}) # Playwright: real keystrokes",
1036
+ "page.locator('#selector').fill('hello') # Playwright: recommended API"
1037
+ ].join("\n")
1038
+ };
1039
+ case "mouse-trajectory":
1040
+ return {
1041
+ title: "Unnatural mouse trajectory blocked",
1042
+ detail: `Your ${method} sequence formed a perfectly linear path. Human hands have micro-tremors (1-3px variation at any point along the arc), acceleration curves, and never draw straight lines between distant points.`,
1043
+ codeExample: [
1044
+ "# \u274C BLOCKED \u2014 linear interpolation:",
1045
+ "for i in range(20):",
1046
+ " page.mouse.move(x0 + (x1-x0)*(i/20), y0 + (y1-y0)*(i/20))",
1047
+ "",
1048
+ "# \u2705 USE INSTEAD \u2014 Bezier curves with overshoot:",
1049
+ '# Use "ghost-cursor" or similar library',
1050
+ "from ghost_cursor import path_to",
1051
+ "path_to(page, (x1, y1))"
1052
+ ].join("\n")
1053
+ };
1054
+ case "input-keystroke":
1055
+ if (method === "Input.insertText") {
1056
+ return {
1057
+ title: "Input.insertText detected (logged, not blocked)",
1058
+ detail: "Input.insertText bypasses native keyDown\u2192keyPress\u2192input\u2192keyUp events. Playwright uses this internally for page.fill(). Logged for observability."
1059
+ };
1060
+ }
1061
+ return {
1062
+ title: "Unnatural keystroke timing blocked",
1063
+ detail: `Your ${method} calls have unnaturally constant timing (e.g., exact 50ms intervals). Human typing always has variation (CV > 0.2).`,
1064
+ codeExample: [
1065
+ "# \u274C BLOCKED \u2014 exact constant delay:",
1066
+ "page.type('#input', 'hello', {delay: 50}) # every keystroke exactly 50ms apart",
1067
+ "",
1068
+ "# \u2705 USE INSTEAD \u2014 variable delay (human-like):",
1069
+ "page.fill('#input', 'hello') # recommended, dispatches events properly",
1070
+ "# OR: type with randomized delay",
1071
+ "page.type('#input', 'hello', {delay: 50 + Math.floor(Math.random() * 80)})"
1072
+ ].join("\n")
1073
+ };
1074
+ case "automation-signals":
1075
+ return {
1076
+ title: "Browser automation marker detected",
1077
+ detail: `Your ${method} call accessed a property/marker that anti-crawler systems check to detect automation. These markers (navigator.webdriver, window.__playwright, etc.) are the #1 detection vector.`,
1078
+ codeExample: [
1079
+ "# \u274C BLOCKED \u2014 don't check for automation markers:",
1080
+ "navigator.webdriver # NEVER check this",
1081
+ "window.__playwright # NEVER check this",
1082
+ "chrome.runtime # NEVER check this",
1083
+ "",
1084
+ "# \u2705 Just go about your business normally.",
1085
+ "# Anti-detection is handled by the CDP firewall automatically."
1086
+ ].join("\n")
1087
+ };
1088
+ case "fingerprinting":
1089
+ return {
1090
+ title: "Browser fingerprinting API access blocked",
1091
+ detail: `Your ${method} call accessed a known fingerprinting API. These APIs (canvas.toDataURL, WebGL getParameter, AudioContext, etc.) are used by anti-crawler systems to build a unique device fingerprint.`,
1092
+ codeExample: [
1093
+ "# \u274C BLOCKED \u2014 fingerprinting vector:",
1094
+ "canvas.toDataURL() # returns unique browser hash",
1095
+ 'gl.getParameter(gl.VENDOR) # returns "SwiftShader" in headless',
1096
+ "screen.availWidth - screen.availHeight # no OS chrome in headless",
1097
+ "",
1098
+ "# \u2705 Avoid accessing these APIs. They are only used for fingerprinting."
1099
+ ].join("\n")
1100
+ };
1101
+ case "event-simulation":
1102
+ return {
1103
+ title: "Synthetic event simulation blocked",
1104
+ detail: `Your ${method} call simulated user interaction via el.click() or dispatchEvent(new Event(...)). These produce isTrusted=false events, which are 100% detectable by any anti-crawler that checks isTrusted on critical events.`,
1105
+ codeExample: [
1106
+ "# \u274C BLOCKED \u2014 synthetic events (isTrusted=false):",
1107
+ "el.click() # isTrusted=false",
1108
+ 'el.dispatchEvent(new Event("click")) # isTrusted=false',
1109
+ "el.focus() # isTrusted=false",
1110
+ "",
1111
+ "# \u2705 USE INSTEAD \u2014 CDP-level input dispatch (isTrusted=true):",
1112
+ "page.click(selector) # uses Input.dispatchMouseEvent",
1113
+ "page.fill(selector, value) # dispatches real input events"
1114
+ ].join("\n")
1115
+ };
1116
+ case "emulation-override":
1117
+ return {
1118
+ title: "CDP emulation override blocked",
1119
+ detail: `Your ${method} call overrides browser behavior in a way that creates detectable inconsistencies. Anti-crawler systems cross-check multiple sources (e.g., navigator.userAgent vs HTTP User-Agent header) to catch these mismatches.`,
1120
+ codeExample: [
1121
+ "# \u274C BLOCKED \u2014 detectable emulation override:",
1122
+ "Emulation.setUserAgentOverride(...) # JS vs HTTP header mismatch",
1123
+ "Emulation.setGeolocationOverride(...) # IP geo vs overridden geo mismatch",
1124
+ "Emulation.setDeviceMetricsOverride(...) # matchMedia vs actual viewport",
1125
+ "",
1126
+ "# \u2705 These are handled automatically by the CDP firewall.",
1127
+ "# Do NOT call them manually."
1128
+ ].join("\n")
1129
+ };
1130
+ case "network-anomaly":
1131
+ return {
1132
+ title: "Network anomaly detected",
1133
+ detail: `Your ${method} call triggered a network pattern that is characteristic of scrapers: blocking URLs, clearing caches, or intercepting requests.`,
1134
+ codeExample: [
1135
+ "# \u274C BLOCKED \u2014 scraper optimization:",
1136
+ "Network.clearBrowserCache() # natural users never do this",
1137
+ 'Network.setBlockedURLs(["*fonts*"]) # blocking resources is detectable',
1138
+ "Fetch.enable() # MITM-style interception",
1139
+ "",
1140
+ "# \u2705 Let the browser manage its own cache and network normally."
1141
+ ].join("\n")
1142
+ };
1143
+ case "page-lifecycle":
1144
+ return {
1145
+ title: "Suspicious page lifecycle pattern blocked",
1146
+ detail: `Your ${method} call reveals an unnatural page interaction sequence: navigating too fast, taking screenshots before the page renders, or generating PDFs (a telltale scraper giveaway).`,
1147
+ codeExample: [
1148
+ "# \u274C BLOCKED \u2014 unnatural lifecycle:",
1149
+ "page.goto(url); page.pdf() # PDF = scraper giveaway",
1150
+ "page.goto(url); page.screenshot() <500ms # screenshot before render",
1151
+ "page.goto(url) 3x in <100ms # rapid navigation barrage",
1152
+ "",
1153
+ "# \u2705 Add proper waits between actions:",
1154
+ 'page.goto(url, {waitUntil: "networkidle"})',
1155
+ 'page.waitForSelector("body")',
1156
+ "page.screenshot() # after rendering"
1157
+ ].join("\n")
1158
+ };
1159
+ default:
1160
+ return {
1161
+ title: decision.reason,
1162
+ detail: decision.suggestion ?? `The CDP call "${method}" was blocked by rule "${decision.ruleId}".`
1163
+ };
1164
+ }
1165
+ }
1166
+
1167
+ // src/cdp-interceptor/proxy.ts
1168
+ function makeCompoundId(cdpSessionId, rawSessionId) {
1169
+ return `${cdpSessionId ?? "nil"}::${rawSessionId ?? "nil"}`;
1170
+ }
1171
+ var CDPInterceptorProxy = class {
1172
+ wss = null;
1173
+ engine;
1174
+ config;
1175
+ logger;
1176
+ started = false;
1177
+ stats = {
1178
+ totalMessages: 0,
1179
+ blockedMessages: 0,
1180
+ transformedMessages: 0,
1181
+ passedMessages: 0,
1182
+ byRule: {}
1183
+ };
1184
+ constructor(config) {
1185
+ this.config = config;
1186
+ this.engine = createRuleEngine(config.rules);
1187
+ this.logger = createLogger({
1188
+ enableLogging: config.enableLogging ?? true,
1189
+ logDir: config.logDir
1190
+ });
1191
+ }
1192
+ /** The port the proxy is listening on (only valid after start()) */
1193
+ get port() {
1194
+ const addr = this.wss?.address();
1195
+ if (addr && typeof addr === "object") return addr.port;
1196
+ return 0;
1197
+ }
1198
+ /** Start the proxy server */
1199
+ async start() {
1200
+ if (this.started) return this.port;
1201
+ return new Promise((resolve, reject) => {
1202
+ this.wss = new WebSocketServer({ port: this.config.listenPort ?? 0 }, () => {
1203
+ const port = this.port;
1204
+ this.engine.start();
1205
+ this.started = true;
1206
+ this.logger.info("CDP interceptor proxy started", { port, endpoint: this.config.cdpEndpoint });
1207
+ resolve(port);
1208
+ });
1209
+ this.wss.on("error", reject);
1210
+ this.wss.on("connection", (clientWs, _req) => {
1211
+ this.handleConnection(clientWs);
1212
+ });
1213
+ });
1214
+ }
1215
+ /** Stop the proxy server */
1216
+ async stop() {
1217
+ this.engine.stop();
1218
+ this.logger.flush();
1219
+ this.started = false;
1220
+ return new Promise((resolve) => {
1221
+ if (!this.wss) return resolve();
1222
+ this.wss.close(() => resolve());
1223
+ this.wss = null;
1224
+ });
1225
+ }
1226
+ /** Get accumulated statistics */
1227
+ getStats() {
1228
+ return { ...this.stats };
1229
+ }
1230
+ /** Get recent log entries (for inspection) */
1231
+ getRecentLogs(count = 50) {
1232
+ return this.logger.getRecent(count);
1233
+ }
1234
+ // ── Connection handling ──────────────────────────────────────
1235
+ handleConnection(clientWs) {
1236
+ let browserWs = null;
1237
+ let isAlive = true;
1238
+ const pendingMessages = [];
1239
+ browserWs = new WebSocket(this.config.cdpEndpoint);
1240
+ clientWs.on("message", (raw) => {
1241
+ if (browserWs && browserWs.readyState === WebSocket.OPEN) {
1242
+ this.handleClientMessage(clientWs, browserWs, raw);
1243
+ } else {
1244
+ pendingMessages.push(raw);
1245
+ }
1246
+ });
1247
+ browserWs.on("open", () => {
1248
+ for (const buf of pendingMessages) {
1249
+ this.handleClientMessage(clientWs, browserWs, buf);
1250
+ }
1251
+ pendingMessages.length = 0;
1252
+ });
1253
+ browserWs.on("error", (err) => {
1254
+ this.logger.info("Browser WebSocket error", { error: String(err) });
1255
+ });
1256
+ browserWs.on("close", (code, reason) => {
1257
+ if (isAlive && clientWs.readyState === WebSocket.OPEN) {
1258
+ this.logger.info("Browser WS closed, closing client", { code, reason: String(reason) });
1259
+ clientWs.close();
1260
+ }
1261
+ });
1262
+ browserWs.on("message", (raw) => {
1263
+ this.handleBrowserMessage(clientWs, browserWs, raw);
1264
+ });
1265
+ const cleanup = () => {
1266
+ isAlive = false;
1267
+ if (browserWs && browserWs.readyState === WebSocket.OPEN) {
1268
+ browserWs.close();
1269
+ }
1270
+ };
1271
+ clientWs.on("close", cleanup);
1272
+ clientWs.on("error", cleanup);
1273
+ browserWs.on("close", () => {
1274
+ if (isAlive && clientWs.readyState === WebSocket.OPEN) {
1275
+ clientWs.close();
1276
+ }
1277
+ });
1278
+ browserWs.on("error", cleanup);
1279
+ }
1280
+ // ── Message processing ───────────────────────────────────────
1281
+ handleClientMessage(clientWs, browserWs, raw) {
1282
+ const msg = this.parseMessage(raw);
1283
+ if (!msg) return;
1284
+ this.stats.totalMessages++;
1285
+ if (!("method" in msg)) {
1286
+ browserWs.send(raw.toString());
1287
+ return;
1288
+ }
1289
+ const request = msg;
1290
+ const ctx = {
1291
+ method: request.method,
1292
+ params: request.params ?? {},
1293
+ sessionId: makeCompoundId(browserWs._cdpSession, request.sessionId),
1294
+ direction: "client\u2192browser"
1295
+ };
1296
+ const decision = this.engine.evaluate(ctx);
1297
+ this.logger.log(ctx.method, "client\u2192browser", ctx.sessionId, { method: ctx.method, params: ctx.params }, decision);
1298
+ if (decision) {
1299
+ this.recordDecision(decision);
1300
+ }
1301
+ if (decision?.action === "block") {
1302
+ const blockMsg = formatBlockMessage(decision, ctx.method);
1303
+ const errorResponse = {
1304
+ id: request.id,
1305
+ error: {
1306
+ code: decision.errorCode ?? -32e3,
1307
+ message: blockMsg
1308
+ },
1309
+ sessionId: request.sessionId
1310
+ };
1311
+ this.stats.blockedMessages++;
1312
+ console.error(`
1313
+ ${blockMsg}
1314
+ `);
1315
+ clientWs.send(JSON.stringify(errorResponse));
1316
+ return;
1317
+ }
1318
+ if (decision?.action === "transform" && decision.transformedParams) {
1319
+ const transformed = { ...request, params: decision.transformedParams };
1320
+ this.stats.transformedMessages++;
1321
+ browserWs.send(JSON.stringify(transformed));
1322
+ return;
1323
+ }
1324
+ this.stats.passedMessages++;
1325
+ browserWs.send(raw.toString());
1326
+ }
1327
+ handleBrowserMessage(clientWs, _browserWs, raw) {
1328
+ const msg = this.parseMessage(raw);
1329
+ if (!msg) {
1330
+ clientWs.send(raw.toString());
1331
+ return;
1332
+ }
1333
+ if ("method" in msg) {
1334
+ const event = msg;
1335
+ const ctx = {
1336
+ method: event.method,
1337
+ params: event.params ?? {},
1338
+ sessionId: event.sessionId ?? "browser",
1339
+ direction: "browser\u2192client"
1340
+ };
1341
+ const decision = this.engine.evaluate(ctx);
1342
+ if (decision?.action === "block") {
1343
+ return;
1344
+ }
1345
+ }
1346
+ clientWs.send(raw.toString());
1347
+ }
1348
+ // ── Utilities ────────────────────────────────────────────────
1349
+ parseMessage(raw) {
1350
+ try {
1351
+ return JSON.parse(raw.toString());
1352
+ } catch {
1353
+ return null;
1354
+ }
1355
+ }
1356
+ recordDecision(decision) {
1357
+ if (!this.stats.byRule[decision.ruleId]) {
1358
+ this.stats.byRule[decision.ruleId] = { matched: 0, blocked: 0, transformed: 0 };
1359
+ }
1360
+ this.stats.byRule[decision.ruleId].matched++;
1361
+ if (decision.action === "block") {
1362
+ this.stats.byRule[decision.ruleId].blocked++;
1363
+ } else if (decision.action === "transform") {
1364
+ this.stats.byRule[decision.ruleId].transformed++;
1365
+ }
1366
+ }
1367
+ };
1368
+
1369
+ export {
1370
+ domMutationRule,
1371
+ mouseTrajectoryRule,
1372
+ inputKeystrokeRule,
1373
+ automationSignalsRule,
1374
+ fingerprintingRule,
1375
+ eventSimulationRule,
1376
+ emulationOverrideRule,
1377
+ networkAnomalyRule,
1378
+ pageLifecycleRule,
1379
+ createRuleEngine,
1380
+ advise,
1381
+ CDPInterceptorProxy
1382
+ };