@openai/codex-security 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -4,8 +4,7 @@ import { accessSync, constants, existsSync, lstatSync, realpathSync, writeSync,
4
4
  import { mkdir, readFile, realpath, stat, writeFile } from "node:fs/promises";
5
5
  import { basename, dirname, isAbsolute, join, parse, relative, resolve, sep, win32, } from "node:path";
6
6
  import { cwd } from "node:process";
7
- import { createInterface } from "node:readline";
8
- import { Readable, Writable as NodeWritable } from "node:stream";
7
+ import { Writable as NodeWritable } from "node:stream";
9
8
  import { pipeline } from "node:stream/promises";
10
9
  import { fileURLToPath, pathToFileURL } from "node:url";
11
10
  import { Cli, z } from "incur";
@@ -15,7 +14,7 @@ import { accountStatus } from "./auth.js";
15
14
  import { createBulkScanDiscoveryDependencies, runBulkScanWizard, } from "./bulk-scan-discovery.js";
16
15
  import { DEFAULT_CODEX_CONFIG, mergedCodexConfig, scanModelConfiguration, } from "./config.js";
17
16
  import { formatUsd } from "./cost.js";
18
- import { CodexSecurityError, OutputInsideProtectedRootError, ScanInterruptedError, } from "./errors.js";
17
+ import { CodexSecurityError, ConfigurationError, InvalidTargetError, OutputDirectoryError, OutputInsideProtectedRootError, PluginPythonUnavailableError, redactedErrorMessage, ScanInterruptedError, } from "./errors.js";
19
18
  import { runMultiscan } from "./multiscan.js";
20
19
  import { bundledPluginRoot, codexSecurityCredentialHome, codexSecurityStateDirectory, expandHome, prepareCodexSecurityCredentialHome, resolveCodexCommand, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, } from "./runtime.js";
21
20
  import { matchScanFindings, } from "./scan-comparison.js";
@@ -28,11 +27,15 @@ const MAX_CODEX_OVERRIDE_VALUE_LENGTH = 64 * 1_024;
28
27
  const MAX_CODEX_OVERRIDE_DEPTH = 64;
29
28
  const MAX_SKILL_INPUT_BYTES = 1_024 * 1_024;
30
29
  const MAX_SKILL_INPUT_COUNT = 64;
30
+ const MAX_SKILL_EVENT_BYTES = 1_024 * 1_024;
31
+ const MAX_SKILL_RESPONSE_BYTES = 256 * 1_024;
32
+ const SKILL_OUTPUT_LIMIT_MESSAGE = "Codex skill output exceeded the 1 MiB event or 256 KiB response safety limit.";
31
33
  const WINDOWS_NETWORK_PATH = /^[\\/]{2}/u;
32
34
  const WINDOWS_LOCAL_DEVICE_ROOT = /^[\\/]{2}[?.][\\/](?:[A-Za-z]:|Volume\{[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\}|GLOBALROOT[\\/]Device[\\/]HarddiskVolume[0-9]+)(?=[\\/]|$)/iu;
33
35
  const SCAN_HISTORY_OUTPUT_OPTION = /^--(?:format|filter-output|full-output|token-count|token-limit|token-offset)(?:=|$)/u;
34
36
  const HIDE_CURSOR = "\u001B[?25l";
35
37
  const SHOW_CURSOR = "\u001B[?25h";
38
+ const CHILD_TERMINATION_GRACE_MS = 1_000;
36
39
  const REPORTABLE_SEVERITIES = [
37
40
  "critical",
38
41
  "high",
@@ -220,13 +223,32 @@ export async function runCodexSkillCommand(args, output, command = resolveCodexC
220
223
  windowsHide: true,
221
224
  });
222
225
  let requestedSignal = null;
226
+ let skillOutputLimitExceeded = false;
227
+ let forcedTermination;
228
+ let forceStatusCompletion = null;
229
+ let forceCaptureCompletion = null;
230
+ let invocationStatus;
231
+ const requestTermination = (signal) => {
232
+ requestedSignal = signal;
233
+ invocation.kill(signal);
234
+ if (forcedTermination !== undefined)
235
+ return;
236
+ forcedTermination = setTimeout(() => {
237
+ forcedTermination = undefined;
238
+ if (invocation.exitCode === null && invocation.signalCode === null) {
239
+ invocation.kill("SIGKILL");
240
+ }
241
+ forceCaptureCompletion?.();
242
+ invocation.stdout?.destroy();
243
+ invocation.stderr?.destroy();
244
+ forceStatusCompletion?.();
245
+ }, CHILD_TERMINATION_GRACE_MS);
246
+ };
223
247
  const onInterrupt = () => {
224
- requestedSignal = "SIGINT";
225
- invocation.kill("SIGINT");
248
+ requestTermination("SIGINT");
226
249
  };
227
250
  const onTerminate = () => {
228
- requestedSignal = "SIGTERM";
229
- invocation.kill("SIGTERM");
251
+ requestTermination("SIGTERM");
230
252
  };
231
253
  process.on("SIGINT", onInterrupt);
232
254
  process.on("SIGTERM", onTerminate);
@@ -237,20 +259,42 @@ export async function runCodexSkillCommand(args, output, command = resolveCodexC
237
259
  });
238
260
  const captured = output === undefined || invocation.stdout === null
239
261
  ? Promise.resolve(undefined)
240
- : readSkillCommandOutput(invocation.stdout);
241
- const [status, events] = await Promise.all([
242
- new Promise((resolve, reject) => {
243
- invocation.once("error", reject);
244
- invocation.once(output === undefined ? "exit" : "close", (code, signal) => {
245
- resolve(requestedSignal === "SIGINT" || signal === "SIGINT"
246
- ? 130
247
- : requestedSignal === "SIGTERM" || signal === "SIGTERM"
248
- ? 143
249
- : code ?? 1);
250
- });
251
- }),
252
- captured,
253
- ]);
262
+ : Promise.race([
263
+ readSkillCommandOutput(invocation.stdout, () => {
264
+ skillOutputLimitExceeded = true;
265
+ requestTermination("SIGTERM");
266
+ }),
267
+ new Promise((resolve) => {
268
+ forceCaptureCompletion = () => resolve(undefined);
269
+ }),
270
+ ]);
271
+ invocationStatus = new Promise((resolve, reject) => {
272
+ let completed = false;
273
+ const complete = (code, signal) => {
274
+ if (completed)
275
+ return;
276
+ completed = true;
277
+ forceStatusCompletion = null;
278
+ resolve(requestedSignal === "SIGINT" || signal === "SIGINT"
279
+ ? 130
280
+ : requestedSignal === "SIGTERM" || signal === "SIGTERM"
281
+ ? 143
282
+ : code ?? 1);
283
+ };
284
+ forceStatusCompletion = () => complete(null, null);
285
+ invocation.once("error", (error) => {
286
+ if (completed)
287
+ return;
288
+ completed = true;
289
+ forceStatusCompletion = null;
290
+ reject(error);
291
+ });
292
+ invocation.once(output === undefined ? "exit" : "close", complete);
293
+ });
294
+ const [status, events] = await Promise.all([invocationStatus, captured]);
295
+ if (skillOutputLimitExceeded) {
296
+ throw new CodexSecurityError(SKILL_OUTPUT_LIMIT_MESSAGE);
297
+ }
254
298
  if (output === undefined || status === 130 || status === 143)
255
299
  return status;
256
300
  if (status !== 0) {
@@ -267,10 +311,15 @@ export async function runCodexSkillCommand(args, output, command = resolveCodexC
267
311
  catch (error) {
268
312
  invocation.stdout?.destroy();
269
313
  invocation.stderr?.destroy();
270
- invocation.kill();
314
+ requestTermination("SIGTERM");
315
+ await invocationStatus?.catch(() => undefined);
271
316
  throw error;
272
317
  }
273
318
  finally {
319
+ if (forcedTermination !== undefined)
320
+ clearTimeout(forcedTermination);
321
+ forceStatusCompletion = null;
322
+ forceCaptureCompletion = null;
274
323
  process.off("SIGINT", onInterrupt);
275
324
  process.off("SIGTERM", onTerminate);
276
325
  }
@@ -358,14 +407,34 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
358
407
  let renderedHistory;
359
408
  const history = async (args, select = (value) => value) => {
360
409
  try {
361
- return select(await dependencies.runWorkbench(args));
410
+ return await select(await dependencies.runWorkbench(args));
362
411
  }
363
412
  catch (error) {
364
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
413
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
365
414
  exitCode = 2;
366
415
  return undefined;
367
416
  }
368
417
  };
418
+ const matchScanPair = async (beforeId, afterId, force = false) => history([
419
+ "compare-scans",
420
+ "--before-scan-id",
421
+ beforeId,
422
+ "--after-scan-id",
423
+ afterId,
424
+ "--include-matching-inputs",
425
+ ], async ({ matchingCached, matchingInputs, ...comparison }) => {
426
+ if (matchingCached && !force)
427
+ return comparison;
428
+ return await dependencies.runWorkbench([
429
+ "save-scan-comparison",
430
+ "--before-scan-id",
431
+ beforeId,
432
+ "--after-scan-id",
433
+ afterId,
434
+ "--matches-json",
435
+ JSON.stringify(await dependencies.matchFindings(matchingInputs)),
436
+ ]);
437
+ });
369
438
  const presentHistory = (result, command, format, settings = {}) => {
370
439
  if (result === undefined ||
371
440
  format !== "toon" ||
@@ -505,7 +574,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
505
574
  scanArguments = scanArgumentsFromRecipe(recipe, args.scanId);
506
575
  }
507
576
  catch (error) {
508
- const message = cliErrorMessage(error);
577
+ const message = redactedErrorMessage(error);
509
578
  errorOutput.write(`codex-security: ${message}\n`);
510
579
  exitCode = 2;
511
580
  return incurError({
@@ -558,40 +627,18 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
558
627
  if (options.all) {
559
628
  return presentHistory(await matchAllScans(dependencies, options.force), "match-all", format);
560
629
  }
561
- const comparison = await history([
562
- "compare-scans",
563
- "--before-scan-id",
564
- args.beforeId,
565
- "--after-scan-id",
566
- args.afterId,
567
- "--include-matching-inputs",
568
- ]);
569
- if (comparison === undefined)
570
- return undefined;
571
- const { matchingCached, matchingInputs, ...visibleComparison } = comparison;
572
- if (matchingCached && !options.force) {
573
- return presentHistory(visibleComparison, "compare", format);
574
- }
575
- const matching = await dependencies.matchFindings(matchingInputs);
576
- return presentHistory(await history([
577
- "save-scan-comparison",
578
- "--before-scan-id",
579
- args.beforeId,
580
- "--after-scan-id",
581
- args.afterId,
582
- "--matches-json",
583
- JSON.stringify(matching),
584
- ]), "compare", format);
630
+ return presentHistory(await matchScanPair(args.beforeId, args.afterId, options.force), "compare", format);
585
631
  }
586
632
  catch (error) {
587
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
633
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
588
634
  exitCode = 2;
589
635
  return undefined;
590
636
  }
591
637
  },
592
638
  })
593
639
  .command("compare", {
594
- description: "Compare findings and coverage using saved matches.",
640
+ description: "Match and compare findings and coverage between scans.",
641
+ destructive: true,
595
642
  mcp: false,
596
643
  args: z.object({
597
644
  beforeId: z.string().min(1).describe("Earlier saved scan identifier."),
@@ -599,14 +646,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
599
646
  }),
600
647
  output: z.record(z.string(), z.unknown()).optional(),
601
648
  async run({ args, format }) {
602
- return presentHistory(await history([
603
- "compare-scans",
604
- "--before-scan-id",
605
- args.beforeId,
606
- "--after-scan-id",
607
- args.afterId,
608
- "--require-matches",
609
- ]), "compare", format);
649
+ return presentHistory(await matchScanPair(args.beforeId, args.afterId), "compare", format);
610
650
  },
611
651
  });
612
652
  const cli = Cli.create("codex-security", {
@@ -825,7 +865,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
825
865
  };
826
866
  }
827
867
  catch (error) {
828
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
868
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
829
869
  exitCode = 2;
830
870
  return undefined;
831
871
  }
@@ -963,7 +1003,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
963
1003
  createSecurity: dependencies.createSecurity,
964
1004
  signal: controller.signal,
965
1005
  onProgress: ({ repository, status, attempt, error }) => {
966
- errorOutput.write(`codex-security: ${repository} ${status} (attempt ${attempt})${error === undefined ? "" : `: ${cliErrorMessage(error)}`}\n`);
1006
+ errorOutput.write(`codex-security: ${repository} ${status} (attempt ${attempt})${error === undefined ? "" : `: ${redactedErrorMessage(error)}`}\n`);
967
1007
  },
968
1008
  });
969
1009
  exitCode = interruptedExitCode() ?? (result.failed > 0 ? 2 : 0);
@@ -975,7 +1015,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
975
1015
  (error instanceof Error && error.name === "ExitPromptError"
976
1016
  ? 130
977
1017
  : 2);
978
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
1018
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
979
1019
  }
