All posts

Agent security

How Can I Prevent AI Agents from Running Unauthorised or Risky SQL?

Constrain coding-agent SQL with scoped database roles, separate migration authority and synthetic acceptance cases for targets, writes and data transfers.

Harden cover: Risky SQL Agent Access. A right-column task to role to database diagram illustrates the workflow. Conceptual editorial artwork; no measured result is depicted.

Executive summary

Prevent risky SQL by restricting the database identity, separating migration authority from diagnostic access, and checking the target and effect of the proposed operation. A prompt that says “read only” does not remove write privileges. Our worked example follows a staging support task through a database tool, a migration proposal, and the evidence needed before release. The example is illustrative; it is not a report of a production incident or a measured Harden database benchmark.

Give the task a database boundary

Suppose an engineer asks a coding agent to explain why yesterday's staging checkout tests failed. The agent needs enough information to inspect test orders and reproduce a calculation. It does not need to delete orders, change database roles, or connect to production.

Write that distinction into an access record: staging database, synthetic customer records, approved tables, permitted reads, and a named owner. Include the database endpoint and identity. A connection called “development” can still point at the wrong host after a configuration change.

The important object is the effective database session. A read-only tool description is weak evidence if the server authenticates with an administrator credential. Inspect how the connection obtains credentials and which role the database actually uses. Check inherited privileges, ownership, and callable routines as part of that review.

Constrain SQL where it executes

PostgreSQL's GRANT documentation distinguishes privileges for tables, schemas, sequences, routines, and other database objects. Those distinctions matter when configuring an agent's role. Permission to read one table does not describe the authority of every function available to that session.

For the staging diagnostic task, ask the database owner to provide an identity with the necessary read access and without migration or role-administration authority. Review future-object defaults as well as today's grants. A safe-looking snapshot can change when a new schema or routine becomes available.

Avoid presenting a single SQL snippet as a universal read-only configuration. PostgreSQL roles, object ownership, inherited privileges, and security-definer routines require environment-specific review. Record the expected boundary and have its owner verify effective permissions in the disposable test environment before connecting the agent.

Read access also has consequences. A query can disclose sensitive rows or consume resources even when it changes nothing. Limit available datasets, execution time, and result size through the controls your database supports. Use synthetic records for the exercise and review the destinations that receive query results.

Keep migration authority separate

The agent may discover that a schema change would fix the test failure. That finding should produce a reviewable migration proposal. It should not silently promote the diagnostic credential into a deployment credential.

An ordered workflow makes the decision visible. The agent writes a migration in a branch. A maintainer reviews its purpose and affected objects. CI checks the change against a disposable database. The release owner then authorizes execution through the established migration process.

Consider destructive operations beyond a familiar DELETE statement. A schema replacement, broad UPDATE, cascading change, or privileged routine can cause damage through a different expression. Review the intended effect and the executing identity, rather than relying on a list of prohibited SQL words.

Transaction rollback is useful during testing, but it is not the whole recovery plan. External effects, separately committed operations, or work performed through another connection require their own analysis. Confirm backups and restoration procedures before authorizing consequential changes to real data.

Test both the useful operation and the prohibited variation

Build a small acceptance matrix for the actual tool path. A staging SELECT against synthetic orders should succeed. An attempted write through the diagnostic identity should fail. A request directed at the production endpoint should remain outside the assignment. A migration proposal should be saved for review rather than executed using the support credential.

For each case, record the agent version, server or connector, database identity, target endpoint, proposed operation, expected decision, and observed database effect. A tool's “blocked” message and an unchanged target are different pieces of evidence. Inspect both where possible.

Repeat the same cases through any alternate route the team permits. A terminal client and an MCP database tool may use different credentials or controls. A passing terminal exercise does not establish the MCP route's behavior. Include the route in the final coverage statement.

If a legitimate query is rejected, resolve the narrow permission or policy mismatch. Broadly widening the role to finish the exercise destroys the boundary the exercise was meant to evaluate. Record necessary exceptions with an owner and expiry, then test the prohibited variation again.

Review SQL text and the data it carries

An authorized query can become an unauthorized transfer when the agent copies its results into a ticket, diagnostic attachment, or external tool. Database permissions govern the query. They do not decide every later recipient of the result.

Follow the result across the workflow: rows returned to the connector, content presented to the agent, local files created, and outbound messages proposed. Preserve useful evidence without copying real customer data into a security worksheet. Synthetic identifiers and controlled recipients are sufficient for an acceptance exercise.

This is also where task context helps. A request to summarize staging test failures does not authorize uploading a full customer export. The database role may permit the read; the recipient and purpose still need a separate decision.

Add a supported action check without overstating coverage

Harden evaluates supported agent calls before execution against relevant task context and policy. For this workflow, the evaluation question is concrete: does the configured integration observe the pending database-related call, and what context does it receive about the operation and destination?

Verify that path before describing the database as protected. Harden does not replace database roles, migration review, backup procedures, or authorization inside the application. A check on one supported call also does not establish visibility into independently executed database activity.

Use our destructive-command guide for the general control design. For a scoped evaluation, bring the staging identity and acceptance matrix to Control. If the connection architecture needs review, Book a Call with the endpoint, tool route, and permitted operations ready.

FAQs

Is telling the agent to use read-only SQL enough?

No. The instruction describes intended behavior; the database identity determines available authority. Verify effective privileges and the actual connection target. Keep the instruction as task context, while using database and execution controls to constrain consequential operations.

Should every agent be prevented from writing to a database?

Choose authority by workflow. A diagnostic session may need only reads, while an approved migration workflow needs tightly controlled writes. Separate those identities and release decisions. A broad prohibition can obstruct legitimate work without providing an appropriate process for authorized changes.

Does a sandbox prevent destructive SQL?

A sandbox can constrain the process and its reachable resources. If a database endpoint and powerful credential remain available, evaluate their authority separately. The remote database enforces its own privileges; containing the local process does not automatically make remote operations read only.

How should we test production restrictions?

Use a controlled environment, synthetic data, and a test designed with the database owner. Avoid destructive production probes. Confirm configuration and authority through approved inspection, then preserve evidence showing which target and identity the agent actually used during the bounded exercise.

What should trigger another review?

Recheck after changes to the agent, database connector, credentials, grants, schema ownership, or migration path. Preserve the previous matrix and compare outcomes. A new route or inherited privilege can change effective authority even when the original prompt and tool name stay the same.