#8818: fix(browser): block unsafe code patterns in browser evaluate
stale
Cluster:
SSRF Protection Enhancements
## Summary
Adds code validation to `evaluateViaPlaywright` to block dangerous patterns that could be used for data exfiltration or code injection.
## The Problem
The browser automation `evaluate` function executes user-provided JavaScript code without any validation. An attacker controlling the code (via prompt injection) could use APIs like `fetch()`, `WebSocket`, or `navigator.sendBeacon()` to exfiltrate cookies, credentials, and sensitive data from the browser context.
Reference: [CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code](https://cwe.mitre.org/data/definitions/95.html)
## Changes
- `src/browser/pw-evaluate-validation.ts`: New validation module with blocklist of dangerous patterns
- `src/browser/pw-tools-core.interactions.ts`: Add validation call before code execution
- `src/browser/pw-evaluate-validation.test.ts`: Unit tests for validation logic (29 tests)
- `src/browser/pw-tools-core.evaluate-blocks-unsafe.test.ts`: Integration tests (13 tests)
**Blocked patterns:**
- Data exfiltration: `fetch`, `XMLHttpRequest`, `WebSocket`, `sendBeacon`
- Code execution: `eval`, `new Function`, `setTimeout`/`setInterval` with strings
- Module loading: dynamic `import`, `importScripts`
## Test Plan
- [x] `pnpm build && pnpm check && pnpm test` passes
- [x] 29 unit tests validate blocking of unsafe patterns and allowing of safe operations
- [x] 13 integration tests confirm `evaluateViaPlaywright` rejects unsafe code
- [x] Validation occurs before page access (early rejection)
## Related
- [CWE-95](https://cwe.mitre.org/data/definitions/95.html)
- Internal audit ref: VULN-037
---
*Built with [bitsec-ai](https://github.com/bitsec-ai). AI-assisted: Yes. Testing: fully tested (test written before fix). Code reviewed and understood.*
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds a pre-execution validation step for `evaluateViaPlaywright` to reject browser-evaluated code containing a set of blocked “dangerous” patterns (exfiltration APIs, dynamic code execution, and module loading). This is implemented via a new `pw-evaluate-validation.ts` module and covered by unit tests for the validator plus integration tests that ensure unsafe `fn` strings are rejected before interacting with a Playwright page.
<h3>Confidence Score: 3/5</h3>
- This PR is directionally correct but has a couple of behavioral and security-footgun edge cases worth addressing before merge.
- The validation is a simple regex blocklist and the tests are extensive, but (1) blocking any `eval(` token is likely to break legitimate evaluate snippets given this function always uses `eval` internally, and (2) the error message currently echoes user code which can leak sensitive data into logs. No other obvious correctness issues were found in the touched paths.
- src/browser/pw-evaluate-validation.ts, src/browser/pw-tools-core.interactions.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#20657: Security: add length validation to browser evaluate function
by mahanandhi · 2026-02-19
81.7%
#8305: fix(browser): add SSRF protection to browser navigation
by yubrew · 2026-02-03
79.9%
#8124: fix(browser): add path validation for file upload and download
by yubrew · 2026-02-03
78.4%
#14197: fix(security): harden browser API auth, token comparisons, and hook...
by leecarollyn-gif · 2026-02-11
75.1%
#8139: fix(config): block dangerous environment variables from config.env
by yubrew · 2026-02-03
74.3%
#11435: fix(security): validate OPENCLAW_BROWSER_CONTROL_MODULE before dyna...
by coygeek · 2026-02-07
73.5%
#8718: fix: sanitize download filenames to prevent path traversal (CWE-22)
by DevZenPro · 2026-02-04
73.4%
#10559: feat(security): add plugin output scanner for prompt injection dete...
by DukeDeSouth · 2026-02-06
72.5%
#8339: fix(tts): validate ElevenLabs base URL against allowlist
by yubrew · 2026-02-03
72.5%
#8161: fix(sandbox): block dangerous environment variables from Docker con...
by yubrew · 2026-02-03
72.5%