Webhook Middleware Phase 3: Mutating webhook middleware with JSONPatch#4372
Open
Sanskarzz wants to merge 2 commits intostacklok:mainfrom
Open
Webhook Middleware Phase 3: Mutating webhook middleware with JSONPatch#4372Sanskarzz wants to merge 2 commits intostacklok:mainfrom
Sanskarzz wants to merge 2 commits intostacklok:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Large PR Detected
This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.
How to unblock this PR:
Add a section to your PR description with the following format:
## Large PR Justification
[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformationAlternative:
Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.
See our Contributing Guidelines for more details.
This review will be automatically dismissed once you add the justification section.
10 tasks
2374b6d to
debd458
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4372 +/- ##
==========================================
+ Coverage 69.47% 69.56% +0.08%
==========================================
Files 485 489 +4
Lines 49805 49987 +182
==========================================
+ Hits 34603 34772 +169
+ Misses 12523 12522 -1
- Partials 2679 2693 +14 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
debd458 to
048ccda
Compare
Signed-off-by: Sanskarzz <sanskar.gur@gmail.com>
3cb6f58 to
5c07075
Compare
Signed-off-by: Sanskarzz <sanskar.gur@gmail.com>
5c07075 to
f4c5947
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements Phase 3 of the Dynamic Webhook Middleware system: Mutating Webhook Middleware, as outlined in RFC THV-0017.
This PR adds support for mutating webhooks that allow external HTTP services to transform incoming MCP requests dynamically using RFC 6902 JSON Patch operations. Mutating webhooks are executed after the MCP parser but before validating webhooks, allowing organizations to cleanly intercept and rewrite requests (e.g., adding default parameters, applying transformations, or injecting audit trails) before validation or authorization logic runs.
Fixes #3398
Changes
1.
pkg/webhook/mutating/PackageIntroduces the core logic for mutating webhooks:
config.go: Defines configuration parameter types (MiddlewareParams,FactoryMiddlewareParams) mirroring the validating package.patch.go: Implements RFC 6902 JSONPatch parsing and application usinggithub.com/evanphx/json-patch/v5.ValidatePatch()to ensure only supported operations are allowed.IsPatchScopedToMCPRequest()) to ensure webhooks can only modify themcp_requestcontainer inside the webhook payload, preventing unauthorized modification of the request principal, context, or envelope metadata.middleware.go: The HTTP middleware factory and handler.failvsignore) for connection errors or malformed patches.2. Runner Wiring (
pkg/runner/)config.go: AddedMutatingWebhooks []webhook.ConfigtoRunConfigto allow configuring mutating webhooks natively via state persistence or configuration YAML.middleware.go: Registered the mutating webhook middleware factory and inserted it into the middleware execution chain. It executes proactively before the validating webhook middleware, matching the RFC specifications.3. Dependencies
github.com/evanphx/json-patch/v5for RFC 6902-compliant patch application.4. Addressed Phase 2 leftover review comments
sendErrorResponsein the validating webhook middleware.pkg/webhook/validating/middleware_test.go.convertToJSONRPC2IDimplementations from all 3 middlewares (authz, validating, mutating) to a central location (pkg/mcp/utils.go).5. Full Middleware Chain Integration Test
TestWebhookMiddlewareChainIntegrationtopkg/runner/webhook_integration_test.goto test the execution flow of both mutating and validating webhooks as initialized by the runner configuration parsing.6. Test Coverage Improvements:
pkg/webhook/mutating/patch.goandpkg/webhook/mutating/middleware.gocoverage.PopulateMiddlewareConfigsinpkg/runner/middleware.goby addingTestPopulateMiddlewareConfigs_FullCoveragewhich spans across all configuration branch setups.Type of change
Test plan
task test)task test-e2e)task lint-fix)