Skip to content

Secure Text-to-SQL: why generated SQL needs a policy boundary

An LLM can generate syntactically valid SQL that violates an application's authorization rules. Treat generated queries as untrusted input, even when the model is instructed to produce only SELECT statements.

SQLGuardLLM is an open-source SQL policy engine and MCP server. Its AST-based approach enables structural validation and query rewriting rather than relying solely on keyword matching.

Defense in depth

Use a least-privilege database account, explicit table and column allowlists, trusted row-filter context, query limits, timeouts and audit logging. Keep authorization context outside model control. Never rely on a prompt or SQL parser as the sole security boundary.

Try the MCP integration

Follow the MCP setup guide to configure a local synthetic database and policy. Install the MCP extra from a release that includes it, or use the documented source installation. Run the demo with a trusted policy and read-only database account. Test allowed reads, denied writes and restricted columns before connecting production data.

SQLGuardLLM is alpha software. Evaluate dialect coverage, policy behavior and database permissions for your own deployment. No security guarantee or benchmark claim is implied.

Source: https://github.com/jmmana/sqlguardllm