@@ -74,7 +74,12 @@ function toProxiedImageUrl(url: string): string {
7474 }
7575}
7676
77- function safeIframeUrl ( url : string ) : string | null {
77+ type SafeIframe = {
78+ href : string ;
79+ isYouTubeEmbed : boolean ;
80+ } ;
81+
82+ function safeIframe ( url : string ) : SafeIframe | null {
7883 const value = ( url || '' ) . trim ( ) ;
7984 if ( ! value ) return null ;
8085 try {
@@ -92,7 +97,7 @@ function safeIframeUrl(url: string): string | null {
9297 ) && path . startsWith ( '/embed/' ) ;
9398
9499 if ( isLocal || isLichessStudyEmbed || isYouTubeEmbed ) {
95- return parsed . href ;
100+ return { href : parsed . href , isYouTubeEmbed } ;
96101 }
97102 return null ;
98103 } catch {
@@ -178,17 +183,20 @@ function sanitizeRenderedFragment(html: string): DocumentFragment {
178183 el . setAttribute ( 'loading' , 'lazy' ) ;
179184 } ) ;
180185
181- fragment . querySelectorAll < HTMLIFrameElement > ( 'iframe[src] ' ) . forEach ( ( el ) => {
182- const safe = safeIframeUrl ( el . getAttribute ( 'src' ) || '' ) ;
186+ fragment . querySelectorAll < HTMLIFrameElement > ( 'iframe' ) . forEach ( ( el ) => {
187+ const safe = safeIframe ( el . getAttribute ( 'src' ) || '' ) ;
183188 if ( ! safe ) {
184189 el . remove ( ) ;
185190 return ;
186191 }
187- el . setAttribute ( 'src' , safe ) ;
192+ el . setAttribute ( 'src' , safe . href ) ;
188193 sanitizePositiveIntAttr ( el , 'width' ) ;
189194 sanitizePositiveIntAttr ( el , 'height' ) ;
190195 el . setAttribute ( 'loading' , 'lazy' ) ;
191- el . setAttribute ( 'referrerpolicy' , 'no-referrer' ) ;
196+ el . setAttribute (
197+ 'referrerpolicy' ,
198+ safe . isYouTubeEmbed ? 'strict-origin-when-cross-origin' : 'no-referrer' ,
199+ ) ;
192200 if ( ! el . hasAttribute ( 'allowfullscreen' ) ) {
193201 el . setAttribute ( 'allowfullscreen' , '' ) ;
194202 }
0 commit comments