Skip to content

Commit bde4ae2

Browse files
JAORMXclaude
andcommitted
Redact webhook response body from returned errors
doHTTPCall previously embedded the first 256 bytes of any non-2xx webhook response body into the returned error. That error then propagated into slog.Error in the validating and mutating middlewares, so a tenant who pointed MCPWebhookConfig.url at an internal service could read up to 256 bytes of any error response straight into the cluster's logs. Move the body preview to slog.Debug inside the webhook package so operators can still troubleshoot, and shape the returned error as status-code-only. The webhook name is still attached by NewNetworkError / NewInvalidResponseError, and the status code is still preserved on InvalidResponseError so IsAlwaysDenyError continues to detect HTTP 422. A new TestClientErrorDoesNotLeakResponseBody test pins the contract: the returned err.Error() must contain the status code but must not contain sentinel bytes from the response body. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9cc163a commit bde4ae2

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

pkg/webhook/client.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"errors"
1313
"fmt"
1414
"io"
15+
"log/slog"
1516
"net"
1617
"net/http"
1718
"os"
@@ -148,16 +149,32 @@ func (c *Client) doHTTPCall(ctx context.Context, body []byte) ([]byte, error) {
148149

149150
// 5xx errors indicate webhook operational failures.
150151
if resp.StatusCode >= http.StatusInternalServerError {
152+
// Body preview is logged at debug level so operators can troubleshoot,
153+
// but is kept out of the returned error chain to avoid surfacing
154+
// potentially sensitive bytes (e.g. from an internal service reached
155+
// via a misconfigured URL) into higher-level error logs.
156+
slog.Debug("webhook returned server error",
157+
"webhook", c.config.Name,
158+
"url", c.config.URL,
159+
"status_code", resp.StatusCode,
160+
"body_preview", truncateBody(respBody),
161+
)
151162
return nil, NewNetworkError(c.config.Name,
152-
fmt.Errorf("webhook returned HTTP %d: %s", resp.StatusCode, truncateBody(respBody)))
163+
fmt.Errorf("webhook returned HTTP %d", resp.StatusCode))
153164
}
154165

155166
// Non-200 responses (excluding 5xx handled above) are treated as invalid.
156167
// The StatusCode is surfaced so callers can distinguish HTTP 422 (RFC always-deny)
157168
// from other non-2xx codes that may follow the failure policy.
158169
if resp.StatusCode != http.StatusOK {
170+
slog.Debug("webhook returned non-2xx response",
171+
"webhook", c.config.Name,
172+
"url", c.config.URL,
173+
"status_code", resp.StatusCode,
174+
"body_preview", truncateBody(respBody),
175+
)
159176
return nil, NewInvalidResponseError(c.config.Name,
160-
fmt.Errorf("webhook returned HTTP %d: %s", resp.StatusCode, truncateBody(respBody)),
177+
fmt.Errorf("webhook returned HTTP %d", resp.StatusCode),
161178
resp.StatusCode)
162179
}
163180

pkg/webhook/client_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,69 @@ func TestDoHTTPCallReadError(t *testing.T) {
783783
assert.Contains(t, err.Error(), "forced read error")
784784
}
785785

786+
// TestClientErrorDoesNotLeakResponseBody verifies that when the webhook returns
787+
// a non-2xx response, the response body bytes are not embedded in the returned
788+
// error chain. This prevents an SSRF-adjacent information-exfiltration path
789+
// where bytes from an internal service reached via a misconfigured webhook URL
790+
// would otherwise be surfaced into higher-level error logs.
791+
func TestClientErrorDoesNotLeakResponseBody(t *testing.T) {
792+
t.Parallel()
793+
794+
const sentinel = "INTERNAL_LEAK_TOKEN"
795+
796+
tests := []struct {
797+
name string
798+
statusCode int
799+
}{
800+
{name: "500 Internal Server Error", statusCode: http.StatusInternalServerError},
801+
{name: "503 Service Unavailable", statusCode: http.StatusServiceUnavailable},
802+
{name: "422 Unprocessable Entity", statusCode: http.StatusUnprocessableEntity},
803+
{name: "418 I'm a teapot", statusCode: http.StatusTeapot},
804+
}
805+
806+
for _, tt := range tests {
807+
t.Run(tt.name, func(t *testing.T) {
808+
t.Parallel()
809+
810+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
811+
w.WriteHeader(tt.statusCode)
812+
_, _ = w.Write([]byte("response containing " + sentinel + " somewhere in the body"))
813+
}))
814+
defer server.Close()
815+
816+
cfg := Config{
817+
Name: "leak-test",
818+
URL: server.URL,
819+
Timeout: 5 * time.Second,
820+
FailurePolicy: FailurePolicyFail,
821+
}
822+
823+
client := newTestClient(cfg, TypeValidating, nil)
824+
825+
req := &Request{
826+
Version: APIVersion,
827+
UID: "test-uid",
828+
Timestamp: time.Now(),
829+
Principal: &auth.PrincipalInfo{Subject: "user1"},
830+
Context: &RequestContext{
831+
ServerName: "test-server",
832+
SourceIP: "127.0.0.1",
833+
Transport: "sse",
834+
},
835+
}
836+
837+
_, err := client.Call(t.Context(), req)
838+
require.Error(t, err)
839+
840+
errMsg := err.Error()
841+
assert.Contains(t, errMsg, strconv.Itoa(tt.statusCode),
842+
"error should surface the HTTP status code so callers can distinguish failure modes")
843+
assert.NotContains(t, errMsg, sentinel,
844+
"error must not embed response body bytes — they may come from an internal service reached via a misconfigured URL")
845+
})
846+
}
847+
}
848+
786849
type mockRoundTripper struct {
787850
resp *http.Response
788851
err error

0 commit comments

Comments
 (0)