Skip to content

FEAT-015 — Sensitive data classes: classify columns once, enforce by class #294

Done

Description

@jmmana

Parent epic: #290
Priority: P1

Where we are

Checked on main with the PostgreSQL dialect, hidden_columns: [customers.ssn] and masked_columns: {customers.email: partial}:

Query Result
SELECT email AS contacto FROM customers Allowed, the mask applied under the alias
SELECT LOWER(email) FROM customers Allowed as LOWER(<mask>)
SELECT x FROM (SELECT email AS x FROM customers) s Allowed, masked inside the derived table
SELECT CONCAT(name, ssn) FROM customers hidden_column
SELECT CAST(ssn AS VARCHAR) FROM customers hidden_column
SELECT name FROM customers WHERE email LIKE 'a%' hidden_column (filtering on a masked value would leak it)

lint flags sensitive-looking names that are exposed (looks_sensitive). Policy.from_database() reads a live schema, and result delivery protects result fields for MCP clients (#223).

So the AST already tracks declared columns through aliases, functions, casts and subqueries. The gap is classification. Sensitive columns are a hand-kept list per table. The kind of data (email, national ID, card, credential) is not recorded anywhere. The same treatment is repeated table by table, and nothing reports which kinds of personal data a query returns.

Proposal

data_classes:
  email:       {action: mask, strategy: partial}
  national_id: {action: hide}        # RFC, CURP, NSS, SSN, DNI…
  card_number: {action: mask, strategy: partial}
  bank_account: {action: hide}       # CLABE, IBAN
  credential:  {action: hide}
columns:
  customers.email: email
  customers.rfc: national_id
  employees.clabe: bank_account
not_sensitive:
  network_docs.rfc_number: "IETF RFC number, not a tax ID"
  • Enforcement stays on the existing code path. Classes compile into hidden_columns and masked_columns when the policy loads. An explicit per-column entry wins over its class default, and a conflict raises PolicyError. Roles in a policy set can override the action for a class.
  • sqlguardllm classify --db URL [--sample N] proposes the columns: mapping:
    • Name heuristics reuse and extend lint.looks_sensitive, with Spanish and Mexican names such as curp, rfc, nss, clabe, correo and telefono.
    • Optional value sampling uses validators: email, Luhn for cards, CURP and RFC formats, CLABE and IBAN check digits.
    • Sampling is off by default, reads at most N rows per column through a read-only connection, and never prints a sampled value.
    • The output is a YAML patch to review. Nothing is applied automatically. Classification helps people write the policy, and the runtime decision stays deterministic.
  • Reporting. Explanation and the MCP results list the classes a query returns, masked or not, for example {"email": ["customers.email"]}.
  • Lint. New code unclassified-sensitive-column for a column the heuristics match that is neither in columns: nor in not_sensitive:.

Acceptance criteria

  • A golden equivalence test runs the existing hiding and masking tests against a class-based policy equivalent to today's explicit one.
  • New corpus cases for every action, in the PostgreSQL, MySQL, T-SQL, SQLite and Snowflake dialects: alias, LOWER, CONCAT, CAST / CONVERT, COALESCE, CASE, window functions, CTE, UNION, and leak attempts through WHERE, GROUP BY, ORDER BY and JOIN.
  • classify on the demo database proposes the expected mapping. A test checks that sampling is off by default, bounded and read-only, and that the output contains no sampled value.
  • Spanish and Mexican name patterns and validators, with false-positive tests (rfc_number in a networking table, email_verified, card_type).
  • Explanation, HTTP, the engine stdio protocol and the Node.js and .NET clients expose the classes. The JSON Schema includes data_classes, columns and not_sensitive.
  • The threat model (EN / ES) says what is covered (named columns) and what isn't: values inside JSON or free-text columns, and inference through aggregates (FEAT-011 — Cross-query privacy: row budgets, paging detection and differencing checks #220).

Files

src/sqlguardllm/classes.py (new) · policy.py · policy_set.py · lint.py · introspect.py · cli.py · explain.py · tests/corpus/ · docs/THREAT_MODEL.md and docs/es/

Size

L

PR #309 merged after all 30 PR checks passed. It adds data_classes, column_classes (named so because columns is too generic on Policy) and not_sensitive; they compile into hidden_columns and masked_columns, and a test shows a class policy decides exactly like the explicit one over 23 queries in five dialects. Guard.explain() and explain_query report the classes, lint skips not_sensitive columns (no new lint code: sensitive-column-exposed already covers it), and sqlguardllm classify proposes the mapping from column names only.

Still open: value sampling and the validators (email, Luhn, CURP/RFC, CLABE/IBAN), which read real data and deserve their own review; corpus cases under tests/corpus/; per-role overrides in policy sets; the Node.js and .NET clients; the threat-model entry (EN / ES). Explanation, HTTP /v1/explain and the engine stdio protocol do carry data_classes.

Done in #313 (explanations, protocol schema, a conformance test across in-process, stdio and HTTP, and the typed Node.js and .NET clients), #315 (value sampling with email, Luhn, CURP, RFC, SSN, CLABE and IBAN checks, Spanish and Mexican names, 130 corpus cases in five dialects also run by the Node.js and .NET conformance suites, per-role overrides, the threat-model row) and #317 (the demo database test). Sampling is off by default, bounded (1000 values, 500 columns), read-only, rolled back and never printed; it was checked on SQLite and on a real PostgreSQL.

One item is left: the threat-model criterion says EN / ES. The English docs/THREAT_MODEL.md has the row and links to the Spanish limits in docs/es/CLASSES.md, but there is no Spanish threat-model page at all (for any topic), and I did not write a partial one.

PR #318 merged: docs/es/THREAT_MODEL.md translates the decision sections in full and summarises the long fuzzing part with a link to the English page. All criteria are met.

Activity

  1. added
    type:feature
    area:security
    area:rls
    size:l
    lang:python
    p1
    on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:rlsarea:securitylang:pythonp1size:ltype:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions