SKY-D226 · Security in code
Cross-site scripting through raw HTML
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
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
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
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
- Runs with
--danger. In.ts,.tsx,.js,.jsx,.mts,.cts,.mjsand.cjsfiles it finds HTML sinks: assignments toinnerHTMLorouterHTML(also+=),document.write()andwriteln(),insertAdjacentHTML(),createContextualFragment(),setHTMLUnsafe(), jQuery.html(),.append()and similar on$(…),jQuery(…)or a$-prefixed variable, and the JSXdangerouslySetInnerHTMLattribute..vueand.sveltetemplates are not read. - In a file that imports
@angular/platform-browser, also reportsDomSanitizer.bypassSecurityTrustHtml,Script,Style,UrlandResourceUrlof a non-constant value: HIGH when the value comes from the URL (route and query params,location),document.cookieor browser storage, MEDIUM otherwise (for example HTTP response data). A literal, or aUrl/ResourceUrlwith a fixedhttps://host/prefix, is not reported. - It does not need to find where the value came from: every sink is reported unless the value is proven safe.
- 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 aconstbound to one) and that markup cannot run script: noon…=attributes, nojavascript:URLs, and no<script>,<iframe>,<svg>or similar tags. The empty string is safe. - A value is also safe when it is the result of an imported sanitizer called with one argument:
DOMPurify.sanitize(x)fromdompurifyorisomorphic-dompurify, orsanitizeHtml(x)fromsanitize-html. This proof holds only while every use of the sanitizer in the file is such a one-argument call. - 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: globalencodeURIComponent/encodeURI, an imported HTML escaper (escape-html, lodash or underscoreescape,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. - 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.textContentand DOMappend(text)are not sinks. - Other languages need outside data: Python
Markup()andmark_safe()on request input, Gotemplate.HTML()and similar on request input (from the Go engine as SKY-G226), servlet writers in Java and Kotlin, and KtorrespondText()with an HTML content type in Kotlin,Html.Raw,HtmlStringandMarkupStringin C#, DrupalMarkup::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.
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 likeICONS[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 asconst 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 plainDOMPurify.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.