Skip to main content

SKY-D228 · Security in code

HTML response built from user input

HighCWE-79

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

app/greet.py · PythonReported on line 9
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

app/greet.py · Python
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

src/server.ts · TypeScriptReported on line 7
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

src/server.ts · TypeScript
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

main.go · GoReported on line 10
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)}
go.mod · Text
module example.com/greet go 1.22

Not flagged

main.go · Go
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)}

go.mod: same as in the flagged example.

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

public/hello.php · PHPReported on line 3
<?php$name = $_GET['name'] ?? '';echo "<h1>Hello, " . $name . "!</h1>";

Not flagged

public/hello.php · PHP
<?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

  1. Runs with --danger. Every language needs a value from outside the program; Skylos never reports a string built only from constants.
  2. Python: looks at each return whose value is an f-string, a + chain, a % format or a .format() call whose text contains a real HTML tag (<h1, <div, <a, <script and so on). Function parameters, request.* and input() count as user data, and assignments carry it to later names.
  3. 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() and Markup.escape() escape quotes too; bleach.clean() only makes text safe. An escaped value in an onclick, a style attribute, a <script> block or an unquoted attribute is still reported. Numbers from int(), len() and similar are clean.
  4. TypeScript and JavaScript: inside an Express, Fastify, Hapi, Koa, NestJS, Next.js or Remix handler, reports request data sent with res.send(), res.write() or res.end() in a string that contains an HTML tag, and Koa ctx.body set to a value that starts with a tag or with the request value. An Express, NestJS or Next.js pages-API res.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"), a Content-Type header), 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.
  5. Go (from the Go engine as SKY-G228): request data written with fmt.Fprint*, io.WriteString or w.Write to an http.ResponseWriter is 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 serves text/plain). Gin c.Data, Echo c.HTML and c.Blob, and Fiber writes are reported when the content type is HTML. html.EscapeString, the html/template escapers, url.QueryEscape, json.Marshal and bluemonday policies count as escaping; cookies do not count as user input.
  6. PHP: request data in echo, print, printf, die/exit, print_r, var_dump or a short <?= ?> tag, unless escaped for where it lands (htmlspecialchars, htmlentities, WordPress esc_html and esc_attr, Laravel e(); strip_tags and wp_kses only for element text). In Blade templates the same finding is reported as SKY-D226. C#: request data passed to Content(x, "text/html") or Response.Write(x) without encoding.
  7. Rust: request data inserted by format!, format_args!, write! or writeln! (also inside another macro such as json!) into a template that contains an HTML tag, where request data is an actix-web or axum extractor (Path, Query, Json, Form, ...), a Rocket Json/Form/Data guard or text route parameter (#[get("/x/<name>")] with name: &str), or the data methods of an HttpRequest/Request (uri(), query_string(), headers(), ...). html_escape, htmlescape, v_htmlescape, askama_escape, ammonia::clean and URL encoding (form_urlencoded::byte_serialize, utf8_percent_encode, urlencoding::encode) clear the value. Rendering the byte count from a known string's len() 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).

app/greet.py · Python · the fix, against the flagged file
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 return values. 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 value
  • Accept 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.

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