T09 · Insecure Skill Coding Practices
- Location
assets/jianfei.html:215- Finding
Stored DOM Cross-Site Scripting Through Unvalidated Weight Configuration
- Content
View full analysis
"$SCRIPT_DIR/config.json" << EOF { "title": "$TITLE", "language": "$LANG", "port": $SELECTED_PORT, "startWeight": $START_WEIGHT, "targetWeight": $TARGET_WEIGHT, "startDate": "$START_DATE", "endDate": "$END_DATE", "startDateLabel": "$START_LABEL", "endDateLabel": "$END_LABEL" } EOF cat > "$SCRIPT_DIR/weight_history.json" << EOF { "records": [ {"date": "$INIT_DATE", "weight": $INIT_WEIGHT} ] } EOF ``` The resulting configuration values are subsequently inserted into the document through `innerHTML`: ```javascript document.getElementById('startWeightDisplay').innerHTML = START_WEIGHT + t.kg + '
' + startLabel + ''; document.getElementById('targetWeightDisplay').innerHTML = TARGET_WEIGHT + t.kg + '
' + endLabel + ''; ``` ### Technical Analysis The setup script treats `START_WEIGHT`, `TARGET_WEIGHT`, and `INIT_WEIGHT` as trusted numeric values, but it does not enforce their type, range, or syntax. Direct interpolation into a heredoc allows an input to become a JSON string instead of a number while still producing syntactically valid JSON. After `config.json` is fetched and parsed, `startWeight` and `targetWeight` are assigned to `START_WEIGHT` ...[truncated 2238 chars]- Remediation
View remediation
0 && value <= 1000) }' } ``` 2. Validate the selected port as an integer from 1 through 65535 and validate dates against a strict `YYYY-MM-DD` format. 3. Generate JSON using a real serializer such as Python or `jq` rather than shell heredoc interpolation. For example, pass values as arguments to Python and use `json.dump()`. 4. Treat values loaded from JSON as untrusted. Explicitly convert and validate them in JavaScript: ```javascript const startWeight = Number(config.startWeight); if (!Number.isFinite(startWeight) || startWeight <= 0 || startWeight > 1000) { throw new Error('Invalid startWeight'); } ``` 5. Eliminate the `innerHTML` sinks. Build the elements with `textContent`: ```javascript const display = document.getElementById('startWeightDisplay'); display.replaceChildren(); const value = document.createTextNode(START_WEIGHT + t.kg); const br = document.createElement('br'); const small = document.createElement('small'); small.textContent = startLabel; display.append(value, br, small); ``` 6. Apply the same validation and safe serialization controls to `INIT_WEIGHT`, dates, the title, language, and port fields. 7. Add a restrictive Content Security Policy as defense in depth, for example by disallowing inline scripts and event handlers. This should supplement, not replace, output encoding and validation. ]]>
