Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

5.2 KB
Raw Blame
import type { ScanPricingContext } from "./cost-model.js";
import { statSync } from "node:fs";
import { join } from "node:path";
import type {
  CoverageDocument,
  Finding,
  FindingsDocument,
  ScanManifest,
  SeverityLevel,
  ThreatModel,
} from "./models.js";
import { estimateScanCost, type ScanCost } from "./cost.js";
import { meetsSeverity, severityThresholdRank } from "./scan-settings.js";

export interface TurnResultMetadata {
  id?: string;
  status?: string;
  model?: string;
  pricingContext?: ScanPricingContext;
  /** Source region recorded for Bedrock scans. */
  bedrockRegion?: string | null;
  durationMs?: number;
  finalResponse?: string;
  usage?: unknown;
  [key: string]: unknown;
}

export interface RepositoryFinding extends Pick<
  Finding,
  "findingId" | "occurrenceId" | "title" | "summary" | "severity"
> {
  scanId: string;
  targetId: string;
  status: "open" | "closed";
  confirmedInLatestScan: boolean;
  knownSince?: string;
  knownScanIds?: string[];
  matchedFindingIds?: string[];
  [key: string]: unknown;
}

export interface ScanResultOptions {
  manifest: ScanManifest;
  findings: FindingsDocument;
  review?: NonNullable<ScanManifest["scan"]["review"]>;
  coverage?: CoverageDocument;
  mode?: NonNullable<ScanManifest["scan"]["mode"]>;
  scanDir: string;
  threadId: string;
  turnResult: TurnResultMetadata;
  /** Cost saved when this scan completed. */
  recordedCost?: Readonly<ScanCost>;
  sarifPath?: string | null;
  threatModelPath?: string | null;
  repositoryFindings?: readonly RepositoryFinding[];
  ownerSuggestionsPath?: string;
  ownerSuggestionsError?: string;
}

export class ScanResult {
  public readonly mode: NonNullable<ScanManifest["scan"]["mode"]>;
  public readonly manifest: ScanManifest;
  public readonly findings: FindingsDocument;
  public readonly review: NonNullable<ScanManifest["scan"]["review"]>;
  public readonly scanDir: string;
  public readonly threadId: string;
  public readonly turnResult: Readonly<TurnResultMetadata>;
  public readonly cost: Readonly<ScanCost> | null;
  public readonly sarifPath: string | null;
  public readonly threatModelPath: string | null;
  public repositoryFindings: readonly RepositoryFinding[] | undefined;
  public ownerSuggestionsPath: string | undefined;
  public ownerSuggestionsError: string | undefined;

  public constructor(options: ScanResultOptions) {
    this.mode =
      options.mode ?? options.manifest.scan.mode ?? options.coverage!.mode;
    this.manifest = options.manifest;
    this.findings = options.findings;
    this.review = options.review ??
      options.manifest.scan.review ?? {
        excludedPaths: options.coverage!.explicitExclusions.map(
          (item) => item.pattern,
        ),
      };
    this.scanDir = options.scanDir;
    this.threadId = options.threadId;
    this.turnResult = options.turnResult;
    this.repositoryFindings = options.repositoryFindings;
    this.ownerSuggestionsPath = options.ownerSuggestionsPath;
    this.ownerSuggestionsError = options.ownerSuggestionsError;
    this.threatModelPath = options.threatModelPath ?? null;
    this.cost =
      options.recordedCost ??
      estimateScanCost(
        options.turnResult.model,
        options.turnResult.usage,
        options.turnResult.pricingContext ?? options.turnResult.bedrockRegion,
      );
    if (options.sarifPath !== undefined) {
      this.sarifPath = options.sarifPath;
    } else {
      const defaultSarifPath = join(
        options.scanDir,
        "exports",
        "results.sarif",
      );
      try {
        this.sarifPath = statSync(defaultSarifPath, {
          throwIfNoEntry: false,
        })?.isFile()
          ? defaultSarifPath
          : null;
      } catch {
        this.sarifPath = null;
      }
    }
  }

  public get threatModel(): ThreatModel | null {
    return this.manifest.scan.threatModel ?? null;
  }

  public get reportPath(): string {
    return join(this.scanDir, "report.md");
  }

  public get pluginVersion(): string {
    return this.manifest.scan.producer.version;
  }

  public get manifestPath(): string {
    return join(this.scanDir, "scan-manifest.json");
  }

  public get findingsPath(): string {
    return join(this.scanDir, "findings.json");
  }

  public get artifactsDir(): string {
    return join(this.scanDir, "artifacts");
  }

  public hasFindingsAtOrAbove(threshold: SeverityLevel): boolean {
    severityThresholdRank(threshold);
    return this.findings.findings.some((finding) =>
      meetsSeverity(finding, threshold),
    );
  }

  public toJSON(): Record<string, unknown> {
    return {
      manifest: this.manifest,
      repositoryFindings: this.repositoryFindings,
      ...(this.ownerSuggestionsPath === undefined
        ? {}
        : { ownerSuggestionsPath: this.ownerSuggestionsPath }),
      ...(this.ownerSuggestionsError === undefined
        ? {}
        : { ownerSuggestionsError: this.ownerSuggestionsError }),
      findings: this.findings,
      review: this.review,
      scanDir: this.scanDir,
      threadId: this.threadId,
      reportPath: this.reportPath,
      threatModel: this.threatModel,
      threatModelPath: this.threatModelPath,
      artifactsDir: this.artifactsDir,
      sarifPath: this.sarifPath,
      cost: this.cost,
      turn: this.turnResult,
    };
  }
}