SKY-D228 · Security in code
HTML response built from user input
A handler that builds an HTML response by pasting request text into a string, without escaping it, lets the visitor write markup into the page. SKY-D228 reports that string where it is returned or written to the response.
- Languages
- Python, TypeScript/JavaScript, Go, PHP, C#, Rust
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Cross-site scripting: HTML built from user input
Why it matters
A link such as /hello?name=<script>…</script> runs that script for whoever opens it, with their cookies and session. Sending such links to staff is a common way to take over admin accounts.
What Skylos flags
from flask import Flask, request app = Flask(__name__) @app.get("/hello")def hello(): name = request.args.get("name", "") return f"<h1>Hello, {name}!</h1>"What Skylos leaves alone
from flask import Flask, request app = Flask(__name__) @app.get("/cart/badge")def cart_badge(): count = int(request.args.get("count", "0")) return f'<span class="badge">{count} items</span>'Why Skylos leaves it alone
The request value goes through int() first. A number cannot carry markup, so Skylos treats the result of int(), float(), len() and similar built-ins as clean.
More languages
TypeScript
Flagged
import express from "express"; const app = express(); app.get("/hello", (req, res) => { const name = String(req.query.name ?? ""); res.send(`<h1>Hello, ${name}!</h1>`);});Not flagged
import express from "express"; const app = express(); app.get("/hello", (req, res) => { const name = String(req.query.name ?? ""); res.type("text/plain").send(`Hello, ${name}!`);});Why Skylos leaves it alone
The response type is set to text/plain before the send, so the browser shows the name as text. Without that call, Express sends a string as HTML, and Skylos would report it even with no tag in it.
Go
Flagged
package main import ( "fmt" "net/http") func hello(w http.ResponseWriter, r *http.Request) { name := r.URL.Query().Get("name") fmt.Fprintf(w, "<h1>Hello, %s!</h1>", name)} func main() { http.HandleFunc("/hello", hello)}module example.com/greet go 1.22Not flagged
package main import ( "fmt" "net/http") func hello(w http.ResponseWriter, r *http.Request) { name := r.URL.Query().Get("name") w.Header().Set("Content-Type", "text/plain; charset=utf-8") fmt.Fprintf(w, "Hello, %s!", name)} func main() { http.HandleFunc("/hello", hello)}Why Skylos leaves it alone
A text/plain Content-Type is set on the same writer before the write, so the response is not HTML.
PHP
Flagged
<?php$name = $_GET['name'] ?? '';echo "<h1>Hello, " . $name . "!</h1>";Not flagged
<?php$name = $_GET['name'] ?? '';echo "<h1>Hello, " . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . "!</h1>";Why Skylos leaves it alone
htmlspecialchars() escapes the name for HTML text and quoted attributes, which is where it lands.
How Skylos decides
- Runs with
--danger. Every language needs a value from outside the program; Skylos never reports a string built only from constants. - Python: looks at each
returnwhose value is an f-string, a+chain, a%format or a.format()call whose text contains a real HTML tag (<h1,<div,<a,<scriptand so on). Function parameters,request.*andinput()count as user data, and assignments carry it to later names. - Python: tracks where each value lands in the HTML: element text needs
<,>and&escaped, a quoted attribute also needs quotes escaped, and a link (href,action) also needs a safe scheme.html.escape(),markupsafe.escape()andMarkup.escape()escape quotes too;bleach.clean()only makes text safe. An escaped value in anonclick, astyleattribute, a<script>block or an unquoted attribute is still reported. Numbers fromint(),len()and similar are clean. - TypeScript and JavaScript: inside an Express, Fastify, Hapi, Koa, NestJS, Next.js or Remix handler, reports request data sent with
res.send(),res.write()orres.end()in a string that contains an HTML tag, and Koactx.bodyset to a value that starts with a tag or with the request value. An Express, NestJS or Next.js pages-APIres.send()of request text is reported even without a tag, since Express sends strings as HTML. It skips the write when a non-HTML content type was set first (res.type("text/plain"), aContent-Typeheader), and when the value is a whole request object or an object or array literal (sent as JSON).encodeURIComponent(), numeric conversion and a same-file helper that escapes<count as escaping. - Go (from the Go engine as SKY-G228): request data written with
fmt.Fprint*,io.WriteStringorw.Writeto anhttp.ResponseWriteris reported unless a non-HTML Content-Type was set on that writer before the write, or every write up to it starts with fixed text whose first character is not<(Go then servestext/plain). Ginc.Data, Echoc.HTMLandc.Blob, and Fiber writes are reported when the content type is HTML.html.EscapeString, thehtml/templateescapers,url.QueryEscape,json.Marshaland bluemonday policies count as escaping; cookies do not count as user input. - PHP: request data in
echo,print,printf,die/exit,print_r,var_dumpor a short<?= ?>tag, unless escaped for where it lands (htmlspecialchars,htmlentities, WordPressesc_htmlandesc_attr, Laravele();strip_tagsandwp_ksesonly for element text). In Blade templates the same finding is reported as SKY-D226. C#: request data passed toContent(x, "text/html")orResponse.Write(x)without encoding. - Rust: request data inserted by
format!,format_args!,write!orwriteln!(also inside another macro such asjson!) into a template that contains an HTML tag, where request data is an actix-web or axum extractor (Path,Query,Json,Form, ...), a RocketJson/Form/Dataguard or text route parameter (#[get("/x/<name>")]withname: &str), or the data methods of anHttpRequest/Request(uri(),query_string(),headers(), ...).html_escape,htmlescape,v_htmlescape,askama_escape,ammonia::cleanand URL encoding (form_urlencoded::byte_serialize,utf8_percent_encode,urlencoding::encode) clear the value. Rendering the byte count from a known string'slen()is safe in HTML text. Test code is excluded.
How to fix it
Escape the value for the place it lands, or render it with a template engine that escapes by default (Jinja, html/template, React). In Python, html.escape() covers element text and quoted attributes; in Go use html.EscapeString; in PHP htmlspecialchars($v, ENT_QUOTES).
Added: import htmlAdded: from flask import Flask, request app = Flask(__name__) @app.get("/hello")def hello(): name = request.args.get("name", "")Removed: return f"<h1>Hello, {name}!</h1>"Added: return f"<h1>Hello, {html.escape(name)}!</h1>"When it can be wrong
- In Python every function parameter counts as user data, so a helper such as
def badge(label): return f"<b>{label}</b>"is reported even when every caller passes a constant. - A value escaped by a helper Skylos cannot see into (another module, or a sanitizer it does not know) is reported.
- Python only checks
returnvalues. HTML assigned to a variable and passed to a response object, or plain text with no tag that Flask still serves as HTML, is not reported.
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
Mark the reported line and say why the value is safe.
Python return f"<b>{label}</b>" # skylos: ignore[SKY-D228] label is a fixed enum valueAccept existing findings with
skylos baseline ., then scan with--baseline. Entries match by file and line, so they stop matching when the line moves.[tool.skylos] ignore = ["SKY-D228"]turns the rule off for the whole project.