Skip to content

Commit d9fc297

Browse files
authored
fix: check kill() input (#1339)
* fix: check `kill()` input closes #1337 * chore: minor code impr
1 parent a1cf179 commit d9fc297

File tree

7 files changed

+27
-7
lines changed

7 files changed

+27
-7
lines changed

.size-limit.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"README.md",
2020
"LICENSE"
2121
],
22-
"limit": "128.17 kB",
22+
"limit": "128.25 kB",
2323
"brotli": false,
2424
"gzip": false
2525
},
@@ -33,7 +33,7 @@
3333
"build/globals.js",
3434
"build/deno.js"
3535
],
36-
"limit": "816.00 kB",
36+
"limit": "816.05 kB",
3737
"brotli": false,
3838
"gzip": false
3939
},
@@ -66,7 +66,7 @@
6666
"README.md",
6767
"LICENSE"
6868
],
69-
"limit": "873.50 kB",
69+
"limit": "873.60 kB",
7070
"brotli": false,
7171
"gzip": false
7272
}

build/core.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ function cd(dir) {
11091109
}
11101110
function kill(_0) {
11111111
return __async(this, arguments, function* (pid, signal = $.killSignal || SIGTERM) {
1112+
if (!/^\d+$/.test(pid)) throw new Fail(`Invalid pid: ${pid}`);
1113+
pid = pid + "";
11121114
$.log({ kind: "kill", pid, signal, verbose: !$.quiet && $.verbose });
11131115
if (import_node_process2.default.platform === "win32" && (yield new Promise((resolve) => {
11141116
import_node_child_process.default.exec(`taskkill /pid ${pid} /t /f`, (err) => resolve(!err));

build/core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,5 @@ export declare const usePwsh: () => void;
208208
export declare const usePowerShell: () => void;
209209
export declare function syncProcessCwd(flag?: boolean): void;
210210
export declare function cd(dir: string | ProcessOutput): void;
211-
export declare function kill(pid: number, signal?: NodeJS.Signals): Promise<void>;
211+
export declare function kill(pid: number | `${number}`, signal?: NodeJS.Signals): Promise<void>;
212212
export declare function resolveDefaults(defs?: Options, prefix?: string, env?: NodeJS.ProcessEnv, allowed?: Set<string>): Options;

build/log.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type LogEntry = {
4141
data: any;
4242
} | {
4343
kind: 'kill';
44-
pid: number;
44+
pid: number | `${number}`;
4545
signal: NodeJS.Signals | null;
4646
});
4747
type LogFormatters = {

src/core.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,13 @@ export function cd(dir: string | ProcessOutput) {
10371037
$[CWD] = process.cwd()
10381038
}
10391039

1040-
export async function kill(pid: number, signal = $.killSignal || SIGTERM) {
1040+
export async function kill(
1041+
pid: number | `${number}`,
1042+
signal = $.killSignal || SIGTERM
1043+
) {
1044+
if (!/^\d+$/.test(pid as string)) throw new Fail(`Invalid pid: ${pid}`)
1045+
pid = (pid + '') as `${number}`
1046+
10411047
$.log({ kind: 'kill', pid, signal, verbose: !$.quiet && $.verbose })
10421048
if (
10431049
process.platform === 'win32' &&

src/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export type LogEntry = {
6767
}
6868
| {
6969
kind: 'kill'
70-
pid: number
70+
pid: number | `${number}`
7171
signal: NodeJS.Signals | null
7272
}
7373
)

test/core.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
usePwsh,
3434
useBash,
3535
Fail,
36+
kill,
3637
} from '../build/core.js'
3738
import {
3839
tempfile,
@@ -1642,6 +1643,17 @@ describe('core', () => {
16421643
})
16431644
})
16441645

1646+
describe('kill()', () => {
1647+
test('throws if pid is invalid', async () => {
1648+
await assert.rejects(() => kill(''), /Invalid/)
1649+
await assert.rejects(() => kill('foo'), /Invalid/)
1650+
await assert.rejects(() => kill('100 foo'), /Invalid/)
1651+
await assert.rejects(() => kill(100.1), /Invalid/)
1652+
await assert.rejects(() => kill(null), /Invalid/)
1653+
await assert.rejects(() => kill({}), /Invalid/)
1654+
})
1655+
})
1656+
16451657
describe('within()', () => {
16461658
test('just works', async () => {
16471659
let resolve, reject

0 commit comments

Comments
 (0)