Skip to content

Commit 293829a

Browse files
fix(core): route media through Web Audio only when the document can actually read it (#3937)
isCorsSilenced() compared a media URL's origin against window.location.origin, which stays URL-shaped even inside a sandboxed iframe without allow-same-origin. Inside such a frame the document's actual security origin (window.origin) is the string "null", so same-URL-origin media without a crossorigin attribute was waved through createMediaElementSource, and per the Web Audio spec's MediaElementAudioSourceNode security section that node outputs silence without throwing. The composition kept playing with the timeline advancing and no audio at all. Compare against window.origin instead, treating "null" as never same-origin, so an opaque document falls back to the existing decode-only path unless the media element opts in with crossorigin.
1 parent d13a89b commit 293829a

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎packages/core/src/runtime/webAudioRoute.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ function withUnreflectedCrossOrigin(el: HTMLAudioElement, value: string): HTMLAu
3434
return el;
3535
}
3636

37+
/** Stubs the document's actual security origin (`window.origin`) for the
38+
* duration of `run`, then restores it — distinct from `location.origin`,
39+
* which stays URL-shaped even inside an opaque sandboxed document. */
40+
function withSelfOrigin<T>(origin: string, run: () => T): T {
41+
const original = Object.getOwnPropertyDescriptor(window, "origin");
42+
Object.defineProperty(window, "origin", { value: origin, configurable: true });
43+
try {
44+
return run();
45+
} finally {
46+
if (original) Object.defineProperty(window, "origin", original);
47+
else delete (window as { origin?: string }).origin;
48+
}
49+
}
50+
3751
describe("classifyWebAudioMediaRoute", () => {
3852
it("routes same-origin media through Web Audio", () => {
3953
expect(classifyWebAudioMediaRoute(audio({ src: "/assets/vo.mp3" }))).toEqual({
@@ -136,6 +150,23 @@ describe("classifyWebAudioMediaRoute", () => {
136150
asset: `${CROSS_ORIGIN}/track.mp3`,
137151
});
138152
});
153+
154+
it("withholds capture when the document's security origin is opaque, even though the URL origins match", () => {
155+
// See withSelfOrigin above for why this differs from location.origin.
156+
const asset = `${SAME_ORIGIN}/assets/vo.mp3`;
157+
const route = withSelfOrigin("null", () => classifyWebAudioMediaRoute(audio({ src: asset })));
158+
159+
expect(route).toEqual({ kind: "decode-only", reason: "cross_origin_no_cors", asset });
160+
});
161+
162+
it("keeps today's same-origin result when window.origin genuinely matches", () => {
163+
const asset = `${SAME_ORIGIN}/assets/vo.mp3`;
164+
const route = withSelfOrigin(SAME_ORIGIN, () =>
165+
classifyWebAudioMediaRoute(audio({ src: asset })),
166+
);
167+
168+
expect(route).toEqual({ kind: "web-audio" });
169+
});
139170
});
140171

141172
describe("isRouteSelectionSettled", () => {

‎packages/core/src/runtime/webAudioRoute.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ function isCorsSilenced(rawUrl: string, el: HTMLMediaElement): boolean {
144144
return false;
145145
}
146146
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
147-
if (url.origin === window.location.origin) return false;
147+
// `location.origin` is the URL's origin, not the document's actual
148+
// security origin: a sandboxed iframe without `allow-same-origin` still
149+
// reports the parent-looking URL there while `window.origin` reads
150+
// `"null"`. An opaque document can't prove same-origin with anything.
151+
const selfOrigin = typeof window.origin === "string" ? window.origin : window.location.origin;
152+
if (selfOrigin !== "null" && url.origin === selfOrigin) return false;
148153
return !hasCorsOptIn(el);
149154
}
150155

0 commit comments

Comments
 (0)