Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(replay): build the canvas replay image in the recorded document (#99457)

Tom Piccirelloe4bdbbb6bad2
diff --git a/common/replay-shared/src/canvas/canvas-plugin.test.ts b/common/replay-shared/src/canvas/canvas-plugin.test.ts
index 5f96056c3e61..2844b7322fec 100644
--- a/common/replay-shared/src/canvas/canvas-plugin.test.ts
+++ b/common/replay-shared/src/canvas/canvas-plugin.test.ts
@@ -394,6 +394,28 @@ describe('CanvasReplayerPlugin', () => {
)
})
+ describe('reconstructed image document', () => {
+ it('builds the image in the document that holds the recorded canvas', () => {
+ const replayDocument = document.implementation.createHTMLDocument()
+ const canvas = replayDocument.createElement('canvas')
+ replayDocument.body.appendChild(canvas)
+
+ const created: Element[] = []
+ const createInReplayDocument = replayDocument.createElement.bind(replayDocument)
+ jest.spyOn(replayDocument, 'createElement').mockImplementation((tagName: string) => {
+ const element = createInReplayDocument(tagName)
+ created.push(element)
+ return element
+ })
+
+ const plugin = CanvasReplayerPlugin([])
+ plugin.onBuild?.(canvas, { id: 1, replayer: mockReplayer } as any)
+
+ const image = created.find((element) => element.tagName === 'IMG')
+ expect(image?.ownerDocument).toBe(replayDocument)
+ })
+ })
+
describe('target canvas sizing from snapshot mutations', () => {
const makeCanvasEvent = (
id: number,
diff --git a/common/replay-shared/src/canvas/canvas-plugin.ts b/common/replay-shared/src/canvas/canvas-plugin.ts
index 5f857bc11f15..1facecda9e84 100644
--- a/common/replay-shared/src/canvas/canvas-plugin.ts
+++ b/common/replay-shared/src/canvas/canvas-plugin.ts
@@ -363,17 +363,22 @@ export const CanvasReplayerPlugin = (
}
if (node.nodeName === 'CANVAS' && node.nodeType === 1) {
- const el = containers.get(id) || document.createElement('img')
const canvasElement = node as HTMLCanvasElement
+ // The <img> takes the recorded canvas's place, so it is built in that canvas's own
+ // document. A blob: URL is fetched under the Content-Security-Policy of the document
+ // that owns the element, and the element owns that policy from the moment `src` is
+ // assigned, which happens below while the <img> is still detached. Building it in the
+ // app's document therefore puts every replayed canvas frame under the app's policy.
+ const el = containers.get(id) || canvasElement.ownerDocument.createElement('img')
for (let i = 0; i < canvasElement.attributes.length; i++) {
const attr = canvasElement.attributes[i]
const name = attr.name.toLowerCase()
- // The reconstructed <img> lives in the top-level document, so it inherits only
- // presentational attributes from the recorded canvas. Skip inline event handlers
- // (every handler is named `on<event>`, so this covers the whole class) and the
- // URL-loading attributes — the plugin points `src` at the rendered canvas blob
- // itself, so a copied `src`/`srcset` would only fetch an attacker-controlled URL.
+ // The reconstructed <img> inherits only presentational attributes from the
+ // recorded canvas. Skip inline event handlers (every handler is named
+ // `on<event>`, so this covers the whole class) and the URL-loading attributes,
+ // because the plugin points `src` at the rendered canvas blob itself, so a
+ // copied `src`/`srcset` would only fetch an attacker-controlled URL.
if (name.startsWith('on') || name === 'src' || name === 'srcset') {
continue
}