Potential fix for code scanning alert no. 2: Open URL redirect#9
Merged
Potential fix for code scanning alert no. 2: Open URL redirect#9
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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.
Potential fix for https://github.com/plexusone/mcpkit/security/code-scanning/2
In general, to fix untrusted URL redirects, the redirect target must be constrained to a trusted set (allowlist) or to same-origin/relative URLs, and this validation must occur as close as possible to the actual redirect call. Relying solely on earlier validation at some call sites is brittle, because other callers may bypass those checks.
For this code, the best minimally invasive fix is to add a defensive check inside
redirectWithErrorto prevent open redirects when theredirectURIis an absolute URL with a different host than the current server. We already parse the URL intou; we can compareu.Hostname()(and optionally scheme) againstr.Host, and only allow the redirect when it is (a) relative (no hostname) or (b) same-host as the current request. If the check fails, we fall back to rendering a local error page instead of redirecting externally. This preserves existing behavior for legitimate local redirects while closing the open redirect vector.Concretely:
oauth2/handlers.goinredirectWithError.normalizedintou, add a host/scheme validation:u.IsAbs()andu.Hostname()is non-empty and not equal to the hostname derived fromr.Host, treat it as invalid.s.renderLoginErrorand return.error,error_description,stateparameters) and the finalhttp.Redirectunchanged for valid URLs.net/urlandstrings, and we can user.Hostandurl.Parse(already in use) plus a tiny bit of string parsing for the hostname comparison.This adds a robust, local safeguard around the redirect sink (
http.Redirect) without changing the observable behavior for properly configured clients using relative or same-host redirect URIs.Suggested fixes powered by Copilot Autofix. Review carefully before merging.