From 1d519229e56f110b419e1ca8811a12ad079e69ec Mon Sep 17 00:00:00 2001 From: Javier Aramberri Date: Tue, 13 Oct 2026 16:58:46 -0300 Subject: [PATCH 1/5] fix(custom-media-element): escape attribute values in the template HTML The template builder wrote attribute values into the HTML string with no escape. A value that has quotes or angle brackets changed the shadow DOM markup. The builder now escapes the special HTML characters. The native media element gets the same attribute values as before, and the HTML from getTemplateHTML stays valid for server rendering. --- .../custom-media-element.ts | 12 +++- .../test/eager-upgrade.html | 5 ++ .../test/lazy-upgrade.html | 5 ++ packages/custom-media-element/test/test.js | 68 +++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) diff --git a/packages/custom-media-element/custom-media-element.ts b/packages/custom-media-element/custom-media-element.ts index 86f0bed9..e49cfa68 100644 --- a/packages/custom-media-element/custom-media-element.ts +++ b/packages/custom-media-element/custom-media-element.ts @@ -513,11 +513,21 @@ function serializeAttributes(attrs: Record): string { const value = attrs[key]; if (value === '') html += ` ${key}`; - else html += ` ${key}="${value}"`; + else html += ` ${key}="${escapeHtml(`${value}`)}"`; } return html; } +function escapeHtml(str: string): string { + return str + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, ''') + .replace(/`/g, '`'); +} + /** * Helper function to convert NamedNodeMap to a plain object. */ diff --git a/packages/custom-media-element/test/eager-upgrade.html b/packages/custom-media-element/test/eager-upgrade.html index b6d7ff6d..f37782d0 100644 --- a/packages/custom-media-element/test/eager-upgrade.html +++ b/packages/custom-media-element/test/eager-upgrade.html @@ -25,5 +25,10 @@ crossorigin src="http://stream.mux.com/DS00Spx1CV902MCtPj5WknGlR102V5HFkDe/low.mp4" > + diff --git a/packages/custom-media-element/test/lazy-upgrade.html b/packages/custom-media-element/test/lazy-upgrade.html index 0c760dab..a0f4cbdc 100644 --- a/packages/custom-media-element/test/lazy-upgrade.html +++ b/packages/custom-media-element/test/lazy-upgrade.html @@ -19,5 +19,10 @@ crossorigin src="http://stream.mux.com/DS00Spx1CV902MCtPj5WknGlR102V5HFkDe/low.mp4" > + diff --git a/packages/custom-media-element/test/test.js b/packages/custom-media-element/test/test.js index 8f1045cd..8f28559c 100644 --- a/packages/custom-media-element/test/test.js +++ b/packages/custom-media-element/test/test.js @@ -76,6 +76,74 @@ test('has default video reflecting props', async function (t) { t.ok(customVideo.nativeEl.hasAttribute('controls'), 'nativeEl has controls attribute'); }); +test('does not parse attribute values as markup in the shadow DOM', async function (t) { + if (document.readyState === 'loading') { + await new Promise((resolve) => addEventListener('DOMContentLoaded', resolve)); + } + + const customVideo = globalThis.escapedAttrsVideo; + const { shadowRoot, nativeEl } = customVideo; + + await Promise.all( + [...shadowRoot.querySelectorAll('img')] + .filter((img) => !img.complete) + .map((img) => new Promise((resolve) => img.addEventListener('error', resolve))) + ); + + t.equal(shadowRoot.querySelectorAll('img').length, 0, 'no element is created from attribute values'); + t.equal(globalThis.injectedHandlerRan, undefined, 'no event handler from attribute values runs'); + t.equal(nativeEl.getAttribute('poster'), customVideo.getAttribute('poster'), 'nativeEl has the same poster'); + t.equal( + nativeEl.getAttribute('crossorigin'), + customVideo.getAttribute('crossorigin'), + 'nativeEl has the same crossorigin' + ); +}); + +test('passes attribute values to the native element unchanged', async function (t) { + const attrs = { + poster: `https://image.mux.com/a/thumbnail.webp?time=1&width=640&label="it's "`, + src: 'https://stream.mux.com/a/low.mp4?token=a.b.c&expires=1&=1', + crossorigin: 'anonymous', + preload: 'none', + }; + const escapeAttr = (value) => value.replace(/&/g, '&').replace(/"/g, '"'); + const serialized = Object.entries(attrs) + .map(([name, value]) => `${name}="${escapeAttr(value)}"`) + .join(' '); + + const customVideo = await fixture(``); + + for (const [name, value] of Object.entries(attrs)) { + t.equal(customVideo.nativeEl.getAttribute(name), value, `nativeEl has the same ${name}`); + } + t.equal(customVideo.nativeEl.getAttribute('controls'), '', 'nativeEl has the controls attribute'); + t.equal(customVideo.nativeEl.getAttribute('playsinline'), '', 'nativeEl has the playsinline attribute'); + + customVideo.remove(); +}); + +test('getTemplateHTML returns markup that keeps attribute values', async function (t) { + const attrs = { + poster: `x">`, + src: `https://stream.mux.com/a/low.mp4?a=1&b='2'&c=<3>`, + crossorigin: `'">`, + controls: '', + }; + + const template = document.createElement('template'); + template.innerHTML = globalThis.customElements.get('custom-video').getTemplateHTML(attrs); + const { content } = template; + + t.equal(content.querySelectorAll('img').length, 0, 'no element is created from attribute values'); + t.equal(content.querySelectorAll('video').length, 1, 'has one video element'); + + const video = content.querySelector('video'); + for (const [name, value] of Object.entries(attrs)) { + t.equal(video.getAttribute(name), value, `video has the same ${name}`); + } +}); + test(`muted prop is set and doesn't reflect to muted attribute`, async function (t) { const customVideo = await fixture( `` From bf8a968f026d5bad29618af6396b526edb99586f Mon Sep 17 00:00:00 2001 From: Javier Aramberri Date: Tue, 13 Oct 2026 16:58:47 -0300 Subject: [PATCH 2/5] fix(hls-video-element): escape the config JSON in the template HTML getTemplateHTML writes the config as JSON in a script element. A config string with a closing script tag ended that element too early. The "<" character is now written as a JSON unicode escape, so JSON.parse gives the same config. --- .../hls-video-element/hls-video-element.js | 2 +- packages/hls-video-element/test/test.js | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/hls-video-element/hls-video-element.js b/packages/hls-video-element/hls-video-element.js index 677dbffa..313933be 100644 --- a/packages/hls-video-element/hls-video-element.js +++ b/packages/hls-video-element/hls-video-element.js @@ -12,7 +12,7 @@ const HlsVideoMixin = (superclass) => { // Required for React SSR because the custom element is initialized long before React client render. return ` ${superclass.getTemplateHTML(rest)} `; diff --git a/packages/hls-video-element/test/test.js b/packages/hls-video-element/test/test.js index 1a5a8a6d..d1ff0da0 100644 --- a/packages/hls-video-element/test/test.js +++ b/packages/hls-video-element/test/test.js @@ -68,6 +68,26 @@ test('play promise', async function (t) { t.ok(!video.paused, 'is playing after video.play()'); }); +test('getTemplateHTML keeps the config inside the config script', async function (t) { + const config = { + xhrSetup: undefined, + licenseUrl: '