Potential fix for code scanning alert no. 3: Incomplete URL substring sanitization#9
Closed
jonathansantilli wants to merge 1 commit into
Closed
Potential fix for code scanning alert no. 3: Incomplete URL substring sanitization#9jonathansantilli wants to merge 1 commit into
jonathansantilli wants to merge 1 commit into
Conversation
… sanitization 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/jonathansantilli/codegate/security/code-scanning/3
In general, to fix this kind of issue, avoid treating the full hostname as an arbitrary string and matching trusted domains with
includesorendsWith. Instead, parse the URL (which is already done here) and then either (a) require an exact match for the hostname, or (b) if subdomains should be allowed, explicitly permit either exact match or a hostname that ends with.+ trusted domain (e.g.,sub.example.comends with.example.com) rather than just any string ending inexample.com.For this code, the lowest-risk change is to replace the three
.endsWith("<domain>")checks with a helper that checks for either exact matches or proper subdomains. This preserves the intention of treatinggithub.com,gitlab.com, andbitbucket.org(and their subdomains likegist.github.comorapi.bitbucket.org) as “likely Git sources”, but prevents hostnames such asgithub.com.evil.comfrom being accepted. We can introduce a small local helper, e.g.isHostOrSubdomain(hostname, baseDomain), and use it insideisLikelyGitSource. All changes stay withinsrc/layer2-static/detectors/plugin-manifest.ts, near the existing function, and no external libraries are needed because hostname string matching is straightforward.Concretely:
isHostOrSubdomain(hostname: string, baseDomain: string): booleanjust aboveisLikelyGitSource.hostnameandbaseDomainto lowercase.trueif they are equal.trueifhostnameends with.+baseDomain.isLikelyGitSourceto use this helper for"github.com","gitlab.com", and"bitbucket.org"instead of.endsWith(...).Suggested fixes powered by Copilot Autofix. Review carefully before merging.