Skip to content

Column masking

Column masks come from an optional rule in the policy, turned on with --policy-masks-query:

Terminal window
curral serve ... --policy-masks-query data.curral.masks
masks := {"lake.analytics.customers": {"ssn": "last:2", "name": "redact", "birth_date": "null"}} if {
not "pii_reader" in input.roles
}

The rule returns {table: {column: mask}} for the current request.

Mask Result
"null" NULL, keeping the column type
"redact" '***'
"last:N" only the last N characters are visible
"first:N" only the first N characters are visible; preserves NULL
"email" e.g. a***@domain.com; preserves NULL
"hash" deterministic salted SHA-256; requires --mask-salt and preserves NULL
{"sql": "<expression>"} any single SQL expression, for special cases

A {"sql": ...} mask must be one expression, under the same rules as row filters: no ;, comments or dollar quotes, and no parentheses that could escape it. It can read the caller through getvariable('curral_user'), getvariable('curral_roles') and, for delegated requests, getvariable('curral_identity').

Set CURRAL_MASK_SALT in the server environment and use "hash" for a column whose equality relationships should remain available to queries. The same value and salt produce the same hash, allowing joins, grouping and COUNT(DISTINCT) on masked values. Changing the salt changes the hashes. The salt is removed from the process environment after boot and is not exposed through session variables. A hash mask without a salt is refused.

In v0.9.0, queries through local DuckDB views over protected local tables are rewritten recursively, preserving column aliases. Row filters and masks apply before the outer query. Depth, cycle and plan-scan checks still fail closed when a view cannot be rewritten safely.

Masking happens in a subquery that runs before the user’s query. A WHERE ssn = '123-45-6789', a join or a GROUP BY operates on the masked value, so the real value cannot be found by elimination.

/v1/schema flags masked columns with "masked": true.

Row filters and masks only cover queries curral can rewrite with certainty. Everything else touching a protected table is denied (403, decided_by: protection):

  • non-SELECT statements that read a protected table (INSERT ... SELECT, CREATE TABLE AS);
  • indirect reads that cannot be verified, including macros and local views over lake tables with hidden remote scans;
  • queries that do not survive the SQL → tree → SQL round trip intact;
  • TABLESAMPLE, time travel (AT) and PIVOT.

A differential test compares every protected query with the same query over a copy of the table that is already filtered and masked; results must be identical. It runs in CI, and a fuzzer runs weekly.

Rewrites are cached per query text and rules. Measured over HTTP on a point lookup:

none RLS mask RLS + mask
local, p50 1.3 ms 1.7 ms 1.7 ms 1.8 ms
Iceberg on R2, p50 7.2 ms 8.6 ms 9.7 ms 8.7 ms

On aggregations the difference disappears into the query’s own time.