From 1d035f337b0cd5f375d7aa9e09e88f95a0f1ba53 Mon Sep 17 00:00:00 2001 From: KMchaudhary Date: Thu, 19 Nov 2026 14:06:01 +0530 Subject: [PATCH] Decode the LearnDash Video URL without reinterpreting it as HTML CodeQL flagged `textarea.innerHTML = value` in the Video URL decode helper as DOM text reinterpreted as HTML. Replace the textarea round-trip with a targeted decode of just the entities WordPress escaping produces, so the field value is never parsed as HTML. `&` is decoded last to avoid double-decoding. Co-Authored-By: Claude Opus 4.8 --- assets/src/js/learndash/admin.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/assets/src/js/learndash/admin.js b/assets/src/js/learndash/admin.js index c3c9570fb..ac4c06df0 100644 --- a/assets/src/js/learndash/admin.js +++ b/assets/src/js/learndash/admin.js @@ -13,17 +13,23 @@ /** * Decode the HTML entities LearnDash may leave in the saved Video URL. * + * Only the entities WordPress escaping produces are decoded, by plain string + * replacement, so the value is never reinterpreted as HTML. `&` is decoded + * last so an already-escaped ampersand (`&lt;`) is not decoded twice. + * * @since n.e.x.t * - * @param {string} value Field value. - * @param {Document} doc Document. + * @param {string} value Field value. * @return {string} Decoded value. */ -const decode = ( value, doc ) => { - const textarea = doc.createElement( 'textarea' ); - textarea.innerHTML = value; - return textarea.value; -}; +const decode = ( value ) => + String( value ) + .replace( /</g, '<' ) + .replace( />/g, '>' ) + .replace( /"/g, '"' ) + .replace( /�*39;/g, "'" ) + .replace( /�*27;/gi, "'" ) + .replace( /&/g, '&' ); /** * Whether a Video URL value is the GoDAM marker, with or without a picked video. @@ -71,7 +77,7 @@ export function setupUseGoDAMSwitch( doc, config ) { pickerRow.style.display = toggle.checked ? '' : 'none'; } - if ( ! toggle.checked && urlInput && isMarker( decode( urlInput.value, doc ), config.markerTag ) ) { + if ( ! toggle.checked && urlInput && isMarker( decode( urlInput.value ), config.markerTag ) ) { urlInput.value = ''; } };