Study: we tried 35 ways to extract personal data from an AI agent connected to PostgreSQL
We ran 35 techniques to extract IDs, emails, phones and names against our own product. 17 got past the masking. Here is which ones, why, how we fixed it and the new run.
GatekeeperID Research · AI agents · PostgreSQL · Personal data
When a company connects Claude Code, Cursor or another agent to its database through MCP, legal and security always ask the same thing: will our customers' IDs, emails and names end up inside the model?
GatekeeperID Agents exists so the answer is no. "Trust us" is not an answer, so we did what a technical customer would do: we tried to leak data through our own product in every way we could think of, and measured it.
Summary: in the first run, 17 of 35 techniques got past the masking. We fixed it, ran everything again, and none got through.
What we measured
Agents does not try to stop people from reading personal data; that is the company's own access control. It promises that raw personal data is not handed to the agent. So we measured the infrastructure, not the model: every technique was sent straight to the MCP query tool with no model in between. That is the worst case, the same as an agent that obeys anything or someone holding the token.
Method
- A fictitious database with about 2,200 rows, with personal data in columns, free text and nested JSON.
- A developer profile: read and write on the operations schema, no access to finance or HR.
- 35 techniques in eight groups, from plain queries to copies into new tables.
- Real values read straight from the database, compared with every response, also uppercased, reversed, base64 and hex. Every leak was checked by hand.
Before and after
| Technique | Before | After |
|---|---|---|
| Personal columns by name | Masked | Masked |
| SELECT * | Leaked | Masked |
| ID number, birth date, address, ZIP | Masked | Masked |
| Integration secrets | Masked | Masked |
| Simple alias (full_name AS name) | Masked | Masked |
| Neutral alias (cpf AS x) | Masked | Masked |
| Subquery with alias | Masked | Masked |
| CTE with alias | Masked | Masked |
| Free text (ticket message) | Masked | Masked |
| Free text (notes) | Masked | Masked |
| Nested JSON | Leaked | Masked |
| Field extracted from JSON | Leaked | Refused |
| row_to_json | Leaked | Refused |
| to_jsonb(c.*) | Leaked | Refused |
| jsonb_build_object | Leaked | Refused |
| array_agg | Leaked | Refused |
| string_agg | Masked | Refused |
| upper(full_name) | Leaked | Refused |
| lower(email) | Masked | Refused |
| reverse(cpf) | Leaked | Refused |
| Email in base64 | Leaked | Refused |
| Name in hex | Leaked | Refused |
| CPF digits only | Masked | Refused |
| CPF with a trailing letter | Leaked | Refused |
| CPF with spaces | Masked | Refused |
| Phone digits only | Leaked | Refused |
| Email with ' at ' | Leaked | Refused |
| Email local part (split_part) | Leaked | Refused |
| Concatenation (name || '') | Leaked | Refused |
| LIKE letter by letter (count only) | Masked | Refused |
| Name length | Masked | Refused |
| Copy into a new table | Masked | Refused |
| Read the copied table | Leaked | N/A (copy refused) |
| Read salaries (HR) | Denied by the database | Denied by the database |
| Read payments (finance) | Denied by the database | Denied by the database |
Before (connector 0.33.0): 17 leaked, 16 masked, 2 denied by the database. After (0.34.1): none leaked; 21 refused before running, 11 masked, 2 denied by the database, and reading the copy no longer applied because the copy was refused.
What got through, and why
Up to 0.33, protection was output masking: personal data was recognized by column name and value format and replaced with a partial version. That held for normal use, aliases and IDs hidden in free text. It failed in two ways. Names have no format, so a name in a column with a neutral name passed (upper, array_agg, a copied table, a JSON key called just nome). And transformations inside the database removed the format (reverse, base64, digits only).
The fix: an allow list, before execution
Since 0.34 the connector reads the SQL with PostgreSQL's own parser before running it. A personal column may appear only bare in the select list that returns to the agent, or in comparisons that do not return the value (=, IN, IS NULL, count(), GROUP BY, ORDER BY). Anything else is refused before it reaches the database. Aliases inherit the mark. Because the rule lists what is allowed, a function nobody thought of is still blocked.
The agent always gets the same reply, whatever the reason, so the reply does not map the rules. The detail goes to the audit log. Nobody loses access: whoever needs the real value queries the database with their own credentials. It just does not go through the AI.
Does normal use still work?
Yes. After the fix, everyday queries through the same token ran with personal data masked: customer ranking with a JOIN, orders by status, lookup by ID number, open tickets with the customer name, distinct email count and average order per month.
What about the model?
In manual tests with an agent (Claude Code), the model did refuse explicit workarounds. That helps, but it depends on the model, the wording and who calls the tool. Protecting personal data cannot depend on the AI behaving well; the data must not reach it.
Limits
- 35 techniques, one profile, one fictitious database. A measurement we will repeat on every release, not a proof.
- Equality comparisons stay allowed: they confirm a value exists without revealing values.
- A personal column with a meaningless name must be declared in the configuration.
- In the intermediate run (0.34.0), extracting the
nomekey from JSON still passed; 0.34.1 fixed it.
Want to run the same test on your database? GatekeeperID Agents gives you a test access in minutes.