Lesson 2 of 2 Dynamic Data Masking and Row-Level Security

Row-Level Security with Predicates and Policies

This lesson explains predicate functions, filter and block predicates, and how session context passes the end user's identity.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how predicate functions and security policies work together.
  • Distinguish between filter and block predicates.
  • Explain how SESSION_CONTEXT supports RLS with a shared application identity.

Predicate functions and security policies

RLS has two parts. A predicate function is a schema-bound inline table-valued function that returns a row (usually SELECT 1) when the current user may access a row with the given column values. A security policy binds the function to one or more tables as filter or block predicates and can be turned on or off.

CREATE SCHEMA Security;
GO
CREATE FUNCTION Security.fnSalesRepFilter (@SalesRepID int)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS Allowed
       WHERE @SalesRepID = CAST(SESSION_CONTEXT(N'SalesRepID') AS int)
          OR IS_MEMBER('SalesManagers') = 1;
GO
CREATE SECURITY POLICY Security.SalesRepPolicy
    ADD FILTER PREDICATE Security.fnSalesRepFilter(SalesRepID) ON Sales.Customer,
    ADD BLOCK PREDICATE  Security.fnSalesRepFilter(SalesRepID) ON Sales.Customer AFTER INSERT,
    ADD BLOCK PREDICATE  Security.fnSalesRepFilter(SalesRepID) ON Sales.Customer AFTER UPDATE
WITH (STATE = ON);

The following diagram shows a security policy filtering rows for one session.

Row-Level Security. A predicate function is a schema-bound inline table-valued function that returns a row when the current user may access a row with given values, for example when the row's SalesRepID matches the SESSION_CONTEXT value. A security policy binds it to tables as a filter predicate, which silently removes rows from reads, and block predicates, which reject inserts or updates that would violate it.
The database enforces the rule for every query.
Put in order

Setting up RLS

Exam skills 2.1.3

Put the steps for Row-Level Security in order.

Log in to try this activity Interactive activities are free with an account.

Filter and block predicates

A filter predicate silently removes rows the user may not see from SELECT, UPDATE and DELETE, so queries behave as if those rows do not exist. A block predicate rejects writes that violate the rule with an error: AFTER INSERT and AFTER UPDATE check the new values, which prevents creating or moving rows into another user's scope; BEFORE UPDATE and BEFORE DELETE check the existing values. Filter predicates alone do not stop a user from inserting a row they then cannot see.

Match the pairs

Filter and block predicates

Exam skills 2.1.3

Match each predicate with what it does.

Log in to try this activity Interactive activities are free with an account.

Identity: user names and session context

When users connect with their own identities, predicates can compare a column with USER_NAME() or check role membership with IS_MEMBER. Applications, APIs and Data API builder usually connect with one shared identity, so the end user's identity is passed in session context: the application calls EXEC sp_set_session_context @key = N'SalesRepID', @value = 17, @read_only = 1 after opening the connection, and the predicate reads SESSION_CONTEXT(N'SalesRepID'). Setting the key read-only prevents later code in the session from changing it. Data API builder can pass the authenticated user's claims to session context automatically when set-session-context is enabled in its data source options.

Predicate functions run for every row access, so they should be simple, avoid joins to large tables where possible and be supported by an index on the filtered column. Users who can alter the policy or the function can bypass RLS, and carefully crafted queries can sometimes infer hidden values through side channels such as errors, so RLS is combined with least-privilege permissions.

Scenario questions

Identifying the user

Exam skills 2.1.3

Choose how RLS should identify the user in each architecture.

Log in to try this activity Interactive activities are free with an account.
Worked example

A multitenant SaaS database

A SaaS application stores all tenants in one Azure SQL database with a TenantID column in every table. The web API connects with a single managed identity. A bug once returned another tenant's orders, and the company wants the database to guarantee isolation even if the API forgets a WHERE clause. What should the developer implement?

Create a predicate function that compares @TenantID with SESSION_CONTEXT(N'TenantID'), a security policy with filter predicates and AFTER INSERT and AFTER UPDATE block predicates on every tenant table, and have the API set the tenant ID in session context with @read_only = 1 on each connection. Filtering by USER_NAME() would not work because all requests use the same identity, and views per tenant do not scale and still rely on the API choosing the right view.

Check yourself

Why is session context set with @read_only = 1 for RLS?

Show answer

So that later code in the same session cannot change the key and impersonate another user or tenant.

Exam Tips

Remember for the exam

  • Hide rows from queries: filter predicate; reject bad writes: block predicates.
  • Shared application identity: pass the end user in SESSION_CONTEXT, set read-only.
  • Predicate functions are schema-bound inline TVFs; index the filtered columns.
  • Filter predicates alone do not stop inserting rows into another scope.