Skip to content

Commit f07ccab

Browse files
Potential fix for code scanning alert no. 1: Incomplete URL substring sanitization
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent c5a73f2 commit f07ccab

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/components/youtube-transformer.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
const YouTubeTransformer = {
22
name: "YouTube",
33
shouldTransform(url) {
4-
return url.includes("youtube.com") || url.includes("youtu.be");
4+
try {
5+
const parsed = new URL(url);
6+
const hostname = parsed.hostname.toLowerCase();
7+
const allowedHosts = [
8+
"youtube.com",
9+
"www.youtube.com",
10+
"m.youtube.com",
11+
"youtu.be",
12+
"www.youtu.be"
13+
];
14+
return allowedHosts.includes(hostname);
15+
} catch (e) {
16+
// If the URL cannot be parsed, it cannot be a valid YouTube URL.
17+
return false;
18+
}
519
},
620
getHTML(url) {
7-
const videoId = url.includes("youtu.be")
8-
? url.split("/").pop()
9-
: new URL(url).searchParams.get("v");
21+
const urlObj = new URL(url);
22+
const isShort = urlObj.hostname.toLowerCase() === "youtu.be" || urlObj.hostname.toLowerCase() === "www.youtu.be";
23+
const videoId = isShort
24+
? urlObj.pathname.split("/").filter(Boolean).pop()
25+
: urlObj.searchParams.get("v");
1026

1127
return `
1228
<iframe

0 commit comments

Comments
 (0)