Potential fix for code scanning alert no. 4: Uncontrolled data used in path expression#4
Merged
scottsgeorge merged 1 commit intomainfrom Dec 24, 2025
Merged
Conversation
…n path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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/4
In general, to fix uncontrolled path usage when serving files, you should: (1) resolve the requested path relative to a known safe root directory, (2) normalize it (removing
..segments), and (3) verify that the resulting absolute path is still within the root directory before using it with filesystem APIs. Optionally, you can further restrict allowed filenames or extensions.For this code, the best minimal fix without changing visible functionality is: after constructing
pathnamefromrootDirand the requested path, resolve it to an absolute, normalized path (e.g. withpath.resolve) and then verify that it is still underrootDir. If it is not, immediately respond with a 403 and do not touch the filesystem. We can also tighten the way we derivesafePathfromparsedUrl.pathnameby making sure we always treat it as a relative path without leading slashes and by forbidding directory traversal segments there as well.Concretely in
scripts/serve.js:safePathandpathnamelogic (lines 28–29) with:safePathfromparsedUrl.pathname || '/'viapath.normalize, strip any leading slashes, and if the result starts with'..'or contains path traversal segments, reject it;pathnameusingpath.resolve(rootDir, safePath || 'index.html');pathnamestarts withrootDir + path.sepor equalsrootDir(to prevent/var/www_evilfrom matching/var/www).pathname, appending'index.html'within that directory is safe because we stay beneathrootDir.No new imports are needed;
pathandfsare already required at the top of the file. The change is confined to the path-handling logic in the request handler.Suggested fixes powered by Copilot Autofix. Review carefully before merging.