T09 · Insecure Skill Coding Practices
- Location
scripts/generate_html.py:223- Finding
Stored DOM-Based Cross-Site Scripting Through Untrusted Douyin API Data
- Content
View full analysis
({ rank: index + 1, title: item.word || '无标题', popularity: item.hot_value || 0, link: item.url || `https://www.douyin.com/search/${encodeURIComponent(item.word || '')}`, label: item.label || null, type: item.type || '未知' })); ``` The values are persisted without content or URL validation in `scripts/db.py:43-73`: ```python for item in items: try: title = item.get('title', '') if not title: continue item_id = generate_id(title, today) rank = item.get('rank', 0) popularity = item.get('popularity', 0) link = item.get('link', '') label = item.get('label', '') item_type = item.get('type', '未知') cursor.execute('SELECT id FROM hot_items WHERE id = ?', (item_id,)) if cursor.fetchone(): continue cursor.execute(''' INSERT INTO hot_items (id, platform, rank, title, popularity, link, label, item_type, fetched_at, fetch_date, raw_data) VALUES (?, 'douyin', ?, ?, ?, ?, ?, ?, ?, ?, ?) ''', (item_id, rank, title, popularity, link, label, item_type, now, today, json.dumps(item, ensure_ascii=False))) ``` The stored data is embedded into an executable script context in `scripts/generate_html.py:223`: ```python- Remediation
View remediation
`, `&`, U+2028, and U+2029 after JSON serialization. - Do not rely on JSON string escaping alone to prevent closing-script injection. 4. **Validate data before persistence** - Enforce expected types and reasonable length limits for titles, labels, and other fields. - Normalize popularity and rank as bounded integers. - Apply the URL allowlist before storing links. - Treat existing database contents as untrusted when generating reports. 5. **Apply browser defense in depth** - Add a restrictive Content Security Policy that disallows inline scripts after moving JavaScript to a separate file. - Use a policy similar to `default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'none'; base-uri 'none'; object-src 'none'`. - Add `rel="noopener noreferrer"` to links using `target="_blank"`. 6. **Clean existing persisted data** - Validate or delete records already stored in `data/douyin.db`. - Regenerate `data/index.html` only after the generator and stored records have been secured. ]]>
