SecurityPolicy is passed to Sandbox.create() via the security parameter. It composes PII detection, injection defense, toxicity / code-security / invisible-text scanners, network policy, transformation rules, audit logging, and environment variable security into a single object.
SecurityPolicy
Prompt injection defense. Pass
True to enable with defaults, or an
InjectionDefenseConfig for custom settings. See
InjectionDefenseConfig.List of regex-based request/response body transformations. See
TransformationRule.
Network allowlist/denylist policy. See NetworkPolicy.
Audit logging. Pass
True to enable with defaults, or an AuditConfig
for custom retention and body logging settings.Toxicity scanner for outbound HTTP request bodies. See
ToxicityConfig.
Code-security scanner for outbound HTTP request bodies. See
CodeSecurityConfig.
Invisible-Unicode scanner for outbound HTTP request bodies. See
InvisibleTextConfig.
Environment variable masking in audit logs. See
EnvSecurityConfig.
Properties
| Property | Type | Description |
|---|---|---|
policy.injection_config | InjectionDefenseConfig | Resolved config regardless of whether a bool or object was passed. |
policy.audit_config | AuditConfig | Resolved audit config. |
policy.requires_tls_interception | bool | True if PII, injection defense, or any transformations are enabled. |
Methods
| Method | Returns | Description |
|---|---|---|
policy.to_dict() | dict | Serialize to a JSON-compatible dict. |
policy.to_json() | str | Serialize to a JSON string. |
SecurityPolicy.from_dict(data) | SecurityPolicy | Deserialize from a dict. |
PIIConfig
Configure detection and handling of personally identifiable information in outbound HTTP traffic.Whether PII scanning is active.
PII types to scan for. Defaults to all
PIIType values. Valid values are
the string values of PIIType.Action to take when PII is detected. One of
'redact', 'block',
'log_only'.When
True, the security proxy replaces redaction tokens in API responses
with the original values so the agent sees real data in replies.Limit PII scanning to requests targeting these domains.
None means scan
all domains.PIIType enum
RedactionAction enum
InjectionDefenseConfig
Detect and block prompt injection attempts in outbound HTTP request bodies.Whether injection defense is active.
Action when injection is detected. One of
'block' (HTTP 403),
'sanitize' (strip the injected text and forward), or 'log_only'
(log and forward unchanged).Preset sensitivity tier. One of
'low', 'medium', 'high'. Adjusts the
scanner’s detection aggressiveness independently of threshold.Confidence threshold between
0.0 and 1.0. Requests with a score above
this value trigger the configured action. Lower values increase sensitivity.Limit injection scanning to these domains.
None means scan all.InjectionAction enum
InjectionSensitivity enum
ToxicityConfig
Scan outbound HTTP request bodies for toxic content (harassment, hate speech, etc.).Whether toxicity scanning is active.
Confidence threshold between
0.0 and 1.0. Requests scoring above this
value trigger the configured action.Action when toxic content is detected. One of
'block' (HTTP 403) or
'log_only'.CodeSecurityConfig
Detect suspicious or unsafe code in outbound HTTP request bodies.Whether code-security scanning is active.
Confidence threshold between
0.0 and 1.0.Action when suspicious code is detected. One of
'block' (HTTP 403) or
'log_only'.Languages to exclude from scanning. Useful when content is intentionally
code but already in a trusted context.
InvisibleTextConfig
Detect invisible or control Unicode characters (often used to smuggle prompt instructions past the model) in outbound HTTP request bodies.Whether invisible-text scanning is active.
Action when invisible characters are detected. One of
'block' (HTTP 403),
'strip' (remove the characters and forward), or 'log_only'.NetworkPolicy
Network allowlist and denylist for outbound traffic from the sandbox. Set this onSecurityPolicy.network to apply it alongside other security controls.
Destinations to allow. Accepts IP addresses, CIDR blocks (e.g.
"10.0.0.0/8"), and domain names with optional wildcard prefix (e.g.
"*.github.com").Destinations to deny. Accepts IP addresses and CIDR blocks only (domains
not accepted in deny rules).
Whether to allow all public traffic by default. Set to
False when using
allow_out to build an allowlist.Replace the
Host header in all outbound requests with this value. Used
for routing through a reverse proxy.ALL_TRAFFIC constant
deny_out=[ALL_TRAFFIC] to block all outbound traffic.
SandboxNetworkOpts
SandboxNetworkOpts is the lower-level equivalent used directly in Sandbox.create(network=...). It has the same fields as NetworkPolicy using snake_case attribute names.
TransformationRule
Regex-based text transformation applied to outbound request bodies, inbound response bodies, or both.Python-compatible regular expression. Must be a valid regex pattern.
Replacement string. Supports Python
re.sub back-references (e.g. \1).Direction to apply the rule. One of
'outbound', 'inbound', 'both'.TransformDirection enum
Methods
| Method | Returns | Description |
|---|---|---|
rule.applies_to(direction) | bool | Whether the rule applies in the given direction string. |
rule.apply(text) | str | Apply the regex substitution to text. |
AuditConfig
Control what the MITM proxy logs for security events.Whether audit logging is active.
Whether to include the request body in audit log entries.
Whether to include the response body in audit log entries.
How many hours to retain audit log entries.
AuditEntry
EnvSecurityConfig
Control how environment variable values are masked in audit logs.Glob patterns matched against uppercase environment variable names. Variables
matching any pattern are masked as
*** in audit logs.Automatically redact matching variable values in all audit log entries.