Dynamic Data Masking and Row-Level Security
Control what users see without changing the underlying data β mask sensitive columns for unauthorised users and filter rows based on who is querying.
Hiding data in plain sight
Dynamic Data Masking is like putting a sticker over part of a credit card number on a receipt β the full number is still there underneath, but most people only see XXXX-XXXX-XXXX-1234. Row-Level Security is like a hotel where guests can only see their own room β the hotel has 500 rooms, but your key only opens yours.
Dynamic Data Masking (DDM)
DDM masks column values in query results. The actual data is not changed β only how it appears to users without the UNMASK permission.
Masking functions
| Function | Example Input | Masked Output | Use For |
|---|---|---|---|
default() | βJohn Smith" | "XXXXβ | General text |
email() | βjohn@bank.com" | "jXXX@XXXX.comβ | Email addresses |
partial(2, 'XXX', 2) | β0274551234" | "02XXX34β | Phone numbers, account numbers |
random(1, 100) | 85000 | 47 | Numeric columns (randomised) |
CREATE TABLE Employees (
EmployeeId INT NOT NULL PRIMARY KEY,
FullName NVARCHAR(200) MASKED WITH (FUNCTION = 'default()') NOT NULL,
Email NVARCHAR(320) MASKED WITH (FUNCTION = 'email()') NOT NULL,
Phone NVARCHAR(20) MASKED WITH (FUNCTION = 'partial(2, "XXX-XXX-", 2)'),
Salary DECIMAL(10,2) MASKED WITH (FUNCTION = 'random(1000, 9999)')
);
-- Grant unmask to specific users
GRANT UNMASK TO [HR_Manager];
Exam tip: DDM is not a security boundary
DDM is a convenience feature, not a security feature. A determined user can infer masked values through clever queries (e.g., binary search with WHERE clauses). For true data protection, use Always Encrypted or column-level encryption.
The exam may present DDM as βsufficient for GDPR complianceβ β that is FALSE. DDM is suitable for reducing casual exposure (like a support agent seeing partial credit card numbers) but not for meeting regulatory encryption requirements.
Row-Level Security (RLS)
RLS transparently filters rows based on who is executing the query. It uses a security predicate function that returns 1 (visible) or 0 (hidden) for each row.
-- Step 1: Create a predicate function
CREATE FUNCTION dbo.fn_SecurityPredicate(@Region NVARCHAR(50))
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS Result
WHERE @Region = USER_NAME()
OR USER_NAME() = 'dbo'
OR IS_MEMBER('GlobalManagers') = 1;
-- Step 2: Create a security policy
CREATE SECURITY POLICY RegionFilter
ADD FILTER PREDICATE dbo.fn_SecurityPredicate(Region) ON dbo.Sales
WITH (STATE = ON);
Now when a user queries the Sales table, they only see rows matching their region β automatically, transparently, on every query.
Filter vs Block predicates
| Predicate Type | What It Does | Applies To |
|---|---|---|
| FILTER | Silently hides rows (WHERE clause injection) | SELECT, UPDATE, DELETE |
| BLOCK | Prevents operations on unauthorised rows (raises error) | INSERT (AFTER INSERT), UPDATE (AFTER/BEFORE) |
| Feature | Dynamic Data Masking | Row-Level Security |
|---|---|---|
| What it hides | Column values (partial or full) | Entire rows |
| Data changed? | No β presentation only | No β transparent filtering |
| Bypass risk | High β clever queries can infer values | Low β enforced at engine level |
| Performance | Minimal β applied on output | Moderate β predicate runs per row |
| Use case | Reduce casual exposure (support agents, reports) | Multi-tenant data isolation, regional access control |
Scenario: Ingrid's multi-region access at Nordic Shield
Nordic Shield Insurance operates across Scandinavia. Each regional office should only see claims from their own country. Ingrid implements RLS with a predicate that checks the userβs country against the claimβs country. Global managers bypass the filter. Now a Norwegian claims officer querying the Claims table automatically sees only Norwegian claims β no code changes needed in any application.
Ingrid at Nordic Shield needs to ensure that claims officers can only see claims from their own region. The filtering must be automatic and cannot be bypassed by direct SQL queries. Which approach?
π¬ Video coming soon
Next up: Permissions, Auditing, and Passwordless Access β control who can do what, track everything, and eliminate passwords.