SI Agent News
DevelopingAgentsSecurity

A community blueprint for keeping AI agents from leaking data out of AlloyDB

A dev.to guide shows how to stop a tricked AI agent from reading other customers' data in AlloyDB, using fixed queries and database-enforced identity.

Sources: 1 outlet · First: DEV Community

17 or laterPostgreSQL version the guide's setup requires

The short version

Why it matters

An agent wired straight to a database is only as safe as the permissions it holds, and a prompt-injection attack can talk it into misusing them. This guide shows a layered pattern in which the model never writes raw SQL and never chooses whose data it sees. Even if you never use AlloyDB, the checklist transfers to any agent that touches live business data.

Why a SELECT role is not safe

The guide starts with a familiar demo: a customer-support or fraud-investigation agent gets a database role that can read an orders or claims table, and the AI model writes SQL on the fly. That works in a notebook. In production, the author says, security and operations teams both block it.

Security worries about prompt injection, which means hiding instructions in text the model reads. The attack can be typed directly by a user or buried in a row of an ingested vendor feed. A successful one could push the model to pull every tenant's personal data or to drop database objects.

The guide cites the Google Cloud Data Agent Kit curriculum for a useful comparison. A human administrator might make one destructive mistake a year, while an agent with the same permission could be tricked into one every minute.

Operations has a different fear. An unthrottled agent can run huge table scans or open so many connections that live transactions stall. Copying data to a separate warehouse does not fix this, because agents need current operational data.

Lock one: a menu of fixed queries

The first defense is MCP Toolbox for Databases, an open-source server. Rather than hand the model a tool that runs any SQL string, you describe approved actions in a configuration file called tools.yaml. It has three parts: sources (which databases exist), tools (pre-written, parameterized SQL statements), and toolsets (named bundles of tools).

The model reads each tool's description, picks one, and supplies only the argument values. One Toolbox server can serve several agents, and each loads only the toolset it is meant to have. That gives role-based access at the tool level.

Database passwords stay out of container images. The guide pulls them from Secret Manager, Google's store for credentials, and injects them at runtime. The Toolbox itself runs on Cloud Run, Google's managed app-hosting service, with internal-only ingress and no unauthenticated access.

Lock two: the database enforces the tenant

Fixed queries alone do not say whose data comes back. For that, the guide uses AlloyDB's parameterized secure views, a feature listed as 1.0 GA. The key move is to split the inputs in two.

The first input is the filter the model extracts from the user's request, such as an order status. The second is the tenant identity, which the application backend takes from a verified login token (a JWT) and passes in out-of-band, meaning the model never sees or controls it. The database then applies that identity inside the view, so a query cannot widen its own reach.

The guide's example wraps the model's value in PostgreSQL's format function with a quoting option. This is needed because the inner query is passed as text, so a plain placeholder would be read as literal characters. The wrapping makes the value a properly quoted literal, which blocks the classic OR 1=1 trick.

The author argues that the two locks together address injection, cross-tenant leakage and connection floods. As dev.to reported, the guide also discusses when to choose this approach over PostgreSQL's row-level security, though the portion available here does not show the verdict.

The plumbing around the locks

The guide treats the whole deployment as a chain of boundaries. The agent runs on Cloud Run with a dedicated service account holding permission to invoke the Toolbox and use MCP tools, and explicitly no database roles. The Toolbox has its own account with the client role for AlloyDB and access to the stored secret. AlloyDB has no public IP address.

Before a prompt reaches Gemini, it passes through Model Armor, a Google screening service. The guide configures it to inspect and block prompt injection, sensitive-data violations and malicious URLs. It also describes a five-gate chain of IAM checks, Model Armor and pgAudit (a PostgreSQL audit-logging extension).

One networking lesson stands out. Private Services Access links your network to AlloyDB, but it does not let one internal Cloud Run service reach another. For that you need Private Google Access plus a private DNS zone that maps *.run.app addresses internally, and the guide warns that propagation takes time.

Requirements and how to read it

The setup needs PostgreSQL 17 or later. PostgreSQL 18 and above turns on in-SQL AI smart batching by default. Several flags, including the one for parameterized views and the one for pgAudit, trigger an instance restart. The parameterized-views flag must be enabled separately on primary, read-pool and cross-region replica instances.

A separate feature, AlloyDB PostgreSQL for Agents, offers fast-starting agent compute that scales to zero. It is still in Preview and requires requesting access.

This is a community engineering guide, not Google's own documentation, so treat it as a pattern to compare against rather than a recipe to paste. The next two parts cover storage and analytics internals and how to avoid lock-in.

What to watch

Try it

List your agent's database access and mark which layers you already have: fixed queries, filtered views, tenant identity set outside the model, separate agent credentials. Then verify details in Google's official docs.

Sources

Liked it? Say why

Add a line for other readers.

No account needed. We never show your email. Comments with links, ads or abuse are held and not shown. Your comment shows after a quick check.

Listen to the AI Wire headlines

Listen to the AI Wire

Listen to our daily AI shows every morning: on your commute, in the shower, wherever you are

Listen to the daily shows

Written by the SI Agent News Desk from the reporting above.Written with AI from the source above. Spot a mistake? Corrections.

More AI news