Skip to content

Write HTML. Render video. Built for agents.

fix(core): route media through Web Audio only when the document can actually read it (#3937)

Miguel Ángel293829a2c1bd
diff --git a/packages/core/src/runtime/webAudioRoute.test.ts b/packages/core/src/runtime/webAudioRoute.test.ts
index 4069630980..f9c072c8cf 100644
--- a/packages/core/src/runtime/webAudioRoute.test.ts
+++ b/packages/core/src/runtime/webAudioRoute.test.ts
@@ -34,6 +34,20 @@ function withUnreflectedCrossOrigin(el: HTMLAudioElement, value: string): HTMLAu
return el;
}
+/** Stubs the document's actual security origin (`window.origin`) for the
+ * duration of `run`, then restores it — distinct from `location.origin`,
+ * which stays URL-shaped even inside an opaque sandboxed document. */
+function withSelfOrigin<T>(origin: string, run: () => T): T {
+ const original = Object.getOwnPropertyDescriptor(window, "origin");
+ Object.defineProperty(window, "origin", { value: origin, configurable: true });
+ try {
+ return run();
+ } finally {
+ if (original) Object.defineProperty(window, "origin", original);
+ else delete (window as { origin?: string }).origin;
+ }
+}
+
describe("classifyWebAudioMediaRoute", () => {
it("routes same-origin media through Web Audio", () => {
expect(classifyWebAudioMediaRoute(audio({ src: "/assets/vo.mp3" }))).toEqual({
@@ -136,6 +150,23 @@ describe("classifyWebAudioMediaRoute", () => {
asset: `${CROSS_ORIGIN}/track.mp3`,
});
});
+
+ it("withholds capture when the document's security origin is opaque, even though the URL origins match", () => {
+ // See withSelfOrigin above for why this differs from location.origin.
+ const asset = `${SAME_ORIGIN}/assets/vo.mp3`;
+ const route = withSelfOrigin("null", () => classifyWebAudioMediaRoute(audio({ src: asset })));
+
+ expect(route).toEqual({ kind: "decode-only", reason: "cross_origin_no_cors", asset });
+ });
+
+ it("keeps today's same-origin result when window.origin genuinely matches", () => {
+ const asset = `${SAME_ORIGIN}/assets/vo.mp3`;
+ const route = withSelfOrigin(SAME_ORIGIN, () =>
+ classifyWebAudioMediaRoute(audio({ src: asset })),
+ );
+
+ expect(route).toEqual({ kind: "web-audio" });
+ });
});
describe("isRouteSelectionSettled", () => {
diff --git a/packages/core/src/runtime/webAudioRoute.ts b/packages/core/src/runtime/webAudioRoute.ts
index 0866e8c60a..9c1252f09c 100644
--- a/packages/core/src/runtime/webAudioRoute.ts
+++ b/packages/core/src/runtime/webAudioRoute.ts
@@ -144,7 +144,12 @@ function isCorsSilenced(rawUrl: string, el: HTMLMediaElement): boolean {
return false;
}
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
- if (url.origin === window.location.origin) return false;
+ // `location.origin` is the URL's origin, not the document's actual
+ // security origin: a sandboxed iframe without `allow-same-origin` still
+ // reports the parent-looking URL there while `window.origin` reads
+ // `"null"`. An opaque document can't prove same-origin with anything.
+ const selfOrigin = typeof window.origin === "string" ? window.origin : window.location.origin;
+ if (selfOrigin !== "null" && url.origin === selfOrigin) return false;
return !hasCorsOptIn(el);
}