Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Remove WebRTC from the lens frame and turn off DNS prefetching

Claudeb5edcf63fc21
diff --git a/cli/beacon/internal/endpoint/dashboard/lenses.go b/cli/beacon/internal/endpoint/dashboard/lenses.go
index ae6d36d3..0c2cae08 100644
--- a/cli/beacon/internal/endpoint/dashboard/lenses.go
+++ b/cli/beacon/internal/endpoint/dashboard/lenses.go
@@ -235,10 +235,16 @@ func (r *lensRegistry) list() LensListResponse {
// lensPrelude is what the dashboard puts ahead of every lens: the style tokens, then the script
// that defines window.beacon. Both are inline because the frame's CSP allows nothing else.
+//
+// The meta element repeats the X-DNS-Prefetch-Control header in the document itself, so a lens
+// file opened outside the frame route still has implicit DNS prefetching turned off.
func lensPrelude() []byte {
- return []byte("<style>" + lensTokensCSS + "</style><script>" + lensPreludeJS + "</script>")
+ return []byte("<style>" + lensTokensCSS + "</style>" + lensDNSPrefetchMeta + "<script>" + lensPreludeJS + "</script>")
}
+// lensDNSPrefetchMeta turns off the browser's implicit DNS prefetching of hostnames in the page.
+const lensDNSPrefetchMeta = `<meta http-equiv="x-dns-prefetch-control" content="off">`
+
// withLensPrelude inserts the prelude right after the lens's doctype, so the document keeps its
// standards mode and the prelude still runs before any lens script. A prelude ahead of <html> is
// parsed into <head>, which is where it belongs.
@@ -295,6 +301,8 @@ func (r *lensRegistry) serveLensFrame(w http.ResponseWriter, req *http.Request)
h.Set("Content-Type", "text/html; charset=utf-8")
h.Set("Cache-Control", "no-store")
h.Set("Cross-Origin-Resource-Policy", "same-origin")
+ // Implicit DNS prefetching resolves hostnames the page mentions, which the CSP does not govern.
+ h.Set("X-DNS-Prefetch-Control", "off")
w.WriteHeader(http.StatusOK)
if req.Method == http.MethodHead {
return
diff --git a/cli/beacon/internal/endpoint/dashboard/lenses/SPEC.md b/cli/beacon/internal/endpoint/dashboard/lenses/SPEC.md
index 18f60209..9e63a33d 100644
--- a/cli/beacon/internal/endpoint/dashboard/lenses/SPEC.md
+++ b/cli/beacon/internal/endpoint/dashboard/lenses/SPEC.md
@@ -177,6 +177,21 @@ The host enforces these rules; a lens does not opt into them.
No `fetch`, XHR, WebSocket, `EventSource`, remote image, remote font,
worker, nested frame, `eval` or `new Function`. Load nothing by URL.
+- WebRTC is not governed by the CSP, so the host removes it: before any lens
+ script runs, every `RTC*` interface on `window` (including
+ `RTCPeerConnection`, `RTCDataChannel` and `webkitRTCPeerConnection`) is
+ defined as `undefined`, non-writable and non-configurable. A lens cannot
+ delete or redefine them. This holds inside the lens's own realm only; it
+ relies on the CSP and sandbox above to keep the lens from reaching a fresh
+ realm (a frame, worker or popup) where the originals still exist.
+- The frame is served with `X-DNS-Prefetch-Control: off`, and the host
+ repeats it as a `<meta http-equiv>` in the document, so the browser does not
+ resolve hostnames the lens mentions on its own.
+- Residual risk. CSP and the sandbox are the boundary, and browsers do not
+ route every speculative or connection-warming request through them; some
+ resource-hint and speculative-loading features can still start a request a
+ CSP does not see, depending on the browser and its settings. Treat a lens
+ as code that can read the whole trace, and install only lenses you trust.
- Trace data reaches the frame only over a private `MessageChannel` port. The
one `postMessage` from the host to the window carries that port and
nothing else. The lens never uses `window.postMessage` itself;
diff --git a/cli/beacon/internal/endpoint/dashboard/lenses/prelude.js b/cli/beacon/internal/endpoint/dashboard/lenses/prelude.js
index a68e5703..92d76bfd 100644
--- a/cli/beacon/internal/endpoint/dashboard/lenses/prelude.js
+++ b/cli/beacon/internal/endpoint/dashboard/lenses/prelude.js
@@ -13,6 +13,23 @@
// the host is the only party allowed to talk to the frame and anything else is ignored.
(function () {
"use strict";
+
+ // WebRTC is the one network API the frame's CSP does not govern: connect-src does not apply to
+ // ICE, and sandbox="allow-scripts" leaves RTCPeerConnection working, so a lens could hand trace
+ // data to a STUN/TURN server it names. Remove every RTC* interface (and the webkit alias) before
+ // any lens code runs, as non-configurable, non-writable undefined properties so a lens can neither
+ // delete nor redefine them. This is hardening inside the lens's own realm, not a browser-enforced
+ // boundary: it holds because the lens cannot reach a fresh realm (the CSP refuses frames and
+ // workers, the sandbox refuses popups, and a sandboxed about:blank child is cross-origin to it).
+ Object.getOwnPropertyNames(window).forEach(function (name) {
+ if (!/^(webkit)?RTC/.test(name)) return;
+ try {
+ Object.defineProperty(window, name, { value: undefined, writable: false, configurable: false, enumerable: false });
+ } catch (e) {
+ // A property that is already non-configurable cannot be replaced; leave it.
+ }
+ });
+
var TIMEOUT_MS = 10000;
var port = null;
var pending = [];
diff --git a/cli/beacon/internal/endpoint/dashboard/lenses_test.go b/cli/beacon/internal/endpoint/dashboard/lenses_test.go
index a185192e..af279d42 100644
--- a/cli/beacon/internal/endpoint/dashboard/lenses_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/lenses_test.go
@@ -172,8 +172,19 @@ func TestLensFrameIsServedSandboxedWithThePrelude(t *testing.T) {
if h.Get("X-Content-Type-Options") != "nosniff" || h.Get("Cache-Control") != "no-store" {
t.Fatalf("headers = %v", h)
}
+ if got := h.Get("X-DNS-Prefetch-Control"); got != "off" {
+ t.Fatalf("X-DNS-Prefetch-Control = %q, want off", got)
+ }
body := rec.Body.String()
+ if !strings.Contains(body, lensDNSPrefetchMeta) {
+ t.Fatal("frame lacks the DNS prefetch meta")
+ }
+ // WebRTC is not governed by the CSP, so the prelude removes it before any lens script runs.
+ rtc := strings.Index(body, "(webkit)?RTC")
+ if rtc < 0 || rtc > strings.Index(body, "window.beacon.getTrace()") {
+ t.Fatal("the prelude must remove the WebRTC interfaces before the lens's own script")
+ }
if !strings.HasPrefix(body, "<!doctype html><style>") {
t.Fatalf("frame does not start with the doctype then the prelude: %.80q", body)
}
diff --git a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/fixtures/probe.lens.html b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/fixtures/probe.lens.html
index 0e9f0234..751554a7 100644
--- a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/fixtures/probe.lens.html
+++ b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/fixtures/probe.lens.html
@@ -75,6 +75,30 @@ window.beacon.getTrace().then(async function (data) {
setTimeout(function () { reject(new Error("timeout")); }, 2000);
});
});
+ // WebRTC is outside the CSP, so the prelude removes it. Each probe passes only if no peer
+ // connection constructor is reachable, whether named directly, by a computed name, through the
+ // webkit alias, or after trying to delete or redefine the removed global.
+ function rtcReachable(get) {
+ return attempt(function () {
+ var ctor = get();
+ if (typeof ctor !== "function") throw new Error("absent");
+ return typeof ctor;
+ });
+ }
+ r.rtcDirect = await rtcReachable(function () { return window.RTCPeerConnection; });
+ r.rtcComputed = await rtcReachable(function () { return window["RTCPeer" + "Connection"]; });
+ r.rtcWebkit = await rtcReachable(function () { return window.webkitRTCPeerConnection; });
+ r.rtcDataChannel = await rtcReachable(function () { return window.RTCDataChannel; });
+ r.rtcDelete = await rtcReachable(function () {
+ try { delete window.RTCPeerConnection; } catch (e) {}
+ return window.RTCPeerConnection;
+ });
+ r.rtcRedefine = await rtcReachable(function () {
+ try { Object.defineProperty(window, "RTCPeerConnection", { value: function () {}, configurable: true, writable: true }); } catch (e) {}
+ try { window.RTCPeerConnection = function () {}; } catch (e) {}
+ return window.RTCPeerConnection;
+ });
+ r.dnsPrefetchMeta = !!document.querySelector('meta[http-equiv="x-dns-prefetch-control"][content="off"]');
r.origin = self.origin;
r.seenPorts = seenPorts;
document.getElementById("results").textContent = JSON.stringify(r);
diff --git a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/lenses.spec.ts b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/lenses.spec.ts
index da19679e..1d5d7b39 100644
--- a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/lenses.spec.ts
+++ b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/lenses.spec.ts
@@ -78,9 +78,13 @@ test('a lens cannot reach the network, the dashboard, storage or code evaluation
for (const probe of [
'fetchSameOrigin', 'fetchRemote', 'xhr', 'websocket', 'image', 'nestedFrame',
'parentDocument', 'topLocation', 'localStorage', 'cookie', 'eval', 'newFunction', 'popup', 'worker',
+ 'rtcDirect', 'rtcComputed', 'rtcWebkit', 'rtcDataChannel', 'rtcDelete', 'rtcRedefine',
]) {
expect(r[probe], probe).toBe('blocked');
}
+ expect(r.dnsPrefetchMeta).toBe(true); // implicit DNS prefetching is off in the document
+ const frameResponse = await page.request.get('/lenses/frame/probe');
+ expect(frameResponse.headers()['x-dns-prefetch-control']).toBe('off');
// Spoofed window messages to the host changed nothing.
const height = await page.locator('iframe.lens-frame').evaluate((f: HTMLIFrameElement) => f.style.height);
expect(height).not.toBe('99999px');
diff --git a/cli/beacon/internal/endpoint/lensstore/lint.go b/cli/beacon/internal/endpoint/lensstore/lint.go
index 3d44e2b7..319f0d9c 100644
--- a/cli/beacon/internal/endpoint/lensstore/lint.go
+++ b/cli/beacon/internal/endpoint/lensstore/lint.go
@@ -46,6 +46,8 @@ var lintChecks = []struct {
"sizes to the viewport; the frame grows to fit the document, so a viewport-height root never settles"},
{regexp.MustCompile(`new\s+Worker\s*\(|new\s+SharedWorker\s*\(|serviceWorker`),
"starts a worker, which the lens CSP blocks"},
+ {regexp.MustCompile(`\b(webkit)?RTC(PeerConnection|DataChannel)\b`),
+ "uses WebRTC, which the lens frame removes; everything comes from getTrace()"},
}
// Lint checks a lens file against spec/lenses/SPEC.md. The file-level rules (size, manifest) are
diff --git a/cli/beacon/internal/endpoint/lensstore/lint_test.go b/cli/beacon/internal/endpoint/lensstore/lint_test.go
index 65d47b8e..79430221 100644
--- a/cli/beacon/internal/endpoint/lensstore/lint_test.go
+++ b/cli/beacon/internal/endpoint/lensstore/lint_test.go
@@ -25,6 +25,7 @@ func TestLintFlagsWhatTheSandboxBlocks(t *testing.T) {
`location.href = "/elsewhere"`: "navigates a window",
`main { height: 100vh }`: "viewport",
`new Worker(url)`: "worker",
+ `new RTCPeerConnection(config)`: "WebRTC",
}
for line, want := range cases {
html := "<!doctype html>\n" + lintManifest + "\n<script>window.beacon.getTrace()</script>\n" + line + "\n"
diff --git a/spec/lenses/SPEC.md b/spec/lenses/SPEC.md
index 18f60209..9e63a33d 100644
--- a/spec/lenses/SPEC.md
+++ b/spec/lenses/SPEC.md
@@ -177,6 +177,21 @@ The host enforces these rules; a lens does not opt into them.
No `fetch`, XHR, WebSocket, `EventSource`, remote image, remote font,
worker, nested frame, `eval` or `new Function`. Load nothing by URL.
+- WebRTC is not governed by the CSP, so the host removes it: before any lens
+ script runs, every `RTC*` interface on `window` (including
+ `RTCPeerConnection`, `RTCDataChannel` and `webkitRTCPeerConnection`) is
+ defined as `undefined`, non-writable and non-configurable. A lens cannot
+ delete or redefine them. This holds inside the lens's own realm only; it
+ relies on the CSP and sandbox above to keep the lens from reaching a fresh
+ realm (a frame, worker or popup) where the originals still exist.
+- The frame is served with `X-DNS-Prefetch-Control: off`, and the host
+ repeats it as a `<meta http-equiv>` in the document, so the browser does not
+ resolve hostnames the lens mentions on its own.
+- Residual risk. CSP and the sandbox are the boundary, and browsers do not
+ route every speculative or connection-warming request through them; some
+ resource-hint and speculative-loading features can still start a request a
+ CSP does not see, depending on the browser and its settings. Treat a lens
+ as code that can read the whole trace, and install only lenses you trust.
- Trace data reaches the frame only over a private `MessageChannel` port. The
one `postMessage` from the host to the window carries that port and
nothing else. The lens never uses `window.postMessage` itself;