T09 · Insecure Skill Coding Practices
- Location
references/html-template-audit.html:214- Finding
Stored JavaScript Injection in Generated HTML Reports
- Content
View full analysis
// Data populated by the report generator var EXAM_DATA = {exam_json}; var AUDIT_LOG = {audit_log}; ``` ```javascript (data.sections || []).forEach(function(section) { var sectionEl = document.createElement('div'); sectionEl.className = 'section'; sectionEl.setAttribute('data-section-type', section.section_type || ''); var html = ''; html += '' + (section.section_title || '') + ''; html += '共' + (section.questions || []).length + '题,' + (section.section_score || '?') + '分'; (section.questions || []).forEach(function(q) { var auditItem = getAuditItem(q.question_id); var auditClass = auditItem ? ' audit-' + auditItem.status : ''; var badgeHtml = auditItem ? '' + getStatusLabel(auditItem.status) + '' : ''; html += ''; html += badgeHtml; html += '' + q.question_id + '.' + (q.score || '?') + '分'; if (q.material) { html += '' + escapeHtml(q.material) + ''; } html += '' + renderLatex(escapeHtml(q.stem || '')) + ''; if (q.options) { html += ''; for (var opt in q.options) { html += '' + ...[truncated 4313 chars]- Remediation
View remediation
SAFE_JSON ``` - Escape at least `<`, `>`, `&`, U+2028, and U+2029 during serialization. - Parse it using: ```javascript const EXAM_DATA = JSON.parse( document.getElementById('exam-data').textContent ); ``` 2. **Replace HTML string construction with safe DOM APIs.** - Create elements using `document.createElement`. - Assign untrusted text only through `textContent`. - Use `classList.add` and validated values for class names. - Avoid assigning OCR-derived strings to `innerHTML`. 3. **Strictly validate structured fields.** - Restrict `status` to `ok`, `corrected`, `warning`, or `error`. - Restrict `confidence` to `high`, `medium`, or `low`. - Restrict question and section types to the documented enumeration. - Require IDs and scores to be finite integers within expected ranges. 4. **Apply context-specific encoding.** - HTML text, HTML attributes, JavaScript strings, and URLs require different encoding strategies. - Do not rely on a single generic HTML-escaping function for all contexts. 5. **Add a restrictive Content Security Policy.** - Remove inline event handlers and inline scripts where feasible. - Use a nonce or hashed scripts. - Restrict `connect-src` to prevent unexpected data transmission. 6. **Add regression tests** using hostile values in every report field, including script-closing sequences, event-handler markup, quotes, malformed IDs, and malicious class values. ]]>
