T09 · Insecure Skill Coding Practices
- Location
scripts/htmlReporter.js:233- Finding
Stored Script Injection in Generated HTML Reports
- Content
View full analysis
const platformData = ` + platformDataJson + `; const crossPlatformData = ` + crossPlatformJson + `; const trendsData = ` + trendsJson + `; const monitorData = ` + monitorJson + `; ``` ### Technical Analysis The values serialized into these JavaScript variables originate from external platform APIs and local snapshot files. Although `JSON.stringify()` produces valid JSON, it does not make the result safe for direct insertion into an HTML script element. In particular, an attacker-controlled string containing an HTML script-closing sequence can terminate the containing script element before the browser's JavaScript parser processes the JSON. For example, a malicious topic can contain a sequence equivalent to: ```html ``` The visible HTML sections use `escapeHtml()`, but that function is not applied to the JSON inserted into the inline ...[truncated 1855 chars]- Remediation
View remediation
` as `\u003e` - `&` as `\u0026` - U+2028 as `\u2028` - U+2029 as `\u2029` 3. Prefer placing safely escaped JSON inside a non-executable element such as: ```html ``` Then parse it using: ```js const reportData = JSON.parse( document.getElementById('report-data').textContent ); ``` 4. Use a maintained serializer specifically designed for embedding data in HTML. 5. Validate all upstream records against a strict schema, including expected scalar types, maximum lengths, numeric ranges, and maximum list sizes. 6. Add a restrictive Content Security Policy. Prefer external, locally bundled scripts and a policy that does not allow arbitrary inline scripts. 7. Add regression tests containing script-closing sequences in every externally controlled string field and verify that opening the generated report cannot execute them. ]]>
