Potential fix for code scanning alert no. 5: Uncontrolled data used in path expression#17
Draft
johnsamuelwrites wants to merge 1 commit intomasterfrom
Draft
Potential fix for code scanning alert no. 5: Uncontrolled data used in path expression#17johnsamuelwrites wants to merge 1 commit intomasterfrom
johnsamuelwrites wants to merge 1 commit intomasterfrom
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/johnsamuelwrites/ShExStatements/security/code-scanning/5
In general, fix uncontrolled path usage by (a) not trusting the client‑supplied filename, (b) normalizing and constraining the path to a dedicated upload directory, and/or (c) replacing the provided name with a safe server‑generated name (or using
secure_filename). Here, we do not actually need the original client filename for anything other than extension detection; the content is passed viastream. The safest minimal fix is therefore to (1) ignore the user’s filename for filesystem paths, (2) generate a unique temporary filename in a safe temp directory, and (3) use that path both foropen()and forremove().Concretely:
generate_shex_from_spreadsheet, importos(ortempfile) and create a temp filename usingtempfile.NamedTemporaryFile(delete=False)ortempfile.mkstemp()for the cases wherestream is not None. Writestreaminto this safe path, and use that path forload_workbook/loadand forremove(). Do not concatenate"tmp" + filepathor otherwise reuse the user‑provided name in a filesystem path. The originalfilepathparameter is still fine to use withsplitext()to determinefile_extension, but not as a path.shexstatements/shexfromspreadsheet.pywithin the shown method.shexstatements/application.pycan stay unchanged because it just passes through the client filename; the dangerous part is using that string as a path ingenerate_shex_from_spreadsheet.This preserves existing behavior (read spreadsheet from the uploaded stream, delete the temporary file afterward) while ensuring that all actual filesystem paths are generated by the server and not influenced by user input.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.