T09 · Insecure Skill Coding Practices
- Location
generate-chart.js:343- Finding
Generated HTML Allows Script Injection Through Untrusted Chart Content
- Content
View full analysis
${title} ``` ```javascript${title}
``` ```javascript ``` ### Technical Analysis The `--title` command-line value is interpolated directly into both the HTML `` element and an `<h1>` element without context-appropriate HTML escaping. A crafted value containing closing tags and executable markup can therefore escape the intended element and insert arbitrary HTML or JavaScript. Chart data is also serialized with `JSON.stringify()` and embedded directly inside an executable `<script>` element. JSON serialization does not make a value safe for an HTML script context. In particular, a user-controlled string containing `</script>` is recognized by the HTML parser as the end of the script element even when that sequence occurs inside a JavaScript string literal. An attacker can follow it with a new `<script>` element containing arbitrary JavaScript. The generated file is explicitly intended to be opened in a browser, so the injected content reaches an executable sink as part of the normal workflow. ### Attack Path 1. An attacker supplies or persuades a user to process crafted financial data or a crafted chart title. 2. The title can contain a payload such as a closing `` or `` tag followed by a malicious ``. 4. The program inserts the malicious value into the generated HTML without safe encoding. ...[truncated 894 chars]- Remediation
View remediation
`, `"`, and `'`. 2. Prefer constructing text nodes with `textContent` rather than inserting user-controlled values into an HTML template. 3. Do not embed raw JSON in an executable script context. Encode HTML-significant characters after serialization, for example: ```javascript function serializeForInlineScript(value) { return JSON.stringify(value) .replace(//g, '\\u003e') .replace(/&/g, '\\u0026') .replace(/\u2028/g, '\\u2028') .replace(/\u2029/g, '\\u2029'); } ``` 4. Prefer placing serialized data in a non-executable ``, closing HTML tags, quotes, ampersands, and Unicode line separators. ]]>
