T09 · Insecure Skill Coding Practices
- Location
fetch_history.py:133- Finding
User-Controlled Path Traversal and File Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
fetch_history.py, lines 133-145
Vulnerability Type: Path traversal and unrestricted file placement
Risk Level: MediumVulnerable Code
python output_path = Path(output_dir) output_path.mkdir(parents=True, exist_ok=True) # 保存为 JSON output_file = output_path / f"{inst_id}_{bar}_{days}d.json" with open(output_file, 'w') as f: json.dump(all_candles, f, indent=2) print(f"已保存:{output_file}") # 保存为 CSV csv_file = output_path / f"{inst_id}_{bar}_{days}d.csv" with open(csv_file, 'w') as f: f.write("timestamp,open,high,low,close,volume\n")The filename components originate from command-line arguments without validation:
python parser.add_argument('--symbol', '-s', required=True, help='品种 ID (如 BTC-USDT-SWAP)') parser.add_argument('--bar', '-b', required=True, help='周期 (1m, 5m, 1H, 4H, 1D 等)') parser.add_argument('--days', '-d', type=int, default=365, help='采集天数') parser.add_argument('--output', '-o', default='./output', help='输出目录')Technical Analysis
The
inst_idandbarvalues are incorporated directly into output filenames. Neither value is restricted to an expected character set, andbaris not checked against the existingBAR_SECONDSallowlist.An attacker who controls the invocation can supply path separators,
..components, or an absolute path. When an absolute child path is combined with apathlib.Path, it can replace the intended base directory. Traversal components can similarly cause the resolved destination to leave the configured output directory.Both files are opened in
wmode. Consequently, a destination that already exists is truncated and replaced. The attacker cannot choose a completely unrestricted final filename because the program appends components such as_{bar}_{days}d.jsonor.csv, but the attacker can still target compatible filenames and create files in unintended directories where ...[truncated 1661 chars]- Remediation
View remediation
Remediation Suggestions
-
Validate
--symbolwith a strict allowlist appropriate for OKX instrument identifiers, such as uppercase letters, digits, and hyphens:python import re if not re.fullmatch(r'[A-Z0-9-]+', args.symbol): parser.error('--symbol contains invalid characters') -
Require
--barto match a supported interval:python parser.add_argument( '--bar', '-b', required=True, choices=sorted(BAR_SECONDS), help='Supported candle interval' ) -
Reject absolute paths, path separators, null characters, and
.or..path components in every filename-derived argument. -
Resolve and verify each destination before opening it:
python output_root = Path(output_dir).expanduser().resolve() output_root.mkdir(parents=True, exist_ok=True) output_file = ( output_root / f"{inst_id}_{bar}_{days}d.json" ).resolve() if output_root not in output_file.parents: raise ValueError('Output file escapes the configured directory') -
Apply the same containment check independently to both JSON and CSV paths.
-
Where overwriting is not required, use exclusive creation mode (
x) or require explicit overwrite authorization. Run the collector under a least-privileged account with access limited to its designated data directory.
-
