Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/cloudflare-video-element/cloudflare-video-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/`/g, '&#x60;');
}

function serialize(props) {
return String(new URLSearchParams(boolToBinary(props)));
}
Expand Down
14 changes: 12 additions & 2 deletions packages/custom-media-element/custom-media-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,22 @@ function serializeAttributes(attrs: Record<string, string>): 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, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/`/g, '&#x60;');
}

/**
* Helper function to convert NamedNodeMap to a plain object.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/custom-media-element/test/eager-upgrade.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@
crossorigin
src="http://stream.mux.com/DS00Spx1CV902MCtPj5WknGlR102V5HFkDe/low.mp4"
></custom-video>
<custom-video
id="escapedAttrsVideo"
poster="x&quot;&gt;&lt;img src=&quot;x&quot; onerror=&quot;globalThis.injectedHandlerRan = true&quot;&gt;"
crossorigin="&quot;&gt;&lt;/video&gt;&lt;img src=&quot;x&quot; onerror=&quot;globalThis.injectedHandlerRan = true&quot;&gt;"
></custom-video>
</body>
</html>
5 changes: 5 additions & 0 deletions packages/custom-media-element/test/lazy-upgrade.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
crossorigin
src="http://stream.mux.com/DS00Spx1CV902MCtPj5WknGlR102V5HFkDe/low.mp4"
></custom-video>
<custom-video
id="escapedAttrsVideo"
poster="x&quot;&gt;&lt;img src=&quot;x&quot; onerror=&quot;globalThis.injectedHandlerRan = true&quot;&gt;"
crossorigin="&quot;&gt;&lt;/video&gt;&lt;img src=&quot;x&quot; onerror=&quot;globalThis.injectedHandlerRan = true&quot;&gt;"
></custom-video>
</body>
</html>
68 changes: 68 additions & 0 deletions packages/custom-media-element/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>"`,
src: 'https://stream.mux.com/a/low.mp4?token=a.b.c&expires=1&amp=1',
crossorigin: 'anonymous',
preload: 'none',
};
const escapeAttr = (value) => value.replace(/&/g, '&amp;').replace(/"/g, '&quot;');
const serialized = Object.entries(attrs)
.map(([name, value]) => `${name}="${escapeAttr(value)}"`)
.join(' ');

const customVideo = await fixture(`<custom-video ${serialized} controls playsinline></custom-video>`);

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"><img src="x" onerror="globalThis.injectedHandlerRan = true">`,
src: `https://stream.mux.com/a/low.mp4?a=1&b='2'&c=<3>`,
crossorigin: `'"></video><img src="x">`,
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(
`<custom-video></custom-video>`
Expand Down
2 changes: 1 addition & 1 deletion packages/hls-video-element/hls-video-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const HlsVideoMixin = (superclass) => {
// Required for React SSR because the custom element is initialized long before React client render.
return `
<script type="application/json" id="config">
${JSON.stringify(props.config || {})}
${JSON.stringify(props.config || {}).replace(/</g, '\\u003c')}
</script>
${superclass.getTemplateHTML(rest)}
`;
Expand Down
20 changes: 20 additions & 0 deletions packages/hls-video-element/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '</script><img src="x"><script>',
comment: '<!--<script>',
};

const template = document.createElement('template');
template.innerHTML = globalThis.customElements.get('hls-video').getTemplateHTML({}, { config });
const { content } = template;

t.equal(content.querySelectorAll('img').length, 0, 'no element is created from config values');
t.equal(content.querySelectorAll('script').length, 1, 'has one config script');
t.deepEqual(
JSON.parse(content.getElementById('config').textContent),
JSON.parse(JSON.stringify(config)),
'config script has the same config'
);
});

function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand Down
14 changes: 12 additions & 2 deletions packages/spotify-audio-element/spotify-audio-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,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, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/`/g, '&#x60;');
}

function serialize(props) {
return String(new URLSearchParams(boolToBinary(props)));
}
Expand Down
Loading