980
1020
  finally {
981
1021
  dependencies.removeSignalListener("SIGINT", onInterrupt);
@@ -1051,7 +1091,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1051
1091
  }
1052
1092
  catch (error) {
1053
1093
  exitCode = 2;
1054
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
1094
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
1055
1095
  }
1056
1096
  },
1057
1097
  })
@@ -1078,7 +1118,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1078
1118
  }
1079
1119
  catch (error) {
1080
1120
  exitCode = 2;
1081
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
1121
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
1082
1122
  }
1083
1123
  },
1084
1124
  })
@@ -1106,7 +1146,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1106
1146
  async run({ args, options }) {
1107
1147
  const credentialHome = dependencies.prepareAuthenticationHome !== undefined
1108
1148
  ? await dependencies.prepareAuthenticationHome(dependencies.environment)
1109
- : codexSecurityCredentialHome(dependencies.environment);
1149
+ : await prepareCodexSecurityCredentialHome(dependencies.environment);
1110
1150
  const authenticationEnvironment = {
1111
1151
  ...dependencies.environment,
1112
1152
  CODEX_HOME: credentialHome,
@@ -1161,7 +1201,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1161
1201
  async run() {
1162
1202
  const credentialHome = dependencies.prepareAuthenticationHome !== undefined
1163
1203
  ? await dependencies.prepareAuthenticationHome(dependencies.environment)
1164
- : codexSecurityCredentialHome(dependencies.environment);
1204
+ : await prepareCodexSecurityCredentialHome(dependencies.environment);
1165
1205
  const authenticationEnvironment = {
1166
1206
  ...dependencies.environment,
1167
1207
  CODEX_HOME: credentialHome,
@@ -1227,7 +1267,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1227
1267
  if (frameworkExit !== undefined) {
1228
1268
  if (exitCode !== 0)
1229
1269
  return exitCode;
1230
- errorOutput.write(`codex-security: ${cliErrorMessage(incurErrorMessage(frameworkOutput))}\n`);
1270
+ errorOutput.write(`codex-security: ${redactedErrorMessage(incurErrorMessage(frameworkOutput))}\n`);
1231
1271
  return 2;
1232
1272
  }
1233
1273
  if (frameworkOutput.length === 0)
@@ -1237,7 +1277,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1237
1277
  return exitCode;
1238
1278
  }
1239
1279
  catch (error) {
1240
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
1280
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
1241
1281
  return 2;
1242
1282
  }
1243
1283
  }
@@ -1629,24 +1669,33 @@ async function runSkill(skill, inputs, codexOverrides, effort, stdout, stderr, d
1629
1669
  stderr,
1630
1670
  });
1631
1671
  }
