diff --git a/e2e/tests/frontend-interactive.spec.ts b/e2e/tests/frontend-interactive.spec.ts
index 1bd5c3855..f08f6c5d3 100644
--- a/e2e/tests/frontend-interactive.spec.ts
+++ b/e2e/tests/frontend-interactive.spec.ts
@@ -1,5 +1,6 @@
-import { test, expect } from 'e2e/test-utils'
-import { publishAndVisitFrontend, getEditorPostId } from 'e2e/test-utils'
+import {
+ test, expect, publishAndVisitFrontend, getEditorPostId,
+} from 'e2e/test-utils'
const pickDefaultLayoutIfPresent = async editor => {
const picker = editor.canvas.locator( '.stk-variation-picker' )
@@ -189,6 +190,46 @@ test.describe( 'Interactive frontend', () => {
await expect( page.locator( '.bp-container, .glightbox-container, .stk-video-popup, iframe' ).first() ).toBeVisible( { timeout: 10_000 } )
} )
+ test( 'Lightbox escapes markup in an entity-decoded video URL', async ( {
+ page, editor, requestUtils, admin,
+ } ) => {
+ await editor.insertBlock( {
+ name: 'stackable/columns',
+ attributes: { uniqueId: 'lightbox-columns' },
+ innerBlocks: [ {
+ name: 'stackable/column',
+ attributes: {
+ uniqueId: 'lightbox-column',
+ blockLinkHasLightbox: true,
+ },
+ innerBlocks: [ {
+ name: 'core/html',
+ attributes: {
+ content: '
',
+ },
+ } ],
+ } ],
+ } )
+
+ await publishAndVisitFrontend( page, editor, requestUtils, admin )
+
+ await page.route( 'https://cdn.plyr.io/**/*.css', route => route.fulfill( {
+ contentType: 'text/css',
+ body: '',
+ } ) )
+ await page.route( 'https://cdn.plyr.io/**/*.js', route => route.fulfill( {
+ contentType: 'text/javascript',
+ body: 'window.Plyr = class { on() {} };',
+ } ) )
+
+ const trigger = page.getByRole( 'img', { name: 'Lightbox trigger' } )
+ await expect( trigger ).toBeVisible()
+ await expect( trigger.locator( '..' ) ).toHaveAttribute( 'role', 'button' )
+ await trigger.click()
+ await expect( page.locator( '.glightbox-container' ) ).toBeVisible()
+ await expect( page.locator( '[data-stackable-lightbox-injection]' ) ).toHaveCount( 0 )
+ } )
+
test( 'Notification can be dismissed', async ( {
page, editor, requestUtils, admin,
} ) => {
@@ -315,6 +356,8 @@ test.describe( 'Interactive frontend', () => {
const text = block.attributes.text || block.attributes.content || ''
const match = anchors.find( heading => heading.text === text )
if ( match && ( block.name === 'core/heading' || block.name === 'stackable/heading' ) ) {
+ // This test setup updates the editor store from outside the block's Edit component.
+ // eslint-disable-next-line stackable/no-update-block-attributes
updateBlockAttributes( block.clientId, { anchor: match.anchor } )
}
if ( block.innerBlocks?.length ) {
diff --git a/src/lightbox/__test__/video-url.test.js b/src/lightbox/__test__/video-url.test.js
new file mode 100644
index 000000000..e80525279
--- /dev/null
+++ b/src/lightbox/__test__/video-url.test.js
@@ -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">
',
+ ] )( '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">
',
+ 'https://cdn.example.com/video.mp4">
',
+ '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 )
+ } )
+} )
diff --git a/src/lightbox/frontend-image-lightbox.js b/src/lightbox/frontend-image-lightbox.js
index 7a34f255a..7e47883d5 100644
--- a/src/lightbox/frontend-image-lightbox.js
+++ b/src/lightbox/frontend-image-lightbox.js
@@ -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 ) ) {
+ return
+ }
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,
diff --git a/src/lightbox/video-url.js b/src/lightbox/video-url.js
new file mode 100644
index 000000000..4a1ef855f
--- /dev/null
+++ b/src/lightbox/video-url.js
@@ -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
+}