Llm Data Leakage in Fiber with Cockroachdb
Llm Data Leakage in Fiber with Cockroachdb — how this specific combination creates or exposes the vulnerability
When building a Fiber API that uses CockroachDB as the authoritative data store, an LLM data leakage risk arises when application code unintentionally exposes sensitive data or system behavior through prompts, logs, or error messages consumed or generated by an integrated LLM endpoint. In this stack, a handler may query CockroachDB for user-specific records and then pass raw query results or database metadata into an LLM client call. If the LLM endpoint is unauthenticated or improperly sandboxed, an attacker may coax the system to reveal training data, internal queries, or database schema details via prompt injection or output extraction techniques.
Consider a scenario where an endpoint accepts a natural language query, converts it to a SQL statement, executes it against CockroachDB, and forwards the resulting rows to an LLM to produce a friendly response. If the LLM processing stage does not sanitize or limit the context window, sensitive columns (such as email, role, or internal identifiers) can appear in LLM responses that are returned to the caller or logged by the assistant. Because CockroachDB often serves distributed workloads with multi-region replication, a misconfigured driver or ORM could also expose connection strings or node metadata, increasing the surface for inference about cluster topology.
The LLM/AI Security checks in middleBrick specifically test for this class of risk by scanning unauthenticated endpoints that include LLM calls. It checks for system prompt leakage using 27 regex patterns tailored to ChatML, Llama 2, Mistral, and Alpaca formats, and runs active prompt injection probes such as system prompt extraction, instruction override, DAN jailbreak, data exfiltration, and cost exploitation. When Fiber handlers stream or return LLM outputs that contain PII, API keys, or executable code, these outputs are flagged. Excessive agency detection also reviews whether the application relies on tool_calls or function_call patterns that could allow an attacker to amplify database actions through the LLM.
Because the Fiber framework does not impose a mandatory schema for separating database access from LLM context, developers must explicitly validate and sanitize data before it enters the LLM pipeline. Without such controls, an attacker who can influence input parameters may cause the handler to request sensitive CockroachDB rows and inadvertently include them in prompts, leading to data exposure through the LLM response. middleBrick’s scan can surface this by correlating OpenAPI/Swagger definitions (with full $ref resolution) against runtime behavior, highlighting endpoints where database-driven context reaches an LLM endpoint without sufficient filtering or redaction.
Cockroachdb-Specific Remediation in Fiber — concrete code fixes
To mitigate LLM data leakage in a Fiber application that uses CockroachDB, structure your handlers to enforce strict separation between database access, data minimization, and LLM interaction. Apply principle of least privilege to database permissions, avoid returning raw rows or schema metadata to the LLM context, and sanitize outputs that traverse the LLM pipeline.
Example secure handler in Go using Fiber and the CockroachDB pgx driver:
// secure_handler.go
package handlers
import (
"context"
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5/pgxpool"
)
type SafeResponse struct {
Summary string `json:"summary"`
}
func MakeSafeHandler(pool *pgxpool.Pool) fiber.Handler {
return func(c *fiber.Ctx) error {
ctx := c.Context()
userID := c.Params("user_id")
var email string
// Explicitly select only required, non-sensitive columns
err := pool.QueryRow(ctx, "SELECT email FROM users WHERE id = $1 AND tenant_id = $2", userID, c.Locals("tenant_id")).Scan(&email)
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "user not found"})
}
// Do NOT pass raw rows or internal identifiers to the LLM
// Instead, pass a sanitized summary or computed value
summary := &SafeResponse{Summary: "request processed for user " + email}
body, _ := json.Marshal(summary)
// If calling an LLM endpoint, ensure the context is cleaned
// Example: client.Post(...)
// Include only necessary data in the prompt, redact sensitive fields
return c.JSON(fiber.Map{"data": string(body)})
}
}
Database-side controls in CockroachDB further reduce leakage risk. Use row-level security (RLS) and well-defined roles so that even if an application credential is over-privileged, the database returns only tenant-scoped rows:
-- cockroach_safe_setup.sql
CREATE TABLE users (
id UUID PRIMARY KEY,
email STRING,
tenant_id UUID,
role STRING
);
CREATE POLICY tenant_isolation ON users
USING (tenant_id = current_setting('app.tenant_id', true)::UUID);
CREATE ROLE app_reader;
GRANT SELECT ON users TO app_reader;
REVOKE ALL ON users FROM PUBLIC;
Instrument your Fiber app to scrub logs and error messages that may include SQL snippets or internal paths before they reach any LLM-related logging layer. Avoid including database metadata in prompts, and validate that any tool-calling or function-call usage does not expose raw query results. With these measures, the combination of Fiber, CockroachDB, and optional LLM integrations remains safer against data leakage while preserving functionality.
Related CWEs: llmSecurity
| CWE ID | Name | Severity |
|---|---|---|
| CWE-754 | Improper Check for Unusual or Exceptional Conditions | MEDIUM |