1632
- export async function readSkillCommandOutput(stream) {
1672
+ export async function readSkillCommandOutput(stream, onLimitExceeded) {
1633
1673
  let message;
1634
1674
  let error;
1635
1675
  let malformed = false;
1636
- for await (const line of createInterface({ input: Readable.from(stream) })) {
1676
+ let exceeded = false;
1677
+ const markExceeded = () => {
1678
+ if (exceeded)
1679
+ return;
1680
+ exceeded = true;
1681
+ onLimitExceeded?.();
1682
+ };
1683
+ const readLine = (bytes) => {
1684
+ const content = bytes.at(-1) === 0x0d ? bytes.subarray(0, bytes.length - 1) : bytes;
1685
+ const line = content.toString("utf8");
1637
1686
  if (line.trim().length === 0)
1638
- continue;
1687
+ return;
1639
1688
  let event;
1640
1689
  try {
1641
1690
  event = JSON.parse(line);
1642
1691
  }
1643
1692
  catch {
1644
1693
  malformed = true;
1645
- continue;
1694
+ return;
1646
1695
  }
1647
1696
  if (typeof event !== "object" || event === null) {
1648
1697
  malformed = true;
1649
- continue;
1698
+ return;
1650
1699
  }
1651
1700
  const value = event;
1652
1701
  if (value["type"] === "item.completed") {
@@ -1657,7 +1706,12 @@ export async function readSkillCommandOutput(stream) {
1657
1706
  item.type === "agent_message" &&
1658
1707
  "text" in item &&
1659
1708
  typeof item.text === "string") {
1660
- message = item.text;
1709
+ if (Buffer.byteLength(item.text, "utf8") > MAX_SKILL_RESPONSE_BYTES) {
1710
+ markExceeded();
1711
+ }
1712
+ else {
1713
+ message = item.text;
1714
+ }
1661
1715
  }
1662
1716
  }
1663
1717
  else if (value["type"] === "turn.failed") {
@@ -1666,14 +1720,62 @@ export async function readSkillCommandOutput(stream) {
1666
1720
  detail !== null &&
1667
1721
  "message" in detail &&
1668
1722
  typeof detail.message === "string") {
1669
- error = detail.message;
1723
+ if (Buffer.byteLength(detail.message, "utf8") > MAX_SKILL_RESPONSE_BYTES) {
1724
+ markExceeded();
1725
+ }
1726
+ else {
1727
+ error = detail.message;
1728
+ }
1670
1729
  }
1671
1730
  }
1672
1731
  else if (value["type"] === "error" &&
1673
1732
  typeof value["message"] === "string") {
1674
- error = value["message"];
1733
+ if (Buffer.byteLength(value["message"], "utf8") > MAX_SKILL_RESPONSE_BYTES) {
1734
+ markExceeded();
1735
+ }
1736
+ else {
1737
+ error = value["message"];
1738
+ }
1739
+ }
1740
+ };
1741
+ const pending = Buffer.alloc(MAX_SKILL_EVENT_BYTES);
1742
+ let pendingBytes = 0;
1743
+ let discardingOversizedLine = false;
1744
+ for await (const rawChunk of stream) {
1745
+ const chunk = Buffer.isBuffer(rawChunk)
1746
+ ? rawChunk
1747
+ : Buffer.from(rawChunk, "utf8");
1748
+ let start = 0;
1749
+ while (start < chunk.length) {
1750
+ const newline = chunk.indexOf(0x0a, start);
1751
+ const end = newline === -1 ? chunk.length : newline;
1752
+ const segment = chunk.subarray(start, end);
1753
+ if (!discardingOversizedLine) {
1754
+ if (pendingBytes + segment.length > MAX_SKILL_EVENT_BYTES) {
1755
+ markExceeded();
1756
+ discardingOversizedLine = true;
1757
+ pendingBytes = 0;
1758
+ }
1759
+ else if (segment.length > 0) {
1760
+ segment.copy(pending, pendingBytes);
1761
+ pendingBytes += segment.length;
1762
+ }
1763
+ }
1764
+ if (newline === -1)
1765
+ break;
1766
+ if (!discardingOversizedLine) {
1767
+ readLine(pending.subarray(0, pendingBytes));
1768
+ }
1769
+ pendingBytes = 0;
1770
+ discardingOversizedLine = false;
1771
+ start = newline + 1;
1675
1772
  }
1676
1773
  }
1774
+ if (!discardingOversizedLine && pendingBytes > 0) {
1775
+ readLine(pending.subarray(0, pendingBytes));
1776
+ }
1777
+ if (exceeded)
1778
+ throw new CodexSecurityError(SKILL_OUTPUT_LIMIT_MESSAGE);
1677
1779
  return {
1678
1780
  ...(message === undefined ? {} : { message }),
1679
1781
  ...(error === undefined ? {} : { error }),
@@ -1759,7 +1861,7 @@ async function runExport(arguments_, output, errorOutput, dependencies) {
1759
1861
  return 0;
1760
1862
  }
1761
1863
  catch (error) {
1762
- errorOutput.write(`codex-security: ${cliErrorMessage(error)}\n`);
1864
+ errorOutput.write(`codex-security: ${redactedErrorMessage(error)}\n`);
1763
1865
  return 2;
1764
1866
  }
1765
1867
  }
@@ -1875,7 +1977,7 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1875
1977
  },
1876
1978
  onOutputArchived: (archiveDir) => {
1877
1979
  progress?.stopTimer();
1878
- errorOutput.write(`Moved existing results to: ${cliErrorMessage(archiveDir)}\n`);
1980
+ errorOutput.write(`Moved existing results to: ${redactedErrorMessage(archiveDir)}\n`);
1879
1981
  },
1880
1982
  signal: preparationAbortController.signal,
1881
1983
  onOutputDirReady: (path) => {
@@ -1930,10 +2032,10 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1930
2032
  progress.startTimer(runningMessage());
1931
2033
  },
1932
2034
  onWarning: (warning) => {
1933
- errorOutput.write(`codex-security: warning: ${cliErrorMessage(warning)}\n`);
2035
+ errorOutput.write(`codex-security: warning: ${redactedErrorMessage(warning)}\n`);
1934
2036
  },
1935
2037
  onObserverError: (observer, error) => {
1936
- errorOutput.write(`codex-security: warning: ${observer} observer failed: ${cliErrorMessage(error)}\n`);
2038
+ errorOutput.write(`codex-security: warning: ${observer} observer failed: ${redactedErrorMessage(error)}\n`);
1937
2039
  },
1938
2040
  };
1939
2041
  if (arguments_.dryRun) {
@@ -1969,19 +2071,14 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1969
2071
  }
1970
2072
  if (failed) {
1971
2073
  const message = failure instanceof OutputInsideProtectedRootError
1972
- ? cliErrorMessage(protectedRootErrorMessage(failure))
2074
+ ? redactedErrorMessage(protectedRootErrorMessage(failure))
1973
2075
  : scanFailureMessage(failure, selectedAuthentication);
1974
- if (failure instanceof OutputInsideProtectedRootError) {
1975
- errorOutput.write(`${message}\n`);
1976
- }
1977
- else {
1978
- errorOutput.write(`codex-security: ${message}\n`);
1979
- }
2076
+ errorOutput.write(`${message}\n`);
1980
2077
  if (failure instanceof ScanInterruptedError) {
1981
2078
  return { exitCode: 2, error: message };
1982
2079
  }
1983
2080
  if (scanDir !== null) {
1984
- errorOutput.write(`codex-security: Partial output was kept at ${cliErrorMessage(scanDir)}.\n`);
2081
+ errorOutput.write(`Partial output was kept at ${redactedErrorMessage(scanDir)}.\n`);
1985
2082
  }
1986
2083
  return { exitCode: 2, error: message };
1987
2084
  }
@@ -1990,7 +2087,7 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1990
2087
  return { exitCode: 0, data: { dryRun: true, ...preflight } };
1991
2088
  }
