T09 · Insecure Skill Coding Practices
- Location
scripts/make_resume.py:72- Finding
Stored JavaScript Injection Through Unsafely Embedded Resume Data
- Content
View full analysis
{section_js} {rules_js} const chat = document.getElementById('chat'); ``` ### Technical Analysis The `sections` and `rules` values can originate from a user-supplied JSON file loaded through the `--data` option. Although `json.dumps()` correctly escapes data for a JavaScript string literal, ordinary JSON encoding does not make the result safe for direct placement inside an HTML `` terminates a script element even when it occurs inside what JavaScript would otherwise treat as a quoted string. Consequently, a crafted resume value can close the existing script element and introduce a new executable element. For example, an attacker-controlled section value could contain: ```html ``` When the generated resume is opened, the browser parses the injected closing tag before JavaScript string-literal semantics apply. The attack therefore becomes stored JavaScript injection in the generated HTML file. Using `d.textContent` when displaying answers does not mitigate this issue because execution occurs while the browser initially parses the generated script block, before the answer is rendered. ### Attack Path 1. An attacker creates or modifies a resume JSON file containing a malicious `sections` value or rule value with a `` sequence and an injected script element. ...[truncated 1437 chars]- Remediation
View remediation
", "\\u003e") .replace("\u2028", "\\u2028") .replace("\u2029", "\\u2029") ) section_js = "const SECTION = " + json_for_script(sections) + ";" rules_js = "const RULES = " + json_for_script( [{"k": k, "w": w} for k, w in rules] ) + ";" ``` Escaping `<` prevents attacker-controlled data from producing a literal `` sequence in the generated HTML. Additional hardening should include: 1. Validate the structure and types of all fields loaded from `--data`. 2. Reject unexpected nested objects or non-string keywords and section values. 3. Add automated tests using payloads containing ``, HTML tags, quotes, ampersands, and U+2028/U+2029. 4. Consider placing serialized data in a dedicated JSON data element with safe escaping, then parsing its text content. 5. Deploy generated pages on an isolated origin rather than an origin shared with authenticated or sensitive applications. 6. Add a restrictive Content Security Policy as defense in depth, preferably using hashes or nonces rather than allowing unrestricted inline scripts. ]]>
