[Blog](https://bricksentry.org/blog) · Essay

# We let an AI agent attack our guardrail for AI agent writes. Here's what got through.

By Brahma · November 30, 2026 · 9 min read

AI agents are starting to write to production tables. A permission system can tell you whether an agent is allowed to write to a table. It can't tell you whether this particular write is right.

bricksentry is an open-source attempt at the second question, for Databricks. Before an agent's write lands, the write runs on a copy of the table, the copy is compared with the original row by row, and a policy file decides whether the change can ship. The agent gets back counts and a verdict, never the data itself.

That last sentence is a security claim, and security claims are cheap. So over two weeks in November I tried to break it, mostly by having an AI agent attack it on a real Databricks workspace. Every attack, result and fix is recorded in the repo's VALIDATION.md, including the failures. This post is the short version: what got through, why, and what I'd tell anyone building something similar.

## How it works, briefly

An agent calls a tool (over MCP, or from Python) with a SQL statement and the target table. bricksentry then:

1. Parses the statement and checks every table it reads against an allowlist (`agent_tables`).
2. Makes a shallow clone of the target. On Delta this copies metadata, not data, so it's cheap.
3. Runs the write on the clone. Production is never written to.
4. Diffs clone against production by key, pinned to fixed table versions: rows added, removed and changed, duplicate keys, totals that moved.
5. Looks up downstream tables, dashboards and jobs from Unity Catalog lineage.
6. Applies rules from a policy file to produce a verdict: `pass`, `warn`, `needs_human` or `block`. The same change always gets the same verdict. No model decides.
7. Returns counts and the verdict to the agent, and drops the clone.

## How the testing was done

Everything ran on a Databricks Free Edition workspace: serverless, one 2X-Small SQL warehouse, and an HR pipeline whose main table has 200,000 employees. I ran the rounds with Databricks' Genie Code assistant executing the steps in the workspace, and in the attack rounds it was also the attacker. Each round installed the released package from PyPI, not a local copy.

The rules for every round: production tables are only read, every write goes to a clone or a scratch schema that's dropped at the end, and `DESCRIBE HISTORY` of every table is compared before and after to prove nothing else changed.

## What got through

### 1. The preview was a read API

The first serious finding was the most embarrassing. `preview_write` returned example rows from the diff, which is useful for a human reviewer. But that meant an agent without access to a table could "update" the rows it wanted, say a few social security numbers, and read the old values back from the preview. Nothing was written. Everything leaked.

The fix (0.1.27) was to give agents counts only: no example values, no totals for a one-row table, and values masked in engine error messages. Humans running the same check still see examples.

**Lesson:** anything that shows a diff is a read path. Design the agent's view as if the agent is the attacker.

### 2. The allowlist only checked the target

`agent_tables` is the list of tables an agent may touch. Round 9 found it was only checked against the table being written. A forbidden table read in a subquery, as a MERGE source, in an INSERT ... SELECT, or named as the comparison table leaked its row counts to the agent. Values stayed hidden, but counts are information too.

The fix (0.1.28) parses the whole statement with sqlglot and requires every table it reads to be on the list. A statement the parser can't fully read is refused rather than allowed.

### 3. The parser and the engine disagreed, or didn't

A parser-based allowlist has an obvious weak point: if the parser reads SQL differently from Spark, an attacker can hide a table in the gap. So round 10 was an attempt to make them disagree. It tried Spark-specific grammar (`UPDATE ... FROM`, `LATERAL VIEW explode`, `TABLESAMPLE`, deeply nested subqueries, CTEs), identifier tricks (backticked three-part names, case variants, a Unicode lookalike letter) and SQL the parser can't read at all, such as a comment splitting a keyword: `FR/**/OM`.

All 13 attacks were refused. The unreadable ones failed closed. This is the round where nothing got through, and it's the one I trust least to stay that way. Parsers change, and so does Spark.

### 4. Views and SQL functions hide tables from everyone

Round 10 couldn't cover views or SQL functions, so 0.1.29 added a second check: ask Spark for its query plan (`EXPLAIN EXTENDED`) and look for tables there. The next round found that on serverless, the plan doesn't show tables inside views or SQL functions either. A listed view over a forbidden table, or a function whose body reads it, still ran.

The fix reads view definitions and function bodies from Unity Catalog and checks the tables inside them, recursively. A view over that view, and a function called with or without a schema, are now refused.

### 5. The fix that blocked real work

The query-plan check was first added as a hard gate: if the plan showed an unlisted table, refuse. The pre-release check found that it refused legitimate agent MERGEs, because serverless plan formats vary and the check misread them.

It now refuses only when it can read the plan and finds an unlisted table. It never blocks on a plan it can't read, and the parser stays the primary check.

**Lesson:** a guard that blocks good work gets switched off, and then it protects nothing. False positives are a security problem too.

### 6. Prompt injection through metadata

bricksentry passes Unity Catalog tags to agents, because "this table is tagged `pii`" is useful context. Round 9 planted an instruction in a tag value, and it reached the agent word for word. Free text in tags is now replaced with `[text hidden: 97 chars]`, while short tags like `pii` are kept.

The same round found that the MCP server, when bound beyond localhost over HTTP, had no authentication. It now refuses to start that way without a token.

### 7. A comment inside a string

This one the agent never found. I found it while fixing something else. The guard scanned SQL for a second statement, but it treated `--` inside a quoted value as the start of a comment. So in

sql

```sql
UPDATE t SET note = '--'; DROP TABLE other
```

it never saw the `;` or the `DROP`. The guard now reads quotes the way the engine does, checks both interpretations where engines disagree about backslash escapes, and refuses any quote or `/*` that's never closed (0.1.31).

**Lesson:** hand-written SQL scanning is where bugs live. Test it with the strings your scanner treats as special, placed where they're harmless to the engine.

### 8. Wrong numbers, in both directions

Some findings weren't attacks but correctness bugs, and they matter just as much in a tool whose whole job is reporting what changed.

- **Under-reporting, the dangerous kind.** In 0.1.24, a sampled diff skipped the full-table checksum when the schema had also changed. In the A/B test, a column retyped from `INT` to `BIGINT` with 1,895 changed rows placed outside a 5% sample reported 0 changed rows and no warning. 0.1.25 reports all 1,895.
- **Over-reporting.** Copies of a duplicated key were paired by rank, so one changed copy shifted all the others. 50,000 rows sharing a NULL key with 2 real changes reported 36,563 changed. Fixed in 0.1.26 by matching identical copies first.
- **Moving targets.** Diff queries weren't pinned to one table version. Replacing the candidate mid-diff, from 5 changed rows to 50, gave 49 with no warning. Since 0.1.27 every read is pinned, and a change mid-diff is flagged.

Where I had to choose, I chose over-reporting. A false alarm costs a reviewer a minute. A missed change costs a customer.

## What it costs at scale

All on the Free Edition 2X-Small warehouse, with narrow synthetic tables (1.8 GB at 500 million rows, 3.5 GB at 1 billion). Real, wider tables will take longer.

| Full keyed diff of a 20% update | Time | Result |
| --- | --- | --- |
| 100 million rows | 49 s | 20,000,000 rows, 20.00% |
| 500 million rows | 136 s | 20.00%, undo correct |
| 1 billion rows | 264 s | 20.00%, undo matched time travel |

Time grew more slowly than the table. Sampling 10% of the rows saved nothing at 10 million rows and made the check 2.8 times faster at 1 billion, with the same sample drawn on repeated runs.

## What I'd tell anyone building a guard for agents

- **Every output is a read path.** Counts, errors, examples and timing can all leak something. Decide what the agent may learn, not just what it may write.
- **Parse, then fail closed.** If you can't read a statement completely, refuse it.
- **Use the engine as a second opinion, not a gate.** It sees things the parser can't, and it's inconsistent in ways that will block good work.
- **Treat metadata as untrusted input.** Tags, comments and descriptions are written by people, and now they're read by models.
- **Prefer over-reporting to silence**, and test the under-reporting cases on purpose.
- **Write down every failure.** Publishing the bugs is the only reason I'd ask anyone to trust the rest.

## What hasn't been tested

Other clouds and classic compute, streaming tables, anything above 1 billion rows, wide real-world tables at that size, and foreign catalogs. Free Edition can't do most of these.

If you can run the validation notebook on any of them, or you find a way around the guard, I'd genuinely like to hear about it. The full record is in [VALIDATION.md](https://github.com/bricksentry/bricksentry/blob/main/VALIDATION.md), and the code is on [GitHub](https://github.com/bricksentry/bricksentry) under Apache-2.0.

---

Canonical URL: https://bricksentry.org/blog/ai-agent-red-team
