Repository navigation
fix(server/web,resources): harden MCP App CSP injection, origin validation, and /ui security headers - #4703
Open
wangauone wants to merge 3 commits into
Open
fix(server/web,resources): harden MCP App CSP injection, origin validation, and /ui security headers#4703wangauone wants to merge 3 commits into
wangauone wants to merge 3 commits into
Conversation
…ation, and /ui security headers
Contributor
There was a problem hiding this comment.
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.
This branch has not been deployed
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.
Description
Addresses security hardening across MCP App UI resource validation, Web UI CSP construction/injection,
/uisecurity headers, andDynamicUIResourceauthorization (internal tracking: b/587680006 and b/591502673):internal/resources/ui.go): HardenedvalidateHTTPOriginto 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.internal/server/static/js/toolDisplay.js):sanitizeCspOrigin/sanitizeCspOriginListusingnew URL(trimmed)insideconstructCspso only stricthttp:/https:origins are emitted and invalid/injected entries are dropped before constructing directives (frameDomains/baseUriDomainsfall back to'none'/'self'when empty).injectCspIntoHtmlto parse untrusted HTML viaDOMParserwhen 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 whenDOMParseris unavailable./uisecurity headers and script cleanup (internal/server/web.go,internal/server/static/*.html,internal/server/static/js/bootstrap.js,internal/server/web_test.go):index.html,tools.html, andtoolsets.htmlintostatic/js/bootstrap.js.Content-Security-Policy(without'unsafe-inline'or'unsafe-eval'inscript-src),X-Frame-Options: DENY,X-Content-Type-Options: nosniff, andReferrer-Policy: no-referrerheaders on/uiroutes.verifyLinkedResourceswith an explicit allowlist of approved external assets and added header, inline-script, and Node JS verification tests.DynamicUIResourceauthRequiredenforcement (internal/resources/dynamic.go,internal/resources/resources.go,internal/util/util.go,internal/server/mcp/util/auth.go,internal/server/mcp/v*/method.go):AuthRequiredwithSetAuthRequired/GetAuthRequired(implementingresources.AuthRequiredResource) and context-based verified-service checking insideDynamicUIResource.Read.resourcesReadHandleracross all 5 MCP protocol versions (2024-11-05,2025-03-26,2025-06-18,2025-11-25,2026-07-28) to verifyauthRequiredviamcputil.AuthorizeResourceReadbefore invokingRead.Testing
gofmt -l .(clean)go vet ./...(clean)go test -race -v ./internal/resources/... ./internal/server/... ./internal/util/...(passed, including Node JS verification ofconstructCspandinjectCspIntoHtml)go build -v ./...andgo test ./internal/... ./cmd/...(all packages passed)PR Checklist
static/js/bootstrap.jsfile