Sensitive data classes
Español: es/CLASSES.md
Experimental: not covered by the 1.x stability guarantee (see STABILITY.md); it may change in a minor release.
Instead of hiding or masking sensitive columns one by one, say once what a kind of data is and what happens to it, then say which columns are of that kind.
dialect: postgres
tables: [customers, staff, network_docs]
schema:
customers: [id, name, email, rfc]
staff: [id, clabe]
data_classes:
email: {action: mask, strategy: partial}
national_id: {action: hide} # RFC, CURP, NSS, SSN, DNI
bank: {action: hide} # CLABE, IBAN
column_classes:
customers.email: email
customers.rfc: national_id
staff.clabe: bank
not_sensitive:
network_docs.rfc_number: "IETF RFC number, not a tax ID"
How it is enforced
There is no second code path. When the policy loads, the classes compile into the same hidden_columns and masked_columns the guard has always enforced, so everything it already does to those columns applies unchanged: aliases, functions, casts, CONCAT, COALESCE, CASE, window functions, subqueries, CTEs, UNION, and attempts to leak through WHERE, GROUP BY, ORDER BY or JOIN. A test runs 23 such queries in five dialects and checks that a class-based policy decides exactly like its explicit equivalent.
hidemakes the column unusable anywhere in a query (hidden_column).masklets it be selected but returns it masked, with astrategy(null,redact,hash,partial). Without one it isredact.- An explicit entry wins. A column written in
hidden_columnsormasked_columnskeeps that treatment even when its class says something else. not_sensitiverecords, with a reason, that a column whose name looks sensitive is not.sqlguardllm lintstops warning about it. A column can't be both classified andnot_sensitive.
Guard.explain() and the explain_query tool report which classes a query reads, masked or not: "data_classes": {"email": ["customers.email"]}. The field is present only when the policy has classes.
Finding the columns: sqlguardllm classify
sqlguardllm classify --db "$DATABASE_URL" --policy policy.yaml > proposal.yaml
By default it reads table and column names only, never a row, and prints the data_classes and column_classes to add, as a patch for you to review. Columns the --policy already hides, masks, classifies or clears with not_sensitive are left out. --format json, --schema, --include and --exclude work as in Policy.from_database. Needs pip install "sqlguardllm[db]" and your database driver.
The classes it proposes: email, phone, national_id (curp, rfc, nss, ssn, dni, nit, pasaporte, imss...), card_number (card, tarjeta...), bank_account (clabe, iban, numero_cuenta...) and credential. A word that describes the data instead of holding it is not a match: last in English (email_verified, card_type), first in Spanish (tipo_tarjeta, estado_tarjeta, fecha_curp).
Looking at values: --sample N
Names miss a column called contact that holds emails. With --sample N (1 to 1000) it also looks at up to N values of each column and checks them against the class: an email shape, the Luhn check of a card number, the shape of a CURP, an RFC or a US SSN, the check digit of a Mexican CLABE, and the mod-97 check of an IBAN. A class is proposed when at least 80% of the sampled values pass and there are at least 3 of them. Phone numbers and credentials have no value check: they stay name-only.
column_classes:
customers.contact: email # values only (3 of 3 sampled)
customers.tarjeta: card_number # name and values (3 of 3 sampled)
network_docs.rfc_number: national_id # name only (values did not confirm it)
The last line is the point: the name says tax ID, the values are plain integers, so it is probably the false positive that not_sensitive is for.
It reads real data, so it is built to be cautious:
- Off by default. Without
--sampleno row is read (a test checks that no statement touches a data table). - Bounded. One
SELECT ... WHERE col IS NOT NULL LIMIT Nper column, never more than 1000 values and 500 columns per run. - Read-only. Each read runs in a transaction that is rolled back, set read-only (PostgreSQL, MySQL and SQLite) and, on PostgreSQL and MySQL, with a 5-second statement timeout. Use a read-only database user anyway.
- Never printed. Values stay in memory for the check and are dropped. The output holds only the class, the counts and a note on the evidence; a column that cannot be read is skipped without showing the error, which can carry the connection URL.
- Columns the
--policyalready decides are not sampled.
Roles in a policy set
A role can change what a class does, in a policy set: add: {data_classes: {email: {action: hide}}} hides emails for that role only, and data_classes: {email: {action: mask, strategy: hash}} replaces the classes for the role. Beware: a key a role sets replaces the inherited value. A role that sets column_classes loses every classification it did not repeat. Use add to extend.
What it does not cover
- Names, and a sample at most. Without
--sample, a sensitive column with an innocent name (contact,notes) is not found. With it, only what the value checks recognise is found, from a sample: a column whose first 1000 values are clean can still hold a sensitive one later. A harmless column can still be flagged (network_docs.rfc_numberabove). Review the proposal. - Whole columns. A class covers a column, not values inside it: sensitive values in a JSON or free-text column are not seen, and the value checks do not look inside them.
- Inference through aggregates. A class hides or masks a column; it does not stop someone from learning a value from
COUNTorSUMover it. See the threat model.