Tutorial: Governance enforcement in a sandbox¶
This tutorial demonstrates a key security property of Cortex Sandboxes: governance policies are enforced at the Snowflake layer, not the application layer. Column masking policies and row access policies apply inside a sandbox exactly as they apply to any other query — code running in a container cannot bypass them.
When you finish, you will have seen the same query return masked results both outside and inside a sandbox, confirming that a sandbox provides isolation without bypassing the governance your organization has already put in place.
Time to complete: about 15 minutes.
Prerequisites¶
- The
snowflake-sandbox-pythonSDK installed and a Snowflake connection configured. See Cortex Sandboxes quickstart. - Privileges to create tables and masking policies in a schema you own.
Step 1: Set up a demo table with masking policies¶
Create a table with columns that hold sensitive data, then attach masking policies so the email and salary fields are hidden from non-privileged roles.
Replace <your_database> and <your_schema> with your own database and schema.
Step 2: Confirm masking outside a sandbox¶
Run the same SELECT as your regular role (one that is neither ACCOUNTADMIN nor SYSADMIN). You should see masked values in the email and salary columns.
Expected result:
The salary column returns 0 and email shows only the domain portion — both policies applied to your role.
Step 3: Run the same query inside a sandbox¶
Now run the identical SELECT from inside a sandbox. Save the following as main.py and replace the database and schema placeholders.
How to run this example¶
Expected output:
The output inside the sandbox is identical to the output from Step 2. The masking policies applied in both cases.
Create the sandbox with the same non-privileged role you used in Step 2. The sandbox runs
with the role of the session that created it, so creating it as ACCOUNTADMIN or
SYSADMIN makes these policies return the real values — the query would come back
unmasked, which is the policy working as written rather than governance being bypassed.
What this demonstrates¶
The sandbox inherits your session identity. When the code inside runs a query, Snowflake evaluates the applicable masking and row access policies for your role — the same way it would for any other SQL session. There is no way for code inside a sandbox to see more data than your role already permits.
This property is what makes Cortex Sandboxes safe for running AI-generated code over sensitive tables:
- An LLM-generated query that tries
SELECT * FROM sensitive_tablereturns only what your role can see. - An agent that iterates over query results cannot exfiltrate unmasked data, because the unmasked values are never returned to the container.
- Audit logs record all queries the sandbox runs, attributed to your user and role, just as they would for any other session.
See Cortex Sandboxes concepts and security for a full explanation of the security model.
Going further¶
- To add a row access policy (to restrict which rows are visible at all, not just which column values), see Row Access Policies in the Snowflake documentation.
- To verify the same behavior with a Cortex AI workload that queries this table, combine this setup with Tutorial: Analyze Snowflake data with Cortex AI in a sandbox.