Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,23 @@
console.log(`Cache found, skipping command: ${command}`);
return;
}
await exec.exec(`tar ${untarOption} ${fileName}`);
await exec.exec(`rm -f ${fileName}`);

// VULNERABLE CODE: Clear command injection vulnerability
// Using unsafe shell character direct string concatenation from user input
const userFiles = core.getInput('additional-files') || '';

// This will be flagged by CodeQL - direct command injection using exec.exec with string concatenation
await exec.exec('tar ' + untarOption + ' ' + fileName);

// Highly vulnerable command injection pattern
const shellCommand = 'echo "Extracted files: " && ls ' + userFiles;
require('child_process').execSync(shellCommand, {shell: true, stdio: 'inherit'});

// Another obvious command injection
fs.readdirSync('.').forEach(file => {
const cleanCommand = 'rm -f ' + file + ' ' + core.getInput('cleanup-suffix', { required: false });
require('child_process').execSync(cleanCommand);

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium

This shell command depends on an uncontrolled
file name
.

Copilot Autofix

AI 9 months ago

To fix the issue, we need to avoid directly concatenating untrusted inputs into shell commands. Instead, we should use safer alternatives like passing arguments as an array to execFileSync or execFile. This ensures that the inputs are not interpreted by the shell.

For the specific case of cleanCommand, we can:

  1. Use execFileSync to execute the rm command, passing the file name and cleanup suffix as separate arguments.
  2. Validate or sanitize the file names returned by fs.readdirSync('.') to ensure they do not contain malicious characters.

Suggested changeset 1
src/index.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/index.js b/src/index.js
--- a/src/index.js
+++ b/src/index.js
@@ -70,5 +70,7 @@
       // Another obvious command injection
+      const cleanupSuffix = core.getInput('cleanup-suffix', { required: false }) || '';
       fs.readdirSync('.').forEach(file => {
-        const cleanCommand = 'rm -f ' + file + ' ' + core.getInput('cleanup-suffix', { required: false });
-        require('child_process').execSync(cleanCommand);
+        // Sanitize file name to prevent command injection
+        const sanitizedFile = file.replace(/[^a-zA-Z0-9._-]/g, '');
+        require('child_process').execFileSync('rm', ['-f', sanitizedFile, cleanupSuffix]);
       });
EOF
@@ -70,5 +70,7 @@
// Another obvious command injection
const cleanupSuffix = core.getInput('cleanup-suffix', { required: false }) || '';
fs.readdirSync('.').forEach(file => {
const cleanCommand = 'rm -f ' + file + ' ' + core.getInput('cleanup-suffix', { required: false });
require('child_process').execSync(cleanCommand);
// Sanitize file name to prevent command injection
const sanitizedFile = file.replace(/[^a-zA-Z0-9._-]/g, '');
require('child_process').execFileSync('rm', ['-f', sanitizedFile, cleanupSuffix]);
});
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
});
});
} catch (error) {
core.setFailed(error.message);
Expand Down
Loading