Repository navigation
fix: sanitize unsafe video url for lightbox #3773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { detectIfVideo, isValidVideoUrl } from '../video-url' | ||
|
|
||
| describe( 'lightbox video URLs', () => { | ||
| it.each( [ | ||
| 'https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=10', | ||
| 'https://YOUTUBE.COM/embed/dQw4w9WgXcQ', | ||
| 'https://www.youtube.com/shorts/dQw4w9WgXcQ', | ||
| 'https://m.youtube.com/watch?v=dQw4w9WgXcQ', | ||
| 'https://music.youtube.com/watch?v=dQw4w9WgXcQ', | ||
| 'https://youtu.be/dQw4w9WgXcQ', | ||
| 'https://youtu.be/dQw4w9WgXcQ/', | ||
| 'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ', | ||
| 'https://vimeo.com/123456789?autoplay=1', | ||
| 'https://vimeo.com/123456789/abcdef1234', | ||
| 'https://cdn.example.com/video.mp4?token=a%26b', | ||
| 'https://cdn.example.com/clip.MP4', | ||
| '/uploads/video.webm?version=2', | ||
| 'https://cdn.example.com/video.ogg', | ||
| 'https://cdn.example.com/video.mov', | ||
| 'https://youtube.com/watch?v=x"><img src=x>', | ||
| ] )( 'accepts %s', value => { | ||
| expect( detectIfVideo( value ) ).toBe( true ) | ||
| expect( isValidVideoUrl( value ) ).toBe( true ) | ||
| } ) | ||
|
|
||
| it.each( [ | ||
| 'javascript:youtube.com/watch?v=abc', | ||
| 'data:video/mp4;base64,AAAA', | ||
| 'https://user:pass@cdn.example.com/video.mp4', | ||
| ] )( 'rejects unsafe URL schemes or credentials in %s', value => { | ||
| expect( isValidVideoUrl( value ) ).toBe( false ) | ||
| } ) | ||
|
|
||
| it.each( [ | ||
| 'https://notyoutube.com/watch?v=abc', | ||
| 'https://youtube.com.evil.test/watch?v=abc', | ||
| 'https://example.com/photo.jpg?next=youtube.com', | ||
| 'https://example.com/page?ref=vimeo.com', | ||
| 'https://example.com/page?file=video.mp4', | ||
| ] )( 'does not detect video URLs from unrelated hostnames or query strings: %s', value => { | ||
| expect( detectIfVideo( value ) ).toBe( false ) | ||
| } ) | ||
|
|
||
| it.each( [ | ||
| 'https://VIMEO.COM/123"><img src=x>', | ||
| 'https://cdn.example.com/video.mp4"><img src=x>', | ||
| 'https://cdn.example.com/video.mp4\t?autoplay=1', | ||
| ] )( 'safely accepts unusual HTTP URL forms for escaping at the HTML sink: %s', value => { | ||
| expect( detectIfVideo( value ) ).toBe( true ) | ||
| expect( isValidVideoUrl( value ) ).toBe( true ) | ||
| } ) | ||
|
|
||
| it( 'does not classify a malformed URL as a video', () => { | ||
| expect( detectIfVideo( 'http://[invalid/video.mp4' ) ).toBe( false ) | ||
| } ) | ||
| } ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import domReady from '@wordpress/dom-ready' | ||
| import GLightbox from 'glightbox' | ||
| import { detectIfVideo, isValidVideoUrl } from './video-url' | ||
|
|
||
| // Gets the highest resolution image from the srcset. | ||
| const getImageFromSrcset = el => { | ||
|
|
@@ -37,18 +38,6 @@ const getImageFromSrcset = el => { | |
| return src.url | ||
| } | ||
|
|
||
| // Detects the type of the URL if it's a video URL. | ||
| const detectIfVideo = url => { | ||
| if ( url.match( /(youtube\.com|youtu\.be)/ ) ) { | ||
| return true | ||
| } else if ( url.match( /vimeo\.com/ ) ) { | ||
| return true | ||
| } else if ( url.match( /\.(mp4|webm|mov)/i ) ) { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // Detects the type of the URL if it's a image URL. | ||
| const detectIfImage = url => { | ||
| if ( ! url ) { | ||
|
|
@@ -100,9 +89,8 @@ function sanitize( string ) { | |
| '>': '>', | ||
| '"': '"', | ||
| "'": ''', | ||
| '/': '/', | ||
| } | ||
| const reg = /[&<>"'/]/ig | ||
| const reg = /[&<>"']/g | ||
| return string.replace( reg, match => map[ match ] ) | ||
| } | ||
|
|
||
|
|
@@ -138,6 +126,10 @@ class StackableImageLightbox { | |
| if ( ! linkToOpen ) { | ||
| return | ||
| } | ||
| // Reject malformed video candidates before wiring the click handler. | ||
| if ( detectIfVideo( linkToOpen ) && ! isValidVideoUrl( linkToOpen ) ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Minor] When the check fails, we skip the lightbox and the click opens the URL directly. |
||
| return | ||
| } | ||
|
Comment on lines
+129
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if ( ! link ) { | ||
| link = el | ||
|
|
@@ -157,7 +149,9 @@ class StackableImageLightbox { | |
| this.elements.push( { | ||
| block: link.classList.contains( 'stk-block' ) ? link : link.closest( '.stk-block' ), | ||
| element: link, | ||
| href: linkToOpen, | ||
| // GLightbox builds video markup with innerHTML. Escape its URL for | ||
| // that second HTML parse, while leaving image and iframe URLs intact. | ||
| href: detectIfVideo( linkToOpen ) ? sanitize( linkToOpen ) : linkToOpen, | ||
| // If we're using an image block as the source, the link to open | ||
| // isn't sometimes detected as an image. | ||
| type: isUsingImageBlock ? 'image' : undefined, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Keep video detection broad enough to catch URLs GLightbox may classify as video. | ||
| const VIDEO_EXTENSION = /\.(mp4|ogg|webm|mov)(?=$|[^a-z0-9])/i | ||
|
|
||
| const parseUrl = value => { | ||
| try { | ||
| return new URL( value, window.location.href ) | ||
| } catch ( error ) { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| export const detectIfVideo = url => { | ||
| const parsedUrl = parseUrl( url ) | ||
| if ( ! parsedUrl ) { | ||
| return false | ||
| } | ||
|
|
||
| const hostname = parsedUrl.hostname.toLowerCase() | ||
| const isVideoProvider = hostname === 'youtu.be' || | ||
| hostname === 'youtube.com' || hostname.endsWith( '.youtube.com' ) || | ||
| hostname === 'youtube-nocookie.com' || hostname.endsWith( '.youtube-nocookie.com' ) || | ||
| hostname === 'vimeo.com' || hostname.endsWith( '.vimeo.com' ) | ||
|
|
||
| return isVideoProvider || VIDEO_EXTENSION.test( parsedUrl.pathname ) | ||
| } | ||
|
|
||
| export const isValidVideoUrl = value => { | ||
| const url = parseUrl( value ) | ||
| if ( ! url ) { | ||
| return false | ||
| } | ||
|
|
||
| // Keep URL checks permissive so GLightbox-supported video formats keep working. | ||
| // The caller HTML-escapes video URLs before GLightbox inserts them with innerHTML. | ||
| return [ 'http:', 'https:' ].includes( url.protocol ) && ! url.username && ! url.password | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Minor] Please add these to the "accepts" list so they keep working: