Skip to content

Commit 64df33e

Browse files
committed
fix(media-embed): dispatch YouTube id by path shape; drop inline comments
- Resolve id from the /embed/ path segment before the ?v= query param so a valid embed URL with a spurious v param still embeds (was returning null) - Remove non-TSDoc inline comments from the module and its test
1 parent f3e4d4b commit 64df33e

2 files changed

Lines changed: 6 additions & 12 deletions

File tree

packages/utils/src/media-embed.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ describe('getEmbedInfo', () => {
77
expect(getEmbedInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ')).toEqual(expected)
88
expect(getEmbedInfo('https://youtu.be/dQw4w9WgXcQ')).toEqual(expected)
99
expect(getEmbedInfo('https://www.youtube.com/embed/dQw4w9WgXcQ')).toEqual(expected)
10-
// Extra query params around v=, a youtu.be tracking param, a trailing slash,
11-
// and an embed query string all still resolve to the same embed.
1210
expect(getEmbedInfo('https://www.youtube.com/watch?list=RD&v=dQw4w9WgXcQ&t=5')).toEqual(
1311
expected
1412
)
1513
expect(getEmbedInfo('https://youtu.be/dQw4w9WgXcQ?si=abc')).toEqual(expected)
1614
expect(getEmbedInfo('https://youtu.be/dQw4w9WgXcQ/')).toEqual(expected)
1715
expect(getEmbedInfo('https://www.youtube.com/embed/dQw4w9WgXcQ?rel=0')).toEqual(expected)
18-
// A non-11-char id is not a valid YouTube video and does not embed.
16+
expect(getEmbedInfo('https://www.youtube.com/embed/dQw4w9WgXcQ?v=notAnId')).toEqual(expected)
1917
expect(getEmbedInfo('https://www.youtube.com/watch?v=short')).toBeNull()
2018
})
2119

@@ -63,13 +61,10 @@ describe('getEmbedInfo', () => {
6361
})
6462

6563
it('only embeds when the parsed host belongs to the provider', () => {
66-
// A provider domain in the path or as a subdomain prefix of an attacker host
67-
// must not be treated as that provider.
6864
expect(getEmbedInfo('https://evil.com/youtube.com/watch?v=dQw4w9WgXcQ')).toBeNull()
6965
expect(getEmbedInfo('https://youtube.com.evil.com/watch?v=dQw4w9WgXcQ')).toBeNull()
7066
expect(getEmbedInfo('https://evil.com/open.spotify.com/track/abc123')).toBeNull()
7167
expect(getEmbedInfo('https://vimeo.com.evil.com/123456')).toBeNull()
72-
// Legitimate subdomains of a provider still embed.
7368
expect(getEmbedInfo('https://m.youtube.com/watch?v=dQw4w9WgXcQ')).toEqual({
7469
url: 'https://www.youtube.com/embed/dQw4w9WgXcQ',
7570
type: 'iframe',
@@ -96,8 +91,6 @@ describe('getEmbedInfo', () => {
9691
})
9792

9893
it('does not apply the Dropbox direct-link rewrite to look-alike hosts', () => {
99-
// Look-alike hosts fall through to the generic video handler with their
100-
// original (untrusted) host intact — never rewritten as if trusted Dropbox.
10194
expect(getEmbedInfo('https://dropbox.com.evil.com/clip.mp4')?.url).not.toContain(
10295
'dropboxusercontent.com'
10396
)

packages/utils/src/media-embed.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ export function getEmbedInfo(url: string): EmbedInfo | null {
6565
const parsed = parseUrl(url)
6666
const host = parsed?.hostname.toLowerCase() ?? null
6767
if (parsed && hostMatches(host, 'youtube.com', 'youtu.be')) {
68-
const id = hostMatches(host, 'youtu.be')
69-
? parsed.pathname.split('/')[1]
70-
: (parsed.searchParams.get('v') ?? parsed.pathname.match(/^\/embed\/([^/?]+)/)?.[1])
68+
const segments = parsed.pathname.split('/')
69+
let id: string | null | undefined
70+
if (hostMatches(host, 'youtu.be')) id = segments[1]
71+
else if (segments[1] === 'embed') id = segments[2]
72+
else id = parsed.searchParams.get('v')
7173
if (id && /^[a-zA-Z0-9_-]{11}$/.test(id)) {
7274
return { url: `https://www.youtube.com/embed/${id}`, type: 'iframe' }
7375
}
@@ -322,7 +324,6 @@ export function getEmbedInfo(url: string): EmbedInfo | null {
322324
}
323325

324326
if (parsed && hostMatches(host, 'giphy.com')) {
325-
// Giphy ids are the trailing hyphen-delimited token of a /gifs/ or /embed/ path segment.
326327
const segment = parsed.pathname.match(/^\/(?:gifs|embed)\/([^/]+)/)?.[1]
327328
const giphyId = segment?.split('-').pop()
328329
if (giphyId && /^[a-zA-Z0-9]+$/.test(giphyId)) {

0 commit comments

Comments
 (0)