Skip to content

Commit fe0a96c

Browse files
committed
fix(youtube-player): validate ID before attaching them to placeholder
Adds some logic that'll validate the YouTube ID before interpolating it into the background image which can become an XSS attack vector. (cherry picked from commit e9089fd)
1 parent eb382ae commit fe0a96c

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/youtube-player/youtube-player-placeholder.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {Component, ViewEncapsulation, computed, input} from '@angular/core';
1111
/** Quality of the placeholder image. */
1212
export type PlaceholderImageQuality = 'high' | 'standard' | 'low';
1313

14+
const VIDEO_ID_REGEX = /^[a-zA-Z0-9_-]+$/;
15+
1416
@Component({
1517
selector: 'youtube-player-placeholder',
1618
encapsulation: ViewEncapsulation.None,
@@ -59,6 +61,18 @@ export class YouTubePlayerPlaceholder {
5961
protected _backgroundImage = computed(() => {
6062
const quality = this.quality();
6163
const videoId = this.videoId();
64+
65+
// Since we're interpolating the ID into a CSS value, we need
66+
// to ensure that it doesn't become an XSS attack vector.
67+
if (!VIDEO_ID_REGEX.test(videoId)) {
68+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
69+
console.error(
70+
`Skipping placeholder image generation for invalid YouTube video ID: ${videoId}`,
71+
);
72+
}
73+
return null;
74+
}
75+
6276
let url: string;
6377

6478
if (quality === 'low') {

0 commit comments

Comments
 (0)