package-registry-manager 0.22.6 → 0.22.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,13 @@
1
+ import {
2
+ compareRepositories,
3
+ identityCommand,
4
+ inspectManifestRepositories,
5
+ provenanceRepositories,
6
+ publisherIdentities,
7
+ publisherMatches,
8
+ resolveRepository,
9
+ } from "./repository-identity.mjs";
10
+
1
11
  /** Identifies the tool to registry APIs, as crates.io asks of clients. */
2
12
  export const USER_AGENT =
3
13
  "package-registry-manager (+https://github.com/link-foundation/package-registry-manager)";
@@ -13,6 +23,15 @@ const ENDPOINTS = {
13
23
  "PACKAGE_REGISTRY_MANAGER_DOCKER_HUB_API",
14
24
  "https://hub.docker.com/v2",
15
25
  ],
26
+ nuget: [
27
+ "PACKAGE_REGISTRY_MANAGER_NUGET_API",
28
+ "https://api.nuget.org/v3-flatcontainer",
29
+ ],
30
+ rubygems: [
31
+ "PACKAGE_REGISTRY_MANAGER_RUBYGEMS_API",
32
+ "https://rubygems.org/api/v1",
33
+ ],
34
+ jsr: ["PACKAGE_REGISTRY_MANAGER_JSR_API", "https://jsr.io"],
16
35
  };
17
36
 
18
37
  /** Returns the registry API base URL, honoring test overrides. */
@@ -44,6 +63,19 @@ export function registryStateUrl(packageInfo, env = process.env) {
44
63
  return `${base}/crates/${encodeURIComponent(name)}`;
45
64
  case "pypi":
46
65
  return `${base}/pypi/${encodeURIComponent(name)}/json`;
66
+ case "nuget":
67
+ return `${base}/${encodeURIComponent(name.toLowerCase())}/index.json`;
68
+ case "rubygems":
69
+ return `${base}/gems/${encodeURIComponent(name)}.json`;
70
+ case "jsr":
71
+ return `${base}/${name
72
+ .split("/")
73
+ .map((part) =>
74
+ part.startsWith("@")
75
+ ? `@${encodeURIComponent(part.slice(1))}`
76
+ : encodeURIComponent(part),
77
+ )
78
+ .join("/")}/meta.json`;
47
79
  default: {
48
80
  const [namespace, repository] = name.split("/");
49
81
  return `${base}/namespaces/${encodeURIComponent(namespace)}/repositories/${encodeURIComponent(repository)}`;
@@ -63,6 +95,8 @@ export function npmName(name) {
63
95
  */
64
96
  export async function probeRegistryState(inspection, options = {}) {
65
97
  const probed = structuredClone(inspection);
98
+ await resolveRepository(probed, options);
99
+ await inspectManifestRepositories(probed);
66
100
  await Promise.all(
67
101
  probed.packages.map(async (packageInfo) => {
68
102
  if (!packageInfo.publishable) {
@@ -75,6 +109,46 @@ export async function probeRegistryState(inspection, options = {}) {
75
109
  if (state.trusted !== undefined) {
76
110
  packageInfo.trusted_publishing = state.trusted;
77
111
  }
112
+ const evidence = { ...packageInfo, ...state };
113
+ if (state.configured_publishers !== undefined) {
114
+ packageInfo.configured_publishers = state.configured_publishers;
115
+ }
116
+ if (state.provenance_repositories?.length) {
117
+ packageInfo.provenance_repositories = state.provenance_repositories;
118
+ }
119
+ if (packageInfo.registry === "npm" && state.exists) {
120
+ const output = await identityCommand(
121
+ "npm",
122
+ ["trust", "list", packageInfo.name, "--browser=false"],
123
+ { ...options, repository: inspection.repository.root },
124
+ );
125
+ if (output !== undefined) {
126
+ evidence.configured_publishers = publisherIdentities(output);
127
+ packageInfo.configured_publishers = evidence.configured_publishers;
128
+ }
129
+ }
130
+ compareRepositories(packageInfo, probed.repository, evidence);
131
+ if (
132
+ state.trusted &&
133
+ ["npm", "crates-io", "pypi"].includes(packageInfo.registry)
134
+ ) {
135
+ const expected = {
136
+ organization: probed.repository.github_owner,
137
+ repository: probed.repository.github_repository,
138
+ workflow: packageInfo.workflow,
139
+ environment: packageInfo.environment,
140
+ };
141
+ packageInfo.publisher_settings_verified = Boolean(
142
+ evidence.configured_publishers?.some((publisher) =>
143
+ publisherMatches(publisher, expected),
144
+ ),
145
+ );
146
+ if (!packageInfo.publisher_settings_verified) {
147
+ (packageInfo.warnings ??= []).push(
148
+ "configured trusted publisher could not be verified; setup must check the repository, workflow and environment in registry settings",
149
+ );
150
+ }
151
+ }
78
152
  }),
79
153
  );
80
154
  return probed;
@@ -100,21 +174,47 @@ export async function probePackage(packageInfo, options = {}) {
100
174
  : { exists: false, trusted: false };
101
175
  }
102
176
  switch (packageInfo.registry) {
103
- case "npm":
177
+ case "npm": {
178
+ const attestations = document.dist?.attestations?.url;
179
+ const provenance = attestations
180
+ ? await getJson(attestations, options)
181
+ : undefined;
182
+ const sources = provenanceRepositories(provenance);
104
183
  return {
105
184
  exists: true,
106
185
  trusted: Boolean(document._npmUser?.trustedPublisher),
107
186
  version: document.version,
187
+ ...(sources.length ? { provenance_repositories: sources } : {}),
108
188
  };
109
- case "crates-io":
189
+ }
190
+ case "crates-io": {
191
+ const latest =
192
+ (document.versions ?? []).find(
193
+ (item) => item.num === document.crate?.max_version,
194
+ ) ?? document.versions?.[0];
195
+ const settings = await getJson(
196
+ `${registryEndpoint("crates-io", env)}/trusted_publishing/github_configs?crate=${encodeURIComponent(packageInfo.name)}`,
197
+ options,
198
+ );
110
199
  return {
111
200
  exists: true,
112
- trusted: (document.versions ?? []).some((item) =>
113
- Boolean(item.trustpub_data),
201
+ trusted: Boolean(latest?.trustpub_data),
202
+ provenance_repositories: publisherIdentities(latest?.trustpub_data).map(
203
+ (publisher) => publisher.repository,
114
204
  ),
205
+ ...(Array.isArray(settings?.github_configs) && !settings.meta?.next_page
206
+ ? { configured_publishers: publisherIdentities(settings) }
207
+ : {}),
115
208
  };
116
- case "pypi":
117
- return { exists: true, trusted: await pypiProvenance(document, options) };
209
+ }
210
+ case "pypi": {
211
+ const provenance = await pypiProvenance(document, options);
212
+ return {
213
+ exists: true,
214
+ trusted: Boolean(provenance),
215
+ provenance_repositories: provenanceRepositories(provenance),
216
+ };
217
+ }
118
218
  default:
119
219
  return { exists: true };
120
220
  }
@@ -128,7 +228,7 @@ async function pypiProvenance(document, options) {
128
228
  }
129
229
  const base = registryEndpoint("pypi", options.env ?? process.env);
130
230
  const url = `${base}/integrity/${encodeURIComponent(document.info.name)}/${encodeURIComponent(version)}/${encodeURIComponent(file)}/provenance`;
131
- return Boolean(await getJson(url, options));
231
+ return getJson(url, options);
132
232
  }
133
233
 
134
234
  /**
@@ -0,0 +1,317 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { execFile } from "node:child_process";
4
+ import { promisify } from "node:util";
5
+ import { ANSI } from "./auth-urls.mjs";
6
+
7
+ const execute = promisify(execFile);
8
+ const SLUG = /^[a-z\d_.-]+\/[a-z\d_.-]+$/i;
9
+
10
+ /** Normalize GitHub HTTPS, SSH, npm shorthand and provenance source URLs. */
11
+ export function githubSlug(value) {
12
+ if (typeof value !== "string") {
13
+ return null;
14
+ }
15
+ let text = value
16
+ .trim()
17
+ .replace(/^git\+/, "")
18
+ .replace(/^github:/, "");
19
+ text = text
20
+ .replace(/^git@github\.com:/, "https://github.com/")
21
+ .replace(/^ssh:\/\/git@github\.com\//, "https://github.com/");
22
+ if (text.startsWith("https://github.com/")) {
23
+ text = text
24
+ .slice(19)
25
+ .split(/[?#]/)[0]
26
+ .split("/")
27
+ .slice(0, 2)
28
+ .join("/")
29
+ .split("@")[0];
30
+ }
31
+ text = text.replace(/\.git\/?$/, "");
32
+ return SLUG.test(text) ? text : null;
33
+ }
34
+
35
+ export function repositorySlug(repository) {
36
+ return repository.github_owner && repository.github_repository
37
+ ? `${repository.github_owner}/${repository.github_repository}`
38
+ : null;
39
+ }
40
+
41
+ /** Read only declared repository metadata, preserving all PyPI source URLs. */
42
+ export function manifestUrls(contents, manifest) {
43
+ contents = contents.replace(/^\uFEFF/, "");
44
+ if (manifest.endsWith("package.json")) {
45
+ let metadata;
46
+ try {
47
+ metadata = JSON.parse(contents).repository;
48
+ } catch {
49
+ return [];
50
+ }
51
+ return [typeof metadata === "string" ? metadata : metadata?.url].filter(
52
+ Boolean,
53
+ );
54
+ }
55
+ let table = "";
56
+ let inlineUrls = null;
57
+ const urls = [];
58
+ for (const line of contents.split(/\r?\n/)) {
59
+ const header = /^\s*\[([^\]]+)\]/.exec(line);
60
+ if (header) {
61
+ table = header[1].trim();
62
+ }
63
+ if (
64
+ ["project", "tool.poetry"].includes(table) &&
65
+ /^\s*urls\s*=\s*\{/.test(line)
66
+ ) {
67
+ inlineUrls = line.slice(line.indexOf("{") + 1);
68
+ } else if (inlineUrls !== null) {
69
+ inlineUrls += `\n${line}`;
70
+ }
71
+ if (inlineUrls?.includes("}")) {
72
+ for (const field of inlineUrls
73
+ .slice(0, inlineUrls.indexOf("}"))
74
+ .matchAll(/(?:[\w-]+|"[^"]+"|'[^']+')\s*=\s*(["'])(.*?)\1/g)) {
75
+ urls.push(field[2]);
76
+ }
77
+ inlineUrls = null;
78
+ }
79
+ const field =
80
+ /^\s*([\w-]+|"[^"]+"|'[^']+')\s*=\s*(["'])(.*?)\2\s*(?:#.*)?$/.exec(line);
81
+ if (!field) {
82
+ continue;
83
+ }
84
+ const key = field[1].replaceAll(/["']/g, "").toLowerCase();
85
+ if (
86
+ (table === "package" || table === "tool.poetry") &&
87
+ key === "repository"
88
+ ) {
89
+ urls.push(field[3]);
90
+ }
91
+ if (["project.urls", "tool.poetry.urls"].includes(table)) {
92
+ urls.push(field[3]);
93
+ }
94
+ }
95
+ return urls;
96
+ }
97
+
98
+ /** Findings name the evidence source; provenance is never treated as configuration. */
99
+ export function compareRepositories(packageInfo, repository, evidence = {}) {
100
+ const slug = repositorySlug(repository);
101
+ if (!slug) {
102
+ return packageInfo;
103
+ }
104
+ const pairs = [
105
+ ...(evidence.manifest_urls ?? []).map((url) => [
106
+ "manifest",
107
+ githubSlug(url),
108
+ ]),
109
+ ...(evidence.provenance_repositories ?? []).map((url) => [
110
+ "provenance",
111
+ githubSlug(url),
112
+ ]),
113
+ ...(evidence.configured_publishers ?? []).map((publisher) => [
114
+ "trusted publisher",
115
+ publisher.repository,
116
+ ]),
117
+ ];
118
+ const findings = pairs
119
+ .filter(([, old]) => old && old.toLowerCase() !== slug.toLowerCase())
120
+ .map(([source, old]) => ({ source, repository: old }));
121
+ packageInfo.repository_mismatches = [
122
+ ...new Map(
123
+ findings.map((finding) => [
124
+ `${finding.source}:${finding.repository.toLowerCase()}`,
125
+ finding,
126
+ ]),
127
+ ).values(),
128
+ ];
129
+ if (!packageInfo.repository_mismatches.length) {
130
+ delete packageInfo.repository_mismatches;
131
+ }
132
+ const warnings = (packageInfo.warnings ?? []).filter(
133
+ (warning) => !warning.startsWith("repository mismatch:"),
134
+ );
135
+ for (const finding of packageInfo.repository_mismatches ?? []) {
136
+ warnings.push(
137
+ `repository mismatch: ${finding.source} still names ${finding.repository}; repository being set up is ${slug}`,
138
+ );
139
+ }
140
+ if (warnings.length) {
141
+ packageInfo.warnings = warnings;
142
+ } else {
143
+ delete packageInfo.warnings;
144
+ }
145
+ return packageInfo;
146
+ }
147
+
148
+ /** Inspect local metadata even offline; retain URLs only when a fix is needed. */
149
+ export async function inspectManifestRepositories(inspection) {
150
+ for (const packageInfo of inspection.packages) {
151
+ if (!["npm", "crates-io", "pypi"].includes(packageInfo.registry)) {
152
+ continue;
153
+ }
154
+ const contents = await readFile(
155
+ path.join(inspection.repository.root, packageInfo.manifest),
156
+ "utf8",
157
+ ).catch(() => null);
158
+ if (contents === null) {
159
+ continue;
160
+ }
161
+ const urls = manifestUrls(contents, packageInfo.manifest);
162
+ compareRepositories(packageInfo, inspection.repository, {
163
+ ...packageInfo,
164
+ manifest_urls: urls,
165
+ });
166
+ if (
167
+ packageInfo.repository_mismatches?.some(
168
+ (finding) => finding.source === "manifest",
169
+ )
170
+ ) {
171
+ packageInfo.manifest_urls = urls;
172
+ }
173
+ }
174
+ return inspection;
175
+ }
176
+
177
+ /** Run read-only identity queries without opening a browser or exposing auth output. */
178
+ export async function identityCommand(program, args, options = {}) {
179
+ if (options.runIdentity) {
180
+ return options.runIdentity(program, args);
181
+ }
182
+ try {
183
+ const { stdout } = await execute(program, args, {
184
+ cwd: options.repository,
185
+ timeout: 10_000,
186
+ maxBuffer: 2 * 1024 * 1024,
187
+ env: options.env ?? process.env,
188
+ });
189
+ return stdout;
190
+ } catch {
191
+ if (options.verbose) {
192
+ console.error(`${program} repository identity lookup unavailable`);
193
+ }
194
+ return undefined;
195
+ }
196
+ }
197
+
198
+ /** Resolve redirects through GitHub's canonical full_name before comparison. */
199
+ export async function resolveRepository(inspection, options = {}) {
200
+ const slug = repositorySlug(inspection.repository);
201
+ if (!slug) {
202
+ return;
203
+ }
204
+ const output = await identityCommand(
205
+ "gh",
206
+ ["api", `repos/${slug}`, "--jq", ".full_name"],
207
+ options,
208
+ );
209
+ const canonical = githubSlug(output);
210
+ if (canonical) {
211
+ [
212
+ inspection.repository.github_owner,
213
+ inspection.repository.github_repository,
214
+ ] = canonical.split("/");
215
+ }
216
+ }
217
+
218
+ /** Convert npm JSON/human output and crates.io/PyPI settings to public identities. */
219
+ export function publisherIdentities(document) {
220
+ if (typeof document === "string") {
221
+ try {
222
+ return publisherIdentities(JSON.parse(document));
223
+ } catch {
224
+ document = document.replaceAll(ANSI, "");
225
+ const blocks = document
226
+ .split(/(?=^\s*type:)/m)
227
+ .filter((block) => /^\s*type:/m.test(block));
228
+ if (blocks.length > 1) {
229
+ return blocks.flatMap(publisherIdentities);
230
+ }
231
+ const values = {};
232
+ for (const line of document.replaceAll(ANSI, "").split(/\r?\n/)) {
233
+ const field =
234
+ /^\s*(id|type|repository|file|environment):\s*(\S.*)$/.exec(line);
235
+ if (field) {
236
+ values[field[1]] = field[2].trim();
237
+ }
238
+ }
239
+ return publisherIdentities(values);
240
+ }
241
+ }
242
+ if (Array.isArray(document)) {
243
+ return document.flatMap(publisherIdentities);
244
+ }
245
+ if (!document || typeof document !== "object") {
246
+ return [];
247
+ }
248
+ if (document.type && document.type !== "github") {
249
+ return [];
250
+ }
251
+ for (const key of ["github_configs", "publishers", "trustedPublishers"]) {
252
+ if (Array.isArray(document[key])) {
253
+ return publisherIdentities(document[key]);
254
+ }
255
+ }
256
+ const repository = githubSlug(
257
+ document.claims?.repository ??
258
+ document.repository ??
259
+ (document.repository_owner && document.repository_name
260
+ ? `${document.repository_owner}/${document.repository_name}`
261
+ : null),
262
+ );
263
+ if (!repository) {
264
+ return [];
265
+ }
266
+ return [
267
+ {
268
+ ...(document.id !== undefined ? { id: String(document.id) } : {}),
269
+ repository,
270
+ workflow:
271
+ document.claims?.workflow_ref?.file ??
272
+ document.workflow_filename ??
273
+ document.workflow ??
274
+ document.file ??
275
+ null,
276
+ environment: document.claims?.environment ?? document.environment ?? null,
277
+ },
278
+ ];
279
+ }
280
+
281
+ export function publisherMatches(publisher, expected) {
282
+ return (
283
+ publisher.repository.toLowerCase() ===
284
+ `${expected.organization}/${expected.repository}`.toLowerCase() &&
285
+ publisher.workflow === expected.workflow &&
286
+ (publisher.environment ?? null) === (expected.environment ?? null)
287
+ );
288
+ }
289
+
290
+ /** Extract source identity from npm SLSA envelopes and PyPI publisher bundles. */
291
+ export function provenanceRepositories(document) {
292
+ const urls = [];
293
+ for (const bundle of document?.attestation_bundles ?? []) {
294
+ if (bundle.publisher?.kind === "GitHub") {
295
+ urls.push(bundle.publisher.repository);
296
+ }
297
+ }
298
+ for (const attestation of document?.attestations ?? []) {
299
+ const payload = attestation.bundle?.dsseEnvelope?.payload;
300
+ if (typeof payload !== "string" || payload.length > 2 * 1024 * 1024) {
301
+ continue;
302
+ }
303
+ try {
304
+ const statement = JSON.parse(
305
+ Buffer.from(payload, "base64").toString("utf8"),
306
+ );
307
+ const predicate = statement.predicate;
308
+ urls.push(
309
+ predicate?.invocation?.configSource?.uri,
310
+ predicate?.buildDefinition?.externalParameters?.workflow?.repository,
311
+ );
312
+ } catch {
313
+ /* Malformed provenance leaves source identity unknown. */
314
+ }
315
+ }
316
+ return [...new Set(urls.map(githubSlug).filter(Boolean))];
317
+ }