Skip to content

Potential fix for code scanning alert no. 5: Uncontrolled data used in path expression - #100

Open
AyushAggarwal1 wants to merge 5 commits into
devfrom
alert-autofix-5
Open

AyushAggarwal1 wants to merge 5 commits into
devfrom
alert-autofix-5

Conversation

@AyushAggarwal1

Copy link
Copy Markdown
Owner

Potential fix for https://github.com/AyushAggarwal1/gst-bill/security/code-scanning/5

Generally, the fix is to constrain defaultTemplate so that it cannot escape the intended directories. This can be done either by: (a) treating it as a simple filename and sanitizing it (e.g., with sanitize-filename), or (b) allowing limited directory components but enforcing that the resolved path remains within an allowed root (e.g., public/templates or public/backup-templates) using path.resolve and checking the prefix. Here, we want to preserve existing behavior (supporting backup-templates/... vs regular templates) while securing it, so the second approach is best.

Concretely, we can:

  1. Define two root directories:
    • const templatesRoot = path.join(process.cwd(), 'public', 'templates');
    • const backupTemplatesRoot = path.join(process.cwd(), 'public', 'backup-templates');
  2. Decide which root to use based on whether defaultTemplate starts with "backup-templates/". Instead of including that prefix in the user-controlled portion of the path, we should strip it before joining, so user input never controls components above the root directory.
  3. Build a candidate path with path.resolve(root, relativePart) to normalize it and collapse .. segments.
  4. Verify that the resolved path stays within its root, e.g. if (!templatePath.startsWith(root + path.sep)) (or equality for the root itself). If it does not, reject the request as invalid.
  5. Then call fs.existsSync(templatePath) as before.

This preserves the existing functionality (choosing between backup and regular templates) but prevents path traversal via defaultTemplate.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…n path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: Ayush Aggarwal <ayushaggarwal1136@gmail.com>
@netlify

netlify Bot commented Jan 13, 2026

Copy link
Copy Markdown

Deploy Preview for gstly ready!

Name Link
🔨 Latest commit b52387d
🔍 Latest deploy log https://app.netlify.com/projects/gstly/deploys/69660940ac970300081e0811
😎 Deploy Preview https://deploy-preview-100--gstly.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@AyushAggarwal1
AyushAggarwal1 marked this pull request as ready for review January 13, 2026 09:03
@netlify

netlify Bot commented Mar 9, 2026

Copy link
Copy Markdown

Deploy Preview for gstly ready!

Name Link
🔨 Latest commit 186ef76
🔍 Latest deploy log https://app.netlify.com/projects/gstly/deploys/69cbeaef5538ce0008607e19
😎 Deploy Preview https://deploy-preview-100--gstly.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant