T09 · Insecure Skill Coding Practices
- Location
sub-skills/etf-pcf-download/scripts/handler.py:14- Finding
Symlink-Based Arbitrary File Overwrite in PCF Download Handler
- Content
View full analysis
str: """将 output 规范为绝对路径,并限制在 base_dir 内,防止路径遍历。""" base_dir = (base_dir or os.getcwd()).rstrip(os.sep) base_abs = os.path.abspath(base_dir) resolved = os.path.abspath(os.path.normpath(path)) if os.path.commonpath([base_abs, resolved]) != base_abs: print( json.dumps({"error": "output path must be under base directory", "base": base_abs}, ensure_ascii=False), file=sys.stderr, ) sys.exit(1) return resolved ``` ```python if args.output is not None: out_path = _safe_output_path(args.output) with open(out_path, "wb") as f: f.write(data) ``` ### Technical Analysis The `_safe_output_path()` function verifies that the normalized lexical path is under the current working directory. However, `os.path.abspath()` and `os.path.normpath()` do not resolve symbolic links. The subsequent `open(out_path, "wb")` operation follows symbolic links by default. Therefore, an output path that appears to reside inside the permitted directory can resolve through a pre-existing symlink to a file outside that directory. Opening the target with `"wb"` immediately truncates an existing file before writing the downloaded PCF response. The containment check consequently does not enforce its documented security boundary against symlink-based escapes. Exploitation requires the attacker to: - Create or control a symbolic link within the working directory; and - Influence the `--output` argument, or predict an output filename that will be used. ### Attack Path 1. The attacker creates a symbolic link inside the permitted working directory: ```bash ln -s /path/to/writable/target output.xml ``` ...[truncated 1420 chars]- Remediation
View remediation
