T09 · Insecure Skill Coding Practices
- Location
scripts/build_report.py:45- Finding
Stored Script Injection Through Unsafe JSON Embedding
- Content
View full analysis
const DATA = __REPORT_DATA__; const DELETE = __DELETE_CONFIG__; // null=static report; {token,endpoint}=server.py dynamic backend ``` ### Technical Analysis The report generators serialize analysis data with `json.dumps()` and directly substitute the result into an HTML ``. If a report field contains a value such as a crafted filename, path, description, or other analysis property with a closing script tag, the browser can terminate the original script element before the JavaScript parser processes the JSON. Subsequent attacker-controlled markup can then introduce a new executable script. Although most dynamically rendered report values later pass through the `esc()` function, that escaping occurs only after the initial `DATA` declaration has already been parsed. It therefore does not prevent this injection. The server configuration, including its random action token, is also embedded in the same page. JavaScr ...[truncated 1811 chars]- Remediation
View remediation
... ``` Then read and parse its text content: ```javascript const DATA = JSON.parse(document.getElementById('report-data').textContent); ``` 3. Before embedding serialized JSON in HTML, replace HTML-significant characters with Unicode escapes. At minimum, escape: - `<` as `\u003c` - `>` as `\u003e` - `&` as `\u0026` - U+2028 as `\u2028` - U+2029 as `\u2029` 4. Apply the same safe serialization function in both `build_report.py` and `server.py` so static and dynamic reports cannot diverge in security behavior. 5. Validate the analysis document against a strict schema, including type and length restrictions for every displayed field. 6. Add a restrictive Content Security Policy. Prefer external scripts with a nonce or hash and disallow arbitrary inline execution. 7. Add regression tests using values containing ``, HTML elements, quotes, Unicode separators, and event-handler payloads. 8. In server mode, consider separating the action token from report-readable JavaScript and require an additional server-enforced user authorization step for destructive requests. ]]>
