@openai/codex-security 0.1.0 → 0.1.1

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
@@ -11,6 +11,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
11
11
  import { Cli, z } from "incur";
12
12
  import { parse as parseToml } from "smol-toml";
13
13
  import { classifyConnectionFailure, CodexSecurity, scanAuthentication, } from "./api.js";
14
+ import { accountStatus } from "./auth.js";
14
15
  import { createBulkScanDiscoveryDependencies, runBulkScanWizard, } from "./bulk-scan-discovery.js";
15
16
  import { DEFAULT_CODEX_CONFIG, mergedCodexConfig, scanModelConfiguration, } from "./config.js";
16
17
  import { formatUsd } from "./cost.js";
@@ -48,6 +49,7 @@ const EXPORT_DEFAULT_OUTPUTS = {
48
49
  sarif: "results.sarif",
49
50
  };
50
51
  const VALUE_OPTIONS = new Set([
52
+ "--auth",
51
53
  "--path",
52
54
  "--knowledge-base",
53
55
  "--diff",
@@ -78,6 +80,12 @@ function optionValue(flag) {
78
80
  const DEFAULT_DEPENDENCIES = {
79
81
  createSecurity: (config) => new CodexSecurity(config),
80
82
  environment: process.env,
83
+ hasStoredChatGPTSignIn: async () => {
84
+ const environment = Object.fromEntries(Object.entries(process.env).filter(([name]) => name.toUpperCase() !== "OPENAI_API_KEY" &&
85
+ name.toUpperCase() !== "CODEX_API_KEY"));
86
+ const status = await accountStatus(resolveCodexCommand(), environment);
87
+ return status.authenticated && /\bchatgpt\b/iu.test(status.details);
88
+ },
81
89
  currentDirectory: cwd,
82
90
  now: Date.now,
83
91
  setInterval: (callback, milliseconds) => setInterval(callback, milliseconds),
@@ -535,6 +543,10 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
535
543
  }),
536
544
  options: z
537
545
  .object({
546
+ auth: z
547
+ .enum(["auto", "chatgpt", "api-key"])
548
+ .default("auto")
549
+ .describe("Select automatic, ChatGPT, or API-key authentication."),
538
550
  path: z
539
551
  .array(optionValue("--path"))
540
552
  .default([])
@@ -619,6 +631,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
619
631
  return;
620
632
  }
621
633
  const outcome = await runScan({
634
+ auth: options.auth,
622
635
  repository: args.repository,
623
636
  paths: options.path,
624
637
  knowledgeBasePaths: options.knowledgeBase,
@@ -961,6 +974,22 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
961
974
  errorOutput.write("To use a ChatGPT sign-in, unset OPENAI_API_KEY and CODEX_API_KEY.\n");
962
975
  }
963
976
  }
977
+ else if (exitCode === 0 &&
978
+ !options.withApiKey &&
979
+ !options.withAccessToken) {
980
+ const authentication = scanAuthentication(dependencies.environment);
981
+ if (authentication.method === "api_key") {
982
+ const configuredApiKeyVariables = Object.entries(dependencies.environment)
983
+ .filter(([name, value]) => value?.trim() &&
984
+ (name.toUpperCase() === "OPENAI_API_KEY" ||
985
+ name.toUpperCase() === "CODEX_API_KEY"))
986
+ .map(([name]) => name);
987
+ errorOutput.write("ChatGPT login succeeded. Interactive scans will ask which account to use; " +
988
+ `noninteractive scans will use ${authentication.source}.\n` +
989
+ "To use your ChatGPT sign-in, pass '--auth chatgpt' or run " +
990
+ `'unset ${configuredApiKeyVariables.join(" ")}'.\n`);
991
+ }
992
+ }
964
993
  },
965
994
  })
