mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-08-18 06:17:12 +02:00
cors: fix possible XSS vuln when embeding youtube-nocookie.com videos
This commit is contained in:
+11
-3
@@ -279,8 +279,8 @@ function serveYouTubeEmbed(url: string) {
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<iframe src="${transformYouTubeUrl(
|
||||
url,
|
||||
<iframe src="${escapeHtmlAttr(
|
||||
transformYouTubeUrl(url),
|
||||
)}" allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture;web-share" allowfullscreen referrerpolicy="strict-origin-when-cross-origin" title="Video player"></iframe>
|
||||
</body>
|
||||
</html>`;
|
||||
@@ -299,6 +299,14 @@ function isYouTubeEmbed(urlString: string) {
|
||||
);
|
||||
}
|
||||
|
||||
function escapeHtmlAttr(str: string): string {
|
||||
return str
|
||||
.replace(/&/g, "&")
|
||||
.replace(/"/g, """)
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">");
|
||||
}
|
||||
|
||||
// Transform YouTube URLs to use youtube-nocookie.com for enhanced privacy
|
||||
function transformYouTubeUrl(urlString: string): string {
|
||||
try {
|
||||
@@ -315,7 +323,7 @@ function transformYouTubeUrl(urlString: string): string {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
return urlString;
|
||||
return url.toString();
|
||||
} catch {
|
||||
return urlString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user