1992
2089
  if (result === null) {
1993
- errorOutput.write("codex-security: scan completed without a result\n");
2090
+ errorOutput.write("scan completed without a result\n");
1994
2091
  return { exitCode: 2, error: "Scan completed without a result." };
1995
2092
  }
1996
2093
  const threshold = arguments_.failOnSeverity;
@@ -2000,7 +2097,9 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
2000
2097
  const blockingCount = result.findings.findings.filter(({ severity }) => blockingSeverities.has(severity.level)).length;
2001
2098
  const incomplete = result.coverage.completeness !== "complete";
2002
2099
  progress?.stage("Scan complete");
2003
- printScanSummary(result, progress, errorOutput);
2100
+ printScanSummary(result, progress, errorOutput, progress?.interactive === true &&
2101
+ dependencies.environment["NO_COLOR"] === undefined &&
2102
+ dependencies.environment["TERM"] !== "dumb");
2004
2103
  if (incomplete) {
2005
2104
  errorOutput.write(threshold === undefined
2006
2105
  ? `codex-security: Scan coverage is ${result.coverage.completeness}; results may be incomplete.\n`
@@ -2009,7 +2108,53 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
2009
2108
  }
2010
2109
  return { exitCode: blockingCount > 0 ? 1 : 0, data: result.toJSON() };
2011
2110
  }
