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.
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.
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.
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: