T09 · Insecure Skill Coding Practices
- Location
mountain_weather.py:53- Finding
Terminal and Output Injection Through Unsanitized GPX Trackpoint Names
- Content
View full analysis
Vulnerability Details
File Location:
mountain_weather.py:53-57, with unsafe propagation and rendering atmountain_weather.py:653andmountain_weather.py:753-757
Vulnerability Type: Terminal/output injection through untrusted GPX metadata
Risk Level: MediumVulnerable Code
python name_el = trkpt.find("gpx:name", ns) name = name_el.text if name_el is not None else None points.append({"lat": lat, "lng": lng, "ele": ele, "name": name})The untrusted value is subsequently copied into the result:
python result_entry = { "name": pt.get("name") or f"{round(pt['ele'] * 3.281):,} ft", "elevation_m": round(pt["ele"]), "elevation_ft": round(pt["ele"] * 3.281), "arrival_hour": arrival_hour, "arrival_time": f"{arrival_hour:02d}:00", "cumulative_hr": round(pt["cumulative_hr"], 1), "weather": weather, }It is then rendered directly to the terminal:
python for r in results: w = r.get("weather") if not w: print(f"{r['arrival_time']:<8} {r['name']:<25} {r['elevation_ft']} ft [no data]") continue wind_str = f"{w['wind_mph']:.0f} ({w['wind_gust_mph']:.0f}g) mph" precip_str = f"{w['precip_in']:.2f}\"" if w['precip_in'] else "0" print(f"{r['arrival_time']:<8} {r['name']:<25} {r['elevation_ft']:<7} ft {w['temp_f']:<6.0f}°F {wind_str:<14} {w['cloud_pct']:<6}% {precip_str}")Technical Analysis
A GPX file is an untrusted input format and its
<name>elements can contain attacker-controlled text. The parser accepts that text without removing ANSI escape sequences, terminal control characters, carriage returns, line feeds, or other non-printable characters. The value is then interpolated directly into terminal output.A crafted name can therefore alter the visible terminal state, overwrite or conceal output, create forged forecast rows, or display misleading safety information. The same value is included in JSON output, where it may affect downstream consumers t ...[truncated 1918 chars]
- Remediation
View remediation
Remediation Suggestions
Treat every GPX-derived text field as untrusted and sanitize it immediately after parsing and again at presentation boundaries.
- Remove ANSI escape sequences and C0/C1 terminal control characters.
- Replace carriage returns, line feeds, and tabs with safe spaces.
- Apply Unicode normalization where appropriate.
- Enforce a conservative maximum length for waypoint names.
- Use a visible fallback when a sanitized name becomes empty.
- Keep structured output as data and ensure downstream renderers do not interpret control sequences.
- Add tests covering escape characters, multiline names, oversized values, bidirectional Unicode controls, and malformed metadata.
For example:
python import re import unicodedata ANSI_ESCAPE_RE = re.compile( r"(?:\x1B[@-_][0-?]*[ -/]*[@-~])|(?:\x9B[0-?]*[ -/]*[@-~])" ) CONTROL_RE = re.compile(r"[\x00-\x1F\x7F-\x9F]") BIDI_CONTROL_RE = re.compile(r"[\u202A-\u202E\u2066-\u2069]") def sanitize_display_text(value, max_length=120): if value is None: return None value = unicodedata.normalize("NFKC", str(value)) value = ANSI_ESCAPE_RE.sub("", value) value = BIDI_CONTROL_RE.sub("", value) value = value.replace("\r", " ").replace("\n", " ").replace("\t", " ") value = CONTROL_RE.sub("", value) value = " ".join(value.split()) return value[:max_length] or NoneApply it while parsing:
python name_el = trkpt.find("gpx:name", ns) raw_name = name_el.text if name_el is not None else None name = sanitize_display_text(raw_name) points.append({"lat": lat, "lng": lng, "ele": ele, "name": name})As defense in depth, sanitize or escape names again before terminal rendering. Clearly delimit untrusted names in output so they cannot visually merge with trusted forecast fields.
