feat(guardrail): add regex#105
Conversation
📝 WalkthroughWalkthroughThis pull request adds regex-based pattern matching guardrails alongside existing bedrock guardrails. The implementation includes a new regex guardrail config and runtime, JSON schema updates for validation, proxy wiring, and comprehensive integration tests across three API endpoints. ChangesRegex Guardrails Feature
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Suggested reviewers
Important Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional. ❌ Failed checks (1 error, 1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
src/proxy/guardrails.rs (1)
650-659: ⚡ Quick winUse project-standard assertion macros in the new Rust test.
Please switch the new assertions to
pretty_assertions::assert_eq!andassert_matches::assert_matches!for consistency with repository test standards.As per coding guidelines "`{tests,src}/**/*.rs`: Use pretty_assertions::assert_eq and assert_matches::assert_matches for better test output."Suggested fix
#[test] fn configured_guardrail_runtime_from_configs_builds_regex_runtime() { let runtime = configured_guardrail_runtime_from_configs(&GuardrailConfig::Regex( RegexGuardrailConfig::new("secret", Some("matched blocked content".into())).unwrap(), )) .unwrap(); - assert_eq!(runtime.name(), "regex"); - assert!(runtime.supports_stage(GuardrailStage::Output)); + pretty_assertions::assert_eq!(runtime.name(), "regex"); + assert_matches::assert_matches!(runtime.supports_stage(GuardrailStage::Output), true); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/proxy/guardrails.rs` around lines 650 - 659, The test configured_guardrail_runtime_from_configs_builds_regex_runtime should use the project-standard test macros: replace assert_eq!(runtime.name(), "regex") with pretty_assertions::assert_eq!(runtime.name(), "regex") and replace the boolean assert!(runtime.supports_stage(GuardrailStage::Output)) with assert_matches::assert_matches!(runtime.supports_stage(GuardrailStage::Output), true); ensure you add/import pretty_assertions::assert_eq and assert_matches::assert_matches if not already in scope and keep the rest of the test (configured_guardrail_runtime_from_configs, GuardrailConfig::Regex, RegexGuardrailConfig::new) unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/proxy/guardrail/shared.ts`:
- Around line 157-165: The cleanup logic for closing resources can leak the
server process if upstream.close() throws; update both the returned close()
implementation and the catch-block cleanup to use try/finally so server.exit()
always runs even when upstream.close() fails. Specifically, in the close: async
() => { ... } and in the catch { ... } replace the sequential awaits with a try
{ await upstream?.close(); } finally { await server?.exit(); } pattern (keeping
the throw in the catch handler) so both upstream.close() and server.exit() are
guaranteed to run; reference the close function and the catch block surrounding
upstream and server variables to locate the changes.
- Around line 23-31: The ensureStatus helper currently includes the full
response body via JSON.stringify(response.data) which may leak sensitive admin
fields; modify ensureStatus to avoid serializing the whole payload — instead log
a minimal/redacted summary (e.g., response.status plus a truncated or redacted
placeholder like "<redacted response>" or the first N chars of a safely
stringified value) and remove JSON.stringify(response.data) from the thrown
Error; update the error message construction in ensureStatus so it only includes
non-sensitive identifiers and the redacted/minimal payload indicator.
---
Nitpick comments:
In `@src/proxy/guardrails.rs`:
- Around line 650-659: The test
configured_guardrail_runtime_from_configs_builds_regex_runtime should use the
project-standard test macros: replace assert_eq!(runtime.name(), "regex") with
pretty_assertions::assert_eq!(runtime.name(), "regex") and replace the boolean
assert!(runtime.supports_stage(GuardrailStage::Output)) with
assert_matches::assert_matches!(runtime.supports_stage(GuardrailStage::Output),
true); ensure you add/import pretty_assertions::assert_eq and
assert_matches::assert_matches if not already in scope and keep the rest of the
test (configured_guardrail_runtime_from_configs, GuardrailConfig::Regex,
RegexGuardrailConfig::new) unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: bb01b40c-6489-41b4-9e03-c3d909b50e36
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (14)
Cargo.tomlcrates/aisix-guardrail/Cargo.tomlcrates/aisix-guardrail/src/guardrails/mod.rscrates/aisix-guardrail/src/guardrails/regex.rssrc/config/entities/guardrails-schema.jsonsrc/config/entities/guardrails.rssrc/proxy/guardrails.rstests/package.jsontests/proxy/guardrail/chat-completions.test.tstests/proxy/guardrail/messages.test.tstests/proxy/guardrail/responses.test.tstests/proxy/guardrail/shared.tstests/utils/admin.tstests/utils/etcd.ts
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In @.github/workflows/build.yaml:
- Around line 35-43: The workflow step that downloads and installs etcdctl (env
ETCD_VER, the curl/tar/sudo install sequence and final etcdctl version check)
lacks checksum verification; update the step to also download the release SHA256
sums (e.g. the repository's SHA256SUMS or per-asset .sha256), verify the
downloaded tarball with sha256sum (or sha256sum -c) before running sudo install,
and fail the job when the checksum does not match so the invalid artifact is
never installed.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a9a0cfe0-f4ab-4ab4-9fc5-c6533f7429e5
📒 Files selected for processing (1)
.github/workflows/build.yaml
Summary by CodeRabbit
New Features
Configuration
Tests
Chores