diff --git a/packages/cloudflare-video-element/cloudflare-video-element.js b/packages/cloudflare-video-element/cloudflare-video-element.js index bfdbb407..660e12cd 100644 --- a/packages/cloudflare-video-element/cloudflare-video-element.js +++ b/packages/cloudflare-video-element/cloudflare-video-element.js @@ -396,12 +396,22 @@ function serializeAttributes(attrs) { let html = ''; for (const key in attrs) { const value = attrs[key]; - if (value === '') html += ` ${key}`; - else html += ` ${key}="${value}"`; + if (value === '') html += ` ${escapeHtml(key)}`; + else html += ` ${escapeHtml(key)}="${escapeHtml(`${value}`)}"`; } return html; } +function escapeHtml(str) { + return str + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, ''') + .replace(/`/g, '`'); +} + function serialize(props) { return String(new URLSearchParams(boolToBinary(props))); } diff --git a/packages/custom-media-element/custom-media-element.ts b/packages/custom-media-element/custom-media-element.ts index 86f0bed9..c8939b0e 100644 --- a/packages/custom-media-element/custom-media-element.ts +++ b/packages/custom-media-element/custom-media-element.ts @@ -512,12 +512,22 @@ function serializeAttributes(attrs: Record): string { if (!Attributes.includes(key as typeof Attributes[number])) continue; const value = attrs[key]; - if (value === '') html += ` ${key}`; - else html += ` ${key}="${value}"`; + if (value === '') html += ` ${escapeHtml(key)}`; + else html += ` ${escapeHtml(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( `` 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: '