T09 · Insecure Skill Coding Practices
- Location
references/html-viz-template.md:110- Finding
Stored Script Injection Through Unsafe JSON Interpolation
- Content
View full analysis
const data = __CPN_DATA__; document.getElementById('pid').textContent = data.project_id; ``` The Skill instructs the agent to replace `__CPN_DATA__` with a JSON object derived from the user's natural-language business description and then write the resulting HTML to `/tmp/.html`. No HTML script-context escaping or safe serialization requirement is imposed. ### Technical Analysis The JSON schema permits unrestricted string values for fields such as project IDs, place names, transition names, guards, annotations, subprojects, chains, and dependency descriptions. These values may originate from untrusted user input. JSON string escaping alone does not make data safe for direct insertion into an HTML `` sequence even when it appears inside what JavaScript would otherwise treat as a quoted string. Consequently, an attacker-controlled model value can terminate the original script element and introduce a new executable script. For example, a crafted business entity or project name could contain a value structurally equivalent to: ```text ``` If preserved in the generated JSON, direct placeholder replacement causes the browser to parse the attacker-controlled second script when the local HTML file is opened. The later use of `textContent` does not mitigate this vulnerability because execution occurs during HTML parsing, before the JSON value is accessed by application code. ### Attack Path 1. An attacker provides a business-process description containing a crafted project, state, action, role, or resource name with a `` ...[truncated 1397 chars]- Remediation
View remediation
SAFE_SERIALIZED_DATA ``` 2. **Apply HTML-safe JSON serialization.** At minimum, replace characters and sequences that can affect HTML parsing: - `<` with `\u003c` - `>` with `\u003e` - `&` with `\u0026` - U+2028 with `\u2028` - U+2029 with `\u2029` Escaping `<` prevents an attacker-controlled `` sequence from being interpreted as markup. 3. **Validate identifier fields.** Restrict machine identifiers such as `project_id`, place IDs, transition IDs, and arc IDs to a conservative allowlist, for example: ```text ^[A-Za-z0-9_-]+$ ``` Human-readable labels should remain separate from machine identifiers and must be safely encoded for their destination context. 4. **Add a restrictive Content Security Policy.** Move application JavaScript into a fixed local script file and disallow inline scripts where practical. A policy without `unsafe-inline` provides defense in depth against injected script elements. 5. **Add regression tests** covering malicious values in every user-derived string field, including: ```text < & U+2028 and U+2029 ``` Tests should generate the HTML, open or parse it with a browser-compatible test harness, and verify that no additional script element is created or executed. 6. **Document contextual output encoding requirements** in both `SKILL.md` and the HTML template instructions so implementations do not rely on ordinary JSON escaping as an HTML security boundary. ]]>
