{"record":{"id":"69b98b774d7a9675","repo":"garrytan/gstack","slug":"usage-browse-cookie-import-json-file","errorCode":null,"errorMessage":"Usage: browse cookie-import <json-file>","messagePattern":"Usage: browse cookie-import <json-file>","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"browse/src/write-commands.ts","lineNumber":644,"sourceCode":"\n    case 'dialog-accept': {\n      const text = args.length > 0 ? args.join(' ') : null;\n      bm.setDialogAutoAccept(true);\n      bm.setDialogPromptText(text);\n      return text\n        ? `Dialogs will be accepted with text: \"${text}\"`\n        : 'Dialogs will be accepted';\n    }\n\n    case 'dialog-dismiss': {\n      bm.setDialogAutoAccept(false);\n      bm.setDialogPromptText(null);\n      return 'Dialogs will be dismissed';\n    }\n\n    case 'cookie-import': {\n      const filePath = args[0];\n      if (!filePath) throw new Error('Usage: browse cookie-import <json-file>');\n      // Path validation — resolve to absolute and check against safe dirs.\n      // Fixes #707: relative paths previously bypassed the safe directory check.\n      // Mirrors validateOutputPath() — resolves symlinks (e.g., macOS /tmp → /private/tmp).\n      const resolved = path.resolve(filePath);\n      let resolvedReal = resolved;\n      try { resolvedReal = fs.realpathSync(resolved); } catch {\n        // File may not exist yet — resolve parent dir instead\n        try { resolvedReal = path.join(fs.realpathSync(path.dirname(resolved)), path.basename(resolved)); } catch {}\n      }\n      if (!SAFE_DIRECTORIES.some(dir => isPathWithin(resolvedReal, dir))) {\n        throw new Error(`Path must be within: ${SAFE_DIRECTORIES.join(', ')}`);\n      }\n      if (!fs.existsSync(filePath)) throw new Error(`File not found: ${filePath}`);\n      const raw = fs.readFileSync(filePath, 'utf-8');\n      let cookies: any[];\n      try { cookies = JSON.parse(raw); } catch (err: any) { throw new Error(`Invalid JSON in ${filePath}: ${err?.message || err}`); }\n      if (!Array.isArray(cookies)) throw new Error('Cookie file must contain a JSON array');\n","sourceCodeStart":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/garrytan/gstack/blob/94993f74012782fd94416dd44b8314f6363a13a4/browse/src/write-commands.ts#L626-L662","documentation":"Thrown by `browse cookie-import` when no file path argument is supplied (`args[0]` is undefined/falsy). The command imports a JSON array of cookies from a local file; without a path it has nothing to read. This is the simplest guard, checked before the path-security, existence, and JSON parsing layers.","triggerScenarios":"Calling `browse cookie-import` with no args; passing only flags like `--help` that the command does not recognize as a path; forwarding an undefined variable as the path.","commonSituations":"Agent intended to export cookies first (`cookie-export`) and import second but ran the import command alone; user expects an interactive file picker; a wrapper script dropped the path arg during argument forwarding.","solutions":["Provide a JSON file path: `browse cookie-import /tmp/errlookup-0o5UJv/cookies.json`.","The file must be inside TEMP_DIR or the project cwd (see the safe-directory check that runs next).","If you have cookies as an in-memory array, write them to a temp JSON file first, then import.","Use `browse cookie-import-browser <browser> --domain <domain>` if you want to pull cookies directly from an installed browser instead of a file."],"exampleFix":"// before\nawait runBrowseCommand(['cookie-import']);\n\n// after\nawait runBrowseCommand(['cookie-import', '/tmp/errlookup-0o5UJv/cookies.json']);","handlingStrategy":"validation","validationCode":"function validateCookieImportArgs(args: string[]): string {\n  const filePath = args[0];\n  if (!filePath) throw new Error('cookie-import requires a json file path');\n  return filePath;\n}","typeGuard":"function hasFilePathArg(args: unknown): args is [string, ...unknown[]] {\n  return Array.isArray(args) && typeof args[0] === 'string' && args[0].length > 0;\n}","tryCatchPattern":null,"preventionTips":["Provide a JSON file path as the first argument.","Ensure the path is inside TEMP_DIR or the project cwd.","Use cookie-import-browser for direct browser-DB extraction instead of a file."],"tags":["cli-usage","cookies","import","argument-validation","browse-command"],"backgroundTag":null,"analyzedSha":"94993f74012782fd94416dd44b8314f6363a13a4","analyzedAt":"2026-08-12T04:06:23.140Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}