Skip to content

Commit 0757991

Browse files
Potential fix for code scanning alert no. 1: Uncontrolled command line
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 71b2808 commit 0757991

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

pages/api/user-search.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// DO NOT USE IN PRODUCTION
33
// This API endpoint demonstrates a command injection vulnerability for CodeQL detection
44

5-
import { exec } from 'child_process';
5+
import { execFile } from 'child_process';
66

77
export default function handler(req, res) {
88
const { username } = req.query;
@@ -11,11 +11,10 @@ export default function handler(req, res) {
1111
return res.status(400).json({ error: 'Username is required' });
1212
}
1313

14-
// VULNERABILITY: User input is directly concatenated into a shell command
15-
// An attacker could inject malicious commands using input like: "user; rm -rf /"
16-
const command = `grep ${username} /var/log/users.log`;
17-
18-
exec(command, (error, stdout, stderr) => {
14+
// SAFER: Use execFile with arguments array to avoid shell command injection
15+
const args = [username, '/var/log/users.log'];
16+
17+
execFile('grep', args, (error, stdout, stderr) => {
1918
if (error) {
2019
return res.status(500).json({ error: error.message });
2120
}

0 commit comments

Comments
 (0)