跳转至

导入 GitHub 代码扫描发现

使用 codex-security import github 生成的 JSON 数组。它包含所选 ref 的每条警报以及 GitHub 返回的实例。

快速开始

在匹配的检出目录中,设置 repository 和 source key。对于 GitHub Enterprise,请先使用你的主机名导出 GH_HOST。对于重复导入,请使用之前预览中的确切 source key,包括大小写。

repo=OWNER/REPO
source_key="${GH_HOST:-github.com}/$repo/code-scanning"

已保存的报告。 如果你已经有采集器的完整 JSON 数组,请设置 report=/private/path/alerts.json。继续查看预览报告。

从 GitHub 导出

使用 gh auth login 登录所选主机。细粒度令牌需要 Code scanning alerts: read 权限。经典令牌需要 security_events 权限,对于公共仓库则需要 public_repo 权限。 GitHub API 权限

下面的命令选择默认分支上的开放警报。对于其他分支,请在采集器命令中添加 --github-ref refs/heads/BRANCH。要包含已修复和已忽略的警报,请添加 --github-state all。要跨状态获取特定警报,请重复 --github-alert NUMBER 并省略 --github-state。保存完整数组,不要使用输出过滤器、令牌限制或 --full-output。请使用位于检出目录之外的私有临时目录。

report=
out=$(mktemp -d) &&
  codex-security import github "$repo" --format json > "$out/alerts.json" &&
  report="$out/alerts.json"

预览报告

codex-security publish findings "$report" --to local --repository . \
  --provider github --source-key "$source_key" --dry-run --format json

保存并验证

对照你的选择检查仓库、ref 和警报编号。警报仓库必须与可比的本地 Git 远程仓库(若有)匹配,否则与所选 Cloud 仓库匹配。如果预览匹配,请在本地保存或发布到 Cloud,并验证结果。

身份与证据

主机、仓库、警报编号和 ref 共同标识一个发现。同一警报在两个 ref 上会保留独立的证据。发现 URL 会打开对应的 GitHub 警报。文件、行号和提交来自所选实例。严重性来自规则的安全评级。SARIF 级别(如 error)会将缺失的安全评级保留为 unknown。

GitHub 的忽略操作适用于整条警报,即使所选 ref 的实例是开放或已修复的。Codex 中的 GitHub 连接可以选择记录。CLI 采集器通过 gh 单独进行身份验证。

使用 SDK

要使用 SDK 进行采集,请使用 gh 的凭据,或提供为该主机签发的 githubHost 和 githubToken。此示例预览 Cloud 导入。

import {
  importGitHubCodeScanningAlerts,
  normalizeGitHubCodeScanningAlerts,
  prepareNormalizedExternalPublication,
} from "@openai/codex-security";

const alerts = await importGitHubCodeScanningAlerts({
  repository: "example/project",
});
const prepared = await prepareNormalizedExternalPublication(
  normalizeGitHubCodeScanningAlerts(alerts, "cloud"),
  {
    provider: "github",
    repository: "https://github.com/example/project",
    sourceKey: "github.com/example/project/code-scanning",
  },
);
console.log(prepared.preview);

查看 Cloud 预览后,调用 await prepared.publish() 进行确认。如果你提供的规范化证据将采集器记录保留在 source_data 中,导入会将原始仓库与目标仓库进行核对。

对于本地存储,请对原始警报调用 normalizeGitHubCodeScanningAlerts(alerts, "local"),以保留超出 Cloud 限制的证据。将结果传递给 prepareNormalizedLocalImport(),并设置 repository: "/path/to/project",检查其预览,然后调用 import()。或者,将已保存的警报文件直接传递给 prepareLocalExternalImport()。使用 listLocalExternalFindings({ repository }) 读取记录。

验证所选警报

若要获取每条警报的处理结果,请根据匹配的检出目录验证已收集的记录。

import {
  CodexSecurity,
  importGitHubCodeScanningAlerts,
} from "@openai/codex-security";

const findings = await importGitHubCodeScanningAlerts({
  repository: "example/repository",
  alertNumbers: [12, 18], // Omit for all open alerts on the default branch.
  githubHost: "github.com",
  githubToken: process.env["GH_TOKEN"],
});

const security = new CodexSecurity();
try {
  for (const finding of findings) {
    const result = await security.validate({
      repositoryPath: "/path/to/repository",
      finding,
    });
    console.log(finding.url, result.disposition, result.outputDir);
  }
} finally {
  await security.close();
}

SDK 选项包括 state(默认为 "open")、ref 和 signal。如果没有 gh,请传入为所选 githubHost 签发的 githubToken。如果省略令牌,SDK 会向 GitHub CLI 请求凭据,包括其支持的令牌环境变量。githubHost 默认为 GH_HOST 或 github.com。令牌需要代码扫描读取权限;访问失败将拒绝导入。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 co/codex-security