Skip to main content

SKY-D226 · Security in code

Cross-site scripting through raw HTML

CriticalCWE-79

TypeScript and JavaScript findings are reported as HIGH (an Angular bypassSecurityTrust* value that is not from the URL or browser storage is MEDIUM), as are Java, Kotlin, C# and PHP findings. Python Markup()/mark_safe() and Go html/template findings built from request data are CRITICAL.

Cross-site scripting (XSS) happens when a page inserts text as HTML, so that markup someone else wrote runs as script in another user's browser. In TypeScript and JavaScript, Skylos reports SKY-D226 when a value that is not proven safe reaches an HTML sink such as innerHTML, document.write() or React's dangerouslySetInnerHTML.

Languages
TypeScript/JavaScript, Python, Go, Java, Kotlin, C#, PHP
Quality gate
counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans). TypeScript and JavaScript findings are HIGH, so they count toward the limits; Python and Go findings are CRITICAL.
Finding type
review
Catalog name
Cross-site scripting: untrusted content marked safe
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A search link carrying <img src=x onerror=…> runs that script for whoever opens it, with their session: it can read what they see on the page and act as them.

What Skylos flags

src/search/heading.ts · TypeScriptReported on line 3
export function showSearchHeading(heading: HTMLElement): void {  const query = new URLSearchParams(window.location.search).get("q") ?? "";  heading.innerHTML = `Results for <mark>${query}</mark>`;}

What Skylos leaves alone

src/search/heading.ts · TypeScript
const SPINNER = '<span class="spinner" aria-hidden="true"></span>'; export function showLoading(panel: HTMLElement): void {  panel.innerHTML = SPINNER;} export function showSearchHeading(heading: HTMLElement): void {  const query = new URLSearchParams(window.location.search).get("q") ?? "";  heading.textContent = `Results for ${query}`;}

Why Skylos leaves it alone

innerHTML only receives SPINNER, a constant whose markup Skylos can read and that cannot run script. The search text goes into textContent, which the browser always treats as text.

How Skylos decides

  1. Runs with --danger. In .ts, .tsx, .js, .jsx, .mts, .cts, .mjs and .cjs files it finds HTML sinks: assignments to innerHTML or outerHTML (also +=), document.write() and writeln(), insertAdjacentHTML(), createContextualFragment(), setHTMLUnsafe(), jQuery .html(), .append() and similar on $(…), jQuery(…) or a $-prefixed variable, and the JSX dangerouslySetInnerHTML attribute. .vue and .svelte templates are not read.
  2. In a file that imports @angular/platform-browser, also reports DomSanitizer.bypassSecurityTrustHtml, Script, Style, Url and ResourceUrl of a non-constant value: HIGH when the value comes from the URL (route and query params, location), document.cookie or browser storage, MEDIUM otherwise (for example HTTP response data). A literal, or a Url/ResourceUrl with a fixed https://host/ prefix, is not reported.
  3. It does not need to find where the value came from: every sink is reported unless the value is proven safe.
  4. A value is proven safe when it is fixed markup Skylos can read (string literals, templates and + joins of literals, both sides of a ? :, or a const bound to one) and that markup cannot run script: no on…= attributes, no javascript: URLs, and no <script>, <iframe>, <svg> or similar tags. The empty string is safe.
  5. A value is also safe when it is the result of an imported sanitizer called with one argument: DOMPurify.sanitize(x) from dompurify or isomorphic-dompurify, or sanitizeHtml(x) from sanitize-html. This proof holds only while every use of the sanitizer in the file is such a one-argument call.
  6. A template or + join is also safe when every interpolated part is a constant, a number (arithmetic, Number(), parseInt(), Math.*(), toFixed() and the like) or the result of an escaper in a place the escaper covers: global encodeURIComponent/encodeURI, an imported HTML escaper (escape-html, lodash or underscore escape, html-escaper, he, validator.escape) or a same-file function that only returns one. Escaped text is safe in element text and quoted attributes, but not in an event handler, style, an unquoted attribute, a tag name or at the start of a link.
  7. Only dangerouslySetInnerHTML={{ __html: value }} can be proven safe; any other form is reported. jQuery .append() and its siblings count only for a template literal or + join that contains a tag. textContent and DOM append(text) are not sinks.
  8. Other languages need outside data: Python Markup() and mark_safe() on request input, Go template.HTML() and similar on request input (from the Go engine as SKY-G226), servlet writers in Java and Kotlin, and Ktor respondText() with an HTML content type in Kotlin, Html.Raw, HtmlString and MarkupString in C#, Drupal Markup::create() and Blade {!! !!} in PHP.

How to fix it

Put outside text in the page as text: textContent, or React's normal {value} rendering, which escapes it. When the content must stay HTML, such as rendered Markdown, pass it through DOMPurify.sanitize(html) first, imported from dompurify and called with that one argument.

src/search/heading.ts · TypeScript · the fix, against the flagged file
export function showSearchHeading(heading: HTMLElement): void {  const query = new URLSearchParams(window.location.search).get("q") ?? "";Removed:   heading.innerHTML = `Results for <mark>${query}</mark>`;Added:   const mark = document.createElement("mark");Added:   mark.textContent = query;Added:   heading.replaceChildren("Results for ", mark);}

When it can be wrong

  • HTML built from your own trusted data, such as ` <b>${row.label}</b> or a lookup like ICONS[kind]`, is reported, because Skylos does not check where the value came from.
  • Literal SVG markup is reported: <svg> is on the list of tags that can carry script.
  • jQuery calls on a receiver not written as $(…), jQuery(…) or a $-prefixed name, such as const box = $("#g"); box.html(x), are missed.
  • A sanitizer called with options, such as DOMPurify.sanitize(html, { ALLOWED_TAGS: [...] }), is not accepted, and its presence also cancels the proof for every plain DOMPurify.sanitize(x) in the same file.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the reported line. In JSX, a {/* … */} comment on the attribute line works.

    TSX
    el.innerHTML = iconSvg; // skylos: ignore[SKY-D226] bundled icon, no user data<div dangerouslySetInnerHTML={{ __html: html }} /> {/* skylos: ignore[SKY-D226] CMS */}
  • [tool.skylos] ignore = ["SKY-D226"] turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.