package-registry-manager 0.22.5 → 0.22.7
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.
- package/README.md +1 -0
- package/package.json +4 -4
- package/src/bootstrap-reference.mjs +76 -0
- package/src/cli.mjs +12 -3
- package/src/containers.mjs +8 -1
- package/src/credential-browser.mjs +82 -0
- package/src/credential-cycle.mjs +91 -0
- package/src/credential-setup.mjs +210 -0
- package/src/credential-verification.mjs +96 -0
- package/src/discovery.mjs +99 -8
- package/src/extra-flows.mjs +88 -0
- package/src/flows.mjs +29 -19
- package/src/manifest-proposal.mjs +201 -0
- package/src/model.mjs +15 -0
- package/src/npm-policy.mjs +69 -0
- package/src/package-coverage.mjs +106 -0
- package/src/plan.mjs +45 -31
- package/src/publishers.mjs +113 -27
- package/src/registry-state.mjs +85 -7
- package/src/repository-identity.mjs +317 -0
- package/src/repository-repair.mjs +375 -0
- package/src/setup-batch.mjs +1 -0
- package/src/setup.mjs +74 -7
- package/src/sign-in-import.mjs +8 -0
- package/src/skips.mjs +3 -0
- package/src/source-code.mjs +61 -0
- package/src/tokens.mjs +8 -0
- package/src/version-guard.mjs +34 -0
- package/src/workflow-proposal.mjs +32 -3
- package/src/workflows.mjs +49 -10
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
executableLines,
|
|
5
|
+
parseWorkflow,
|
|
6
|
+
scriptReferences,
|
|
7
|
+
} from "./publishers.mjs";
|
|
8
|
+
|
|
9
|
+
/** Keep executable steps for this manifest, preserving the job's OIDC identity. */
|
|
10
|
+
export function packageWorkflows(workflows, packageInfo) {
|
|
11
|
+
const directory = path.posix.dirname(packageInfo.manifest);
|
|
12
|
+
const working = (lines) =>
|
|
13
|
+
lines
|
|
14
|
+
.map(
|
|
15
|
+
(line) => /^\s*working-directory\s*:\s*['"]?([\w./-]+)/.exec(line)?.[1],
|
|
16
|
+
)
|
|
17
|
+
.filter(Boolean)
|
|
18
|
+
.at(-1);
|
|
19
|
+
return workflows.map((workflow) => {
|
|
20
|
+
const parsed = parseWorkflow(workflow.contents);
|
|
21
|
+
const jobs = parsed.jobs.flatMap((job) => {
|
|
22
|
+
const start = job.lines.findIndex((line) => /^\s*steps\s*:/.test(line));
|
|
23
|
+
if (start < 0) {
|
|
24
|
+
const called = job.lines.some((line) =>
|
|
25
|
+
/^\s*uses\s*:\s*["']?\.\/\.github\/workflows\//.test(line),
|
|
26
|
+
);
|
|
27
|
+
const cwd = working(job.lines) ?? working(parsed.header) ?? ".";
|
|
28
|
+
return called &&
|
|
29
|
+
cwd.replace(/^\.\//, "").replace(/\/$/, "") === directory
|
|
30
|
+
? [` ${job.name}:\n${job.lines.join("\n")}`]
|
|
31
|
+
: [];
|
|
32
|
+
}
|
|
33
|
+
const prefix = job.lines.slice(0, start + 1);
|
|
34
|
+
const blocks = [];
|
|
35
|
+
for (const line of job.lines.slice(start + 1)) {
|
|
36
|
+
if (/^\s*-\s+[\w-]+\s*:/.test(line)) {
|
|
37
|
+
blocks.push([]);
|
|
38
|
+
}
|
|
39
|
+
if (blocks.length) {
|
|
40
|
+
blocks.at(-1).push(line);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const fallback = working(prefix) ?? working(parsed.header) ?? ".";
|
|
44
|
+
const matching = blocks.filter((block) => {
|
|
45
|
+
const commands = executableLines(block);
|
|
46
|
+
if (!commands.length) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const text = commands.join("\n");
|
|
50
|
+
if (/--workspaces|--recursive|\bpublish\s+--all\b/.test(text)) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (
|
|
54
|
+
text.includes(`--workspace=${packageInfo.name}`) ||
|
|
55
|
+
text.includes(`--workspace ${packageInfo.name}`)
|
|
56
|
+
) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
const cd = /\bcd\s+['"]?([\w./-]+)/.exec(text)?.[1];
|
|
60
|
+
const scripts = scriptReferences(commands);
|
|
61
|
+
const inferred = scripts.find(
|
|
62
|
+
(script) => directory !== "." && script.startsWith(`${directory}/`),
|
|
63
|
+
)
|
|
64
|
+
? directory
|
|
65
|
+
: undefined;
|
|
66
|
+
const cwd =
|
|
67
|
+
(working(block) ?? cd ?? inferred ?? fallback)
|
|
68
|
+
.replace(/^\.\//, "")
|
|
69
|
+
.replace(/\/$/, "") || ".";
|
|
70
|
+
return cwd === directory;
|
|
71
|
+
});
|
|
72
|
+
return matching.length
|
|
73
|
+
? [
|
|
74
|
+
` ${job.name}:\n${prefix.join("\n")}\n${matching.flat().join("\n")}`,
|
|
75
|
+
]
|
|
76
|
+
: [];
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
...workflow,
|
|
80
|
+
contents: `${parsed.header.join("\n")}\njobs:\n${jobs.join("\n")}`,
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Warn when a second npm manifest re-exports a sibling but stops tracking it. */
|
|
86
|
+
export async function auditWrappers(root, packages) {
|
|
87
|
+
const npm = packages.filter((item) => item.registry === "npm");
|
|
88
|
+
for (const wrapper of npm) {
|
|
89
|
+
const data = JSON.parse(
|
|
90
|
+
(await readFile(path.join(root, wrapper.manifest), "utf8")).replace(
|
|
91
|
+
/^\uFEFF/,
|
|
92
|
+
"",
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
for (const target of npm.filter(
|
|
96
|
+
(target) => target !== wrapper && data.dependencies?.[target.name],
|
|
97
|
+
)) {
|
|
98
|
+
if (wrapper.version !== target.version) {
|
|
99
|
+
wrapper.warnings = [
|
|
100
|
+
...(wrapper.warnings ?? []),
|
|
101
|
+
`wrapper ${wrapper.name}@${wrapper.version} differs from ${target.name}@${target.version}; update both manifests and the wrapper dependency for every release`,
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/plan.mjs
CHANGED
|
@@ -6,10 +6,14 @@ import {
|
|
|
6
6
|
npmFlow,
|
|
7
7
|
pypiFlow,
|
|
8
8
|
} from "./flows.mjs";
|
|
9
|
+
import { credentialPolicy } from "./credential-cycle.mjs";
|
|
10
|
+
import { trustedFlow, tokenFlow } from "./extra-flows.mjs";
|
|
11
|
+
import { bootstrapRef } from "./bootstrap-reference.mjs";
|
|
9
12
|
import { pagesSteps } from "./pages.mjs";
|
|
10
13
|
import { planPrerequisites } from "./prerequisites.mjs";
|
|
11
14
|
import { TRUSTED_REGISTRIES } from "./publishers.mjs";
|
|
12
15
|
import { applyPython } from "./python.mjs";
|
|
16
|
+
import { repairSteps } from "./repository-repair.mjs";
|
|
13
17
|
|
|
14
18
|
const FLOWS = new Map([
|
|
15
19
|
["npm", npmFlow],
|
|
@@ -17,6 +21,9 @@ const FLOWS = new Map([
|
|
|
17
21
|
["pypi", pypiFlow],
|
|
18
22
|
["docker-hub", dockerHubFlow],
|
|
19
23
|
["ghcr", ghcrFlow],
|
|
24
|
+
["rubygems", trustedFlow],
|
|
25
|
+
["nuget", trustedFlow],
|
|
26
|
+
["jsr", trustedFlow],
|
|
20
27
|
]);
|
|
21
28
|
|
|
22
29
|
/**
|
|
@@ -65,6 +72,9 @@ function buildPlan(inspection, packageInfo, options) {
|
|
|
65
72
|
case "pypi":
|
|
66
73
|
case "docker-hub":
|
|
67
74
|
case "ghcr":
|
|
75
|
+
case "rubygems":
|
|
76
|
+
case "nuget":
|
|
77
|
+
case "jsr":
|
|
68
78
|
return flowPlan(inspection, packageInfo, options);
|
|
69
79
|
case "go-modules":
|
|
70
80
|
steps = [
|
|
@@ -84,37 +94,17 @@ function buildPlan(inspection, packageInfo, options) {
|
|
|
84
94
|
},
|
|
85
95
|
];
|
|
86
96
|
break;
|
|
87
|
-
case "nuget":
|
|
88
|
-
steps = [
|
|
89
|
-
check(
|
|
90
|
-
"pack-package",
|
|
91
|
-
"Build the NuGet package",
|
|
92
|
-
"Create the package locally without pushing it.",
|
|
93
|
-
command("dotnet", ["pack", "--configuration", "Release"]),
|
|
94
|
-
),
|
|
95
|
-
browser(
|
|
96
|
-
"configure-trusted-publishing",
|
|
97
|
-
"Configure NuGet trusted publishing",
|
|
98
|
-
"Sign in and add a GitHub Actions federated credential for this package.",
|
|
99
|
-
"https://www.nuget.org/account/TrustedPublishing",
|
|
100
|
-
),
|
|
101
|
-
];
|
|
102
|
-
break;
|
|
103
97
|
case "maven-central":
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"Sign in to the Central Portal and verify the namespace used by the package coordinates.",
|
|
115
|
-
"https://central.sonatype.com/publishing/namespaces",
|
|
116
|
-
),
|
|
117
|
-
];
|
|
98
|
+
case "vscode-marketplace":
|
|
99
|
+
case "open-vsx":
|
|
100
|
+
case "chrome-web-store":
|
|
101
|
+
if (!packageInfo.workflow && !options.workflow) {
|
|
102
|
+
return {
|
|
103
|
+
...basePlan(inspection, packageInfo, []),
|
|
104
|
+
skipped_reason: `no workflow publishes ${packageInfo.name} to ${packageInfo.registry}; pass --workflow <file> for an explicit setup target`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
steps = tokenFlow(packageInfo);
|
|
118
108
|
break;
|
|
119
109
|
case "packagist":
|
|
120
110
|
steps = [
|
|
@@ -143,6 +133,7 @@ function basePlan(inspection, packageInfo, steps) {
|
|
|
143
133
|
return {
|
|
144
134
|
schema_version: 1,
|
|
145
135
|
registry: packageInfo.registry,
|
|
136
|
+
credential_policy: credentialPolicy(packageInfo.registry),
|
|
146
137
|
package: structuredClone(packageInfo),
|
|
147
138
|
repository: structuredClone(inspection.repository),
|
|
148
139
|
steps,
|
|
@@ -203,6 +194,22 @@ function flowPlan(inspection, packageInfo, options) {
|
|
|
203
194
|
steps = [];
|
|
204
195
|
} else if (mode === "attach") {
|
|
205
196
|
steps = steps.filter((item) => !BOOTSTRAP_CONDITIONS.has(item.when));
|
|
197
|
+
} else if (mode === "repair") {
|
|
198
|
+
steps = repairSteps(
|
|
199
|
+
packageInfo,
|
|
200
|
+
context,
|
|
201
|
+
packageInfo.exists_on_registry !== true
|
|
202
|
+
? steps
|
|
203
|
+
: steps.filter((item) => !BOOTSTRAP_CONDITIONS.has(item.when)),
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
if (options.ref) {
|
|
207
|
+
const ref = bootstrapRef(options.ref);
|
|
208
|
+
const fetch = steps.find((step) => step.id === "fetch-default-branch");
|
|
209
|
+
if (fetch) {
|
|
210
|
+
fetch.command.args = ["fetch", "origin", ref];
|
|
211
|
+
fetch.title = `Fetch bootstrap ref ${ref}`;
|
|
212
|
+
}
|
|
206
213
|
}
|
|
207
214
|
// Pages readiness belongs to the repository, so it is checked in every mode.
|
|
208
215
|
const pages = inspection.repository.pages_workflow;
|
|
@@ -241,10 +248,17 @@ function flowPlan(inspection, packageInfo, options) {
|
|
|
241
248
|
|
|
242
249
|
/**
|
|
243
250
|
* Chooses `bootstrap` for a package missing from its registry, `attach` for
|
|
244
|
-
* one without trusted publishing,
|
|
251
|
+
* one without trusted publishing, `repair` for stale or unverified repository
|
|
252
|
+
* identities, and `complete` when nothing is left to do.
|
|
245
253
|
* Unknown registry state leaves the mode unset.
|
|
246
254
|
*/
|
|
247
255
|
export function planMode(packageInfo) {
|
|
256
|
+
if (
|
|
257
|
+
packageInfo.repository_mismatches?.length ||
|
|
258
|
+
packageInfo.publisher_settings_verified === false
|
|
259
|
+
) {
|
|
260
|
+
return "repair";
|
|
261
|
+
}
|
|
248
262
|
if (packageInfo.exists_on_registry === false) {
|
|
249
263
|
return "bootstrap";
|
|
250
264
|
}
|
package/src/publishers.mjs
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import { readFile, realpath, stat } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { stripComments, commandPosition } from "./source-code.mjs";
|
|
3
4
|
|
|
4
5
|
/** Registries whose trusted publisher is bound to a workflow file. */
|
|
5
|
-
export const TRUSTED_REGISTRIES = Object.freeze([
|
|
6
|
+
export const TRUSTED_REGISTRIES = Object.freeze([
|
|
7
|
+
"npm",
|
|
8
|
+
"crates-io",
|
|
9
|
+
"pypi",
|
|
10
|
+
"rubygems",
|
|
11
|
+
"nuget",
|
|
12
|
+
"jsr",
|
|
13
|
+
]);
|
|
6
14
|
|
|
7
|
-
const JOB_PATTERNS = new Map([
|
|
15
|
+
export const JOB_PATTERNS = new Map([
|
|
16
|
+
["rubygems", /\bgem\s+push\b|rubygems\/release-gem/],
|
|
17
|
+
["nuget", /\bdotnet\s+nuget\s+push\b|\bnuget\s+push\b/],
|
|
18
|
+
["jsr", /\bdeno\s+publish\b|\bjsr\s+publish\b/],
|
|
19
|
+
["docker-hub", /docker\/login-action|\bdocker\s+(?:login|push)\b/],
|
|
20
|
+
["maven-central", /\bmvn\s+(?:-\S+\s+)*deploy\b|\bgradle(?:w)?\s+publish\b/],
|
|
21
|
+
["vscode-marketplace", /\bvsce\s+publish\b/],
|
|
22
|
+
["open-vsx", /\bovsx\s+publish\b/],
|
|
23
|
+
[
|
|
24
|
+
"chrome-web-store",
|
|
25
|
+
/chrome-webstore-upload|chrome-web-store|chromewebstore/,
|
|
26
|
+
],
|
|
8
27
|
[
|
|
9
28
|
"npm",
|
|
10
29
|
/\b(?:npm|pnpm)\s+(?:-r\s+|--recursive\s+)?(?:stage\s+)?publish\b|\byarn\s+npm\s+publish\b|\bchangeset\s+publish\b|changesets\/action\/publish@|JS-DevTools\/npm-publish/,
|
|
@@ -24,6 +43,12 @@ const JOB_PATTERNS = new Map([
|
|
|
24
43
|
// `.arg("publish")` within the next few lines.
|
|
25
44
|
const SCRIPT_WINDOW = 3;
|
|
26
45
|
const SCRIPT_PATTERNS = new Map([
|
|
46
|
+
["rubygems", [/\bgem\b/, /["'`]push["'`]/]],
|
|
47
|
+
["nuget", [/\b(?:dotnet|nuget)\b/, /["'`]push["'`]/]],
|
|
48
|
+
["jsr", [/\b(?:deno|jsr)\b/, /["'`]publish["'`]/]],
|
|
49
|
+
["maven-central", [/\b(?:mvn|gradle)\b/, /["'`]deploy["'`]/]],
|
|
50
|
+
["vscode-marketplace", [/\bvsce\b/, /["'`]publish["'`]/]],
|
|
51
|
+
["open-vsx", [/\bovsx\b/, /["'`]publish["'`]/]],
|
|
27
52
|
["npm", [/\b(?:npm|pnpm|yarn)\b/, /["'`]publish["'`]/]],
|
|
28
53
|
["crates-io", [/\bcargo\b/, /["'`]publish["'`]/]],
|
|
29
54
|
[
|
|
@@ -33,6 +58,27 @@ const SCRIPT_PATTERNS = new Map([
|
|
|
33
58
|
]);
|
|
34
59
|
|
|
35
60
|
const TOKEN_SECRETS = new Map([
|
|
61
|
+
["rubygems", ["GEM_HOST_API_KEY", "RUBYGEMS_API_KEY"]],
|
|
62
|
+
["nuget", ["NUGET_API_KEY", "NUGET_TOKEN"]],
|
|
63
|
+
["jsr", ["JSR_TOKEN"]],
|
|
64
|
+
["docker-hub", ["DOCKERHUB_TOKEN", "DOCKER_HUB_TOKEN", "DOCKER_PASSWORD"]],
|
|
65
|
+
[
|
|
66
|
+
"maven-central",
|
|
67
|
+
[
|
|
68
|
+
"MAVEN_CENTRAL_TOKEN",
|
|
69
|
+
"MAVEN_CENTRAL_PASSWORD",
|
|
70
|
+
"OSSRH_TOKEN",
|
|
71
|
+
"OSSRH_PASSWORD",
|
|
72
|
+
"SONATYPE_TOKEN",
|
|
73
|
+
"CENTRAL_TOKEN",
|
|
74
|
+
],
|
|
75
|
+
],
|
|
76
|
+
["vscode-marketplace", ["VSCE_PAT", "VSCE_TOKEN"]],
|
|
77
|
+
["open-vsx", ["OVSX_PAT", "OVSX_TOKEN"]],
|
|
78
|
+
[
|
|
79
|
+
"chrome-web-store",
|
|
80
|
+
["CHROME_WEB_STORE_REFRESH_TOKEN", "CHROME_REFRESH_TOKEN"],
|
|
81
|
+
],
|
|
36
82
|
["npm", ["NPM_TOKEN", "NPM_AUTH_TOKEN"]],
|
|
37
83
|
[
|
|
38
84
|
"crates-io",
|
|
@@ -61,9 +107,6 @@ const RUN_SCRIPT =
|
|
|
61
107
|
const CHANGESETS_PUBLISH = /^\s*publish(?:-script)?\s*:\s*(.+?)\s*$/;
|
|
62
108
|
const LOCAL_WORKFLOW =
|
|
63
109
|
/^\s*uses\s*:\s*["']?\.\/\.github\/workflows\/([\w.-]+\.ya?ml)/;
|
|
64
|
-
const SCRIPT_COMMENT = /^\s*(?:#|\/\/|\/?\*)/;
|
|
65
|
-
const COMMAND_PREFIX =
|
|
66
|
-
/^\s*(?:(?:npx|bunx|exec|sudo|env|time|command|pnpm\s+exec|yarn|\w+=\S*)\s+)*$/;
|
|
67
110
|
const MAX_SCRIPT_BYTES = 1024 * 1024;
|
|
68
111
|
|
|
69
112
|
/**
|
|
@@ -71,7 +114,7 @@ const MAX_SCRIPT_BYTES = 1024 * 1024;
|
|
|
71
114
|
* parser is bundled. Full-line comments are dropped.
|
|
72
115
|
*/
|
|
73
116
|
export function parseWorkflow(contents) {
|
|
74
|
-
const lines = contents.split(/\r?\n/)
|
|
117
|
+
const lines = stripComments(contents).split(/\r?\n/);
|
|
75
118
|
const start = lines.findIndex((line) => /^jobs\s*:\s*$/.test(line));
|
|
76
119
|
if (start === -1) {
|
|
77
120
|
return { header: lines, jobs: [] };
|
|
@@ -145,7 +188,9 @@ export async function detectPublisher(root, workflows, registry) {
|
|
|
145
188
|
});
|
|
146
189
|
}
|
|
147
190
|
}
|
|
148
|
-
const trusted = found.filter(
|
|
191
|
+
const trusted = found.filter(
|
|
192
|
+
(item) => !TRUSTED_REGISTRIES.includes(registry) || item.idToken,
|
|
193
|
+
);
|
|
149
194
|
const files = [...new Set(trusted.map((item) => item.workflow))];
|
|
150
195
|
if (files.length > 1) {
|
|
151
196
|
return {
|
|
@@ -215,7 +260,7 @@ export function tokenSecrets(workflows, registry) {
|
|
|
215
260
|
const names = TOKEN_SECRETS.get(registry) ?? [];
|
|
216
261
|
const found = [];
|
|
217
262
|
for (const workflow of workflows) {
|
|
218
|
-
const lines = workflow.contents
|
|
263
|
+
const lines = stripComments(workflow.contents)
|
|
219
264
|
.split(/\r?\n/)
|
|
220
265
|
.filter((line) => !/^\s*#/.test(line));
|
|
221
266
|
for (const name of names) {
|
|
@@ -231,7 +276,9 @@ export function tokenSecrets(workflows, registry) {
|
|
|
231
276
|
|
|
232
277
|
/** Explains how to replace a long-lived token secret with trusted publishing. */
|
|
233
278
|
export function tokenSecretWarning(registry, { workflow, secret }) {
|
|
234
|
-
return
|
|
279
|
+
return TRUSTED_REGISTRIES.includes(registry)
|
|
280
|
+
? `${workflow} reads secrets.${secret}; publish with ${TRUSTED_PUBLISHING_HINTS.get(registry) ?? `${registry} trusted publishing (id-token: write)`} and delete the long-lived token after verification`
|
|
281
|
+
: `${workflow} reads secrets.${secret}; ensure a scoped expiring publishing token through gh-manager`;
|
|
235
282
|
}
|
|
236
283
|
|
|
237
284
|
/** Names of the long-lived token secrets that trusted publishing replaces. */
|
|
@@ -250,14 +297,15 @@ async function anyJobPublishes(root, parsed, registry) {
|
|
|
250
297
|
|
|
251
298
|
async function jobPublishes(root, parsed, job, registry) {
|
|
252
299
|
const pattern = JOB_PATTERNS.get(registry);
|
|
253
|
-
|
|
300
|
+
const executable = executableLines(job.lines);
|
|
301
|
+
if (publishingLine(executable, pattern)) {
|
|
254
302
|
return true;
|
|
255
303
|
}
|
|
256
304
|
const directories = workingDirectories([...parsed.header, ...job.lines]);
|
|
257
305
|
const scripts = [];
|
|
258
|
-
for (const script of scriptReferences(
|
|
306
|
+
for (const script of scriptReferences(executable)) {
|
|
259
307
|
const contents = await readInside(root, directories, script);
|
|
260
|
-
const lines = contents === null ? [] : codeLines(contents);
|
|
308
|
+
const lines = contents === null ? [] : codeLines(contents, script);
|
|
261
309
|
if (scriptPublishes(lines, registry)) {
|
|
262
310
|
return true;
|
|
263
311
|
}
|
|
@@ -274,8 +322,11 @@ async function jobPublishes(root, parsed, job, registry) {
|
|
|
274
322
|
}
|
|
275
323
|
// A package script may run from the job, its changesets command, or a
|
|
276
324
|
// script, as in `$\`npm run changeset:publish\``.
|
|
277
|
-
for (const line of [...
|
|
325
|
+
for (const line of [...executable, ...commands, ...scripts]) {
|
|
278
326
|
for (const match of line.matchAll(RUN_SCRIPT)) {
|
|
327
|
+
if (!commandPosition(line.slice(0, match.index))) {
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
279
330
|
const manifest = await readInside(root, directories, "package.json");
|
|
280
331
|
const command =
|
|
281
332
|
manifest === null
|
|
@@ -312,17 +363,52 @@ function onlyCalled(header) {
|
|
|
312
363
|
}
|
|
313
364
|
|
|
314
365
|
function publishingLine(lines, pattern) {
|
|
366
|
+
if (!pattern) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
const matcher = new RegExp(pattern.source, "g");
|
|
315
370
|
return lines.some(
|
|
316
|
-
(line) =>
|
|
371
|
+
(line) =>
|
|
372
|
+
!line.includes("--dry-run") &&
|
|
373
|
+
[...line.matchAll(matcher)].some(
|
|
374
|
+
(match) =>
|
|
375
|
+
commandPosition(line.slice(0, match.index)) ||
|
|
376
|
+
/^\s*-?\s*uses\s*:\s*['"]?[\w./-]*$/.test(line.slice(0, match.index)),
|
|
377
|
+
),
|
|
317
378
|
);
|
|
318
379
|
}
|
|
319
380
|
|
|
320
|
-
|
|
321
|
-
|
|
381
|
+
/** Extract executable run blocks and action references; names and env values are inert. */
|
|
382
|
+
export function executableLines(lines) {
|
|
383
|
+
const result = [];
|
|
384
|
+
let blockIndent = null;
|
|
385
|
+
for (const line of lines) {
|
|
386
|
+
const indent = indentOf(line);
|
|
387
|
+
if (blockIndent !== null && (line.trim() === "" || indent > blockIndent)) {
|
|
388
|
+
result.push(line);
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
blockIndent = null;
|
|
392
|
+
if (/^\s*-?\s*uses\s*:/.test(line)) {
|
|
393
|
+
result.push(line);
|
|
394
|
+
}
|
|
395
|
+
const match = /^\s*-?\s*run\s*:\s*(.*)$/.exec(line);
|
|
396
|
+
if (!match) {
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (/^[|>][-+\d]*$/.test(match[1])) {
|
|
400
|
+
blockIndent = indent + (line.trimStart().startsWith("-") ? 2 : 0);
|
|
401
|
+
} else {
|
|
402
|
+
result.push(unquote(match[1]));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return result;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function codeLines(contents, script) {
|
|
409
|
+
return stripComments(contents, script)
|
|
322
410
|
.split(/\r?\n/)
|
|
323
|
-
.filter(
|
|
324
|
-
(line) => !line.includes("--dry-run") && !SCRIPT_COMMENT.test(line),
|
|
325
|
-
);
|
|
411
|
+
.filter((line) => !line.includes("--dry-run"));
|
|
326
412
|
}
|
|
327
413
|
|
|
328
414
|
function scriptPublishes(lines, registry) {
|
|
@@ -336,23 +422,23 @@ function scriptPublishes(lines, registry) {
|
|
|
336
422
|
) {
|
|
337
423
|
return true;
|
|
338
424
|
}
|
|
339
|
-
const
|
|
425
|
+
const patterns = SCRIPT_PATTERNS.get(registry);
|
|
426
|
+
if (!patterns) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
const [program, subcommand] = patterns;
|
|
340
430
|
return lines.some(
|
|
341
431
|
(line, index) =>
|
|
342
432
|
program.test(line) &&
|
|
433
|
+
/(?:exec|spawn|run|Command::new|subprocess|system|(?:npm|pnpm|yarn)\s*\(\s*\[)/.test(
|
|
434
|
+
line,
|
|
435
|
+
) &&
|
|
343
436
|
lines
|
|
344
437
|
.slice(index, index + SCRIPT_WINDOW)
|
|
345
438
|
.some((nearby) => subcommand.test(nearby)),
|
|
346
439
|
);
|
|
347
440
|
}
|
|
348
441
|
|
|
349
|
-
// A script mentions `cargo publish` in messages and comments too; it runs it
|
|
350
|
-
// only where the command starts a line, a shell command or a string literal.
|
|
351
|
-
function commandPosition(prefix) {
|
|
352
|
-
const segment = prefix.split(/["'`;&|(]/).at(-1);
|
|
353
|
-
return COMMAND_PREFIX.test(segment);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
442
|
function packageScript(contents, name) {
|
|
357
443
|
try {
|
|
358
444
|
const command = JSON.parse(contents).scripts?.[name];
|
package/src/registry-state.mjs
CHANGED
|
@@ -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)";
|
|
@@ -63,6 +73,8 @@ export function npmName(name) {
|
|
|
63
73
|
*/
|
|
64
74
|
export async function probeRegistryState(inspection, options = {}) {
|
|
65
75
|
const probed = structuredClone(inspection);
|
|
76
|
+
await resolveRepository(probed, options);
|
|
77
|
+
await inspectManifestRepositories(probed);
|
|
66
78
|
await Promise.all(
|
|
67
79
|
probed.packages.map(async (packageInfo) => {
|
|
68
80
|
if (!packageInfo.publishable) {
|
|
@@ -75,6 +87,46 @@ export async function probeRegistryState(inspection, options = {}) {
|
|
|
75
87
|
if (state.trusted !== undefined) {
|
|
76
88
|
packageInfo.trusted_publishing = state.trusted;
|
|
77
89
|
}
|
|
90
|
+
const evidence = { ...packageInfo, ...state };
|
|
91
|
+
if (state.configured_publishers !== undefined) {
|
|
92
|
+
packageInfo.configured_publishers = state.configured_publishers;
|
|
93
|
+
}
|
|
94
|
+
if (state.provenance_repositories?.length) {
|
|
95
|
+
packageInfo.provenance_repositories = state.provenance_repositories;
|
|
96
|
+
}
|
|
97
|
+
if (packageInfo.registry === "npm" && state.exists) {
|
|
98
|
+
const output = await identityCommand(
|
|
99
|
+
"npm",
|
|
100
|
+
["trust", "list", packageInfo.name, "--browser=false"],
|
|
101
|
+
{ ...options, repository: inspection.repository.root },
|
|
102
|
+
);
|
|
103
|
+
if (output !== undefined) {
|
|
104
|
+
evidence.configured_publishers = publisherIdentities(output);
|
|
105
|
+
packageInfo.configured_publishers = evidence.configured_publishers;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
compareRepositories(packageInfo, probed.repository, evidence);
|
|
109
|
+
if (
|
|
110
|
+
state.trusted &&
|
|
111
|
+
["npm", "crates-io", "pypi"].includes(packageInfo.registry)
|
|
112
|
+
) {
|
|
113
|
+
const expected = {
|
|
114
|
+
organization: probed.repository.github_owner,
|
|
115
|
+
repository: probed.repository.github_repository,
|
|
116
|
+
workflow: packageInfo.workflow,
|
|
117
|
+
environment: packageInfo.environment,
|
|
118
|
+
};
|
|
119
|
+
packageInfo.publisher_settings_verified = Boolean(
|
|
120
|
+
evidence.configured_publishers?.some((publisher) =>
|
|
121
|
+
publisherMatches(publisher, expected),
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
if (!packageInfo.publisher_settings_verified) {
|
|
125
|
+
(packageInfo.warnings ??= []).push(
|
|
126
|
+
"configured trusted publisher could not be verified; setup must check the repository, workflow and environment in registry settings",
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
78
130
|
}),
|
|
79
131
|
);
|
|
80
132
|
return probed;
|
|
@@ -100,21 +152,47 @@ export async function probePackage(packageInfo, options = {}) {
|
|
|
100
152
|
: { exists: false, trusted: false };
|
|
101
153
|
}
|
|
102
154
|
switch (packageInfo.registry) {
|
|
103
|
-
case "npm":
|
|
155
|
+
case "npm": {
|
|
156
|
+
const attestations = document.dist?.attestations?.url;
|
|
157
|
+
const provenance = attestations
|
|
158
|
+
? await getJson(attestations, options)
|
|
159
|
+
: undefined;
|
|
160
|
+
const sources = provenanceRepositories(provenance);
|
|
104
161
|
return {
|
|
105
162
|
exists: true,
|
|
106
163
|
trusted: Boolean(document._npmUser?.trustedPublisher),
|
|
107
164
|
version: document.version,
|
|
165
|
+
...(sources.length ? { provenance_repositories: sources } : {}),
|
|
108
166
|
};
|
|
109
|
-
|
|
167
|
+
}
|
|
168
|
+
case "crates-io": {
|
|
169
|
+
const latest =
|
|
170
|
+
(document.versions ?? []).find(
|
|
171
|
+
(item) => item.num === document.crate?.max_version,
|
|
172
|
+
) ?? document.versions?.[0];
|
|
173
|
+
const settings = await getJson(
|
|
174
|
+
`${registryEndpoint("crates-io", env)}/trusted_publishing/github_configs?crate=${encodeURIComponent(packageInfo.name)}`,
|
|
175
|
+
options,
|
|
176
|
+
);
|
|
110
177
|
return {
|
|
111
178
|
exists: true,
|
|
112
|
-
trusted: (
|
|
113
|
-
|
|
179
|
+
trusted: Boolean(latest?.trustpub_data),
|
|
180
|
+
provenance_repositories: publisherIdentities(latest?.trustpub_data).map(
|
|
181
|
+
(publisher) => publisher.repository,
|
|
114
182
|
),
|
|
183
|
+
...(Array.isArray(settings?.github_configs) && !settings.meta?.next_page
|
|
184
|
+
? { configured_publishers: publisherIdentities(settings) }
|
|
185
|
+
: {}),
|
|
115
186
|
};
|
|
116
|
-
|
|
117
|
-
|
|
187
|
+
}
|
|
188
|
+
case "pypi": {
|
|
189
|
+
const provenance = await pypiProvenance(document, options);
|
|
190
|
+
return {
|
|
191
|
+
exists: true,
|
|
192
|
+
trusted: Boolean(provenance),
|
|
193
|
+
provenance_repositories: provenanceRepositories(provenance),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
118
196
|
default:
|
|
119
197
|
return { exists: true };
|
|
120
198
|
}
|
|
@@ -128,7 +206,7 @@ async function pypiProvenance(document, options) {
|
|
|
128
206
|
}
|
|
129
207
|
const base = registryEndpoint("pypi", options.env ?? process.env);
|
|
130
208
|
const url = `${base}/integrity/${encodeURIComponent(document.info.name)}/${encodeURIComponent(version)}/${encodeURIComponent(file)}/provenance`;
|
|
131
|
-
return
|
|
209
|
+
return getJson(url, options);
|
|
132
210
|
}
|
|
133
211
|
|
|
134
212
|
/**
|