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(product-tours): add null safety checks to embed URL parsing (#59219)

Adam Bowker89b6f31c7dc8
diff --git a/frontend/src/scenes/product-tours/editor/EmbedExtension.test.ts b/frontend/src/scenes/product-tours/editor/EmbedExtension.test.ts
new file mode 100644
index 000000000000..5a62d5a6b885
--- /dev/null
+++ b/frontend/src/scenes/product-tours/editor/EmbedExtension.test.ts
@@ -0,0 +1,54 @@
+import { EmbedExtension } from './EmbedExtension'
+
+type RenderResult = unknown[]
+
+function renderEmbedHTML(attrs: Record<string, unknown>): RenderResult {
+ const config = (
+ EmbedExtension as unknown as {
+ config: { renderHTML: (args: { HTMLAttributes: Record<string, unknown> }) => RenderResult }
+ }
+ ).config
+ return config.renderHTML.call({ options: { HTMLAttributes: {} } } as ThisParameterType<typeof config.renderHTML>, {
+ HTMLAttributes: attrs,
+ })
+}
+
+function findIframe(tree: unknown): unknown[] | null {
+ if (!Array.isArray(tree)) {
+ return null
+ }
+ if (tree[0] === 'iframe') {
+ return tree as unknown[]
+ }
+ for (const child of tree) {
+ const found = findIframe(child)
+ if (found) {
+ return found
+ }
+ }
+ return null
+}
+
+describe('EmbedExtension.renderHTML (VERIA-305)', () => {
+ it.each([
+ ['javascript:', 'javascript:alert(document.cookie)'],
+ ['arbitrary https', 'https://attacker.example/x.html'],
+ ['data:', 'data:text/html,<script>alert(1)</script>'],
+ ['empty', ''],
+ ['null', null],
+ ])('emits no iframe for unsupported src (%s)', (_label, src) => {
+ const tree = renderEmbedHTML({ src, provider: 'youtube' })
+ expect(findIframe(tree)).toBeNull()
+ })
+
+ it('emits a canonical iframe src for a valid YouTube URL — never the raw input', () => {
+ const tree = renderEmbedHTML({
+ src: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ&attacker=1',
+ provider: 'youtube',
+ })
+ const iframe = findIframe(tree)
+ expect(iframe).not.toBeNull()
+ const attrs = iframe![1] as Record<string, string>
+ expect(attrs.src).toBe('https://www.youtube.com/embed/dQw4w9WgXcQ')
+ })
+})
diff --git a/frontend/src/scenes/product-tours/editor/EmbedExtension.tsx b/frontend/src/scenes/product-tours/editor/EmbedExtension.tsx
index 97ccdb684219..0d4e2de93b76 100644
--- a/frontend/src/scenes/product-tours/editor/EmbedExtension.tsx
+++ b/frontend/src/scenes/product-tours/editor/EmbedExtension.tsx
@@ -21,6 +21,10 @@ const VIMEO_REGEX = /(?:https?:\/\/)?(?:www\.)?(?:vimeo\.com\/|player\.vimeo\.co
const LOOM_REGEX = /(?:https?:\/\/)?(?:www\.)?loom\.com\/(?:share|embed)\/([a-zA-Z0-9]+)/g
export function parseEmbedUrl(url: string): { provider: EmbedProvider; videoId: string; embedUrl: string } | null {
+ if (typeof url !== 'string') {
+ return null
+ }
+
const youtubeMatch = url.match(/(?:youtube\.com\/(?:watch\?v=|embed\/|v\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/)
if (youtubeMatch) {
return {
@@ -54,7 +58,10 @@ export function parseEmbedUrl(url: string): { provider: EmbedProvider; videoId:
function EmbedNodeView({ node }: { node: { attrs: Record<string, any> } }): JSX.Element {
const { src, provider } = node.attrs as EmbedAttributes
const parsed = parseEmbedUrl(src)
- const embedUrl = parsed?.embedUrl
+
+ if (!parsed) {
+ return <NodeViewWrapper className="embed-wrapper" data-provider="unknown" />
+ }
return (
<NodeViewWrapper className="embed-wrapper" data-provider={provider}>
@@ -70,7 +77,7 @@ function EmbedNodeView({ node }: { node: { attrs: Record<string, any> } }): JSX.
}}
>
<iframe
- src={embedUrl}
+ src={parsed.embedUrl}
title="Embedded media"
style={{
position: 'absolute',
@@ -132,15 +139,17 @@ export const EmbedExtension = Node.create<EmbedOptions>({
},
renderHTML({ HTMLAttributes }) {
- const src = HTMLAttributes.src as string
- const parsed = parseEmbedUrl(src)
- const embedUrl = parsed?.embedUrl || src
+ const parsed = parseEmbedUrl(HTMLAttributes.src as string)
+
+ if (!parsed) {
+ return ['div', mergeAttributes(this.options.HTMLAttributes, { class: 'ph-tour-embed' })]
+ }
return [
'div',
mergeAttributes(this.options.HTMLAttributes, {
class: 'ph-tour-embed',
- 'data-provider': HTMLAttributes.provider,
+ 'data-provider': parsed.provider,
}),
[
'div',
@@ -148,7 +157,7 @@ export const EmbedExtension = Node.create<EmbedOptions>({
[
'iframe',
{
- src: embedUrl,
+ src: parsed.embedUrl,
frameborder: '0',
allowfullscreen: 'true',
allow: 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture',