# SecondGuard guardrails

> SecondGuard is the guardrail in front of SecondGate that blocks, masks or logs secrets and personal data in each request and restores masked values in answers.

Canonical: <https://docs.secondstack.ai/gateway/secondguard/>
Updated: 2026-11-06
Checked against: v3.7.2

---

SecondGuard is the guardrail service in front of SecondGate that blocks, masks or logs secrets and personal data on the request path and restores masked values in the answer. It runs on your own hosts and guards the Chat App, every agent, Agent Claude and your own code. Administrators choose what each guard does.

**Labels in the app**

| Our word | Label in the app |
|---|---|
| a masked value, in a sent message | Masked as [@##@:EMAIL_1] |
| a restored value, in an answer | Unmasked from [@##@:EMAIL_1] |
| the guardrails panel | Detected and masked |
| a guardrail rule | Disposition |
| the admin page | Guardrails |

The screen below shows one turn in the Chat App with two values masked. The sent message keeps its marks. The **Detected and masked** popover lists the checks that ran and the placeholder types. The answer shows the address restored.

## Where SecondGuard sits

SecondGuard is its own proxy between the web front door and SecondGate: a request goes from the client to OpenResty, to the SecondGuard proxy, to [SecondGate](/gateway/) and on to the provider.

SecondStack deploys the proxy by default, with `SECONDGUARD_DEPLOY=true`. The proxy fails closed, so no request reaches a model while it is deployed and down. A site that sets `SECONDGUARD_DEPLOY=false` inspects nothing, and every request passes through unchanged. Detection runs in a worker fleet beside the proxy, so a site can add workers when it needs more capacity.

## What a guard can do

Each guard binds to models, or to model tags, with one of three dispositions, and the strictest wins when several match: `block` over `mask` over `log`.

- **block** rejects the request with a structured error. The Chat App shows **Blocked by a guardrail** and names the check, and an agent turn reports "Request blocked by guardrails" with the guards that matched.
- **mask** replaces each matched span with a placeholder, forwards the request, and restores the original in the answer.
- **log** detects and audits only, and forwards the request untouched.

The guard types at v3.7.2 are regex rules, word lists, an LLM check on a message, an LLM check on an image or file part, a system prompt instruction that tells the model how to treat placeholders, high-entropy secret detection, Presidio for named entities, and site modules a site writes itself. A `mask` binding on a structural field, such as a tool name, is applied as `log`, because the model needs that field intact.

## How masking and restoring work

SecondGuard replaces a detected value with a placeholder such as `[@##@:EMAIL_1]`, sends the placeholder to the model, and puts the original back wherever the placeholder appears in the answer.

- The same original gets the same placeholder every time, across requests, sessions and users, so a model can reason about "EMAIL_1" through a whole conversation.
- Originals are stored encrypted, and a request can restore only the placeholders it was shown.
- Streaming answers are restored on the way through. The proxy keeps a short tail per field, so a placeholder split across two chunks still reassembles.
- Tool-call arguments are restored too, before the Chat App stores a message or an agent runs a tool. An agent that echoes a masked credential into a tool call therefore gets the real value, and the model never did.
- In the Chat App, a masked span in your sent message carries a tooltip (**Masked as**) with its placeholder, and a restored span in the answer carries **Unmasked from**.

## How attachments are scanned

The Chat App submits every upload for scanning before the file becomes usable, images as bytes and everything else as the text extracted from them.

A blocked file loses its bytes, a masked file gets a preview, and the real masking happens inline when the file's text enters a request. An attachment shows **Security scanned**, **Sensitive content masked**, **Blocked**, **Scan unavailable** or **Security scan incomplete** in the thread. Collections are scanned when their text is retrieved into a prompt, not when a file is added.

## What the default site does

The default site masks credentials and API keys for every model, masks email addresses and payment data toward provider-hosted models, logs high-entropy strings, and ships Presidio configured but off.

SecondStack's own test suite runs behind this light posture, and it detects almost no personal data, so a production site needs its own policy. A second site configuration, `default-guardrails`, turns on every guard type. Your administrator sets the policy on the **Guardrails** page in ControlTower, with the tabs **Guards**, **Stats**, **Audit log**, **Validation** and **Cache**. **Deploy Guardrails Configuration** applies it. The **Stats** tab counts outcomes as **Allowed (as-is)**, **Allowed (logged)**, **Allowed (with masking)**, **Blocked** and **Refused (unavailable)**.

## For administrators

An administrator sets the guards, binds them to models or tags, chooses what a failed check does and adds exemptions.

- Each model carries **Guardrail Tags (SecondGuard)** in its editor, and a guard binds to every model, to one model or to a tag such as `external`, so one rule can cover all provider-hosted models and skip the ones on your own GPU.
- A guard's fail mode decides whether a check that times out or errors stops the request or lets it through.
- Exemptions come from an allow list, an email domain allow list and, per request, a header that names literals to leave alone.
- A site that ran without guardrails adds the worker fleet file, `guardrails.yaml` and the `guardrails_*` settings, sets `SECONDGUARD_DEPLOY=true` and runs `just init`.
- A site can journal requests and attachments for security review under its retention policy, and the popover tells people so.

[Self-hosting](/self-hosting/) covers the deployment, and [What is SecondStack](/getting-started/what-is-secondstack/) shows where SecondGuard sits on the map.

## What SecondGuard does not do

- SecondGuard does not scan answers. It only restores masked values on the way back, so it never looks for prompt injection, dangerous tool calls or wrong facts in a response.
- SecondGuard catches accidental disclosure. A value split across messages, encoded or spelled in a variant is a different string to it and gets its own placeholder, or none.
- SecondGuard does not run OCR, transcription or PDF parsing itself. It cannot mask anything inside a binary attachment, and audio sent for transcription passes verbatim.
- SecondGuard never fetches a URL that a message mentions.
- SecondGuard does not mask dates or birthdays. It also judges a value without context, so a number that looks like a card number counts as one.
- Guards bind only to models and tags. An administrator cannot set a rule per team, group or person.
- The LLM check guards see the unmasked content, because they are the ones deciding what to mask.
- SecondGuard does not encrypt your database. It protects what leaves for a provider, and application stores keep plaintext.

## Related

- [Gateway](/gateway/)
- [Model providers](/gateway/providers/)
- [Call SecondGate from your code](/developers/gateway-api/)
- [Concepts and names](/getting-started/concepts/)

On secondstack.ai: [How SecondGate checks every AI call](https://secondstack.ai/platform/gateway/)
