8000 fix(http,auth/generic): harden SSRF blocklist, path params, redirects, and OIDC JWKS client by wangauone · Pull Request #4701 · googleapis/mcp-toolbox · GitHub
[go: up one dir, main page]

Skip to content

fix(http,auth/generic): harden SSRF blocklist, path params, redirects, and OIDC JWKS client - #4701

Open
wangauone wants to merge 2 commits into
mainfrom
fix/http-ssrf-redirect-oidc-hardening
Open

wangauone wants to merge 2 commits into
mainfrom
fix/http-ssrf-redirect-oidc-hardening

Conversation

@wangauone
Copy link
Copy Markdown
Contributor

Description

Hardens the HTTP source, HTTP tool, and generic OIDC auth service against SSRF bypasses, path parameter smuggling, cross-origin credential leakage on redirects, and unhardened JWKS HTTP fetches (tracking b/587679837 and b/591501548).

Summary of Changes

  1. HTTP source SSRF IP blocklist (internal/sources/http/http.go):
    • Normalized IPs with net/netip.AddrFromSlice and Unmap() when AllowPrivateNetworks is false so IPv4-mapped IPv6 addresses (e.g. ::ffff:127.0.0.1, ::ffff:169.254.169.254) receive the same checks as IPv4.
    • Blocked !addr.IsGlobalUnicast() || addr.IsPrivate() plus IANA special-purpose and transition ranges missing from Go stdlib: 0.0.0.0/8, 100.64.0.0/10, 192.0.0.0/24, 192.0.2.0/24, 198.18.0.0/15, 198.51.100.0/24, 203.0.113.0/24, 240.0.0.0/4, ::/96, 64:ff9b::/96, 64:ff9b:1::/48, 100::/64, 2001::/23, 2001:db8::/32, 2002::/16, and fec0::/10, while preserving AllowedRanges and CustomBlocked precedence.
    • Added zone-tolerant IP literal parsing in Initialize, dialer Control, and CheckRedirect.
  2. Cross-origin redirect header stripping (internal/sources/http/http.go):
    • Updated createHTTPClient CheckRedirect to detect cross-origin redirects across all prior hops in via (normalizing scheme, case-insensitive hostname, IPv6 brackets, and default ports 80/443), including for IP-literal redirects.
    • On cross-origin redirects (including A -> B -> A bounces), stripped all headers except safe non-credential negotiation headers (User-Agent, Accept, Accept-Encoding, Accept-Language), dropping X-Api-Key, Authorization, Cookie, and custom headerParams. Same-origin redirects preserve all headers.
  3. HTTP tool path parameter smuggling (internal/tools/http/http.go):
    • Split the configured path template at its first ? before template execution.
    • Rejected ? and # in the rendered path portion (and # injection in rendered query templates) so unescaped {{.param}} values cannot smuggle query parameters or fragments, while keeping {{pathEscape .param}} and static query templates working without double escaping.
  4. Generic OIDC JWKS HTTP client and lifecycle (internal/auth/generic/generic.go, internal/server/primitives/primitives.go, internal/server/server.go):
    • Replaced keyfunc.NewDefault with keyfunc.NewDefaultOverrideCtx using a cancelable context and httpClient (newSecureHTTPClient(), 10s timeout, CheckRedirect returning http.ErrUseLastResponse).
    • Added AuthService.Close() and closed replaced/stopped auth services on PrimitiveManager.SetPrimitives and Server.Shutdown so background JWKS refresh goroutines do not leak on reload or shutdown.

Security Verification & Tests Run

  • gofmt -l . (clean)
  • go vet ./...
  • go build ./...
  • go mod tidy && git diff --exit-code go.mod go.sum
  • go test -race -count=1 ./internal/sources/http/... ./internal/tools/http/... ./internal/auth/... ./internal/server/primitives/... ./internal/server
  • go test -race ./internal/auth/... ./internal/server/... ./cmd/...
  • go test -run '^$' ./tests/http ./tests/auth (compile check)

PR Checklist

  • Ensure you have manually reviewed the entire diff before requesting a review
  • Ensure the tests and linter pass
  • Code coverage does not decrease

@wangauone
wangauone requested a review from a team as a code owner December 4, 2026 22:25
@gemini-code-assist gemini-code-assist Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several security and resource management improvements. It hardens the generic auth service by using a secure HTTP client for JWKS fetching, introducing a Close method to cancel background refresh goroutines, and ensuring replaced or shut down auth services are properly closed. It also enhances SSRF protection by expanding blocked non-global IP ranges using netip and stripping sensitive headers on cross-origin redirects. Additionally, the HTTP tool's URL construction is refactored to prevent query string or fragment injection via path parameters. Feedback on these changes suggests updating the Close method receiver in AuthService to a pointer receiver to avoid copying the struct.

Comment thread internal/auth/generic/generic.go Resolved
@wangauone
Copy link
Copy Markdown
Contributor Author

/gcbrun

This branch has not been deployed

No deployments
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.

2 participants

0