Skip to content

Potential fix for code scanning alert no. 1: Regular expression injection#20

Merged
eschultink merged 2 commits intomainfrom
alert-autofix-1
Feb 25, 2026
Merged

Potential fix for code scanning alert no. 1: Regular expression injection#20
eschultink merged 2 commits intomainfrom
alert-autofix-1

Conversation

@eschultink
Copy link
Member

Potential fix for https://github.com/Worklytics/datastore-backup/security/code-scanning/1

To fix the issue, we need to sanitize the bucketPrefix variable before using it in the new RegExp constructor. The best way to achieve this is by using a library like lodash to escape special characters in the user-provided input. Specifically, we can use the _.escapeRegExp function to sanitize bucketPrefix before constructing the regular expression.

Changes will be made in the index.js file:

  1. Import the _.escapeRegExp function from lodash.
  2. Apply _.escapeRegExp to bucketPrefix before passing it to the new RegExp constructor.

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

…tion

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@eschultink eschultink marked this pull request as ready for review February 9, 2026 22:44
@eschultink eschultink self-assigned this Feb 25, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a GitHub code scanning alert for regular-expression injection by escaping the GCS bucket prefix before constructing a dynamic RegExp in the backup listing output.

Changes:

  • Escape bucketPrefix via _.escapeRegExp before passing it into new RegExp(...).
  • Use the escaped value when stripping the prefix from gsutil ls output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

index.js Outdated

let bucketPrefix = `gs://${bucket}/`;
let escapedBucketPrefix = _.escapeRegExp(bucketPrefix);
let files = child_process.execSync('gsutil ls ' + bucketPrefix).toString('utf8');
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

child_process.execSync('gsutil ls ' + bucketPrefix) executes through a shell, and bucketPrefix can be influenced via the --bucketPrefix CLI option (see lib/cmd.getBackupBucket). This leaves a command-injection risk (and can also break if the bucket name contains spaces/shell metacharacters). Prefer execFileSync/spawnSync with an argv array (e.g., ['ls', bucketPrefix]) and no shell.

Suggested change
let files = child_process.execSync('gsutil ls ' + bucketPrefix).toString('utf8');
let files = child_process.execFileSync('gsutil', ['ls', bucketPrefix]).toString('utf8');

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is outdated with new file version, @copilot review again

Copy link

Copilot AI commented Feb 25, 2026

@aperez-worklytics I've opened a new pull request, #26, to work on those changes. Once the pull request is ready, I'll request review from you.

@eschultink eschultink merged commit bf5ff53 into main Feb 25, 2026
11 checks passed
@eschultink eschultink deleted the alert-autofix-1 branch February 25, 2026 21:51
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.

4 participants