T05 · Unauthorized Access and Privilege Escalation
- Location
handoff_reader.py:28- Finding
Path Traversal Through Unvalidated Date Argument
- Content
View full analysis
Vulnerability Details
File Location:
handoff_reader.py, lines 28–34
Vulnerability Type: Path traversal and unauthorized local file read
Risk Level: MediumVulnerable Code
python daily_file = HANDOFF_DIR / f"{date_str}.jsonl" if not daily_file.exists(): print(f"[WARN] File not found: {daily_file}") return [] handoffs = [] with open(daily_file, 'r', encoding='utf-8') as f:The displayed warning text above is an English rendering of the original message; the executable path construction and file-opening logic are unchanged.
Technical Analysis
The
date_strvalue originates from the command-line--dateargument and is inserted directly into a filesystem path. The program does not verify that the value is a valid date or that the resolved file remains insideHANDOFF_DIR.pathlib.Pathdoes not automatically prevent traversal. A value containing parent-directory components can therefore escape~/.sumo/handoffs. The mandatory.jsonlsuffix limits accessible targets to filenames ending in that suffix, and the selected file must contain valid line-delimited JSON for parsing to succeed, but these restrictions do not eliminate the unauthorized read.For example, a value conceptually equivalent to
../../shared/recordscauses the application to attempt to open:text ~/.sumo/handoffs/../../shared/records.jsonlAttack Path
- An attacker obtains the ability to supply or influence the
--datecommand-line argument. - The attacker provides a value containing
../traversal components. - The application combines that value with
HANDOFF_DIRand appends.jsonl. - The operating system resolves the traversal components to a location outside the intended handoff directory.
- If the target is readable and contains compatible JSONL records, the application parses it.
- Selected values from those records are printed to standard output.
Impact A
...[truncated 573 chars]
- An attacker obtains the ability to supply or influence the
- Remediation
View remediation
Remediation Suggestions
- Parse the input with
datetime.strptime(date_str, "%Y-%m-%d")and reject invalid values. - Reformat the parsed date into a canonical
YYYY-MM-DDstring before constructing the filename. - Resolve both the base directory and candidate file, then verify that the candidate is a descendant of the base directory.
- Reject path separators, parent-directory components, absolute paths, and unexpected filename characters.
- Handle malformed JSON and file-access failures without exposing unnecessary filesystem details.
Example hardening approach:
python def validate_date(date_str): parsed = datetime.strptime(date_str, "%Y-%m-%d") return parsed.strftime("%Y-%m-%d") safe_date = validate_date(date_str) base = HANDOFF_DIR.resolve() daily_file = (base / f"{safe_date}.jsonl").resolve() if daily_file.parent != base: raise ValueError("The requested file is outside the handoff directory")- Parse the input with
