T09 · Insecure Skill Coding Practices
- Location
scripts/generate_html.py:11- Finding
Stored Script Injection in Generated Mind Map HTML
- Content
View full analysis
\n' '\n' '\n' '\n' '\n' '' + workspace_title + ' - 思维导图\n' # ... '` sequence terminates the script element even when that sequence occurs inside a JavaScript string. Consequently, a malicious title containing a sequence such as `` can escape the data block and introduce executable JavaScript. The separate insertion into `` is also unsafe. A value containing ` ...[truncated 1412 chars]- Remediation
View remediation
` element: ```python import html safe_workspace_title = html.escape(workspace_title, quote=True) ``` 2. Do not concatenate ordinary JSON directly into an executable script element. Store it in a non-executable JSON element and escape characters significant to HTML parsing: ```python json_str = json.dumps(data, ensure_ascii=False, separators=(',', ':')) json_str = ( json_str .replace('&', '\\u0026') .replace('<', '\\u003c') .replace('>', '\\u003e') .replace('\u2028', '\\u2028') .replace('\u2029', '\\u2029') ) ``` Then embed and parse it as data: ```html ``` 3. Prefer DOM APIs such as `textContent` whenever untrusted values are rendered into HTML. 4. Add a restrictive Content Security Policy to limit the impact of any remaining injection issue. Moving JavaScript to a separate local file would allow inline scripts to be disabled. 5. Add regression tests containing values such as: ```text <>&"' and Unicode line separators ``` The tests should verify that these strings remain inert text in the generated document. ]]>
