@mago-foundation/mago-ski 0.4.1 → 0.5.0-beta.1
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 +5 -3
- package/dist/hosts/github/check.d.ts +21 -0
- package/dist/hosts/github/check.js +124 -0
- package/dist/hosts/github/publish.d.ts +7 -0
- package/dist/hosts/github/publish.js +52 -0
- package/dist/hosts/github/run.d.ts +3 -0
- package/dist/hosts/github/run.js +2 -2
- package/dist/src/admission-evidence.d.ts +15 -0
- package/dist/src/admission-evidence.js +48 -0
- package/dist/src/admission.d.ts +102 -0
- package/dist/src/admission.js +316 -0
- package/dist/src/cli.js +199 -2
- package/dist/src/diff.d.ts +4 -0
- package/dist/src/diff.js +1 -1
- package/dist/src/experimental/sigstore.d.ts +24 -0
- package/dist/src/experimental/sigstore.js +22 -0
- package/dist/src/fs-safe.d.ts +15 -2
- package/dist/src/fs-safe.js +68 -50
- package/dist/src/gate/adapters/claude-code.js +8 -6
- package/dist/src/gate/adapters/codex.js +2 -6
- package/dist/src/gate/instance.js +7 -8
- package/dist/src/gate/setup.js +36 -46
- package/dist/src/profile.d.ts +34 -0
- package/dist/src/profile.js +182 -0
- package/dist/src/published-by.d.ts +64 -0
- package/dist/src/published-by.js +227 -0
- package/dist/src/usf-check.d.ts +28 -0
- package/dist/src/usf-check.js +128 -0
- package/dist/src/usf.d.ts +48 -0
- package/dist/src/usf.js +238 -0
- package/docs/hosts/publish.md +65 -0
- package/docs/hosts/skill-check.md +91 -0
- package/docs/spec/admission-v0.md +84 -0
- package/docs/spec/observed-profile-v1.md +51 -0
- package/package.json +4 -1
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// Admission rules (mago.admission/v0): an organisation's short, readable rule file that decides which evidence is
|
|
2
|
+
// enough to admit a Skill. A decision is a pure function of (facts, rules, time): no model, no network, no
|
|
3
|
+
// probability. Facts are computed beforehand from bytes and verified documents (admission-evidence.ts), so the same
|
|
4
|
+
// inputs give the same decision anywhere, and a past decision can be replayed against a new rule file.
|
|
5
|
+
// This is the admission language only. It never describes what an agent may do at runtime.
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import { parseDocument } from 'yaml';
|
|
8
|
+
import { hostAllowed } from "./usf.js";
|
|
9
|
+
export const ADMISSION_VERSION = 'mago.admission/v0';
|
|
10
|
+
export const ACTIONS = ['ALLOW', 'LOG_ONLY', 'FLAG', 'REQUIRE_REVIEW', 'BLOCK'];
|
|
11
|
+
/** Stricter actions win: deny wins over allow. */
|
|
12
|
+
const RANK = { ALLOW: 0, LOG_ONLY: 1, FLAG: 2, REQUIRE_REVIEW: 3, BLOCK: 4 };
|
|
13
|
+
const ADMITTED = new Set(['ALLOW', 'LOG_ONLY', 'FLAG']);
|
|
14
|
+
const MAX_RULES = 200;
|
|
15
|
+
const MAX_RULES_BYTES = 256 * 1024;
|
|
16
|
+
export class AdmissionRulesError extends Error {
|
|
17
|
+
}
|
|
18
|
+
const isMap = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
19
|
+
function keys(value, label, allowed, required = []) {
|
|
20
|
+
if (!isMap(value))
|
|
21
|
+
throw new AdmissionRulesError(`${label} must be a block of keys`);
|
|
22
|
+
for (const key of Object.keys(value))
|
|
23
|
+
if (!allowed.includes(key))
|
|
24
|
+
throw new AdmissionRulesError(`${label}: unknown key "${key}" (allowed: ${allowed.join(', ')})`);
|
|
25
|
+
for (const key of required)
|
|
26
|
+
if (!Object.hasOwn(value, key))
|
|
27
|
+
throw new AdmissionRulesError(`${label}: "${key}" is required`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function action(value, label) {
|
|
31
|
+
if (typeof value !== 'string' || !ACTIONS.includes(value))
|
|
32
|
+
throw new AdmissionRulesError(`${label} must be one of ${ACTIONS.join(', ')}`);
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
const text = (value, label, pattern, hint) => {
|
|
36
|
+
if (typeof value !== 'string' || !pattern.test(value))
|
|
37
|
+
throw new AdmissionRulesError(`${label} must be ${hint}`);
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
const yes = (value, label) => {
|
|
41
|
+
if (value !== true)
|
|
42
|
+
throw new AdmissionRulesError(`${label} can only be true`);
|
|
43
|
+
return true;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Parses and lints a rule file. Every mistake the closed language can detect is an error here, not a surprise at
|
|
47
|
+
* decision time: unknown keys, duplicate ids, a loosening rule built on self-declared or heuristic facts, a strict
|
|
48
|
+
* action without a reason, an ALLOW that matches every Skill.
|
|
49
|
+
*/
|
|
50
|
+
export function parseRules(source) {
|
|
51
|
+
if (Buffer.byteLength(source) > MAX_RULES_BYTES)
|
|
52
|
+
throw new AdmissionRulesError(`the rule file is larger than ${MAX_RULES_BYTES} bytes`);
|
|
53
|
+
const doc = parseDocument(source, { version: '1.2', uniqueKeys: true, prettyErrors: false });
|
|
54
|
+
if (doc.errors[0])
|
|
55
|
+
throw new AdmissionRulesError(`the rule file is not valid YAML: ${doc.errors[0].message.split('\n')[0]}`);
|
|
56
|
+
let raw;
|
|
57
|
+
try {
|
|
58
|
+
raw = doc.toJS({ maxAliasCount: 0 });
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
throw new AdmissionRulesError(`the rule file uses aliases, which are not accepted: ${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
const top = keys(raw, 'rule file', ['admission_version', 'default', 'rules'], ['admission_version', 'default', 'rules']);
|
|
64
|
+
if (top.admission_version !== ADMISSION_VERSION)
|
|
65
|
+
throw new AdmissionRulesError(`admission_version must be ${ADMISSION_VERSION}`);
|
|
66
|
+
const fallback = action(top.default, 'default');
|
|
67
|
+
if (!Array.isArray(top.rules) || top.rules.length > MAX_RULES)
|
|
68
|
+
throw new AdmissionRulesError(`rules must be a list of at most ${MAX_RULES} rules`);
|
|
69
|
+
const ids = new Set();
|
|
70
|
+
const rules = top.rules.map((item, index) => {
|
|
71
|
+
const r = keys(item, `rule ${index + 1}`, ['id', 'when', 'action', 'reason'], ['id', 'when', 'action']);
|
|
72
|
+
const id = text(r.id, `rule ${index + 1} id`, /^[a-z0-9][a-z0-9-]{0,62}$/u, 'lower-case letters, digits and dashes');
|
|
73
|
+
if (ids.has(id))
|
|
74
|
+
throw new AdmissionRulesError(`rule id "${id}" is used twice`);
|
|
75
|
+
ids.add(id);
|
|
76
|
+
const label = `rule "${id}"`;
|
|
77
|
+
const w = keys(r.when, `${label} when`, ['publisher', 'reviewed', 'capabilities', 'update']);
|
|
78
|
+
const when = {};
|
|
79
|
+
if (w.publisher !== undefined) {
|
|
80
|
+
const p = keys(w.publisher, `${label} publisher`, ['repository_id', 'owner_id', 'workflow', 'branch', 'trigger']);
|
|
81
|
+
if (Object.keys(p).length === 0)
|
|
82
|
+
throw new AdmissionRulesError(`${label} publisher must name a repository_id, owner_id or workflow`);
|
|
83
|
+
if (p.workflow !== undefined && p.repository_id === undefined)
|
|
84
|
+
throw new AdmissionRulesError(`${label}: a workflow path only means something with a repository_id`);
|
|
85
|
+
when.publisher = {};
|
|
86
|
+
if (p.repository_id !== undefined)
|
|
87
|
+
when.publisher.repository_id = text(p.repository_id, `${label} repository_id`, /^[0-9]{1,20}$/u, 'a numeric repository id, quoted');
|
|
88
|
+
if (p.owner_id !== undefined)
|
|
89
|
+
when.publisher.owner_id = text(p.owner_id, `${label} owner_id`, /^[0-9]{1,20}$/u, 'a numeric owner id, quoted');
|
|
90
|
+
if (p.workflow !== undefined)
|
|
91
|
+
when.publisher.workflow = text(p.workflow, `${label} workflow`, /^\.github\/workflows\/[A-Za-z0-9_.\-/]+\.ya?ml$/u, 'a path such as .github/workflows/publish.yml');
|
|
92
|
+
// Branches only: tags move, so no rule can refer to one.
|
|
93
|
+
if (p.branch !== undefined)
|
|
94
|
+
when.publisher.branch = text(p.branch, `${label} branch`, /^[A-Za-z0-9._\-/]{1,200}$/u, 'a branch name such as main');
|
|
95
|
+
if (p.trigger !== undefined) {
|
|
96
|
+
if (!Array.isArray(p.trigger) || p.trigger.length === 0 || !p.trigger.every((t) => typeof t === 'string' && /^[a-z_]{1,40}$/u.test(t)))
|
|
97
|
+
throw new AdmissionRulesError(`${label} trigger must be a list of GitHub event names such as [release, push]`);
|
|
98
|
+
when.publisher.trigger = p.trigger;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (w.reviewed !== undefined) {
|
|
102
|
+
if (typeof w.reviewed !== 'boolean')
|
|
103
|
+
throw new AdmissionRulesError(`${label} reviewed must be true or false`);
|
|
104
|
+
when.reviewed = w.reviewed;
|
|
105
|
+
}
|
|
106
|
+
if (w.capabilities !== undefined) {
|
|
107
|
+
const c = keys(w.capabilities, `${label} capabilities`, ['shell', 'websites', 'any_website', 'undeclared', 'no_declaration', 'incomplete']);
|
|
108
|
+
if (Object.keys(c).length === 0)
|
|
109
|
+
throw new AdmissionRulesError(`${label} capabilities must name at least one condition`);
|
|
110
|
+
when.capabilities = {};
|
|
111
|
+
for (const flag of ['shell', 'any_website', 'undeclared', 'no_declaration', 'incomplete'])
|
|
112
|
+
if (c[flag] !== undefined)
|
|
113
|
+
when.capabilities[flag] = yes(c[flag], `${label} capabilities.${flag}`);
|
|
114
|
+
if (c.websites !== undefined) {
|
|
115
|
+
if (!Array.isArray(c.websites) || c.websites.length === 0 || !c.websites.every((h) => typeof h === 'string' && /^(?:\*\.)?[a-z0-9.-]+$/u.test(h)))
|
|
116
|
+
throw new AdmissionRulesError(`${label} capabilities.websites must be a list of host names`);
|
|
117
|
+
when.capabilities.websites = c.websites;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (w.update !== undefined) {
|
|
121
|
+
const u = keys(w.update, `${label} update`, ['same_publisher', 'max_days_since_review'], ['same_publisher']);
|
|
122
|
+
when.update = { same_publisher: yes(u.same_publisher, `${label} update.same_publisher`) };
|
|
123
|
+
if (u.max_days_since_review !== undefined) {
|
|
124
|
+
if (!Number.isInteger(u.max_days_since_review) || u.max_days_since_review < 0 || u.max_days_since_review > 3650)
|
|
125
|
+
throw new AdmissionRulesError(`${label} update.max_days_since_review must be a whole number of days up to 3650`);
|
|
126
|
+
when.update.max_days_since_review = u.max_days_since_review;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const act = action(r.action, `${label} action`);
|
|
130
|
+
const rule = { id, when, action: act };
|
|
131
|
+
if (r.reason !== undefined)
|
|
132
|
+
rule.reason = text(r.reason, `${label} reason`, /^[^\u0000-\u001f\u007f]{1,300}$/u, 'one line of up to 300 characters');
|
|
133
|
+
// The linter: what a closed language can check completely.
|
|
134
|
+
if ((act === 'BLOCK' || act === 'REQUIRE_REVIEW') && rule.reason === undefined)
|
|
135
|
+
throw new AdmissionRulesError(`${label}: a ${act} rule needs a reason, shown to the person it stops`);
|
|
136
|
+
if (when.capabilities && act === 'ALLOW')
|
|
137
|
+
throw new AdmissionRulesError(`${label}: a rule with capability conditions can only make a decision stricter, so ALLOW would have no effect`);
|
|
138
|
+
// ALLOW, LOG_ONLY and FLAG all admit: a basis rule with one of them must rest on a publisher, a review or an update.
|
|
139
|
+
if (!when.capabilities && (act === 'ALLOW' || act === 'LOG_ONLY' || act === 'FLAG')) {
|
|
140
|
+
if (when.reviewed === false)
|
|
141
|
+
throw new AdmissionRulesError(`${label}: "reviewed: false" cannot be a reason to admit (${act})`);
|
|
142
|
+
if (!when.publisher && when.reviewed !== true && !when.update)
|
|
143
|
+
throw new AdmissionRulesError(`${label}: a ${act} rule must require a publisher, a review or a non-escalating update; this one would admit every Skill`);
|
|
144
|
+
}
|
|
145
|
+
return rule;
|
|
146
|
+
});
|
|
147
|
+
return { admission_version: ADMISSION_VERSION, default: fallback, rules, digest: `sha256:${createHash('sha256').update(source).digest('hex')}` };
|
|
148
|
+
}
|
|
149
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
150
|
+
/** Everything the Skill may reach: hosts it names or declares, and whether it runs code or declares a shell. */
|
|
151
|
+
function reach(facts) {
|
|
152
|
+
return {
|
|
153
|
+
hosts: new Set([...facts.profile.web_hosts, ...(facts.declaration?.network.allow ?? [])]),
|
|
154
|
+
shell: facts.profile.scripts.length > 0 || facts.declaration?.shell === true,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/** Why this version reaches further than its base, or null when it does not. Compares declared and observed together. */
|
|
158
|
+
export function escalation(base, skill) {
|
|
159
|
+
// A declaration that cannot be read one way only may say anything to some reader.
|
|
160
|
+
// Even when the base's could not be read either: then nobody can say what the new one grants.
|
|
161
|
+
if (skill.declaration_error !== null && skill.declaration === null)
|
|
162
|
+
return 'its declaration cannot be read reliably';
|
|
163
|
+
if (base.declaration !== null && skill.declaration === null)
|
|
164
|
+
return 'it no longer has a declaration';
|
|
165
|
+
const before = reach(base);
|
|
166
|
+
const after = reach(skill);
|
|
167
|
+
if (after.shell && !before.shell)
|
|
168
|
+
return 'it runs code or asks for a shell, and the reviewed version did not';
|
|
169
|
+
const newHosts = [...after.hosts].filter((host) => host !== '*' && !before.hosts.has(host) && !hostAllowed([...before.hosts], host));
|
|
170
|
+
if (after.hosts.has('*') && !before.hosts.has('*'))
|
|
171
|
+
return 'it declares any website (network.allow: "*")';
|
|
172
|
+
if (newHosts.length > 0)
|
|
173
|
+
return `it reaches new websites: ${newHosts.slice(0, 10).join(', ')}`;
|
|
174
|
+
if (!skill.profile.complete)
|
|
175
|
+
return 'its profile is incomplete';
|
|
176
|
+
const added = (before = [], after = []) => after.filter((value) => !before.includes(value));
|
|
177
|
+
const d0 = base.declaration;
|
|
178
|
+
const d1 = skill.declaration;
|
|
179
|
+
const checks = [
|
|
180
|
+
['asks to read new files', added(d0?.files.read, d1?.files.read)],
|
|
181
|
+
['asks to write new files', added(d0?.files.write, d1?.files.write)],
|
|
182
|
+
['no longer protects files from writes', added(d1?.files.deny_write, d0?.files.deny_write)],
|
|
183
|
+
['asks for new tools', added(d0?.tools, d1?.tools)],
|
|
184
|
+
['needs new programs', added(d0?.requires.binaries, d1?.requires.binaries)],
|
|
185
|
+
['names new programs in shell code', added(base.profile.programs, skill.profile.programs)],
|
|
186
|
+
['adds declaration keys this version of mago-ski does not know', added(d0?.unknownKeys, d1?.unknownKeys)],
|
|
187
|
+
['no longer denies other websites (network.deny)', d0?.network.deny === '*' && d1?.network.deny !== '*' ? ['network.deny'] : []],
|
|
188
|
+
];
|
|
189
|
+
for (const [what, values] of checks)
|
|
190
|
+
if (values.length > 0)
|
|
191
|
+
return `it ${what}: ${values.slice(0, 10).join(', ')}`;
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
function samePublisher(a, b) {
|
|
195
|
+
return a !== null && b !== null && a.repository_id === b.repository_id && a.workflow === b.workflow;
|
|
196
|
+
}
|
|
197
|
+
function matches(rule, facts, now) {
|
|
198
|
+
const { skill } = facts;
|
|
199
|
+
const { publisher, reviewed, capabilities, update } = rule.when;
|
|
200
|
+
if (publisher) {
|
|
201
|
+
const p = skill.published_by;
|
|
202
|
+
if (!p)
|
|
203
|
+
return false;
|
|
204
|
+
if (publisher.repository_id !== undefined && p.repository_id !== publisher.repository_id)
|
|
205
|
+
return false;
|
|
206
|
+
if (publisher.owner_id !== undefined && p.owner_id !== publisher.owner_id)
|
|
207
|
+
return false;
|
|
208
|
+
if (publisher.workflow !== undefined && p.workflow !== publisher.workflow)
|
|
209
|
+
return false;
|
|
210
|
+
if (publisher.branch !== undefined && p.ref !== `refs/heads/${publisher.branch}`)
|
|
211
|
+
return false;
|
|
212
|
+
if (publisher.trigger !== undefined && !publisher.trigger.includes(p.trigger))
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
if (reviewed !== undefined && (skill.reviewed_by !== null) !== reviewed)
|
|
216
|
+
return false;
|
|
217
|
+
if (capabilities) {
|
|
218
|
+
const r = reach(skill);
|
|
219
|
+
if (capabilities.shell && !r.shell)
|
|
220
|
+
return false;
|
|
221
|
+
if (capabilities.any_website && r.hosts.size === 0)
|
|
222
|
+
return false;
|
|
223
|
+
if (capabilities.websites && ![...r.hosts].some((host) => hostAllowed(capabilities.websites, host) || host === '*'))
|
|
224
|
+
return false;
|
|
225
|
+
if (capabilities.no_declaration && skill.declaration !== null)
|
|
226
|
+
return false;
|
|
227
|
+
if (capabilities.incomplete && skill.profile.complete)
|
|
228
|
+
return false;
|
|
229
|
+
if (capabilities.undeclared) {
|
|
230
|
+
const d = skill.declaration;
|
|
231
|
+
const undeclared = d !== null && (skill.profile.web_hosts.some((host) => !hostAllowed(d.network.allow, host)) || (skill.profile.scripts.length > 0 && d.shell !== true));
|
|
232
|
+
if (!undeclared)
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (update) {
|
|
237
|
+
const base = facts.base;
|
|
238
|
+
if (!base || base.reviewed_by === null || base.digest === skill.digest || base.name !== skill.name)
|
|
239
|
+
return false;
|
|
240
|
+
if (!samePublisher(base.published_by, skill.published_by))
|
|
241
|
+
return false;
|
|
242
|
+
if (escalation(base, skill) !== null)
|
|
243
|
+
return false;
|
|
244
|
+
if (update.max_days_since_review !== undefined && now.getTime() - Date.parse(base.reviewed_by.issued_at) > update.max_days_since_review * DAY_MS)
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
/** Capability rules only tighten: they come from heuristics and self-declarations, so they can never be why a Skill is admitted. */
|
|
250
|
+
const isTightener = (rule) => rule.when.capabilities !== undefined;
|
|
251
|
+
export function evaluate(rules, facts, now) {
|
|
252
|
+
const matched = rules.rules.filter((rule) => matches(rule, facts, now));
|
|
253
|
+
// Strictest wins; among equals, the first rule in the file names the decision.
|
|
254
|
+
const strictest = (list) => list.reduce((best, rule) => (best === null || RANK[rule.action] > RANK[best.action] ? rule : best), null);
|
|
255
|
+
// The base decision comes from publisher, review and update rules (or the default); tighteners can only raise it.
|
|
256
|
+
const basis = strictest(matched.filter((rule) => !isTightener(rule)));
|
|
257
|
+
const tightener = strictest(matched.filter(isTightener));
|
|
258
|
+
const baseAction = basis?.action ?? rules.default;
|
|
259
|
+
const deciding = tightener && RANK[tightener.action] > RANK[baseAction] ? tightener : basis;
|
|
260
|
+
const act = deciding?.action ?? rules.default;
|
|
261
|
+
let missing = null;
|
|
262
|
+
if (act === 'REQUIRE_REVIEW' || (deciding === null && !ADMITTED.has(act))) {
|
|
263
|
+
if (facts.base && facts.skill.reviewed_by === null) {
|
|
264
|
+
const why = facts.base.name !== facts.skill.name ? `the supplied base is another Skill (${facts.base.name})`
|
|
265
|
+
: facts.base.reviewed_by === null ? 'the supplied base version has no valid review'
|
|
266
|
+
: !samePublisher(facts.base.published_by, facts.skill.published_by) ? 'it was not published by the same repository and workflow as the reviewed version'
|
|
267
|
+
: escalation(facts.base, facts.skill);
|
|
268
|
+
missing = `a review of this exact version (reviewed-by)${why ? `; it is not a plain update because ${why}` : ''}`;
|
|
269
|
+
}
|
|
270
|
+
else if (facts.skill.reviewed_by === null)
|
|
271
|
+
missing = 'a review of this exact version (reviewed-by)';
|
|
272
|
+
}
|
|
273
|
+
return { action: act, admitted: ADMITTED.has(act), rule_id: deciding?.id ?? 'default', matched: matched.map((rule) => rule.id), reason: deciding?.reason ?? null, missing };
|
|
274
|
+
}
|
|
275
|
+
const isList = (value, item) => Array.isArray(value) && value.every(item);
|
|
276
|
+
const isText = (value) => typeof value === 'string';
|
|
277
|
+
const nullOr = (value, check) => value === null || check(value);
|
|
278
|
+
const isDate = (value) => typeof value === 'string' && /^\d{4}-\d\d-\d\dT/u.test(value) && !Number.isNaN(Date.parse(value));
|
|
279
|
+
const texts = (value) => isList(value, isText);
|
|
280
|
+
function validDeclaration(d) {
|
|
281
|
+
return isMap(d) && isMap(d.network) && texts(d.network.allow) && nullOr(d.network.deny, isText)
|
|
282
|
+
&& isMap(d.files) && texts(d.files.read) && texts(d.files.write) && texts(d.files.deny_write)
|
|
283
|
+
&& nullOr(d.shell, (v) => typeof v === 'boolean') && texts(d.tools) && texts(d.unknownKeys)
|
|
284
|
+
&& isMap(d.requires) && texts(d.requires.binaries) && nullOr(d.requires.min_runtime_version, isText);
|
|
285
|
+
}
|
|
286
|
+
/** Checks a logged skill record field by field: a missing field must never read as present (undefined !== null). */
|
|
287
|
+
function validSkillFacts(value) {
|
|
288
|
+
if (!isMap(value))
|
|
289
|
+
return false;
|
|
290
|
+
const p = value.profile;
|
|
291
|
+
return isText(value.name) && isText(value.digest) && isMap(p) && isList(p.web_hosts, isText) && isList(p.programs, isText)
|
|
292
|
+
&& isList(p.scripts, (s) => isMap(s) && isText(s.path)) && typeof p.complete === 'boolean'
|
|
293
|
+
&& nullOr(value.declaration, validDeclaration) && nullOr(value.declaration_error, isText)
|
|
294
|
+
&& nullOr(value.reviewed_by, (r) => isMap(r) && isDate(r.issued_at) && isDate(r.expires_at) && isText(r.certificate_id) && isText(r.approver_key_id))
|
|
295
|
+
&& nullOr(value.published_by, (b) => isMap(b) && isText(b.repository_id) && isText(b.workflow));
|
|
296
|
+
}
|
|
297
|
+
export function validRecord(value) {
|
|
298
|
+
return isMap(value) && isText(value.decided_at) && isText(value.rules_digest) && isMap(value.facts)
|
|
299
|
+
&& validSkillFacts(value.facts.skill) && nullOr(value.facts.base, validSkillFacts);
|
|
300
|
+
}
|
|
301
|
+
/** Every past decision whose action would change under `next`, each re-evaluated at its own time. */
|
|
302
|
+
export function replay(previous, next, records) {
|
|
303
|
+
const flips = [];
|
|
304
|
+
for (const [index, record] of records.entries()) {
|
|
305
|
+
if (!validRecord(record))
|
|
306
|
+
throw new Error(`log record ${index + 1} is not a complete admission record`);
|
|
307
|
+
const at = new Date(record.decided_at);
|
|
308
|
+
if (Number.isNaN(at.getTime()))
|
|
309
|
+
throw new Error(`log record ${index + 1} has an invalid time`);
|
|
310
|
+
const before = evaluate(previous, record.facts, at);
|
|
311
|
+
const after = evaluate(next, record.facts, at);
|
|
312
|
+
if (before.action !== after.action)
|
|
313
|
+
flips.push({ decided_at: record.decided_at, skill: record.facts.skill.name, digest: record.facts.skill.digest, before, after });
|
|
314
|
+
}
|
|
315
|
+
return flips;
|
|
316
|
+
}
|
package/dist/src/cli.js
CHANGED
|
@@ -5,6 +5,11 @@ import { fileURLToPath } from 'node:url';
|
|
|
5
5
|
import { addApprover, approveSkill, initRoot, removeApprover, renewTrustRoot, updateRevocations } from "./admin.js";
|
|
6
6
|
import { parseCertificate } from "./certificate.js";
|
|
7
7
|
import { diffSkillTrees } from "./diff.js";
|
|
8
|
+
import { checkSkill } from "./usf-check.js";
|
|
9
|
+
import { verifyPublishedBy } from "./published-by.js";
|
|
10
|
+
import { AdmissionRulesError, evaluate, parseRules, replay } from "./admission.js";
|
|
11
|
+
import { collectSkillFacts } from "./admission-evidence.js";
|
|
12
|
+
import { appendFile } from 'node:fs/promises';
|
|
8
13
|
import { readRegularFile } from "./fs-safe.js";
|
|
9
14
|
import { generateKeyPair, validateKeyId } from "./keys.js";
|
|
10
15
|
import { appendDecision, decisionRecord, defaultPolicyPath, effectiveMode, findSkillDirs, forbiddenApproverKeys, loadPolicy, loadPolicyTrust, tooDeepResult, trustFailureEvent, trustFailureRecord, verifyWithPolicy, } from "./policy.js";
|
|
@@ -46,6 +51,14 @@ CHECKING SKILLS
|
|
|
46
51
|
verify SKILL_DIR Check one Skill, print its verdict
|
|
47
52
|
verify-all Check every Skill in the policy
|
|
48
53
|
digest SKILL_DIR Print a Skill's tree digest
|
|
54
|
+
check SKILL_DIR Show what a Skill's files contain and check its USF declaration
|
|
55
|
+
attestation verify SKILL_DIR
|
|
56
|
+
Check a publisher's published-by attestation for a Skill's exact bytes
|
|
57
|
+
|
|
58
|
+
ADMISSION RULES (organisation; preview: hosts do not enforce them yet)
|
|
59
|
+
admit SKILL_DIR Decide whether a Skill is admitted under a rule file
|
|
60
|
+
admission check RULES Check a rule file
|
|
61
|
+
admission diff OLD NEW Show which logged decisions a new rule file would change
|
|
49
62
|
diff Compare two versions of a Skill
|
|
50
63
|
inspect CERTIFICATE Print a certificate's contents
|
|
51
64
|
policy check Check a policy the way a host does
|
|
@@ -324,6 +337,67 @@ Exits 1 if any Skill is not VERIFIED, in shadow mode too (shadow mode affects ho
|
|
|
324
337
|
/* ****************************************************************************************** */
|
|
325
338
|
/** Per-command: digest */
|
|
326
339
|
/* ****************************************************************************************** */
|
|
340
|
+
'attestation verify': `Usage: mago-ski attestation verify SKILL_DIR --bundle FILE --trusted-root FILE --cosign FILE --repo OWNER/NAME
|
|
341
|
+
[--workflow .github/workflows/FILE.yml] [--repo-id ID]
|
|
342
|
+
|
|
343
|
+
Checks a published-by attestation (made by the mago-ski publish Action through actions/attest) for the
|
|
344
|
+
Skill's exact current bytes, offline:
|
|
345
|
+
|
|
346
|
+
- the Sigstore signature, certificate chain and transparency-log proof, against --trusted-root
|
|
347
|
+
(make one with: cosign trusted-root create --with-default-services --out trusted_root.json);
|
|
348
|
+
- that the signer is a GitHub Actions workflow of --repo (and --workflow, when given);
|
|
349
|
+
- that the statement names this Skill's tree digest.
|
|
350
|
+
|
|
351
|
+
It then recomputes the observed profile and the USF declaration from the bytes and reports any difference
|
|
352
|
+
from what the publisher recorded. It prints who published these bytes, from which repository (with its
|
|
353
|
+
numeric id), workflow and commit. It does not say that the Skill is safe.
|
|
354
|
+
|
|
355
|
+
--bundle FILE the Sigstore bundle (the publish Action's bundle-path, or from the GitHub attestations API)
|
|
356
|
+
--trusted-root FILE Sigstore trusted root JSON; nothing is fetched during the check
|
|
357
|
+
--cosign FILE Cosign v3.1.3; its SHA-256 must match the official release checksum
|
|
358
|
+
--repo OWNER/NAME the repository that must have signed (as GitHub spells it)
|
|
359
|
+
--workflow PATH also require this workflow file
|
|
360
|
+
--repo-id ID also require this numeric repository id (survives renames; a fork or a new
|
|
361
|
+
repository with the same name has a different id)
|
|
362
|
+
|
|
363
|
+
Exit 0 when the attestation verifies and the record matches the bytes, 1 when it does not, 2 for usage errors.`,
|
|
364
|
+
admit: `Usage: mago-ski admit SKILL_DIR --rules FILE [--policy FILE] [--published-by BUNDLE]
|
|
365
|
+
[--base DIR [--base-published-by BUNDLE]] [--trusted-root FILE --cosign FILE] [--log FILE]
|
|
366
|
+
|
|
367
|
+
Decides whether the Skill is admitted under an admission rule file (mago.admission/v0). The decision is a
|
|
368
|
+
pure function of the facts, the rules and the time: no model and no network.
|
|
369
|
+
|
|
370
|
+
The facts are computed here: the observed profile and USF declaration from the bytes; a review, when
|
|
371
|
+
--policy names an organisation policy whose trust root has a valid certificate for these exact bytes; the
|
|
372
|
+
publisher, when --published-by holds a published-by bundle that verifies for these bytes (offline, with
|
|
373
|
+
--trusted-root and --cosign). --base names the reviewed version this one updates (its folder), for
|
|
374
|
+
non-escalating update rules.
|
|
375
|
+
|
|
376
|
+
--log FILE append the decision and its facts as one JSON line, for "mago-ski admission diff"
|
|
377
|
+
|
|
378
|
+
Prints the action (ALLOW, LOG_ONLY, FLAG, REQUIRE_REVIEW or BLOCK), the rule that decided it, and what
|
|
379
|
+
evidence is missing. Exit 0 when admitted (ALLOW, LOG_ONLY, FLAG), 1 when not, 2 for usage errors.`,
|
|
380
|
+
'admission check': `Usage: mago-ski admission check RULES
|
|
381
|
+
|
|
382
|
+
Parses and checks an admission rule file: unknown keys, duplicate rule ids, BLOCK and REQUIRE_REVIEW
|
|
383
|
+
rules without a reason, ALLOW rules built on capability conditions (they can only make a decision
|
|
384
|
+
stricter) and ALLOW rules that would admit every Skill. Prints the rule ids and the file's digest.
|
|
385
|
+
Exit 0 when the file is valid, 1 when it is not.`,
|
|
386
|
+
'admission diff': `Usage: mago-ski admission diff OLD NEW --against-log FILE
|
|
387
|
+
|
|
388
|
+
Re-evaluates every decision in a log written by "mago-ski admit --log" under both rule files, each at the
|
|
389
|
+
time it was made, and lists the decisions whose action would change. Run it before switching rules.
|
|
390
|
+
Exit 0 when nothing changes, 1 when something would, 2 for usage errors.`,
|
|
391
|
+
check: `Usage: mago-ski check SKILL_DIR [--require-manifest]
|
|
392
|
+
|
|
393
|
+
Prints the Skill's observed profile (mago.observed-profile/v1: websites, scripts and shell code, programs
|
|
394
|
+
named in shell code, files not searched) and, when SKILL.md declares its needs in an OWASP Universal Skill
|
|
395
|
+
Format manifest, whether the declaration covers the profile. The same check as the Skill check Action.
|
|
396
|
+
It states what the files contain; it does not say that a Skill is safe.
|
|
397
|
+
|
|
398
|
+
--require-manifest fail when SKILL.md has no readable USF declaration
|
|
399
|
+
|
|
400
|
+
Exit 0 when the check passes, 1 when it fails, 2 for usage or input errors.`,
|
|
327
401
|
digest: `Usage: mago-ski digest SKILL_DIR [--explain]
|
|
328
402
|
|
|
329
403
|
Prints the Skill's tree digest (sha256:...). --explain also lists every covered file, its hash and
|
|
@@ -458,7 +532,7 @@ function readVersion() {
|
|
|
458
532
|
class UsageError extends Error {
|
|
459
533
|
}
|
|
460
534
|
/* The actual boolean flags that should take no value. */
|
|
461
|
-
const BOOLEAN_FLAGS = new Set(['--json', '--explain', '--help', '--force', '--print', '--no-deny-read', '--lock-user-skill-folders', '--new-root']);
|
|
535
|
+
const BOOLEAN_FLAGS = new Set(['--require-manifest', '--json', '--explain', '--help', '--force', '--print', '--no-deny-read', '--lock-user-skill-folders', '--new-root']);
|
|
462
536
|
/* Only `gate init` takes these; everywhere else they are refused, as before 0.4.0. */
|
|
463
537
|
const GATE_INIT_ONLY = new Set(['--force', '--print', '--no-deny-read', '--lock-user-skill-folders', '--new-root']);
|
|
464
538
|
/* flags Map: A map of flags to their values. */
|
|
@@ -704,6 +778,53 @@ async function runRevoke(sub, parsed, now) {
|
|
|
704
778
|
});
|
|
705
779
|
return 0;
|
|
706
780
|
}
|
|
781
|
+
async function runAdmit(parsed, now) {
|
|
782
|
+
allow(parsed, ['--rules', '--policy', '--published-by', '--base', '--base-published-by', '--trusted-root', '--cosign', '--log'], 1);
|
|
783
|
+
let rules;
|
|
784
|
+
try {
|
|
785
|
+
rules = parseRules((await readRegularFile(one(parsed, '--rules'), 'Rule file', 256 * 1024)).toString('utf8'));
|
|
786
|
+
}
|
|
787
|
+
catch (error) {
|
|
788
|
+
if (error instanceof AdmissionRulesError)
|
|
789
|
+
throw new UsageError(error.message);
|
|
790
|
+
throw error;
|
|
791
|
+
}
|
|
792
|
+
const policyPath = optional(parsed, '--policy');
|
|
793
|
+
const policy = policyPath ? await loadPolicy(policyPath) : undefined;
|
|
794
|
+
const trustedRootPath = optional(parsed, '--trusted-root');
|
|
795
|
+
const cosignPath = optional(parsed, '--cosign');
|
|
796
|
+
if ((trustedRootPath === undefined) !== (cosignPath === undefined))
|
|
797
|
+
throw new UsageError('--trusted-root and --cosign go together');
|
|
798
|
+
const sigstore = trustedRootPath && cosignPath ? { trustedRootPath, cosignPath } : undefined;
|
|
799
|
+
const base = optional(parsed, '--base');
|
|
800
|
+
if (base === undefined && optional(parsed, '--base-published-by') !== undefined)
|
|
801
|
+
throw new UsageError('--base-published-by needs --base');
|
|
802
|
+
const evidence = (bundle) => ({ ...(policy ? { policy } : {}), ...(bundle ? { publishedBy: bundle } : {}) });
|
|
803
|
+
let facts;
|
|
804
|
+
try {
|
|
805
|
+
facts = {
|
|
806
|
+
skill: await collectSkillFacts(parsed.positional[0], evidence(optional(parsed, '--published-by')), sigstore, now),
|
|
807
|
+
base: base === undefined ? null : await collectSkillFacts(base, evidence(optional(parsed, '--base-published-by')), sigstore, now),
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
catch (error) {
|
|
811
|
+
if (error instanceof RangeError)
|
|
812
|
+
throw new UsageError(error.message);
|
|
813
|
+
throw error;
|
|
814
|
+
}
|
|
815
|
+
const decision = evaluate(rules, facts, now);
|
|
816
|
+
const logPath = optional(parsed, '--log');
|
|
817
|
+
if (logPath) {
|
|
818
|
+
const record = { decided_at: now.toISOString(), rules_digest: rules.digest, facts, decision };
|
|
819
|
+
await appendFile(logPath, `${JSON.stringify(record)}\n`, { mode: 0o600 });
|
|
820
|
+
}
|
|
821
|
+
print({
|
|
822
|
+
skill: facts.skill.name, digest: facts.skill.digest, ...decision,
|
|
823
|
+
evidence: { reviewed_by: facts.skill.reviewed_by, published_by: facts.skill.published_by, declaration: facts.skill.declaration !== null, base: facts.base?.digest ?? null },
|
|
824
|
+
rules_digest: rules.digest,
|
|
825
|
+
});
|
|
826
|
+
return decision.admitted ? 0 : 1;
|
|
827
|
+
}
|
|
707
828
|
/** Loads the policy named by --policy, else the host default. The checkout being verified never chooses its own root. */
|
|
708
829
|
async function loadCliPolicy(parsed) {
|
|
709
830
|
const explicit = optional(parsed, '--policy');
|
|
@@ -1231,7 +1352,7 @@ export async function runCli(argv = process.argv.slice(2), now = new Date(), gat
|
|
|
1231
1352
|
process.stdout.write(`${text}\n`);
|
|
1232
1353
|
return 0;
|
|
1233
1354
|
}
|
|
1234
|
-
const hasSub = ['root', 'revoke', 'recovery', 'root-events', 'timestamp', 'policy', 'gate'].includes(command);
|
|
1355
|
+
const hasSub = ['root', 'revoke', 'recovery', 'root-events', 'timestamp', 'policy', 'gate', 'attestation', 'admission'].includes(command);
|
|
1235
1356
|
const parsed = parseArgs(argv.slice(hasSub ? 2 : 1));
|
|
1236
1357
|
switch (command) {
|
|
1237
1358
|
case 'keygen': {
|
|
@@ -1294,6 +1415,82 @@ export async function runCli(argv = process.argv.slice(2), now = new Date(), gat
|
|
|
1294
1415
|
process.stdout.write(`${tree.digest}\n`);
|
|
1295
1416
|
return 0;
|
|
1296
1417
|
}
|
|
1418
|
+
case 'attestation': {
|
|
1419
|
+
if (maybeSub !== 'verify')
|
|
1420
|
+
throw new UsageError('Usage: mago-ski attestation verify SKILL_DIR --bundle FILE --trusted-root FILE --cosign FILE --repo OWNER/NAME');
|
|
1421
|
+
allow(parsed, ['--bundle', '--trusted-root', '--cosign', '--repo', '--workflow', '--repo-id'], 1);
|
|
1422
|
+
const workflow = parsed.flags.get('--workflow')?.[0];
|
|
1423
|
+
const repositoryId = parsed.flags.get('--repo-id')?.[0];
|
|
1424
|
+
let facts;
|
|
1425
|
+
try {
|
|
1426
|
+
facts = await verifyPublishedBy(parsed.positional[0], {
|
|
1427
|
+
bundlePath: one(parsed, '--bundle'), trustedRootPath: one(parsed, '--trusted-root'), cosignPath: one(parsed, '--cosign'), repository: one(parsed, '--repo'),
|
|
1428
|
+
...(workflow ? { workflow } : {}), ...(repositoryId ? { repositoryId } : {}),
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
catch (error) {
|
|
1432
|
+
if (error instanceof RangeError)
|
|
1433
|
+
throw new UsageError(error.message);
|
|
1434
|
+
print({ result: 'NOT_VERIFIED', reason: error.message });
|
|
1435
|
+
return 1;
|
|
1436
|
+
}
|
|
1437
|
+
print({ result: facts.differences.length === 0 ? 'PUBLISHED_BY' : 'RECORD_MISMATCH', digest: facts.digest, publisher: facts.publisher, differences: facts.differences, declaration: facts.recorded.declaration, profile: facts.recorded.profile });
|
|
1438
|
+
return facts.differences.length === 0 ? 0 : 1;
|
|
1439
|
+
}
|
|
1440
|
+
case 'admit':
|
|
1441
|
+
return await runAdmit(parsed, now);
|
|
1442
|
+
case 'admission': {
|
|
1443
|
+
if (maybeSub === 'check') {
|
|
1444
|
+
allow(parsed, [], 1);
|
|
1445
|
+
let rules;
|
|
1446
|
+
try {
|
|
1447
|
+
rules = parseRules((await readRegularFile(parsed.positional[0], 'Rule file', 256 * 1024)).toString('utf8'));
|
|
1448
|
+
}
|
|
1449
|
+
catch (error) {
|
|
1450
|
+
if (!(error instanceof AdmissionRulesError))
|
|
1451
|
+
throw error;
|
|
1452
|
+
print({ valid: false, error: error.message });
|
|
1453
|
+
return 1;
|
|
1454
|
+
}
|
|
1455
|
+
print({ valid: true, digest: rules.digest, default: rules.default, rules: rules.rules.map((rule) => `${rule.id}: ${rule.action}`) });
|
|
1456
|
+
return 0;
|
|
1457
|
+
}
|
|
1458
|
+
if (maybeSub === 'diff') {
|
|
1459
|
+
allow(parsed, ['--against-log'], 2);
|
|
1460
|
+
const load = async (file) => {
|
|
1461
|
+
try {
|
|
1462
|
+
return parseRules((await readRegularFile(file, 'Rule file', 256 * 1024)).toString('utf8'));
|
|
1463
|
+
}
|
|
1464
|
+
catch (error) {
|
|
1465
|
+
if (error instanceof AdmissionRulesError)
|
|
1466
|
+
throw new UsageError(`${file}: ${error.message}`);
|
|
1467
|
+
throw error;
|
|
1468
|
+
}
|
|
1469
|
+
};
|
|
1470
|
+
const [previous, next] = [await load(parsed.positional[0]), await load(parsed.positional[1])];
|
|
1471
|
+
const lines = (await readRegularFile(one(parsed, '--against-log'), 'Decision log', 256 * 1024 * 1024)).toString('utf8').split('\n').filter(Boolean);
|
|
1472
|
+
const records = lines.map((line, index) => {
|
|
1473
|
+
try {
|
|
1474
|
+
return JSON.parse(line);
|
|
1475
|
+
}
|
|
1476
|
+
catch {
|
|
1477
|
+
throw new UsageError(`log line ${index + 1} is not JSON`);
|
|
1478
|
+
}
|
|
1479
|
+
});
|
|
1480
|
+
const flips = replay(previous, next, records);
|
|
1481
|
+
print({ decisions: records.length, changed: flips.length, flips: flips.map((flip) => ({ decided_at: flip.decided_at, skill: flip.skill, digest: flip.digest, from: `${flip.before.action} (${flip.before.rule_id})`, to: `${flip.after.action} (${flip.after.rule_id})` })) });
|
|
1482
|
+
return flips.length === 0 ? 0 : 1;
|
|
1483
|
+
}
|
|
1484
|
+
throw new UsageError('Usage: mago-ski admission check RULES | mago-ski admission diff OLD NEW --against-log FILE');
|
|
1485
|
+
}
|
|
1486
|
+
case 'check': {
|
|
1487
|
+
allow(parsed, [], 1);
|
|
1488
|
+
const skillDir = path.resolve(parsed.positional[0]);
|
|
1489
|
+
const result = await checkSkill(skillDir, path.dirname(skillDir), { requireManifest: parsed.booleans.has('--require-manifest') });
|
|
1490
|
+
const { format: _format, dir: _dir, ...rest } = result;
|
|
1491
|
+
print({ skill_dir: skillDir, usf_format: result.format, ...rest });
|
|
1492
|
+
return result.ok ? 0 : 1;
|
|
1493
|
+
}
|
|
1297
1494
|
case 'diff': {
|
|
1298
1495
|
if (parsed.positional.length > 0)
|
|
1299
1496
|
throw new UsageError(`diff takes the two directories as options: mago-ski diff --base ${parsed.positional[0]} --candidate ${parsed.positional[1] ?? 'DIR'}`);
|
package/dist/src/diff.d.ts
CHANGED
|
@@ -28,4 +28,8 @@ export interface DiffResult {
|
|
|
28
28
|
candidate: string[];
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
|
+
export declare function collectOrigins(contents: Map<string, Buffer>): {
|
|
32
|
+
origins: string[];
|
|
33
|
+
complete: boolean;
|
|
34
|
+
};
|
|
31
35
|
export declare function diffSkillTrees(baseRoot: string, candidateRoot: string): Promise<DiffResult>;
|
package/dist/src/diff.js
CHANGED
|
@@ -30,3 +30,27 @@ export declare function verifySigstoreBlob(options: {
|
|
|
30
30
|
}): Promise<{
|
|
31
31
|
valid: true;
|
|
32
32
|
}>;
|
|
33
|
+
export declare function buildCosignVerifyAttestationArgs(options: {
|
|
34
|
+
bundlePath: string;
|
|
35
|
+
trustedRootPath: string;
|
|
36
|
+
digestHex: string;
|
|
37
|
+
predicateType: string;
|
|
38
|
+
identityRegexp: string;
|
|
39
|
+
issuer: string;
|
|
40
|
+
}): string[];
|
|
41
|
+
/**
|
|
42
|
+
* Verifies an in-toto attestation in a Sigstore bundle for a subject known only by its sha256 digest, offline against
|
|
43
|
+
* the given trusted root: the signature, the certificate chain, the transparency-log proof, the signer identity and
|
|
44
|
+
* that the statement names this digest. The caller reads the statement from the same bundle bytes afterwards.
|
|
45
|
+
*/
|
|
46
|
+
export declare function verifySigstoreAttestation(options: {
|
|
47
|
+
binaryPath: string;
|
|
48
|
+
bundlePath: string;
|
|
49
|
+
trustedRootPath: string;
|
|
50
|
+
digestHex: string;
|
|
51
|
+
predicateType: string;
|
|
52
|
+
identityRegexp: string;
|
|
53
|
+
issuer: string;
|
|
54
|
+
}): Promise<{
|
|
55
|
+
valid: true;
|
|
56
|
+
}>;
|
|
@@ -159,3 +159,25 @@ export async function verifySigstoreBlob(options) {
|
|
|
159
159
|
await runCosign(options.binaryPath, buildCosignVerifyArgs(bundle, trustedRoot, options.certificateIdentity, options.certificateOidcIssuer, payload), 60_000, verificationEnv(process.env));
|
|
160
160
|
return { valid: true };
|
|
161
161
|
}
|
|
162
|
+
export function buildCosignVerifyAttestationArgs(options) {
|
|
163
|
+
return [
|
|
164
|
+
'verify-blob-attestation', '--bundle', options.bundlePath, '--trusted-root', options.trustedRootPath,
|
|
165
|
+
'--digest', options.digestHex, '--digestAlg', 'sha256', '--type', options.predicateType, '--check-claims=true',
|
|
166
|
+
'--certificate-identity-regexp', options.identityRegexp, '--certificate-oidc-issuer', options.issuer,
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Verifies an in-toto attestation in a Sigstore bundle for a subject known only by its sha256 digest, offline against
|
|
171
|
+
* the given trusted root: the signature, the certificate chain, the transparency-log proof, the signer identity and
|
|
172
|
+
* that the statement names this digest. The caller reads the statement from the same bundle bytes afterwards.
|
|
173
|
+
*/
|
|
174
|
+
export async function verifySigstoreAttestation(options) {
|
|
175
|
+
const bundlePath = await requireRegularFile(options.bundlePath, 'Sigstore bundle');
|
|
176
|
+
const trustedRootPath = await requireRegularFile(options.trustedRootPath, 'Sigstore trusted root');
|
|
177
|
+
if (!/^[0-9a-f]{64}$/u.test(options.digestHex))
|
|
178
|
+
throw new Error('subject digest must be 64 lowercase hex characters');
|
|
179
|
+
if (!options.identityRegexp || !options.issuer)
|
|
180
|
+
throw new Error('a certificate identity and OIDC issuer are required');
|
|
181
|
+
await runCosign(options.binaryPath, buildCosignVerifyAttestationArgs({ ...options, bundlePath, trustedRootPath }), 60_000, verificationEnv(process.env));
|
|
182
|
+
return { valid: true };
|
|
183
|
+
}
|