Potential fix for code scanning alert no. 5: Uncontrolled data used in path expression#3
Draft
scottsgeorge wants to merge 1 commit intomainfrom
Draft
Potential fix for code scanning alert no. 5: Uncontrolled data used in path expression#3scottsgeorge wants to merge 1 commit intomainfrom
scottsgeorge wants to merge 1 commit intomainfrom
Conversation
…n path expression 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/hellopaywaz/paywaz-docs/security/code-scanning/5
In general terms, the fix is to enforce that any path derived from
req.urlis constrained to a specific root directory. This is usually done by resolving the request path against the root directory, normalizing it (removing..segments), and then verifying that the resulting absolute path still starts with the intended root directory path. If it does not, the server should reject the request (for example, with HTTP 403) instead of attempting to read the file.The best change here, without altering existing behavior beyond security, is: after obtaining
safePath, computepathnamewithpath.resolve(rootDir, safePath)rather thanpath.join, then check thatpathnamestarts withrootDir + path.sep(or equalsrootDir), before using it. If the check fails, respond with 403 and return. This ensures that even ifsafePathcontains traversal segments or tricks that bypass the current regex, the resolved path cannot escaperootDir. The rest of the logic (addingindex.htmlfor directories, determining MIME types, etc.) can remain unchanged, but must operate on the validatedpathname. Concretely, in scripts/serve.js around lines 28–35, replace thesafePathcomputation andpath.join(rootDir, safePath)usage with apath.resolvecall and a containment check; if invalid, send a 403 response and skipfs.readFile.No new external libraries are required: Node's built-in
pathandfsmodules already provide what we need. We will keep the imports as they are and only adjust the calculation ofsafePath/pathnameand add a small validation block.Suggested fixes powered by Copilot Autofix. Review carefully before merging.