[SECURITY] Path Traversal in upload_temp_docs allows arbitrary file write to any directory (CWE-22, CVSS 8.1)
Langchain-Chatchat upload_temp_docs Path Traversal Arbitrary File Write
Summary
The upload_temp_docs endpoint's filename parameter has no fixed subdirectory constraint. os.path.join(dir, filename) discards the base dir when encountering an absolute path, allowing attackers to write arbitrary content files to any directory such as /etc/cron.d/, /root/.ssh/, or /tmp/. In environments with cron or SSH, this directly leads to RCE or SSH persistence. No authentication required, end-to-end HTTP.
Affected Version & Commit
- Repository: https://github.com/chatchat-space/Langchain-Chatchat
- Branch: master
- Commit: 49165d6af4438aa7e8a1f71ce276db55f4405151
Vulnerability Description
The upload_temp_docs endpoint accepts multipart file uploads where the filename parameter is directly used in os.path.join(dir, filename) path construction. Python's os.path.join discards the prefix path (base dir) when encountering an absolute path argument, so an attacker only needs to pass an absolute path as the filename (e.g., /etc/cron.d/evil) to write the uploaded file to any directory on the server, with fully controllable content.
Verified 7 different root partition locations (/tmp, /var/tmp, /opt, /srv, /usr/local, /etc/cron.d, /root/.ssh) — all written successfully.
Affected Endpoints
POST /knowledge_base/upload_temp_docs(port 7861)
Exploitation Conditions
- Exploitable with default configuration, no authentication required
Proof of Concept
# Step 1: POST upload_temp_docs, filename uses absolute path (os.path.join discards base dir on absolute path)
curl -s -X POST http://127.0.0.1:7861/knowledge_base/upload_temp_docs \
-F "files=@-;filename=/tmp/entry017_marker.txt" <<< "PWNED_E017"
# Expected: {"code":200,...} ← file lands at /tmp/entry017_marker.txt instead of upload_dir
# Step 2: Read back to verify
docker exec chatchat-local cat /tmp/entry017_marker.txt
# Expected: PWNED_E017 ← arbitrary directory write + content control confirmedVerified 7 different root partition locations (/tmp, /var/tmp, /opt, /srv, /usr/local, /etc/cron.d, /root/.ssh) — all written successfully, closed-loop match.
Impact
An unauthenticated attacker can write arbitrary content files to any directory on the server. In environments with cron or SSH, writing to /etc/cron.d/evil directly achieves RCE, and writing to /root/.ssh/authorized_keys achieves SSH persistence.
Severity
CVSS v3.1: 8.1 (High) Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:N CWE: CWE-22 (Path Traversal)
Credit
- Jiecub3 (GitHub ID: 87791178)
- Aur0ra-m (GitHub ID: 103031059)
- lz2y (GitHub ID: 55266300)
Source: chatchat-space/Langchain-Chatchat