Skip to content

Commit efe226f

Browse files
committed
fix(api): improve CVE to GHSA conversion caching and error messaging
1 parent 429d6cc commit efe226f

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/utils/cve-to-ghsa.mts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { CResult } from '../types.mts'
55

66
/**
77
* Converts CVE IDs to GHSA IDs using GitHub API.
8+
* CVE to GHSA mappings are permanent, so we cache for 30 days.
89
*/
910
export async function convertCveToGhsa(
1011
cveId: string,
@@ -13,11 +14,17 @@ export async function convertCveToGhsa(
1314
const cacheKey = `cve-to-ghsa-${cveId}`
1415
const octokit = getOctokit()
1516

16-
const response = await cacheFetch(cacheKey, () =>
17-
octokit.rest.securityAdvisories.listGlobalAdvisories({
18-
cve_id: cveId,
19-
per_page: 1,
20-
}),
17+
// CVE to GHSA mappings don't change, cache for 30 days (in milliseconds).
18+
const THIRTY_DAYS_MS = 2_592_000_000
19+
20+
const response = await cacheFetch(
21+
cacheKey,
22+
() =>
23+
octokit.rest.securityAdvisories.listGlobalAdvisories({
24+
cve_id: cveId,
25+
per_page: 1,
26+
}),
27+
THIRTY_DAYS_MS,
2128
)
2229

2330
if (!response.data.length) {
@@ -32,9 +39,19 @@ export async function convertCveToGhsa(
3239
data: response.data[0]!.ghsa_id,
3340
}
3441
} catch (e) {
42+
const errorCause = getErrorCause(e)
43+
// Detect GitHub API rate limit errors.
44+
const isGitHubRateLimit =
45+
errorCause.includes('rate limit') ||
46+
errorCause.includes('EPIPE') ||
47+
errorCause.includes('ECONNRESET') ||
48+
errorCause.includes('403')
49+
3550
return {
3651
ok: false,
37-
message: `Failed to convert CVE to GHSA: ${getErrorCause(e)}`,
52+
message: isGitHubRateLimit
53+
? 'GitHub API rate limit exceeded while converting CVE to GHSA. Wait an hour or set SOCKET_CLI_GITHUB_TOKEN environment variable with a personal access token for higher limits.'
54+
: `Failed to convert CVE to GHSA: ${errorCause}`,
3855
}
3956
}
4057
}

0 commit comments

Comments
 (0)