Skip to content

Commit

Permalink
Merge pull request from GHSA-vm67-7vmg-66vm
Browse files Browse the repository at this point in the history
Adding check to ensure function arguments are numbers
  • Loading branch information
fishcharlie authored Mar 31, 2021
2 parents fffceb0 + 069417e commit 8681121
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ async function task() {
const port = process.argv.pop();
const command = process.argv.pop();

let result;
switch (command) {
case "kill":
const result = await main.killAllProcessesOnPort(port);
result = await main.killAllProcessesOnPort(port);
console.log(result.filter(item => !item.success).map(item => `Failed to kill process ${item.pid}`).join('\n'));
break;
case "list":
const result = await main.listProcessesOnPort(port);
result = await main.listProcessesOnPort(port);
console.log(result);
break;
default:
Expand Down
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const exec = require('./exec');
const listProcessesOnPort = module.exports.listProcessesOnPort = async port => {
const portNumber = parseInt(port, 10);
if (Number.isNaN(portNumber)) {
console.error("Must provide number for port.");
return;
}
try {
const result = (await exec(`lsof -i :${port}`)).output.split('\n');
const result = (await exec(`lsof -i :${portNumber}`)).output.split('\n');
const headers = result.shift().split(' ').filter(item => !!item.trim() && item.trim() !== "").map(item => item.toLowerCase());
return result.filter(item => !!item.trim() && item.trim() !== "").reduce((accumulator, currentValue) => {
accumulator.push(currentValue.split(' ').filter(item => !!item.trim() && item.trim() !== "").reduce((accumulator, currentValue, index) => {
Expand All @@ -19,8 +24,14 @@ const listProcessesOnPort = module.exports.listProcessesOnPort = async port => {
}
};
const killProcess = module.exports.killProcess = async pid => {
const pidNumber = parseInt(pid, 10);
if (Number.isNaN(pidNumber)) {
console.error("Must provide number for process identifier.");
return false;
}

try {
await exec(`kill ${pid}`);
await exec(`kill ${pidNumber}`);
return true;
} catch (e) {
return false;
Expand Down

0 comments on commit 8681121

Please sign in to comment.