Detect and sanitize risky SQL patterns in LLM outputs using TrueFoundry’s built-in SQL Sanitizer guardrail.
This guide explains how to use TrueFoundry’s built-in SQL Sanitizer guardrail to detect and handle potentially dangerous SQL patterns in LLM interactions.
What is SQL Sanitizer?
SQL Sanitizer is a built-in TrueFoundry guardrail that identifies and handles risky SQL patterns in text content. It can detect destructive SQL statements, unsafe query patterns, and potential SQL injection vectors. The guardrail runs directly within the AI Gateway without requiring external API calls.
When Require Parameterization is enabled, the guardrail detects potential SQL injection vectors by looking for string interpolation patterns:
Pattern Type
Detection
Example
String Concatenation
+ operator
"SELECT * FROM " + table_name
Python % Formatting
%. pattern
"SELECT * FROM %s" % table
Template Literals / F-strings
{...} braces
f"SELECT * FROM {table}"
Interpolation detection is heuristic-based and may have false positives (e.g., JSON data with braces). Enable this option only when you specifically need to enforce parameterized queries.
SQL Comment Patterns
The guardrail detects and can strip SQL comments:
Comment Type
Pattern
Example
Line Comment
-- ...
SELECT * FROM users; -- admin bypass
Block Comment
/* ... */
SELECT * /* hidden */ FROM users
Comments can be used to hide malicious SQL. Enable Strip SQL Comments to remove them, especially when processing user-provided or LLM-generated SQL.
How It Works
Recursively scans all string content (including nested objects/arrays)
Strips comments (if enabled) before checking patterns
Checks for blocked statements and missing WHERE clauses
Returns verdict and optionally sanitized content (up to 10 issues per request)
When configured in validate mode, the guardrail blocks requests containing risky patterns.
Blocked: DROP statement
DROP TABLE users;
Result: Blocked - blocked_statement: Blocked SQL statement detected: DROP
Blocked: DELETE without WHERE
DELETE FROM orders;
Result: Blocked - delete_without_where: DELETE without WHERE clause detected
Allowed: Safe SELECT query
SELECT * FROM users WHERE id = 1;
Result: Allowed - No SQL issues detected
Allowed: DELETE with WHERE
DELETE FROM orders WHERE created_at < '2024-01-01';
Result: Allowed - DELETE has WHERE clause
Mutate Mode
When configured in mutate mode, the guardrail:
Strips SQL comments from the output (line comments -- and block comments /* */)
Logs all detected issues but allows the request to continue
Returns sanitized content with comments removed
Sanitized: Line comment removed
Input:
SELECT * FROM users; -- bypass authentication
Output:
SELECT * FROM users;
Issues logged: Comment stripped (no blocking issue)
Sanitized: Block comment removed
Input:
SELECT * /* admin */ FROM users WHERE role = 'user'
Output:
SELECT * FROM users WHERE role = 'user'
Flagged but allowed: DROP in mutate mode
Input:
DROP TABLE temp_data;
Output: Same as input (not modified) Issues logged: blocked_statement: Blocked SQL statement detected: DROP Verdict: Allowed (mutate mode doesn’t block)
Use mutate mode carefully: In mutate mode, even dangerous statements like DROP are logged but not blocked. Use this mode only when you have additional safeguards in place or need to sanitize comments while monitoring for issues.
Use Cases
Text-to-SQL Applications
When building applications that convert natural language to SQL, apply SQL Sanitizer on MCP Pre Tool to validate queries before they’re executed by the database tool:
type: gateway-guardrails-configname: text-to-sql-safetywhen: target: operator: or conditions: mcpServers: values: - database-tools condition: in mcpTools: values: - execute_query condition: in subjects: operator: and conditions: in: - team:data-engineering not_in: - user:db-admin@example.comllm_input_guardrails: []llm_output_guardrails: []mcp_tool_pre_invoke_guardrails: - my-guardrail-group/sql-sanitizermcp_tool_post_invoke_guardrails: []
This ensures dangerous SQL (DROP, TRUNCATE, DELETE without WHERE) is blocked before reaching the database.
Input Validation
Apply SQL Sanitizer to user inputs to detect potential SQL injection attempts: