-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (33 loc) · 1.3 KB
/
Copy pathindex.js
File metadata and controls
42 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Checks if an npm package version was published via a Trusted Publisher (OIDC/Provenance).
* * @param {string} name - The package name (e.g., 'esbuild' or '@scope/pkg')
* @param {string} version - The version string (defaults to 'latest')
* @returns {Promise<boolean>}
*/
export async function isVerified(name, version = "latest") {
if (typeof name !== "string" || !name.trim() || name.length > 214) {
return false;
}
const encodedName = name.includes("/")
? `@${encodeURIComponent(name.slice(1))}`
: encodeURIComponent(name);
const url = `https://registry.npmjs.org/${encodedName}/${version}`;
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 5000);
const res = await fetch(url, {
signal: controller.signal,
headers: { Accept: "application/json" },
});
clearTimeout(timeout);
if (!res.ok) return false;
const pkg = await res.json();
if (!pkg || typeof pkg !== "object") return false;
const hasTrustedPublisher = !!pkg.trustedPublisher;
const hasAttestations = !!(pkg.dist?.attestations || pkg.dist?.["sigstore.bundle"]);
const hasUserTrust = !!pkg._npmUser?.trustedPublisher;
return hasTrustedPublisher || hasAttestations || hasUserTrust;
} catch {
return false;
}
}