T09 · Insecure Skill Coding Practices
- Location
roic_calc.py:506- Finding
Unsanitized stock codes can escape intended output directories
- Content
View full analysis
Vulnerability Details
File Location:
roic_calc.py:506-513;roic_fetch.py:590-622
Vulnerability Type: Path traversal through user-controlled output filenames
Risk Level: MediumVulnerable Code
roic_calc.py:506-513:python company = ts_code.split(".")[0] filename = f"{company}_ROIC穿透分析_{results[0]['year']}-{results[-1]['year']}.md" workspace = os.environ.get('OPENCLAW_WORKSPACE', os.path.expanduser('~/.openclaw/workspace')) output_dir = os.path.join(workspace, 'output', 'roic') os.makedirs(output_dir, exist_ok=True) filepath = os.path.join(output_dir, filename) with open(filepath, "w", encoding="utf-8") as f: f.write(report)roic_fetch.py:590-622:python DATA_DIR = 'c:/Users/Corey/WorkBuddy/Claw/data/sec_filings' def main(): if len(sys.argv) < 3: print("用法: python roic_fetch.py <股票代码> <年份范围>") print(" A股: python roic_fetch.py 600426.SH 2021-2024") print(" 美股: python roic_fetch.py AAPL 2021-2024") print(" 港股: python roic_fetch.py 09988.HK 2021-2024") sys.exit(1) code = sys.argv[1] years_spec = " ".join(sys.argv[2:]) years = parse_years(years_spec) market = detect_market(code) print(f'[ROIC] {code} → 市场: {market}, 年份: {years[0]}-{years[-1]}') # 确保数据目录存在 os.makedirs(DATA_DIR, exist_ok=True) # ... output_file = os.path.join(DATA_DIR, f'{code}_{years[0]}-{years[-1]}_raw.json') with open(output_file, 'w', encoding='utf-8') as f: clean_results = [] for r in results: clean = {k: v for k, v in r.items() if not k.startswith('raw_')} clean_results.append(clean) json.dump(clean_results, f, indent=2, ensure_ascii=False, default=str)Technical Analysis
Both scripts derive output filenames directly from command-line stock codes without rejecting path separators, absol ...[truncated 2167 chars]
- Remediation
View remediation
Remediation Suggestions
- Validate stock codes using strict, market-specific allowlists before making requests or constructing paths. For example:
- A-share:
^[0-9]{6}\.(SH|SZ)$ - Hong Kong:
^[0-9]{4,5}\.HK$ - US:
^[A-Z][A-Z0-9.-]{0,9}$, followed by separate filename normalization.
- A-share:
- Reject identifiers containing
/,\,.., drive prefixes, null bytes, or other filesystem metacharacters. - Convert the identifier to a dedicated safe filename component rather than using the original input.
- Resolve and verify the destination before writing:
python base = os.path.realpath(output_dir) target = os.path.realpath(os.path.join(base, safe_filename)) if os.path.commonpath([base, target]) != base: raise ValueError("Output path escapes the designated directory") - Use a configurable workspace directory instead of the developer-specific hardcoded path in
roic_fetch.py. - Open new output files with exclusive creation mode where overwriting is not required.
- Add tests covering absolute paths, parent traversal, Windows drive paths, mixed separators, and malformed market identifiers.
- Validate stock codes using strict, market-specific allowlists before making requests or constructing paths. For example:
