Skip to content

Commit

Permalink
fix (lightbox): sanitize lightbox title (#3390)
Browse files Browse the repository at this point in the history
Co-authored-by: bfintal@gmail.com <>
  • Loading branch information
bfintal authored Jan 15, 2025
1 parent ae145d8 commit e0b1d5c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/lightbox/frontend-image-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ const isImageBlock = el => {
return el && el.classList.contains( 'stk-block-image' )
}

function sanitize( string ) {
const map = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
'/': '&#x2F;',
}
const reg = /[&<>"'/]/ig
return string.replace( reg, match => map[ match ] )
}

class StackableImageLightbox {
init = () => {
this.elements = []
Expand Down Expand Up @@ -135,6 +148,10 @@ class StackableImageLightbox {
title = imageBlock.getAttribute( 'alt' ) || null
}

// Sanitize strings.
title = title ? sanitize( title ) : null
link = typeof link === 'string' ? sanitize( link ) : link

const isUsingImageBlock = ( ! link || ! href ) && imageBlock

this.elements.push( {
Expand Down

0 comments on commit e0b1d5c

Please sign in to comment.