> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Mask PII and Secrets

> Classify sensitive columns, mask them with labels, and redact SSNs in LLM requests.

Data Discovery labels sensitive columns during scheduled scans. A response-stage policy replaces matching SSNs and passwords with `[REDACTED]`. A request-stage rewrite can redact SSNs in LLM messages.

## 1. Classify sensitive columns

Deploy a [Data Discovery Satellite](https://docs.formal.ai/docs/guides/core-concepts/satellites.md#data-discovery-satellite) and schedule scans for each resource.
Discovery applies label matchers during scans. Editing a matcher alone does not reclassify columns.

Built-in labels include `social_security_number` and `password`.
Prompt labels use column metadata and up to 10 sampled values. To generate them, you need an LLM classification backend and a native user with `SELECT` permission.
Review labels in [Data Inventory](https://docs.formal.ai/docs/guides/core-concepts/data-inventory.md), then lock columns you classified manually.

## 2. Mask columns by label

This response-stage policy replaces labeled SSNs and passwords with `[REDACTED]`:

```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
package formal.v2

import future.keywords.if
import future.keywords.in

response := {
  "action": "mask",
  "type": "redact.constant_characters",
  "sub_type": "redact.constant_characters",
  "redact": "[REDACTED]",
  "columns": columns
} if {
  columns := [col |
    col := input.row[_]
    col["data_label"] in ["social_security_number", "password"]
  ]
  count(columns) > 0
}
```

To hash, nullify, or fake values instead, see [Mask Action](https://docs.formal.ai/docs/guides/policies/enforcement.md#mask-action).

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# Directly to the database
psql -h DB_HOSTNAME -p 5432 -d app -U postgres \
  -c "SELECT customer_id, social_security_number FROM customers LIMIT 1;"
 customer_id | social_security_number
-------------+------------------------
        1042 | 123-45-6789

# Through the Connector
psql -h CONNECTOR_HOSTNAME -p 5432 -d app@RESOURCE_NAME \
  -U idp:formal:human:john@joinformal.com \
  -c "SELECT customer_id, social_security_number FROM customers LIMIT 1;"
 customer_id | social_security_number
-------------+------------------------
        1042 | [REDACTED]
```

## 3. Test, then activate

Create the policy in **Draft** while you review the Rego.
Set it to **Dry-run** to log matches.
Review [Logs](https://docs.formal.ai/docs/guides/observability/logs.md) for the expected policy and labeled columns.
Then set the policy to **Active** and verify a test query returns masked values.

Dry-run does not enforce policy actions, so use an active test query to confirm masking.

## 4. Redact SSNs from LLM requests

For LLM resources, use a request-stage rewrite. This example replaces dashed US SSNs in text and tool results:

```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
package formal.v2

import future.keywords.if
import future.keywords.in

ssn_pattern := `\b\d{3}-\d{2}-\d{4}\b`

request := {
  "action": "rewrite",
  "body": regex.replace(input.http.body, ssn_pattern, "[REDACTED-SSN]"),
  "reason": "Redact SSNs from LLM request messages",
} if {
  input.resource.technology == "llm"
  some msg in input.llm.messages
  some part in msg.content_parts
  part.type in {"text", "tool_result"}
  regex.match(ssn_pattern, part.text)
}
```

This replaces matching text in `input.http.body` before forwarding the request.
The example matches dashed SSNs only. Adjust the pattern for your formats.
Start the policy in **Dry-run**, review the logs, and then activate it.

## Next Steps

> [card] [Mask Action](https://docs.formal.ai/docs/guides/policies/enforcement.md#mask-action)
>
> Review supported types and subtypes

> [card] [Policy Examples](https://docs.formal.ai/docs/guides/policies/examples.md)
>
> Explore label-based and request-stage policies


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.