8000 fix(server/web,resources): harden MCP App CSP injection, origin validation, and /ui security headers by wangauone · Pull Request #4703 · googleapis/mcp-toolbox · GitHub
[go: up one dir, main page]

Skip to content

fix(server/web,resources): harden MCP App CSP injection, origin validation, and /ui security headers - #4703

Open
wangauone wants to merge 3 commits into
mainfrom
fix/webui-csp-origin-and-dynamic-ui-auth
Open

wangauone wants to merge 3 commits into
mainfrom
fix/webui-csp-origin-and-dynamic-ui-auth

Conversation

@wangauone
Copy link
Copy Markdown
Contributor

Description

Addresses security hardening across MCP App UI resource validation, Web UI CSP construction/injection, /ui security headers, and DynamicUIResource authorization (internal tracking: b/587680006 and b/591502673):

  1. Strict HTTP origin validation (internal/resources/ui.go): Hardened validateHTTPOrigin to reject control characters, whitespace, CSP/HTML delimiters (;, ,, ', ", <, >, \, ?, #, %), userinfo (u.User != nil), non-empty paths (u.Path/u.RawPath, including trailing /), queries, fragments, invalid ports (1..65535), missing hostnames, and malformed wildcard hosts.
  2. CSP origin sanitization and HTML injection hardening (internal/server/static/js/toolDisplay.js):
    • Added sanitizeCspOrigin / sanitizeCspOriginList using new URL(trimmed) inside constructCsp so only strict http: / https: origins are emitted and invalid/injected entries are dropped before constructing directives (frameDomains/baseUriDomains fall back to 'none'/'self' when empty).
    • Rewrote injectCspIntoHtml to parse untrusted HTML via DOMParser when available and prepend the telemetry script immediately before <meta http-equiv="Content-Security-Policy"> at the start of <head>, with a comment-stripping and pre-<head> resource-ordering fallback when DOMParser is unavailable.
  3. /ui security headers and script cleanup (internal/server/web.go, internal/server/static/*.html, internal/server/static/js/bootstrap.js, internal/server/web_test.go):
    • Moved inline bootstrap scripts from index.html, tools.html, and toolsets.html into static/js/bootstrap.js.
    • Added Content-Security-Policy (without 'unsafe-inline' or 'unsafe-eval' in script-src), X-Frame-Options: DENY, X-Content-Type-Options: nosniff, and Referrer-Policy: no-referrer headers on /ui routes.
    • Replaced the permissive external host skip in verifyLinkedResources with an explicit allowlist of approved external assets and added header, inline-script, and Node JS verification tests.
  4. DynamicUIResource authRequired enforcement (internal/resources/dynamic.go, internal/resources/resources.go, internal/util/util.go, internal/server/mcp/util/auth.go, internal/server/mcp/v*/method.go):
    • Added AuthRequired with SetAuthRequired / GetAuthRequired (implementing resources.AuthRequiredResource) and context-based verified-service checking inside DynamicUIResource.Read.
    • Updated resourcesReadHandler across all 5 MCP protocol versions (2024-11-05, 2025-03-26, 2025-06-18, 2025-11-25, 2026-07-28) to verify authRequired via mcputil.AuthorizeResourceRead before invoking Read.

Testing

  • gofmt -l . (clean)
  • go vet ./... (clean)
  • go test -race -v ./internal/resources/... ./internal/server/... ./internal/util/... (passed, including Node JS verification of constructCsp and injectCspIntoHtml)
  • go build -v ./... and go test ./internal/... ./cmd/... (all packages passed)

PR Checklist

  • Ensure the entire diff has been reviewed
  • Ensure tests and linters pass locally
  • Code coverage maintained/improved with new regression tests
  • Added Apache-2.0 license header to new static/js/bootstrap.js file

@wangauone
wangauone requested a review from a team as a code owner December 4, 2026 22:30
@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 robust authentication and authorization checks for dynamic UI resources, enforcing configured required auth services during resource reads across multiple MCP protocol versions. It also significantly hardens the web UI security by introducing strict CSP origin sanitization, implementing secure HTTP headers (such as X-Frame-Options and Referrer-Policy), removing inline scripts in favor of a centralized bootstrap.js file, and utilizing DOMParser for safer CSP injection. Feedback on the changes suggests a minor optimization in internal/resources/ui.go to remove a redundant strings.ToLower call, as Go's net/url.Parse automatically lowercases the scheme.

Comment thread internal/resources/ui.go Resolved

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