Potential fix for code scanning alert no. 2: Open URL redirect #2
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.
Potential fix for https://github.com/agentplexus/mcpkit/security/code-scanning/2
In general, to fix open redirect issues you must ensure that any URL used in
http.Redirectis either chosen from a server-side whitelist or is strictly validated (e.g., ensuring it is a relative URL or that its scheme/host/path match an approved value) before redirecting. User-controlled input should never determine the redirect target without such checks.For this specific code, the best fix without changing existing functionality is to validate
redirectURIbefore using it inredirectWithError. The file already definesisValidRedirectURI(uri string, allowed []string) bool, which checks that a requested redirect URI matches an allowed value. We should therefore use that helper insideredirectWithError. SinceredirectWithErrorcurrently has no access to client metadata or an allowlist, the most minimal and conservative change is to enforce thatredirectURIis local (no scheme/host), similar to the example in the background description: normalize backslashes, parse the URI, and require that the hostname is empty before redirecting. If validation fails, we should fall back to rendering an error page instead of redirecting.Concretely:
redirectWithError, normalizeredirectURIby replacing\with/, then callurl.Parseon the normalized string.u.Hostname()is empty. If not, treat it as invalid and calls.renderLoginErrorinstead of redirecting.oauth2/handlers.go, in theredirectWithErrorfunction; imports remain the same (net/urlandstringsare already imported).Suggested fixes powered by Copilot Autofix. Review carefully before merging.