2111
+ // Filesystem and OS syscall failures cannot originate from the model transport,
2112
+ // so they must never be rewritten as connectivity or credential advice. Network
2113
+ // errno codes are deliberately absent: they are genuine transport failures.
2114
+ const LOCAL_SYSCALL_CODES = new Set([
2115
+ "EACCES",
2116
+ "EBUSY",
2117
+ "EEXIST",
2118
+ "EFBIG",
2119
+ "EIO",
2120
+ "EISDIR",
2121
+ "ELOOP",
2122
+ "EMFILE",
2123
+ "ENAMETOOLONG",
2124
+ "ENFILE",
2125
+ "ENOENT",
2126
+ "ENOMEM",
2127
+ "ENOSPC",
2128
+ "ENOTDIR",
2129
+ "ENOTEMPTY",
2130
+ "EPERM",
2131
+ "EROFS",
2132
+ "EXDEV",
2133
+ ]);
2134
+ function isLocalScanFailure(error) {
2135
+ if (error instanceof InvalidTargetError ||
2136
+ error instanceof OutputDirectoryError ||
2137
+ error instanceof ConfigurationError ||
2138
+ error instanceof PluginPythonUnavailableError) {
2139
+ return true;
2140
+ }
2141
+ return (typeof error === "object" &&
2142
+ error !== null &&
2143
+ "code" in error &&
2144
+ typeof error.code === "string" &&
2145
+ LOCAL_SYSCALL_CODES.has(error.code));
2146
+ }
2012
2147
  function scanFailureMessage(error, authentication) {
2148
+ // A local failure keeps its own message. Classification matches bare words
2149
+ // such as "permission denied" anywhere in the text, so an EACCES from a
2150
+ // read-only TMPDIR would otherwise be reported as a credential problem.
2151
+ //
2152
+ // The advice branches below still replace the underlying text rather than
2153
+ // appending it. That is deliberate: upstream authentication and authorization
2154
+ // errors can name the organization or project, which must not reach stderr or
2155
+ // the JSON error field.
2156
+ if (isLocalScanFailure(error))
2157
+ return redactedErrorMessage(error);
2013
2158
  switch (classifyConnectionFailure(error)) {
2014
2159
  case "unauthorized":
2015
2160
  return authentication?.method === "api_key"
@@ -2029,7 +2174,7 @@ function scanFailureMessage(error, authentication) {
2029
2174
  case "network_error":
2030
2175
  case "timeout":
2031
2176
  case "unknown":
2032
- return cliErrorMessage(error);
2177
+ return redactedErrorMessage(error);
2033
2178
  }
2034
2179
  }
2035
2180
  function scanScope(arguments_) {
@@ -2041,7 +2186,7 @@ function scanScope(arguments_) {
2041
2186
  portable.startsWith("//")
2042
2187
  ? portable.split("/").at(-1) ?? portable
2043
2188
  : portable;
2044
- return cliErrorMessage(scoped.replaceAll(/[\u0000-\u001F\u007F]/gu, " "));
2189
+ return redactedErrorMessage(scoped.replaceAll(/[\u0000-\u001F\u007F]/gu, " "));
2045
2190
  });
2046
2191
  return `${displayed.join(", ")}${arguments_.paths.length > displayed.length ? `, +${arguments_.paths.length - displayed.length} more` : ""}`;
2047
2192
  }
