Security: Symlink Path Traversal in /view endpoint allows arbitrary file read
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
## Summary
The `/view` endpoint in `server.py` is vulnerable to symlink-based path traversal, allowing an attacker to read arbitrary files from the server filesystem.
## Vulnerability Details
**Type:** CWE-59 (Improper Link Resolution Before File Access)
**Severity:** High
**Location:** `server.py:496-500`
## Root Cause
The path validation using `os.path.commonpath()` occurs **before** symlink resolution. An attacker can create a symlink in an allowed directory (input/output/temp) pointing to any location on the filesystem, then access files through that symlink.
## Vulnerable Code
```python
if "subfolder" in request.rel_url.query:
full_output_dir = os.path.join(output_dir, request.rel_url.query["subfolder"])
if os.path.commonpath((os.path.abspath(full_output_dir), output_dir)) != output_dir:
return web.Response(status=403)
output_dir = full_output_dir # Symlink resolved AFTER validation
```
## Steps to Reproduce
1. Create a symlink in the input directory: `ln -s /etc input/etc_link`
2. Request: `GET /view?filename=passwd&subfolder=etc_link&type=input`
3. Contents of `/etc/passwd` are returned
## Impact
- Read any file accessible to the ComfyUI process
- Exfiltrate SSH keys, cloud credentials, application secrets
- System reconnaissance for further attacks
## Suggested Fix
Resolve symlinks BEFORE validation:
```python
if "subfolder" in request.rel_url.query:
full_output_dir = os.path.join(output_dir, request.rel_url.query["subfolder"])
real_output_dir = os.path.realpath(full_output_dir)
real_base = os.path.realpath(output_dir)
if not real_output_dir.startswith(real_base + os.sep):
return web.Response(status=403)
```
I'm happy to submit a PR with the fix if this is confirmed.
Contributor guide
Assessment
This issue has not been assessed yet.