Unauthenticated Path Traversal in Khoj
An unauthenticated path traversal vulnerability in the Khoj /home/ endpoint allows remote attackers to read arbitrary files from the server filesystem.
The Khoj application contains an unauthenticated path traversal vulnerability within the /home/ endpoint, defined in 'src/khoj/routers/web_client.py'. This endpoint is intended to serve static files from a specific directory but fails to perform path normalization or validation before resolving user-supplied input. An attacker can append directory traversal sequences (such as ../) to the URI to escape the intended directory and access sensitive files on the host filesystem. Because the endpoint lacks authentication decorators, exploitation is possible by any unauthenticated attacker with network access to the Khoj instance. This flaw affects Khoj versions from 2.0.0-beta.23 to 2.0.0-beta.25.
Attack Chain
- Attacker performs network reconnaissance to identify a reachable Khoj instance.
- Attacker interacts with the /home/ endpoint via an HTTP GET request.
- Attacker crafts a malicious URI containing path traversal sequences (e.g., ../../../etc/passwd).
- The application logic in 'web_client.py' concatenates the malicious path to the base directory without validation.
- The underlying operating system resolves the traversal sequences to a target file path outside the web directory.
- The 'FileResponse' object retrieves the content of the unintended file.
- The application returns the contents of the requested file in the HTTP response body, leading to information disclosure.
Impact
Successful exploitation allows unauthenticated remote attackers to read any file on the server accessible to the application process. This includes sensitive data such as database credentials, API keys, application secrets (e.g., Django SECRET_KEY), and system-level files like '/etc/passwd' or '/proc/self/environ'. Access to these files can lead to complete service compromise, facilitate further lateral movement, or allow the attacker to gain persistent unauthorized access to the environment.
Recommendation
- Upgrade to a version of Khoj patched against this vulnerability (>= 2.0.0-beta.25).
- Implement a WAF or reverse proxy rule to block or sanitize incoming HTTP requests containing directory traversal sequences (e.g., ../) in the /home/ URI path.
- Apply the suggested code-level patch to 'src/khoj/routers/web_client.py' by adding path resolution and validation logic that verifies the requested file remains within the intended base directory.
- Deploy the suggested Sigma rule to monitor for suspicious traversal patterns in web server logs.
Immediate actions
Upgrade Khoj to version 2.0.0-beta.25 or later
Mitigations
Block requests to /home/ containing ../ using a WAF
Path traversal exploitation
Detection coverage 1
Detect Path Traversal Attempt in /home/ Endpoint
highDetects path traversal attempts directed at the vulnerable Khoj /home/ endpoint by looking for dot-dot-slash patterns.
Detection queries are available on the platform. Get full rules →