From 085888056aa84ba5b0d3d42e50c42ee77b5b9003 Mon Sep 17 00:00:00 2001 From: Arukuen Date: Tue, 3 Nov 2026 22:33:03 +0800 Subject: [PATCH 1/2] fix: sanitize unsafe video url for lightbox --- e2e/tests/frontend-interactive.spec.ts | 77 ++++++++++++++++++++++++- src/lightbox/__test__/video-url.test.js | 37 ++++++++++++ src/lightbox/frontend-image-lightbox.js | 24 +++----- src/lightbox/video-url.js | 49 ++++++++++++++++ 4 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 src/lightbox/__test__/video-url.test.js create mode 100644 src/lightbox/video-url.js diff --git a/e2e/tests/frontend-interactive.spec.ts b/e2e/tests/frontend-interactive.spec.ts index 1bd5c38550..e42acdd968 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,76 @@ test.describe( 'Interactive frontend', () => { await expect( page.locator( '.bp-container, .glightbox-container, .stk-video-popup, iframe' ).first() ).toBeVisible( { timeout: 10_000 } ) } ) + test( 'Lightbox rejects 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: 'Lightbox trigger', + }, + } ], + } ], + } ) + + 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( '..' ) ).not.toHaveAttribute( 'role', 'button' ) + await expect( page.locator( '[data-stackable-lightbox-injection]' ) ).toHaveCount( 0 ) + } ) + + test( 'Lightbox preserves image URL query parameters', async ( { + page, editor, requestUtils, admin, + } ) => { + const imageUrl = 'https://example.invalid/lightbox.png?one=1&two=2' + await editor.insertBlock( { + name: 'stackable/columns', + attributes: { uniqueId: 'lightbox-image-columns' }, + innerBlocks: [ { + name: 'stackable/column', + attributes: { + uniqueId: 'lightbox-image-column', + blockLinkHasLightbox: true, + }, + innerBlocks: [ { + name: 'core/html', + attributes: { + content: `Linked lightbox image`, + }, + } ], + } ], + } ) + + await publishAndVisitFrontend( page, editor, requestUtils, admin ) + await page.route( imageUrl, route => route.fulfill( { + contentType: 'image/gif', + body: Buffer.from( 'R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==', 'base64' ), + } ) ) + + await page.getByRole( 'img', { name: 'Linked lightbox image' } ).click() + await expect( page.locator( '.gslide-image img' ).first() ).toHaveAttribute( 'src', imageUrl ) + } ) + test( 'Notification can be dismissed', async ( { page, editor, requestUtils, admin, } ) => { @@ -315,6 +386,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 0000000000..79b5cba3c9 --- /dev/null +++ b/src/lightbox/__test__/video-url.test.js @@ -0,0 +1,37 @@ +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://youtu.be/dQw4w9WgXcQ', + 'https://youtu.be/dQw4w9WgXcQ/', + 'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ', + 'https://vimeo.com/123456789?autoplay=1', + 'https://cdn.example.com/video.mp4?token=a%26b', + '/uploads/video.webm?version=2', + 'https://cdn.example.com/video.ogg', + 'https://cdn.example.com/video.mov', + ] )( 'accepts %s', value => { + expect( detectIfVideo( value ) ).toBe( true ) + expect( isValidVideoUrl( value ) ).toBe( true ) + } ) + + it.each( [ + 'https://youtube.com/watch?v=x">', + 'https://VIMEO.COM/123">', + 'https://cdn.example.com/video.mp4">', + 'https://cdn.example.com/video.mp4\t?autoplay=1', + 'https://notyoutube.com/watch?v=abc', + 'https://youtube.com.evil.test/watch?v=abc', + 'https://youtube.com/watch?v=abc/def', + 'https://youtube.com/watch?v=abc&v=def', + 'https://vimeo.com/123/extra', + 'https://cdn.example.com/video.mp4/extra', + 'javascript:youtube.com/watch?v=abc', + 'https://user:pass@cdn.example.com/video.mp4', + ] )( 'rejects %s', value => { + expect( detectIfVideo( value ) ).toBe( true ) + expect( isValidVideoUrl( value ) ).toBe( false ) + } ) +} ) diff --git a/src/lightbox/frontend-image-lightbox.js b/src/lightbox/frontend-image-lightbox.js index 7a34f255a3..7e47883d58 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 0000000000..fa733e28eb --- /dev/null +++ b/src/lightbox/video-url.js @@ -0,0 +1,49 @@ +// Keep video detection broad enough to catch URLs GLightbox may classify as video. +const VIDEO_EXTENSION = /\.(mp4|ogg|webm|mov)$/i +const VIDEO_ID = /^[a-zA-Z0-9_-]+$/ +const UNSAFE_URL_CHARACTER = /[<>"'\s\x00-\x1f\x7f]/ + +export const detectIfVideo = url => { + return /(youtube(?:-nocookie)?\.com|youtu\.be|vimeo\.com|\.(mp4|ogg|webm|mov))/i.test( url ) +} + +export const isValidVideoUrl = value => { + // These characters can break out of GLightbox's HTML-string video builder. + if ( UNSAFE_URL_CHARACTER.test( value ) ) { + return false + } + + let url + try { + url = new URL( value, window.location.href ) + } catch ( error ) { + return false + } + + if ( ! [ 'http:', 'https:' ].includes( url.protocol ) || url.username || url.password ) { + return false + } + + // Validate provider URLs by exact host and expected path/ID shape. + const hostname = url.hostname.toLowerCase() + if ( [ 'youtube.com', 'www.youtube.com', 'youtube-nocookie.com', 'www.youtube-nocookie.com' ].includes( hostname ) ) { + if ( url.port ) { + return false + } + if ( url.pathname === '/watch' ) { + const ids = url.searchParams.getAll( 'v' ) + return ids.length === 1 && VIDEO_ID.test( ids[ 0 ] ) + } + const embed = url.pathname.match( /^\/embed\/([a-zA-Z0-9_-]+)\/?$/ ) + return Boolean( embed ) + } + if ( hostname === 'youtu.be' ) { + return ! url.port && /^\/[a-zA-Z0-9_-]+\/?$/.test( url.pathname ) + } + if ( hostname === 'vimeo.com' || hostname === 'www.vimeo.com' ) { + return ! url.port && /^\/[0-9]+\/?$/.test( url.pathname ) + } + + // Other supported video hosts are allowed for direct video files only. + return VIDEO_EXTENSION.test( url.pathname ) +} From 09cf5bfc6803958c023cee0382b4085841a14ade Mon Sep 17 00:00:00 2001 From: Arukuen Date: Fri, 6 Nov 2026 12:50:40 +0800 Subject: [PATCH 2/2] fix: safely handle valid lightbox video URLs --- e2e/tests/frontend-interactive.spec.ts | 38 ++------------- src/lightbox/__test__/video-url.test.js | 41 ++++++++++++----- src/lightbox/video-url.js | 61 ++++++++++--------------- 3 files changed, 58 insertions(+), 82 deletions(-) diff --git a/e2e/tests/frontend-interactive.spec.ts b/e2e/tests/frontend-interactive.spec.ts index e42acdd968..f08f6c5d39 100644 --- a/e2e/tests/frontend-interactive.spec.ts +++ b/e2e/tests/frontend-interactive.spec.ts @@ -190,7 +190,7 @@ test.describe( 'Interactive frontend', () => { await expect( page.locator( '.bp-container, .glightbox-container, .stk-video-popup, iframe' ).first() ).toBeVisible( { timeout: 10_000 } ) } ) - test( 'Lightbox rejects markup in an entity-decoded video URL', async ( { + test( 'Lightbox escapes markup in an entity-decoded video URL', async ( { page, editor, requestUtils, admin, } ) => { await editor.insertBlock( { @@ -224,42 +224,12 @@ test.describe( 'Interactive frontend', () => { const trigger = page.getByRole( 'img', { name: 'Lightbox trigger' } ) await expect( trigger ).toBeVisible() - await expect( trigger.locator( '..' ) ).not.toHaveAttribute( 'role', 'button' ) + 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( 'Lightbox preserves image URL query parameters', async ( { - page, editor, requestUtils, admin, - } ) => { - const imageUrl = 'https://example.invalid/lightbox.png?one=1&two=2' - await editor.insertBlock( { - name: 'stackable/columns', - attributes: { uniqueId: 'lightbox-image-columns' }, - innerBlocks: [ { - name: 'stackable/column', - attributes: { - uniqueId: 'lightbox-image-column', - blockLinkHasLightbox: true, - }, - innerBlocks: [ { - name: 'core/html', - attributes: { - content: `Linked lightbox image`, - }, - } ], - } ], - } ) - - await publishAndVisitFrontend( page, editor, requestUtils, admin ) - await page.route( imageUrl, route => route.fulfill( { - contentType: 'image/gif', - body: Buffer.from( 'R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==', 'base64' ), - } ) ) - - await page.getByRole( 'img', { name: 'Linked lightbox image' } ).click() - await expect( page.locator( '.gslide-image img' ).first() ).toHaveAttribute( 'src', imageUrl ) - } ) - test( 'Notification can be dismissed', async ( { page, editor, requestUtils, admin, } ) => { diff --git a/src/lightbox/__test__/video-url.test.js b/src/lightbox/__test__/video-url.test.js index 79b5cba3c9..e805252791 100644 --- a/src/lightbox/__test__/video-url.test.js +++ b/src/lightbox/__test__/video-url.test.js @@ -4,34 +4,53 @@ 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( [ - 'https://youtube.com/watch?v=x">', + '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', - 'https://notyoutube.com/watch?v=abc', - 'https://youtube.com.evil.test/watch?v=abc', - 'https://youtube.com/watch?v=abc/def', - 'https://youtube.com/watch?v=abc&v=def', - 'https://vimeo.com/123/extra', - 'https://cdn.example.com/video.mp4/extra', - 'javascript:youtube.com/watch?v=abc', - 'https://user:pass@cdn.example.com/video.mp4', - ] )( 'rejects %s', value => { + ] )( 'safely accepts unusual HTTP URL forms for escaping at the HTML sink: %s', value => { expect( detectIfVideo( value ) ).toBe( true ) - expect( isValidVideoUrl( value ) ).toBe( false ) + 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/video-url.js b/src/lightbox/video-url.js index fa733e28eb..4a1ef855f4 100644 --- a/src/lightbox/video-url.js +++ b/src/lightbox/video-url.js @@ -1,49 +1,36 @@ // Keep video detection broad enough to catch URLs GLightbox may classify as video. -const VIDEO_EXTENSION = /\.(mp4|ogg|webm|mov)$/i -const VIDEO_ID = /^[a-zA-Z0-9_-]+$/ -const UNSAFE_URL_CHARACTER = /[<>"'\s\x00-\x1f\x7f]/ +const VIDEO_EXTENSION = /\.(mp4|ogg|webm|mov)(?=$|[^a-z0-9])/i -export const detectIfVideo = url => { - return /(youtube(?:-nocookie)?\.com|youtu\.be|vimeo\.com|\.(mp4|ogg|webm|mov))/i.test( url ) -} - -export const isValidVideoUrl = value => { - // These characters can break out of GLightbox's HTML-string video builder. - if ( UNSAFE_URL_CHARACTER.test( value ) ) { - return false - } - - let url +const parseUrl = value => { try { - url = new URL( value, window.location.href ) + return new URL( value, window.location.href ) } catch ( error ) { - return false + return null } +} - if ( ! [ 'http:', 'https:' ].includes( url.protocol ) || url.username || url.password ) { +export const detectIfVideo = url => { + const parsedUrl = parseUrl( url ) + if ( ! parsedUrl ) { return false } - // Validate provider URLs by exact host and expected path/ID shape. - const hostname = url.hostname.toLowerCase() - if ( [ 'youtube.com', 'www.youtube.com', 'youtube-nocookie.com', 'www.youtube-nocookie.com' ].includes( hostname ) ) { - if ( url.port ) { - return false - } - if ( url.pathname === '/watch' ) { - const ids = url.searchParams.getAll( 'v' ) - return ids.length === 1 && VIDEO_ID.test( ids[ 0 ] ) - } - const embed = url.pathname.match( /^\/embed\/([a-zA-Z0-9_-]+)\/?$/ ) - return Boolean( embed ) - } - if ( hostname === 'youtu.be' ) { - return ! url.port && /^\/[a-zA-Z0-9_-]+\/?$/.test( url.pathname ) - } - if ( hostname === 'vimeo.com' || hostname === 'www.vimeo.com' ) { - return ! url.port && /^\/[0-9]+\/?$/.test( url.pathname ) + 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 } - // Other supported video hosts are allowed for direct video files only. - return VIDEO_EXTENSION.test( url.pathname ) + // 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 }