Web Components

webc/lit-unsafe-html

Lit's unsafeHTML, unsafeSVG and unsafeStatic only receive strings the developer wrote or sanitized, never data.

seriousstaticfe-web-components

Why

A Lit expression such as ${comment} is always rendered as text. unsafeHTML(comment) parses it as markup instead: a comment, a product description or a URL parameter that contains <img src=x onerror=…> runs script in the page with the user's session.

Fix

Render data with a normal expression (${comment}) and build structure with templates. When rich text must be shown, sanitize it first (unsafeHTML(DOMPurify.sanitize(html)), or the Sanitizer API's setHTML() where supported) and keep that in one helper. Use unsafeStatic only with constants, such as a tag name chosen from a fixed list.

Examples

// bad
render() { return html`<div class="comment">${unsafeHTML(this.comment.body)}</div>` }
// good
render() { return html`<div class="comment">${this.comment.body}</div>` }

Sources

Facts

Id
webc/lit-unsafe-html
Severity
serious
Check
static checked in the source by audit
Applies to
jsx
Only in projects with
lit
Taught by the skill
fe-web-components Web components and Lit
Since version
0.1.0

Rule file on GitHub

Read it in the terminal

npx github:dawidolko/frontend-skills explain webc/lit-unsafe-html