Browse Source

fix(grep): harden normalize and zip extraction

dhaern 3 months ago
parent
commit
2014bff74e
2 changed files with 10 additions and 2 deletions
  1. 8 1
      src/tools/grep/normalize.ts
  2. 2 1
      src/utils/zip-extractor.ts

+ 8 - 1
src/tools/grep/normalize.ts

@@ -137,7 +137,14 @@ export function normalizeGrepInput(
   const caseSensitive = args.case_sensitive !== false;
   const caseSensitive = args.case_sensitive !== false;
   const smartCase = caseSensitive && args.smart_case === true;
   const smartCase = caseSensitive && args.smart_case === true;
 
 
-  if (!searchStat.isFile() && !searchStat.isDirectory()) {
+  const isFile =
+    typeof searchStat.isFile === 'function' ? searchStat.isFile() : undefined;
+  const isDirectory =
+    typeof searchStat.isDirectory === 'function'
+      ? searchStat.isDirectory()
+      : undefined;
+
+  if (isFile === false && isDirectory === false) {
     throw new Error(
     throw new Error(
       `Search path must be a file or directory: ${requestedPath}`,
       `Search path must be a file or directory: ${requestedPath}`,
     );
     );

+ 2 - 1
src/utils/zip-extractor.ts

@@ -152,6 +152,7 @@ export async function extractZip(
 
 
   signal?.addEventListener('abort', onAbort, { once: true });
   signal?.addEventListener('abort', onAbort, { once: true });
 
 
+  const stderrPromise = proc.stderr();
   const exitCode = await proc.exited;
   const exitCode = await proc.exited;
   signal?.removeEventListener('abort', onAbort);
   signal?.removeEventListener('abort', onAbort);
 
 
@@ -159,8 +160,8 @@ export async function extractZip(
     throw createAbortError();
     throw createAbortError();
   }
   }
 
 
+  const stderr = await stderrPromise;
   if (exitCode !== 0) {
   if (exitCode !== 0) {
-    const stderr = await proc.stderr();
     throw new Error(`zip extraction failed (exit ${exitCode}): ${stderr}`);
     throw new Error(`zip extraction failed (exit ${exitCode}): ${stderr}`);
   }
   }
 }
 }