The simplest data-access policy is a list of columns nobody may read. It is also the one that fails first. An analyst’s report selects the customer table with a dozen columns, one of which is an SSN she never looks at; the policy blocks the whole query, the report breaks, and the analyst asks for an exception that quietly becomes permanent. InsightAIShield takes a different route for most cases: it rewrites the query so that the sensitive column comes back masked, the rows come back filtered to what the person may see, and the result is capped, all inside the SQL, in the database’s own dialect. The query runs; the data that shouldn’t leave doesn’t.
In short
- Masks inside the SQL. MASK, REDACT and the SKIP rules become expressions on the base column, so an alias, a subquery or a CTE can’t undo them.
- Row filters and limits. FILTER adds a condition to every SELECT that reads the table (with the person’s own attributes bound in); LIMIT caps the rows, and the smallest limit wins.
- One plan, many dialects. The rewrite is built as a syntax tree and rendered for PostgreSQL, MySQL, Snowflake, SQL Server, Oracle and the other sources: LIMIT, TOP or FETCH FIRST as each expects.
- Tokens on the way back. TOKENIZE and HASH apply to the result rows, so the same value always gets the same token and joins keep working.
Blocking is the blunt instrument
Blocking still has its place. A command nobody granted, a card security code, a join between two tables that must never meet: those are refused outright, and the post on failing closed explains how. But most sensitive data is read legitimately every day, by people who need part of it. A fraud analyst needs the last four digits of a card, not the whole number. A branch analyst needs her own region’s customers, not the bank’s. A support agent needs to confirm an account, not export the ledger.
For those cases the useful question is not “may this query run?” but “what should this query return for this person?”. Answering it at the database layer, rather than in every application, means the answer is the same in the BI tool, the notebook, the ad-hoc SQL client and the AI agent.
One query, four reasons
On Northmere Bank, the fictional bank in the demo, Lena Okafor is an analyst in the North-East region.
Her policy, core_customer_pii, gives analysts read access to core banking with four
conditions: only their own region’s customers, at most 5,000 rows per query, SSNs redacted except
for the compliance team, and dates of birth masked. She runs
select customer_id, full_name, ssn, region from customers.
The rewritten statement does three things her query didn’t ask for, and each has a line under Why:
- Filter:
WHERE customers.region = 'NE'. The policy saysregion = ${user.region}; the engine binds Lena’s own directory attribute. If the attribute were missing, the query would be refused, not run unfiltered. - Limit:
LIMIT 5000, the policy’s cap for her group. - Redact: a
CASEexpression that replaces the first six characters of the SSN withXand keeps the rest, with explicit handling for NULLs and values shorter than the redaction.
The fourth line, Grant, is the permission itself: she holds the grants this SELECT needs. A rewrite never adds access; it only narrows what an allowed query returns.
The query is parsed once, decided once, and rendered for the database it is going to.
Where the mask goes matters
A naive rewriter masks the output column: it wraps whatever appears in the SELECT list. That is easy to
defeat. Rename the column with an alias, read it in a subquery and select from the subquery, or pull it
into a CTE, and the outer SELECT no longer mentions ssn at all.
InsightAIShield’s analyser resolves every column reference back to its base table and column, through aliases, CTEs and subqueries, and the rewriter applies the mask at the point where the base column is read. Whatever the outer query does with the value afterwards, it is working with the masked value. The same resolution tells the engine which data classes a query touched, which is what the activity record shows under “Sensitive data read”.
| The query tries | A rewriter that masks the output | Masking at the base column |
|---|---|---|
select ssn as x from customers | Misses the alias | Masked |
select x from (select ssn as x from customers) t | Misses the subquery | Masked |
with c as (select ssn from customers) select * from c | Misses the CTE | Masked |
select substring(ssn, 7) from customers | Depends on the function | Masked before the function sees it |
The same rule, in the database’s own words
A mask is a string expression, and string functions are where SQL dialects disagree most. A rewriter that
builds the masked expression as text for one database produces broken SQL on the next. InsightAIShield
builds the rewrite as a syntax tree and renders it per dialect, so one policy produces correct SQL
on each source it covers. The row limit follows the same principle: LIMIT for PostgreSQL
and MySQL, TOP for SQL Server, FETCH FIRST for Oracle, and when a query
already has a smaller limit, the smaller one wins.
Here is Northmere’s cards platform, a MySQL database. Devon Price in card operations selects card numbers and holder names. The cardholder-data policy keeps the last four digits of a PAN and masks the rest:
CHAR_LENGTH, CONCAT and RIGHT, where the PostgreSQL version used LENGTH and SUBSTRING.The rewriter also fixes the edge cases that hand-written masking tends to get wrong. A value shorter than
the mask length comes back fully masked instead of erroring or slipping through in clear. A left mask
keeps exactly the characters it should (SQL’s SUBSTRING is 1-based, a classic
off-by-one). NULL stays NULL, so counts and joins on nullable columns behave.
Tokens on the response, not in the SQL
Not every protection can be written as SQL. A format-preserving token needs a key the database must never see, and a vault token needs the vault. For those, the plan carries a response transform: the query runs with the column selected normally, and the enforcement point replaces each value in the result rows before they reach the client.
Northmere’s customer support assistant is an AI agent. When it looks up a customer’s
accounts, the ai_agent_data policy caps the result at 50 rows and tokenizes the account
number on the way back:
Because the token is deterministic per column (the same account number always gets the same token in the same column), the agent can still group, count and match records. It can’t reverse a token: detokenizing is a separate, audited request with a purpose, made by a person. The post on tokens covers the choice between FF1, FF3-1 and the vault.
What the record keeps
Every rewritten query is recorded with both versions of the SQL. The activity detail shows the original beside what ran, the reasons with the policy version and rule type that produced each change, and the context: the person and the database user, the client and its location, the enforcement point, the rows and bytes returned, and the duration.
limit 3 was smaller than the policy’s, so it stayed.That record answers the question an investigator actually asks after an incident: not “did this person query the customer table?” but “what did they get back?”. A query that ran with a redacted SSN column is a very different event from one that returned SSNs in clear, and the record says which.
Choosing between rewrite and refuse
Right for commands nobody granted, columns nobody may read (BLOCK_FIELD), forbidden joins, and anything the engine can’t evaluate.
Right for columns people need part of (MASK, REDACT), rows they need some of (FILTER, LIMIT), and values applications need the shape of (TOKENIZE, HASH).
Most of Northmere’s policies use both. The cardholder-data policy masks the PAN, blocks the card security code entirely, denies destructive commands on the cards database and forbids joining cards to disputes. Each rule does the least it can to protect the data, and the strongest rule on a column always wins.
Protect the column, keep the query.
People keep working when the policy gives them what they are allowed to see instead of an error. Rewriting at the base column, in the database’s own dialect, with both versions of the SQL on the record, is how InsightAIShield does that without leaving a way around it.
Where people stay in charge
- Exemptions from a mask or a filter are named groups in the policy, so who sees a column in clear is a decision on the record, made by the lead who approved the version.
- Every agent action is capped at L2, “ask first”: an agent prepares, and a person confirms, activates, revokes, sends or decides.
- A policy version goes live only when a second lead, not the person who proposed it, approves it.
- A revocation is proposed by one reviewer and approved by another; the REVOKE statement is kept on the record for your DBA to run.
- Detokenizing needs a stated purpose, is recorded, and is capped per person per period.
- SIEM batches and attestation requests are held until a person releases them.