Potential fix for code scanning alert no. 2: Server-side request forgery#43
Open
Potential fix for code scanning alert no. 2: Server-side request forgery#43
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/saad2134/donor-sync/security/code-scanning/2
In general, to fix SSRF when making server-side HTTP requests using client-supplied URLs, you must prevent the client from controlling the full target URL or host. Common strategies are: (1) don’t accept arbitrary URLs at all; instead accept an ID or enum and map it to a known endpoint, or (2) strictly validate the URL (scheme, host, port, and path) and reject anything outside a narrow allow‑list.
For this specific route, the minimal fix while preserving functionality is to validate
user_json_urlbefore callingfetch. We can (a) parse it with the built‑inURLconstructor, (b) ensure the protocol ishttps:(or at leasthttp:/https:), (c) ensure the hostname is in an allow‑list of trusted verification providers, and optionally (d) constrain the port and path. If validation fails, we return400rather than issuing the request. This keeps the current pattern of “backend fetches a given verification JSON” but prevents use of arbitrary internal or attacker-controlled hosts. We can implement this entirely withinfrontend-web/app/api/verify-phone/route.tsusing only standard language features, so no new external dependencies are necessary.Concretely, in
POSTwe will:!user_json_url, add a URL parsing/validation block.new URL(user_json_url)in atry/catch; on failure, return400with an error.const ALLOWED_HOSTS = [...]) and verifyparsed.hostnameis included.https:).fetch(validatedUrl.toString(), ...)instead offetch(user_json_url, ...).Only lines around 7–10 and 19–25 need logic inserted/updated; the rest of the function can remain unchanged.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.