@@ -2059,7 +2204,8 @@ function scanPhase(value) {
2059
2204
  attack_path: "analyzing attack paths",
2060
2205
  }[value];
2061
2206
  }
2062
- function printScanSummary(result, progress, errorOutput) {
2207
+ function printScanSummary(result, progress, errorOutput, color) {
2208
+ const paint = (value, code) => color ? `\u001B[${code}m${value}\u001B[0m` : value;
2063
2209
  const severities = new Map();
2064
2210
  for (const finding of result.findings.findings) {
2065
2211
  severities.set(finding.severity.level, (severities.get(finding.severity.level) ?? 0) + 1);
@@ -2070,7 +2216,6 @@ function printScanSummary(result, progress, errorOutput) {
2070
2216
  })
2071
2217
  .filter((value) => value !== null)
2072
2218
  .join(", ");
2073
- errorOutput.write(`codex-security: Findings: ${result.findings.findings.length}${severitySummary === "" ? "" : ` (${severitySummary})`}. Coverage: ${result.coverage.completeness}.\n`);
2074
2219
  const started = Date.parse(result.manifest.scan.startedAt);
2075
2220
  const completed = Date.parse(result.manifest.scan.completedAt);
2076
2221
  const elapsed = Number.isFinite(started) &&
@@ -2078,16 +2223,29 @@ function printScanSummary(result, progress, errorOutput) {
2078
2223
  completed >= started
2079
2224
  ? Math.floor((completed - started) / 1_000)
2080
2225
  : progress?.elapsedSeconds ?? 0;
2081
- errorOutput.write(`codex-security: Elapsed: ${elapsed}s.\n`);
2226
+ const duration = elapsed < 60
2227
+ ? `${elapsed}s`
2228
+ : `${Math.floor(elapsed / 60)}m ${elapsed % 60}s`;
2229
+ const findingCount = result.findings.findings.length;
2230
+ const findingColor = findingCount === 0
2231
+ ? 32
2232
+ : severities.has("critical") || severities.has("high")
2233
+ ? 31
2234
+ : severities.has("medium")
2235
+ ? 33
2236
+ : 36;
2237
+ errorOutput.write(`\n ${paint("REPORT", "1;36")} ${paint(redactedErrorMessage(result.reportPath), 4)}\n\n` +
2238
+ ` ${paint("FINDINGS", 1)} ${paint(`${findingCount}${severitySummary === "" ? "" : ` (${severitySummary})`}`, findingColor)}\n` +
2239
+ ` ${paint("COVERAGE", 1)} ${result.coverage.completeness}\n` +
2240
+ ` ${paint("ELAPSED", 1)} ${duration}\n`);
2082
2241
  const tokenSummary = formatTokenUsage(result.turnResult.usage);
2083
2242
  if (tokenSummary !== null) {
2084
- errorOutput.write(`codex-security: Tokens: ${tokenSummary}.\n`);
2243
+ errorOutput.write(` ${paint("TOKENS", 1)} ${tokenSummary}\n`);
2085
2244
  }
2086
2245
  if (result.cost !== null) {
2087
- errorOutput.write(`codex-security: Estimated cost: ${formatUsd(result.cost.estimatedUsd)} USD.\n`);
2246
+ errorOutput.write(` ${paint("COST", 1)} ${formatUsd(result.cost.estimatedUsd)}\n`);
2088
2247
  }
2089
- errorOutput.write(`codex-security: Report: ${cliErrorMessage(result.reportPath)}\n`);
2090
- errorOutput.write(`codex-security: Results: ${cliErrorMessage(result.scanDir)}\n`);
2248
+ errorOutput.write(` ${paint("RESULTS", 1)} ${redactedErrorMessage(result.scanDir)}\n`);
2091
2249
  }
2092
2250
  function formatTokenUsage(usage) {
2093
2251
  if (usage === null || typeof usage !== "object")
@@ -2127,7 +2285,7 @@ function protectedRootErrorMessage(error) {
2127
2285
  ? "Set TMPDIR (or TEMP on Windows) to a writable directory outside the protected root."
2128
2286
  : `Set TMPDIR (or TEMP on Windows) to ${quoteCliPath(suggestion)} after creating that directory.`;
2129
2287
  return [
2130
- `codex-security: ${description} must be outside the scanned directory and any enclosing Git worktree.`,
2288
+ `${description} must be outside the scanned directory and any enclosing Git worktree.`,
2131
2289
  ` Resolved path: ${error.outputDirectory}`,
2132
2290
  ` Protected root: ${error.protectedRoot}`,
2133
2291
  ` Reason: ${reason}`,
@@ -2328,7 +2486,7 @@ function interruptedExit(signal, scanDir, errorOutput) {
2328
2486
  errorOutput.write(`codex-security: Scan ${ctrlC ? "canceled by Ctrl-C" : "terminated by SIGTERM"}.\n`);
2329
2487
  errorOutput.write(scanDir === null
2330
2488
  ? "codex-security: No partial output was kept.\n"
2331
- : `codex-security: Partial output was kept at ${cliErrorMessage(scanDir)}.\n`);
2489
+ : `codex-security: Partial output was kept at ${redactedErrorMessage(scanDir)}.\n`);
2332
2490
  return ctrlC ? 130 : 143;
2333
2491
  }
2334
2492
  function isJsonObject(value) {
@@ -2347,22 +2505,11 @@ function invokedAsMain() {
2347
2505
  return false;
2348
2506
  }
2349
2507
  }
2350
- function cliErrorMessage(error) {
2351
- const message = error instanceof Error ? error.message : String(error);
2352
- return message
2353
- .replaceAll(/(\b[A-Za-z0-9_-]{0,64}(?:api[_-]?key|access[_-]?key(?:[_-]?id)?|token|secret|credential|signature|sig|password|passwd)\b(?:\\?["'])?\s*[:=]\s*(?:\\?["'])?)[^\s"',;}&\\\]]+/giu, "$1[redacted]")
2354
- .replaceAll(/sk-(?:proj-)?[A-Za-z0-9_*=-]{8,}/gu, "[redacted]")
2355
- .replaceAll(/(?:github_pat_|gh[pousr]_)[A-Za-z0-9_-]{8,}/giu, "[redacted]")
2356
- .replaceAll(/npm_[A-Za-z0-9_-]{8,}/giu, "[redacted]")
2357
- .replaceAll(/(^|%20|[^A-Za-z0-9_])(Bearer|Basic|Token)((?:\s|%20|\+)+)[A-Za-z0-9.%_~+/*=-]{8,}/giu, "$1$2$3[redacted]")
2358
- .replaceAll(/((?:https?|ssh|git\+ssh):\/\/)[^\s/@]+@/giu, "$1[redacted]@")
2359
- .replaceAll(/((?:[?&]|%3F|%26)(?:(?!%3F|%26|%3D)(?:[A-Za-z0-9_.%-]|\[|\])){0,64}(?:api[_-]?key|access(?:[_-]|%5F|%2D)?key(?:(?:[_-]|%5F|%2D)?id)?|token|secret|credential|signature|sig|password|passwd)(?:\]|%5D)?(?:=|%3D))(?:(?!%26)[^&\s])+/giu, "$1[redacted]");
2360
- }
2361
2508
  if (invokedAsMain()) {
2362
2509
  void main().then((exitCode) => {
2363
2510
  process.exitCode = exitCode;
2364
2511
  }, (error) => {
2365
- process.stderr.write(`codex-security: ${cliErrorMessage(error)}\n`);
2512
+ process.stderr.write(`codex-security: ${redactedErrorMessage(error)}\n`);
2366
2513
  process.exitCode = 2;
2367
2514
  });
2368
2515
  }