T09 · Insecure Skill Coding Practices
- Location
template.html:178- Finding
Unescaped Template Data Enables HTML and JavaScript Injection
- Content
View full analysis
{{DESTINATION_EMOJI}} {{DESTINATION_NAME}} 旅行 App 工具包 ``` ```html{{DESTINATION_EMOJI}}{{DESTINATION_NAME}} 旅行 App 工具包
{{TRAVEL_STYLE}} · 共 款精选 App
``` ```html{{FOOTER_TIPS}}``` ```javascript const APPS = {{APPS_JSON}}; ``` ```javascript grid.innerHTML = list.map(app => { const p = PRI[app.priority] || PRI.optional; const url = platform === "ios" ? app.iosUrl : app.androidUrl; const ok = url != null; const label = platform === "ios" ? "🍎 App Store 下载" : "🤖 Play 商店下载"; const na = platform === "ios" ? "iOS 暂不可用" : "Android 暂不可用"; return '' + ''; }).join(""); ``` ### Technical Analysis The template inserts destination data, footer content, and the application array into HTML and JavaScript contexts without context-sensitive escaping. The `APPS_JSON` placeholder is placed directly inside a `script` element. JSON serialization alone does not make this safe because a string containing `` can terminate the surrounding script e ...[truncated 3045 chars]' + app.icon + '' + '' + '' + app.name + '' + '' + p.label + '' + '' + app.description + '
' + (app.tips ? '💡 ' + app.tips + '' : '') + (ok ? '' + label + '' : '' + na + '') + '- Remediation
View remediation
` from terminating the element. - Prefer placing serialized data in a non-executable element and parsing it explicitly, while still escaping HTML-significant characters. 2. **Replace `innerHTML` with DOM construction** - Create cards with `document.createElement`. - Assign application names, descriptions, tips, icons, and labels through `textContent`. - Set fixed CSS classes with `classList` rather than concatenating markup. 3. **Treat template fields as text** - Insert destination name, emoji, and travel style through `textContent`. - Do not permit arbitrary HTML in `FOOTER_TIPS`. - If formatted footer content is required, sanitize it with a strict allowlist that excludes scripts, event-handler attributes, embedded frames, forms, and dangerous URLs. 4. **Validate download URLs** - Parse each link with `new URL(value)`. - Require the `https:` protocol. - Allowlist only expected hosts such as `apps.apple.com` and `play.google.com`. - Reject credentials, unexpected ports, malformed URLs, and all other protocols or hosts. 5. **Constrain generated supplemental data** - Validate every generated application record against a strict schema. - Enforce length limits and enumerated values for categories and priorities. - Apply the same URL restrictions to Agent-supplemented records as to bundled records. 6. **Add defense in depth** - Use a restrictive Content Security Policy. - Move the inline script into a fixed local script file or authorize it with a generated nonce or hash. - Disallow object embedding and restrict navigation and network destinations where compatible with the page's function. - Add automated tests containing payloads such as closing script tags, quotation mark ...[truncated 116 chars]
