Skip to content

feat: add configurable request and response guardrails - #2696

Open
fernandoescolar wants to merge 10 commits into
theagentrouter:mainfrom
fernandoescolar:feat/guardrails
Open

fernandoescolar wants to merge 10 commits into
theagentrouter:mainfrom
fernandoescolar:feat/guardrails

Conversation

@fernandoescolar

@fernandoescolar fernandoescolar commented Sep 16, 2026

Copy link
Copy Markdown

Description

This change adds native content guardrails to Envoy AI Gateway through a new GuardrailPolicy resource and request/response enforcement in the external processor.

The design proposal is included at docs/proposals/013-guardrails/proposal.md. It compares the ext-proc architecture with the Built on Envoy Bedrock Guardrails and Azure Content Safety dynamic-module extensions and records the adopted decisions, current scope, follow-up work, and open questions.

The API is available in both v1alpha1 and v1beta1 and targets AIServiceBackend resources. Rules support request or response evaluation, deterministic multi-policy composition, configurable payload limits and timeouts, and explicit fail-open or fail-closed behavior. Actions can block, monitor, or mask matching content. CRD admission validates provider configuration, actions, endpoints, targets, and unique rule names.

The runtime supports:

  • deterministic in-process regular expression rules;
  • Presidio analyzer requests;
  • AWS Bedrock ApplyGuardrail requests signed with SigV4; and
  • Azure AI Content Safety text analysis.

Provider credentials are resolved from Kubernetes Secrets or, for Bedrock, from the standard AWS credential chain. Secret changes and policy deletion trigger reconciliation of affected routes. Missing or revoked provider configuration follows the rule's failure mode and does not leave stale gateway configuration active.

External providers receive schema-extracted text instead of serialized request envelopes. Mask actions write transformed text back to its original JSON field. Response guardrails keep streaming responses buffered until evaluation completes, preventing unsafe content from being partially delivered. Violations return HTTP 403 with the GuardrailViolation error type.

The implementation adds structured logs without payload content, an OpenTelemetry evaluation counter, and guardrail span events. It also includes example manifests and task-oriented documentation.

Validation includes unit tests, CRD admission tests, controller lifecycle and Secret rotation tests, HTTP-stub integration tests, Envoy dataplane tests for request and response blocking, and a Testcontainers test against the pinned official Presidio analyzer image. Live Azure AI Content Safety and AWS Bedrock Guardrails tests passed against real provider resources. Credential-gated live tests remain skipped by default.

GitHub Copilot was used to assist with implementation, tests, documentation, and review. The contributor has reviewed and takes responsibility for the resulting changes.

Related Issues/PRs (if applicable)

fixes: #1409 (Prompt guard moderation)
fixes: #1415 (pii detection)
fixes: #1919 (external security provider)
related: #2128 (content governance)

Special notes for reviewers (if applicable)

The main review areas are the dual-version CRD contract, Secret lifecycle behavior, backend scoping, fail-open/fail-closed semantics, response buffering, and external provider request formats.

Live provider validation requires local credentials and is intentionally not part of default CI. Presidio requires Docker but no credentials.

# Presidio
export TEST_PRESIDIO_ENDPOINT="https://..."
export TEST_PRESIDIO_API_KEY="..." # optional
export TEST_PRESIDIO_BLOCKED_TEXT="text that your policy blocks"

# Azure Content Safety
export TEST_AZURE_CONTENT_SAFETY_ENDPOINT="https://..."
export TEST_AZURE_CONTENT_SAFETY_API_KEY="..."
export TEST_AZURE_CONTENT_SAFETY_BLOCKED_TEXT="text that should be blocked"

# Bedrock Guardrails
export TEST_AWS_BEDROCK_GUARDRAIL_REGION="us-east-1"
export TEST_AWS_BEDROCK_GUARDRAIL_ID="..."
export TEST_AWS_BEDROCK_GUARDRAIL_VERSION="1"
export TEST_AWS_BEDROCK_GUARDRAIL_BLOCKED_TEXT="text that should be blocked"
# AWS credentials for Bedrock Guardrails
export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."

@fernandoescolar
fernandoescolar requested a review from a team as a code owner September 16, 2026 17:20
@netlify

netlify Bot commented Sep 16, 2026

Copy link
Copy Markdown

Deploy Preview for theagentrouter canceled.

Name Link
🔨 Latest commit 5d28c3f
🔍 Latest deploy log https://app.netlify.com/projects/theagentrouter/deploys/6aacdfd5feedd300083b09ae

@missBerg

missBerg commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

I haven't checked, as there are many PRs open at the moment, but have you submitted a design doc PR for this? Would be helpful!
Also, have you checked these extensions:

https://builtonenvoy.io/extensions/bedrock-guardrails/
https://builtonenvoy.io/extensions/azure-content-safety/

@fernandoescolar
fernandoescolar marked this pull request as draft September 16, 2026 18:19
@fernandoescolar
fernandoescolar force-pushed the feat/guardrails branch 2 times, most recently from ee3754a to 6f33ff0 Compare September 16, 2026 21:19
Fernando Escolar added 8 commits September 16, 2026 23:24
- add GuardrailPolicy CRD in v1alpha1 and v1beta1
- support dual-version schema and runtime config wiring
- compile regex-based guardrails into runtime matcher state
- enforce request/response guardrails in extproc before upstream or client response
- add guardrail regression tests for runtime evaluation

Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
- Implemented support for regex guardrails and external providers including Presidio, AWS Bedrock, and Azure AI Content Safety.
- Added failure behavior configuration for external providers.
- Enhanced observability with structured logging for guardrail evaluations.
- Created test cases for various guardrail policy scenarios including validation of rules and provider configurations.
- Added documentation for content guardrails and examples for usage.

Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
…ed documentation

Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
- Introduced maxRequestBodyBytes and maxResponseBodyBytes to limit payload sizes.
- Updated GuardrailEvaluator interface to return GuardrailEvaluationResult.
- Enhanced Presidio and Bedrock evaluators to support masking functionality.

Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
Signed-off-by: Fernando Escolar <fernando.escolar@digits.schwarz>
@fernandoescolar

Copy link
Copy Markdown
Author

I’ve added a proposal/technical design document (docs/proposals/013-guardrails/proposal.md) to help clarify the motivation, architecture, implementation details, and current scope of this PR.

I’m sorry I did not follow the project’s contribution and review protocol correctly from the beginning. I’ve reviewed the guidelines and updated the PR to align with them.

@fernandoescolar
fernandoescolar marked this pull request as ready for review September 16, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding support for external security provider integration Native Integraion with vLLM Semantic Router Prompt Gaurd/Moderation

2 participants