Potential fix for code scanning alert no. 28: Server-side request forgery#16
Draft
TheInfamousToTo wants to merge 1 commit intomainfrom
Draft
Potential fix for code scanning alert no. 28: Server-side request forgery#16TheInfamousToTo wants to merge 1 commit intomainfrom
TheInfamousToTo wants to merge 1 commit intomainfrom
Conversation
…gery Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
backend/routes/ssh.js
Outdated
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/TheInfamousToTo/PiHoleVault/security/code-scanning/28
In general, to fix SSRF in this context, we must restrict which hosts and ports the server is willing to contact based on user input. That usually means: validating host format; blocking localhost and link-local/metadata IP ranges; optionally enforcing an allow-list of domains/IP ranges; and restricting ports to a reasonable, expected set or range. We should also ensure we do not accidentally permit DNS rebinding tricks where an allowed hostname resolves to a forbidden IP.
For this specific route, the best minimally invasive fix is:
Add validation helpers near the top of
backend/routes/ssh.js:dns.promises.lookup), checks that the resulting IP is not loopback, private, link-local, or otherwise sensitive (e.g., 127.0.0.0/8, 10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12, 169.254.0.0/16, 0.0.0.0, ::1, etc.).In the
/debugroute, before performing any network operation (ping,socket.connect, SSH), call the validation helpers:hostandportvalues (e.g.,resolvedHostandvalidatedPort) inpingandsocket.connectinstead of the raw user input.Since
socket.connectis the sink CodeQL is flagging, ensuring thatportandhosthave passed strict validation and IP-range checks will address the alert. We will also switch the second argument ofsocket.connectto use the resolved, allowed IP address, not the raw hostname.To implement this:
dnsimport (const dns = require('dns').promises;) at the top ofbackend/routes/ssh.js.isValidHostnameOrIP,isDisallowedIp,resolveAndValidateHost,validatePort) before the route definitions./debug, before buildingdebugor running tests, callawait resolveAndValidateHost(host)andvalidatePort(port). Use the returned safe values in the rest of the function, especially insocket.connect(...)andping ....This keeps current functionality (testing connectivity to a host/port) but prevents use with internal/loopback/metadata addresses and invalid ports, satisfying SSRF concerns.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.