966
995
  .command("logout", {
@@ -1601,6 +1630,7 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1601
1630
  let security = null;
1602
1631
  let result = null;
1603
1632
  let preflight = null;
1633
+ let selectedAuthentication = null;
1604
1634
  let failed = false;
1605
1635
  let failure;
1606
1636
  try {
@@ -1612,6 +1642,30 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1612
1642
  codexOverrides: arguments_.codexOverrides ??
1613
1643
  parseCodexOverrides(arguments_.codex, arguments_.model),
1614
1644
  };
1645
+ let auth = arguments_.auth;
1646
+ selectedAuthentication = scanAuthentication(dependencies.environment, auth);
1647
+ if ((auth === undefined || auth === "auto") &&
1648
+ !arguments_.dryRun &&
1649
+ interactive &&
1650
+ errorOutput.isTTY === true &&
1651
+ selectedAuthentication.method === "api_key") {
1652
+ const prompt = dependencies.scanAuthenticationPrompt ??
1653
+ createBulkScanDiscoveryDependencies({
1654
+ output: errorOutput,
1655
+ now: dependencies.now,
1656
+ currentDirectory: dependencies.currentDirectory,
1657
+ }).prompt;
1658
+ if (prompt.isInteractive() &&
1659
+ (await dependencies.hasStoredChatGPTSignIn?.()) === true) {
1660
+ const source = selectedAuthentication.source;
1661
+ errorOutput.write(`Both a ChatGPT sign-in and an API key from ${source} are available.\n`);
1662
+ auth = await prompt.select("How would you like to authenticate this scan?", [
1663
+ { label: "ChatGPT subscription", value: "chatgpt" },
1664
+ { label: `API key from ${source}`, value: "api-key" },
1665
+ ]);
1666
+ selectedAuthentication = scanAuthentication(dependencies.environment, auth);
1667
+ }
1668
+ }
1615
1669
  progress = new Progress(errorOutput, dependencies, interactive);
1616
1670
  const scope = scanScope(arguments_);
1617
1671
  const runningMessage = () => phase === null
@@ -1622,6 +1676,7 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1622
1676
  progress.startTimer(arguments_.dryRun ? "Validating scan inputs" : "Preparing scan");
1623
1677
  security = dependencies.createSecurity(config);
1624
1678
  const options = {
1679
+ auth,
1625
1680
  target,
1626
1681
  knowledgeBasePaths: arguments_.knowledgeBasePaths,
1627
1682
  mode: arguments_.mode,
@@ -1649,14 +1704,11 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1649
1704
  scanDir = path;
1650
1705
  },
1651
1706
  onAuthentication: (authentication) => {
1707
+ selectedAuthentication = authentication;
1652
1708
  progress?.stopTimer();
1653
1709
  if (authentication.method === "api_key") {
1654
1710
  progress?.stage(`Authentication: API key from ${authentication.source}.`);
1655
- if (errorOutput.isTTY === true) {
1656
- progress?.stage(process.platform === "win32"
1657
- ? "To use a ChatGPT sign-in, unset OPENAI_API_KEY and CODEX_API_KEY, then retry the scan."
1658
- : "Retry with ChatGPT: env -u OPENAI_API_KEY -u CODEX_API_KEY codex-security scan ...");
1659
- }
1711
+ progress?.stage("To use your ChatGPT sign-in, retry with --auth chatgpt.");
1660
1712
  }
1661
1713
  else {
1662
1714
  progress?.stage("Authentication: stored Codex credentials.");
@@ -1739,7 +1791,7 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1739
1791
  if (failed) {
1740
1792
  const message = failure instanceof OutputInsideProtectedRootError
1741
1793
  ? cliErrorMessage(protectedRootErrorMessage(failure))
1742
- : scanFailureMessage(failure);
1794
+ : scanFailureMessage(failure, selectedAuthentication);
1743
1795
  if (failure instanceof OutputInsideProtectedRootError) {
1744
1796
  errorOutput.write(`${message}\n`);
1745
1797
  }
@@ -1778,12 +1830,21 @@ async function runScan(arguments_, errorOutput, dependencies, interactive = true
1778
1830
  }
1779
1831
  return { exitCode: blockingCount > 0 ? 1 : 0, data: result.toJSON() };
1780
1832
  }
1781
- function scanFailureMessage(error) {
1833
+ function scanFailureMessage(error, authentication) {
1782
1834
  switch (classifyConnectionFailure(error)) {
1783
1835
  case "unauthorized":
1784
- return "Authentication failed. Sign in again or provide a valid API key.";
1836
+ return authentication?.method === "api_key"
1837
+ ? `Authentication failed using ${authentication.source}. ` +
1838
+ "Your ChatGPT sign-in was not used. " +
1839
+ "Retry with '--auth chatgpt' or provide a valid API key."
1840
+ : "Authentication failed using stored ChatGPT credentials. " +
1841
+ "Sign in again with 'codex-security login' or provide a valid API key.";
1785
1842
  case "forbidden":
1786
- return "The selected credentials cannot access the configured model. Use an account or API key with model access.";
1843
+ return authentication?.method === "api_key"
1844
+ ? `The API key from ${authentication.source} cannot access the configured model. ` +
1845
+ "Retry with '--auth chatgpt' or use an API key with model access."
1846
+ : "The stored ChatGPT credentials cannot access the configured model. " +
1847
+ "Use an account or API key with model access.";
1787
1848
  case "rate_limited":
1788
1849
  return "The configured account reached its rate limit. Wait and retry.";
1789
1850
  case "network_error":