Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression #61
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/InditexTech/weavejs-backend/security/code-scanning/9
General Approach:
Sanitize and validate
roomId
andimageId
before using them to build filesystem paths. There are two principal approaches: (1) restrictroomId
andimageId
to simple, safe file/folder names (no slashes or special chars), or (2) use path normalization and restrict all constructed paths to a fixedroot
directory.Best solution for this case:
Given the context (image storage in a
/temp
subdirectory), the simplest secure approach is to restrictroomId
andimageId
to safe filenames (no slashes, path traversal, or special characters). This can be accomplished using a library such assanitize-filename
, which removes unsafe characters. Since we are using only code shown, we'll apply a minimal regex check to only allow alphanumeric (and, optionally,-
or_
) characters and reject anything else. If invalid input is detected, return a 400 error.Implementation steps:
roomId
andimageId
at the start of the controller, after line 24./^[a-zA-Z0-9_-]+$/
for both.Suggested fixes powered by Copilot Autofix. Review carefully before merging.