Commit d9fc297

Anton Golub <antongolub@antongolub.com>
2025-09-19 22:04:56
fix: check `kill()` input (#1339)
* fix: check `kill()` input closes #1337 * chore: minor code impr
1 parent a1cf179
build/core.cjs
@@ -1109,6 +1109,8 @@ function cd(dir) {
 }
 function kill(_0) {
   return __async(this, arguments, function* (pid, signal = $.killSignal || SIGTERM) {
+    if (!/^\d+$/.test(pid)) throw new Fail(`Invalid pid: ${pid}`);
+    pid = pid + "";
     $.log({ kind: "kill", pid, signal, verbose: !$.quiet && $.verbose });
     if (import_node_process2.default.platform === "win32" && (yield new Promise((resolve) => {
       import_node_child_process.default.exec(`taskkill /pid ${pid} /t /f`, (err) => resolve(!err));
build/core.d.ts
@@ -208,5 +208,5 @@ export declare const usePwsh: () => void;
 export declare const usePowerShell: () => void;
 export declare function syncProcessCwd(flag?: boolean): void;
 export declare function cd(dir: string | ProcessOutput): void;
-export declare function kill(pid: number, signal?: NodeJS.Signals): Promise<void>;
+export declare function kill(pid: number | `${number}`, signal?: NodeJS.Signals): Promise<void>;
 export declare function resolveDefaults(defs?: Options, prefix?: string, env?: NodeJS.ProcessEnv, allowed?: Set<string>): Options;
build/log.d.ts
@@ -41,7 +41,7 @@ export type LogEntry = {
     data: any;
 } | {
     kind: 'kill';
-    pid: number;
+    pid: number | `${number}`;
     signal: NodeJS.Signals | null;
 });
 type LogFormatters = {
src/core.ts
@@ -1037,7 +1037,13 @@ export function cd(dir: string | ProcessOutput) {
   $[CWD] = process.cwd()
 }
 
-export async function kill(pid: number, signal = $.killSignal || SIGTERM) {
+export async function kill(
+  pid: number | `${number}`,
+  signal = $.killSignal || SIGTERM
+) {
+  if (!/^\d+$/.test(pid as string)) throw new Fail(`Invalid pid: ${pid}`)
+  pid = (pid + '') as `${number}`
+
   $.log({ kind: 'kill', pid, signal, verbose: !$.quiet && $.verbose })
   if (
     process.platform === 'win32' &&
src/log.ts
@@ -67,7 +67,7 @@ export type LogEntry = {
     }
   | {
       kind: 'kill'
-      pid: number
+      pid: number | `${number}`
       signal: NodeJS.Signals | null
     }
 )
test/core.test.js
@@ -33,6 +33,7 @@ import {
   usePwsh,
   useBash,
   Fail,
+  kill,
 } from '../build/core.js'
 import {
   tempfile,
@@ -1642,6 +1643,17 @@ describe('core', () => {
     })
   })
 
+  describe('kill()', () => {
+    test('throws if pid is invalid', async () => {
+      await assert.rejects(() => kill(''), /Invalid/)
+      await assert.rejects(() => kill('foo'), /Invalid/)
+      await assert.rejects(() => kill('100 foo'), /Invalid/)
+      await assert.rejects(() => kill(100.1), /Invalid/)
+      await assert.rejects(() => kill(null), /Invalid/)
+      await assert.rejects(() => kill({}), /Invalid/)
+    })
+  })
+
   describe('within()', () => {
     test('just works', async () => {
       let resolve, reject
.size-limit.json
@@ -19,7 +19,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "128.17 kB",
+    "limit": "128.25 kB",
     "brotli": false,
     "gzip": false
   },
@@ -33,7 +33,7 @@
       "build/globals.js",
       "build/deno.js"
     ],
-    "limit": "816.00 kB",
+    "limit": "816.05 kB",
     "brotli": false,
     "gzip": false
   },
@@ -66,7 +66,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "873.50 kB",
+    "limit": "873.60 kB",
     "brotli": false,
     "gzip": false
   }