Web Components

webc/lit-unsafe-html

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

poważnastatycznefe-web-components

Dlaczego

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.

Poprawka

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.

Przykłady

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

Źródła

Fakty

Identyfikator
webc/lit-unsafe-html
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
jsx
Tylko w projektach z
lit
Uczy jej skill
fe-web-components Web components and Lit
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

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