web/src/utils/fetch-security-info.ts

59 lines · 1460 bytes

 1import { error } from './logger';
 2import { safeFetch } from './safe-fetch';
 3import { apiBase, enrichHeaders } from './api-config';
 4
 5const normalizeRepo = (github: string): string =>
 6  github.replace(/^https?:\/\/github\.com\//, '').replace(/\/+$/, '');
 7
 8export const fetchSecurityReport = async (
 9  github: string,
10): Promise<SecurityReportResponse | null> => {
11  const repo = normalizeRepo(github);
12  const endpoint = `${apiBase}/v1/enrich/security/${repo}`;
13  try {
14    const res = await safeFetch(endpoint, { headers: enrichHeaders() });
15    if (!res.ok) {
16      error('Security Report', `HTTP ${res.status} for ${repo} (${endpoint})`);
17      return null;
18    }
19    return await res.json();
20  } catch (err) {
21    error('Security Report', `Network error for ${repo}: ${err}`);
22    return null;
23  }
24};
25
26export interface SecurityCheck {
27  name: string;
28  score: number;
29  reason: string;
30  url: string;
31}
32
33export interface SecurityAdvisory {
34  ghsaId: string;
35  cveId: string | null;
36  summary: string;
37  severity: string;
38  cvssScore: number | null;
39  publishedAt: string;
40  url: string;
41  firstPatchedVersion: string | null;
42  vulnerableRange: string;
43  isPatched: boolean;
44}
45
46export interface SecurityReportResponse {
47  repo: string;
48  scorecard: {
49    available: boolean;
50    overallScore: number | null;
51    generatedAt: string;
52    checks: SecurityCheck[];
53  };
54  advisories: {
55    count: number;
56    items: SecurityAdvisory[];
57  };
58  fuzzed: boolean;
59}