@mago-foundation/mago-ski 0.0.0-stage → 0.2.0
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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +139 -2
- package/action.yml +58 -0
- package/dist/hosts/github/run.d.ts +27 -0
- package/dist/hosts/github/run.js +152 -0
- package/dist/hosts/pi/index.d.ts +13 -0
- package/dist/hosts/pi/index.js +550 -0
- package/dist/hosts/pi/paths.d.ts +9 -0
- package/dist/hosts/pi/paths.js +55 -0
- package/dist/src/admin.d.ts +74 -0
- package/dist/src/admin.js +243 -0
- package/dist/src/canonical-json.d.ts +12 -0
- package/dist/src/canonical-json.js +149 -0
- package/dist/src/certificate.d.ts +34 -0
- package/dist/src/certificate.js +112 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +595 -0
- package/dist/src/diff.d.ts +26 -0
- package/dist/src/diff.js +404 -0
- package/dist/src/dsse.d.ts +25 -0
- package/dist/src/dsse.js +71 -0
- package/dist/src/experimental/sigstore.d.ts +26 -0
- package/dist/src/experimental/sigstore.js +153 -0
- package/dist/src/fs-safe.d.ts +27 -0
- package/dist/src/fs-safe.js +229 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.js +14 -0
- package/dist/src/keys.d.ts +21 -0
- package/dist/src/keys.js +154 -0
- package/dist/src/main-module.d.ts +5 -0
- package/dist/src/main-module.js +19 -0
- package/dist/src/names.d.ts +7 -0
- package/dist/src/names.js +38 -0
- package/dist/src/policy.d.ts +81 -0
- package/dist/src/policy.js +349 -0
- package/dist/src/revocations.d.ts +28 -0
- package/dist/src/revocations.js +93 -0
- package/dist/src/time.d.ts +25 -0
- package/dist/src/time.js +74 -0
- package/dist/src/tree-digest.d.ts +46 -0
- package/dist/src/tree-digest.js +268 -0
- package/dist/src/trust-root.d.ts +38 -0
- package/dist/src/trust-root.js +136 -0
- package/dist/src/verify.d.ts +96 -0
- package/dist/src/verify.js +380 -0
- package/docs/hosts/adapter-interface.md +33 -0
- package/docs/hosts/github-action.md +45 -0
- package/docs/hosts/pi.md +41 -0
- package/docs/spec/root-recovery-v1.md +430 -0
- package/docs/spec/skill-certificate-v1.md +60 -0
- package/docs/spec/skill-tree-v2.md +59 -0
- package/docs/spec/trust-documents-v1.md +103 -0
- package/docs/spec/verification.md +56 -0
- package/docs/threat-model.md +45 -0
- package/package.json +77 -4
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { parseCertificate } from "./certificate.js";
|
|
3
|
+
import { verifyEnvelopeSignature } from "./dsse.js";
|
|
4
|
+
import { readRegularFile } from "./fs-safe.js";
|
|
5
|
+
import { scopeMatches, skillNameFromFrontmatter, validateDigest, validateSkillName } from "./names.js";
|
|
6
|
+
import { verifyRevocations } from "./revocations.js";
|
|
7
|
+
import { CLOCK_SKEW_MS, formatTimestamp, requireValidDate } from "./time.js";
|
|
8
|
+
import { collectSkillTree, MAX_FILE_BYTES, SKILL_FILENAME } from "./tree-digest.js";
|
|
9
|
+
import { TrustDocumentError, verifyTrustRoot } from "./trust-root.js";
|
|
10
|
+
export const VERDICTS = [
|
|
11
|
+
'VERIFIED',
|
|
12
|
+
'UNAPPROVED_CHANGE',
|
|
13
|
+
'NO_CERTIFICATE',
|
|
14
|
+
'UNTRUSTED_SIGNER',
|
|
15
|
+
'REVOKED',
|
|
16
|
+
'EXPIRED',
|
|
17
|
+
'UNVERIFIABLE',
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Precedence rank for candidate selection (lower rank wins):
|
|
21
|
+
* VERIFIED (0) beats all failures, followed by the failure priority order.
|
|
22
|
+
*/
|
|
23
|
+
const VERDICT_RANK = {
|
|
24
|
+
VERIFIED: 0,
|
|
25
|
+
REVOKED: 1,
|
|
26
|
+
UNAPPROVED_CHANGE: 2,
|
|
27
|
+
EXPIRED: 3,
|
|
28
|
+
UNTRUSTED_SIGNER: 4,
|
|
29
|
+
UNVERIFIABLE: 5,
|
|
30
|
+
NO_CERTIFICATE: 6,
|
|
31
|
+
};
|
|
32
|
+
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
|
|
33
|
+
function errorMessage(error) {
|
|
34
|
+
return error instanceof Error ? error.message : String(error);
|
|
35
|
+
}
|
|
36
|
+
function requireValidMinimumVersion(value, label) {
|
|
37
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 1) {
|
|
38
|
+
throw new Error(`${label} must be a positive integer`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Verifies the trust root and revocation list once; every Skill check reuses the result. */
|
|
42
|
+
export function loadTrustContext(options) {
|
|
43
|
+
try {
|
|
44
|
+
// Inside the try, so a bad clock or minimum version becomes an UNVERIFIABLE result like any other failure here.
|
|
45
|
+
requireValidDate(options.now, 'Verification time');
|
|
46
|
+
if (options.minimum !== undefined) {
|
|
47
|
+
requireValidMinimumVersion(options.minimum.trustRootVersion, 'Minimum trust root version');
|
|
48
|
+
requireValidMinimumVersion(options.minimum.revocationsVersion, 'Minimum revocation list version');
|
|
49
|
+
}
|
|
50
|
+
const trustRoot = verifyTrustRoot(options.trustRootBytes, options.rootKeyId, options.now);
|
|
51
|
+
const revocations = verifyRevocations(options.revocationsBytes, trustRoot, options.now);
|
|
52
|
+
if (options.minimum && trustRoot.version < options.minimum.trustRootVersion) {
|
|
53
|
+
return {
|
|
54
|
+
ok: false,
|
|
55
|
+
verdict: 'UNVERIFIABLE',
|
|
56
|
+
reason: `trust root version ${trustRoot.version} is older than version ${options.minimum.trustRootVersion} already seen (rollback)`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (options.minimum && revocations.version < options.minimum.revocationsVersion) {
|
|
60
|
+
return {
|
|
61
|
+
ok: false,
|
|
62
|
+
verdict: 'UNVERIFIABLE',
|
|
63
|
+
reason: `revocation list version ${revocations.version} is older than version ${options.minimum.revocationsVersion} already seen (rollback)`,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return { ok: true, context: { trustRoot, revocations } };
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error instanceof TrustDocumentError) {
|
|
70
|
+
return { ok: false, verdict: error.code, reason: error.message };
|
|
71
|
+
}
|
|
72
|
+
return { ok: false, verdict: 'UNVERIFIABLE', reason: errorMessage(error) };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Checks one certificate for one Skill: trust chain, scope, signature, expiry, revocation, then bytes. */
|
|
76
|
+
export function evaluateCertificate(bytes, skillName, digest, context, now) {
|
|
77
|
+
// The issued-in-the-future and expiry checks compare against `now`; an invalid Date would pass them all.
|
|
78
|
+
requireValidDate(now, 'Verification time');
|
|
79
|
+
let certificate;
|
|
80
|
+
try {
|
|
81
|
+
validateSkillName(skillName, 'Skill name');
|
|
82
|
+
validateDigest(digest, 'Skill tree digest');
|
|
83
|
+
certificate = parseCertificate(bytes);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return { verdict: 'UNVERIFIABLE', reason: `malformed certificate: ${errorMessage(error)}` };
|
|
87
|
+
}
|
|
88
|
+
if (certificate.skillName !== skillName) {
|
|
89
|
+
return {
|
|
90
|
+
verdict: 'NO_CERTIFICATE',
|
|
91
|
+
reason: `certificate is for Skill "${certificate.skillName}", not "${skillName}"`,
|
|
92
|
+
certificate,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const { trustRoot, revocations } = context;
|
|
96
|
+
const revokedKey = revocations.keys.find((entry) => entry.keyId === certificate.approverKeyId);
|
|
97
|
+
if (revokedKey) {
|
|
98
|
+
return {
|
|
99
|
+
verdict: 'REVOKED',
|
|
100
|
+
reason: `approver key ${certificate.approverKeyId} is revoked: ${revokedKey.reason}`,
|
|
101
|
+
certificate,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const approver = trustRoot.approvers.find((entry) => entry.keyId === certificate.approverKeyId);
|
|
105
|
+
if (!approver) {
|
|
106
|
+
return {
|
|
107
|
+
verdict: 'UNTRUSTED_SIGNER',
|
|
108
|
+
reason: `signer ${certificate.approverKeyId} is not an approver in trust root v${trustRoot.version}`,
|
|
109
|
+
certificate,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if (!approver.scopes.some((scope) => scopeMatches(scope, skillName))) {
|
|
113
|
+
return {
|
|
114
|
+
verdict: 'UNTRUSTED_SIGNER',
|
|
115
|
+
reason: `approver "${approver.name}" is not authorized for Skill "${skillName}" (scopes: ${approver.scopes.join(', ')})`,
|
|
116
|
+
certificate,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (!verifyEnvelopeSignature(certificate.envelope, approver.publicKey)) {
|
|
120
|
+
return {
|
|
121
|
+
verdict: 'UNVERIFIABLE',
|
|
122
|
+
reason: 'certificate signature is invalid',
|
|
123
|
+
certificate,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (certificate.issuedAt.getTime() > now.getTime() + CLOCK_SKEW_MS) {
|
|
127
|
+
return {
|
|
128
|
+
verdict: 'UNVERIFIABLE',
|
|
129
|
+
reason: `certificate is issued in the future (${formatTimestamp(certificate.issuedAt)})`,
|
|
130
|
+
certificate,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (certificate.issuedAt >= approver.expiresAt) {
|
|
134
|
+
return {
|
|
135
|
+
verdict: 'UNTRUSTED_SIGNER',
|
|
136
|
+
reason: `certificate was issued after approver "${approver.name}" expired`,
|
|
137
|
+
certificate,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (approver.expiresAt <= now) {
|
|
141
|
+
return {
|
|
142
|
+
verdict: 'EXPIRED',
|
|
143
|
+
reason: `approver "${approver.name}" expired at ${formatTimestamp(approver.expiresAt)}`,
|
|
144
|
+
certificate,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (certificate.expiresAt <= now) {
|
|
148
|
+
return {
|
|
149
|
+
verdict: 'EXPIRED',
|
|
150
|
+
reason: `certificate expired at ${formatTimestamp(certificate.expiresAt)}`,
|
|
151
|
+
certificate,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const revokedDigest = revocations.digests.find((entry) => entry.digest === certificate.digest);
|
|
155
|
+
if (revokedDigest) {
|
|
156
|
+
return {
|
|
157
|
+
verdict: 'REVOKED',
|
|
158
|
+
reason: `approved version ${certificate.digest} is revoked: ${revokedDigest.reason}`,
|
|
159
|
+
certificate,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (certificate.digest !== digest) {
|
|
163
|
+
return {
|
|
164
|
+
verdict: 'UNAPPROVED_CHANGE',
|
|
165
|
+
reason: `Skill bytes changed since approval (approved ${certificate.digest}, found ${digest}); a new certificate is required`,
|
|
166
|
+
certificate,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
verdict: 'VERIFIED',
|
|
171
|
+
reason: `approved by "${approver.name}" until ${formatTimestamp(certificate.expiresAt)}`,
|
|
172
|
+
certificate,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The Skill name from SKILL.md's bytes: an explicit name, else frontmatter "name", else the directory name.
|
|
177
|
+
* Pure, so a caller that hashes and signs those bytes can derive the name from the very same buffer.
|
|
178
|
+
*/
|
|
179
|
+
export function skillNameFromSkillFile(skillDir, skillFile, explicit) {
|
|
180
|
+
if (explicit) {
|
|
181
|
+
return validateSkillName(explicit);
|
|
182
|
+
}
|
|
183
|
+
let text;
|
|
184
|
+
if (skillFile !== undefined) {
|
|
185
|
+
try {
|
|
186
|
+
text = utf8Decoder.decode(skillFile);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
throw new Error(`${SKILL_FILENAME} is not valid UTF-8`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const fromFrontmatter = text === undefined ? undefined : skillNameFromFrontmatter(text);
|
|
193
|
+
if (fromFrontmatter) {
|
|
194
|
+
return validateSkillName(fromFrontmatter, `${SKILL_FILENAME} name`);
|
|
195
|
+
}
|
|
196
|
+
return validateSkillName(path.basename(path.resolve(skillDir)), 'Skill directory name');
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Resolves the Skill name. SKILL.md comes from an untrusted checkout, so it is read only if it is a regular,
|
|
200
|
+
* non-symlink file no larger than the tree collector's per-file limit; anything else throws (UNVERIFIABLE).
|
|
201
|
+
*/
|
|
202
|
+
export async function resolveSkillName(skillDir, explicit) {
|
|
203
|
+
if (explicit) {
|
|
204
|
+
return validateSkillName(explicit);
|
|
205
|
+
}
|
|
206
|
+
let rawBytes;
|
|
207
|
+
try {
|
|
208
|
+
rawBytes = await readRegularFile(path.join(skillDir, SKILL_FILENAME), SKILL_FILENAME, MAX_FILE_BYTES);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (error?.code !== 'ENOENT') {
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return skillNameFromSkillFile(skillDir, rawBytes);
|
|
216
|
+
}
|
|
217
|
+
/** Keeps the best candidate seen so far: VERIFIED beats any failure, then VERDICT_RANK; the first of equals wins. */
|
|
218
|
+
function selectBetterCandidate(current, next) {
|
|
219
|
+
if (!current) {
|
|
220
|
+
return next;
|
|
221
|
+
}
|
|
222
|
+
const currentRank = VERDICT_RANK[current.evaluation.verdict];
|
|
223
|
+
const nextRank = VERDICT_RANK[next.evaluation.verdict];
|
|
224
|
+
return nextRank < currentRank ? next : current;
|
|
225
|
+
}
|
|
226
|
+
function buildDecision(chosen) {
|
|
227
|
+
if (!chosen) {
|
|
228
|
+
return {
|
|
229
|
+
verdict: 'NO_CERTIFICATE',
|
|
230
|
+
reason: 'no certificate found for this Skill',
|
|
231
|
+
files: null,
|
|
232
|
+
certificateId: null,
|
|
233
|
+
certificateSource: null,
|
|
234
|
+
approverKeyId: null,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
const { evaluation, source } = chosen;
|
|
238
|
+
const certificate = evaluation.certificate;
|
|
239
|
+
const verifiedFiles = evaluation.verdict === 'VERIFIED' && certificate
|
|
240
|
+
? certificate.files.map((file) => ({ ...file }))
|
|
241
|
+
: null;
|
|
242
|
+
return {
|
|
243
|
+
verdict: evaluation.verdict,
|
|
244
|
+
reason: evaluation.reason,
|
|
245
|
+
files: verifiedFiles,
|
|
246
|
+
certificateId: certificate?.id ?? null,
|
|
247
|
+
certificateSource: source.source,
|
|
248
|
+
approverKeyId: certificate?.approverKeyId ?? null,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
const MAX_NAMED_PATHS = 3;
|
|
252
|
+
/**
|
|
253
|
+
* Paths come from the Skill (untrusted): quoted, so a name cannot imitate the reason's own wording, and with DEL, C1 controls
|
|
254
|
+
* and bidi or invisible characters escaped, which tree paths may contain and terminals and annotations would apply.
|
|
255
|
+
*/
|
|
256
|
+
function quotePath(file) {
|
|
257
|
+
return JSON.stringify(file).replace(/[\u007f-\u009f\u200b-\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069\ufeff]/gu, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
258
|
+
}
|
|
259
|
+
function namePaths(paths) {
|
|
260
|
+
const shown = paths.slice(0, MAX_NAMED_PATHS).join(', ');
|
|
261
|
+
return paths.length > MAX_NAMED_PATHS ? `${shown} and ${paths.length - MAX_NAMED_PATHS} more` : shown;
|
|
262
|
+
}
|
|
263
|
+
/** Says what differs between the approved file list and the tree on disk, e.g. an executable bit with no byte changed. */
|
|
264
|
+
export function describeTreeChange(approved, found) {
|
|
265
|
+
const before = new Map(approved.map((file) => [file.path, file]));
|
|
266
|
+
const content = [];
|
|
267
|
+
const mode = [];
|
|
268
|
+
const added = [];
|
|
269
|
+
for (const file of found) {
|
|
270
|
+
const old = before.get(file.path);
|
|
271
|
+
before.delete(file.path);
|
|
272
|
+
if (!old) {
|
|
273
|
+
added.push(file.exec ? `${quotePath(file.path)} (executable)` : quotePath(file.path));
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (old.sha256 !== file.sha256 || old.size !== file.size)
|
|
277
|
+
content.push(quotePath(file.path));
|
|
278
|
+
// Reported even when the content changed too: a file becoming executable matters on its own.
|
|
279
|
+
if (old.exec !== file.exec)
|
|
280
|
+
mode.push(`${quotePath(file.path)} (${file.exec ? 'now executable' : 'no longer executable'})`);
|
|
281
|
+
}
|
|
282
|
+
const removed = [...before.keys()].map(quotePath);
|
|
283
|
+
const parts = [
|
|
284
|
+
content.length ? `content changed in ${namePaths(content)}` : '',
|
|
285
|
+
mode.length ? `executable bit changed on ${namePaths(mode)}` : '',
|
|
286
|
+
added.length ? `added ${namePaths(added)}` : '',
|
|
287
|
+
removed.length ? `removed ${namePaths(removed)}` : '',
|
|
288
|
+
].filter(Boolean);
|
|
289
|
+
return parts.length ? parts.join('; ') : 'files changed';
|
|
290
|
+
}
|
|
291
|
+
/** Replaces the generic UNAPPROVED_CHANGE reason with one naming the changed files, when the certificate lists them. */
|
|
292
|
+
function explainChange(decision, best, tree) {
|
|
293
|
+
const certificate = best?.evaluation.certificate;
|
|
294
|
+
if (decision.verdict !== 'UNAPPROVED_CHANGE' || !certificate)
|
|
295
|
+
return decision;
|
|
296
|
+
return {
|
|
297
|
+
...decision,
|
|
298
|
+
reason: `Skill changed since approval: ${describeTreeChange(certificate.files, tree.files)} (approved ${certificate.digest}, found ${tree.digest}); a new certificate is required`,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function buildBaseResult(options) {
|
|
302
|
+
return {
|
|
303
|
+
skillName: options.skillName,
|
|
304
|
+
skillDir: options.tree.root,
|
|
305
|
+
digest: options.tree.digest,
|
|
306
|
+
excluded: [...options.tree.excluded],
|
|
307
|
+
checkedAt: formatTimestamp(options.now),
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
export function decide(options) {
|
|
311
|
+
requireValidDate(options.now, 'Verification time');
|
|
312
|
+
let best;
|
|
313
|
+
for (const source of options.certificates) {
|
|
314
|
+
const evaluation = evaluateCertificate(source.bytes, options.skillName, options.tree.digest, options.context, options.now);
|
|
315
|
+
best = selectBetterCandidate(best, { source, evaluation });
|
|
316
|
+
if (evaluation.verdict === 'VERIFIED') {
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return { ...buildBaseResult(options), ...explainChange(buildDecision(best), best, options.tree) };
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Same verdict as `decide`, but candidates are evaluated one at a time as they are produced, so only the best
|
|
324
|
+
* one is retained and the first VERIFIED candidate ends the scan (later candidates are never read).
|
|
325
|
+
*/
|
|
326
|
+
export async function decideStreaming(options) {
|
|
327
|
+
requireValidDate(options.now, 'Verification time');
|
|
328
|
+
let best;
|
|
329
|
+
for await (const source of options.certificates) {
|
|
330
|
+
const evaluation = evaluateCertificate(source.bytes, options.skillName, options.tree.digest, options.context, options.now);
|
|
331
|
+
best = selectBetterCandidate(best, { source, evaluation });
|
|
332
|
+
if (evaluation.verdict === 'VERIFIED') {
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return { ...buildBaseResult(options), ...explainChange(buildDecision(best), best, options.tree) };
|
|
337
|
+
}
|
|
338
|
+
/** Full check of one Skill directory against an already-verified trust context. */
|
|
339
|
+
export async function verifySkill(options) {
|
|
340
|
+
// Before the try: its catch builds an UNVERIFIABLE result, which needs a valid time for `checkedAt`.
|
|
341
|
+
requireValidDate(options.now, 'Verification time');
|
|
342
|
+
const skillDir = path.resolve(options.skillDir);
|
|
343
|
+
let skillName = options.skillName ?? path.basename(skillDir);
|
|
344
|
+
try {
|
|
345
|
+
skillName = await resolveSkillName(skillDir, options.skillName);
|
|
346
|
+
const tree = await collectSkillTree(skillDir);
|
|
347
|
+
const provided = typeof options.certificates === 'function'
|
|
348
|
+
? options.certificates(skillName, tree.root)
|
|
349
|
+
: options.certificates;
|
|
350
|
+
const certificates = Symbol.asyncIterator in provided ? provided : await provided;
|
|
351
|
+
return await decideStreaming({
|
|
352
|
+
skillDir,
|
|
353
|
+
skillName,
|
|
354
|
+
tree,
|
|
355
|
+
certificates,
|
|
356
|
+
context: options.context,
|
|
357
|
+
now: options.now,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
return unverifiable(skillDir, skillName, errorMessage(error), options.now);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
export function unverifiable(skillDir, skillName, reason, now, verdict = 'UNVERIFIABLE') {
|
|
365
|
+
requireValidDate(now, 'Verification time');
|
|
366
|
+
const safeVerdict = verdict === 'VERIFIED' || !(verdict in VERDICT_RANK) ? 'UNVERIFIABLE' : verdict;
|
|
367
|
+
return {
|
|
368
|
+
verdict: safeVerdict,
|
|
369
|
+
reason,
|
|
370
|
+
skillName,
|
|
371
|
+
skillDir,
|
|
372
|
+
digest: null,
|
|
373
|
+
files: null,
|
|
374
|
+
certificateId: null,
|
|
375
|
+
certificateSource: null,
|
|
376
|
+
approverKeyId: null,
|
|
377
|
+
excluded: [],
|
|
378
|
+
checkedAt: formatTimestamp(now),
|
|
379
|
+
};
|
|
380
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Host adapter interface
|
|
2
|
+
|
|
3
|
+
What a coding-agent host must let mago-ski do, so that Claude Code, Cursor and others can be added the way Pi was. The library calls are the same for every host; only the hook points differ.
|
|
4
|
+
|
|
5
|
+
## Required hook points
|
|
6
|
+
|
|
7
|
+
| # | Hook | Why | Pi 1.0.4 |
|
|
8
|
+
|---|---|---|---|
|
|
9
|
+
| H1 | **Change the Skill list before the model sees it**, with each Skill's name and directory | Unverified Skills must not be advertised | `before_agent_start` → `systemPromptOptions.skills` |
|
|
10
|
+
| H2 | **Intercept explicit Skill invocation before expansion**, and supply the expanded text | Stop unverified loads; expand verified Skills from verified bytes | `input` event (`handled` / `transform`) |
|
|
11
|
+
| H3 | **Block or allow file tool calls**, with the target path | Refuse unverified files; re-hash verified ones on read | `tool_call` (`block`) |
|
|
12
|
+
| H4 | **Block or allow shell commands**, with the command text | Best-effort coverage of scripts | `tool_call` for `bash` |
|
|
13
|
+
| H5 | **Report to the user** (notification, status) | Explain blocks and how to fix them | `ctx.ui.notify`, `ctx.ui.setStatus` |
|
|
14
|
+
| H6 | **Load configuration from a user- or admin-controlled location** | Policy must not come from the code under review | Extension reads `~/.mago-ski/policy.json` |
|
|
15
|
+
|
|
16
|
+
A host that lacks H1 or H2 can fall back to a **verified-install store**: copy verified Skills into a content-addressed directory and point the host's Skill path only there.
|
|
17
|
+
|
|
18
|
+
## Library calls
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { loadPolicy, loadPolicyTrust, verifyWithPolicy, hashFile, appendDecision, decisionRecord } from 'mago-ski';
|
|
22
|
+
|
|
23
|
+
const policy = await loadPolicy(policyPath);
|
|
24
|
+
const trust = await loadPolicyTrust(policy, new Date()); // once per run; enforces rollback state
|
|
25
|
+
const result = await verifyWithPolicy(policy, skillDir, { now: new Date(), skillName, context: trust.ok ? trust.context : undefined });
|
|
26
|
+
// result.verdict, result.reason, result.files (per-file hashes when VERIFIED)
|
|
27
|
+
const current = await hashFile(absolutePath, relativePath); // compare with result.files on read
|
|
28
|
+
await appendDecision(policy.decisionLogPath, decisionRecord(result, policy.mode, 'advertise'));
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Claude Code (planned)
|
|
32
|
+
|
|
33
|
+
Claude Code hooks (`PreToolUse`, `UserPromptSubmit`) cover H2–H5. Whether H1 is possible (removing a Skill from what the model is told) needs the same spike Pi had; if not, the verified-install store applies.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# GitHub Action
|
|
2
|
+
|
|
3
|
+
In enforce mode, fails the check when a Skill found under the policy's `skill_dirs` is not covered by a valid certificate for its exact bytes.
|
|
4
|
+
|
|
5
|
+
```yaml
|
|
6
|
+
# .github/workflows/skills.yml
|
|
7
|
+
name: skills
|
|
8
|
+
on: [pull_request]
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
jobs:
|
|
12
|
+
verify:
|
|
13
|
+
runs-on: ubuntu-24.04
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
16
|
+
with:
|
|
17
|
+
persist-credentials: false
|
|
18
|
+
- uses: mago-foundation/mago-ski@<full commit sha>
|
|
19
|
+
with:
|
|
20
|
+
policy: .mago-ski/policy.json
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Use a Linux or macOS runner; the action stops on Windows runners (not supported yet). Pin the action to a full commit SHA: the verifier runs from the action's own checkout, so the pin decides which verifier code runs.
|
|
24
|
+
|
|
25
|
+
## Repository layout
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
.mago-ski/
|
|
29
|
+
policy.json # mode, root_fingerprint, trust_root, revocations, skill_dirs, certificate_dirs
|
|
30
|
+
trust-root.json
|
|
31
|
+
revocations.json
|
|
32
|
+
certs/ # optional: <skill-name>.cert.json
|
|
33
|
+
skills/
|
|
34
|
+
pdf-tools/
|
|
35
|
+
SKILL.md
|
|
36
|
+
.mago-ski-cert.json # or keep certificates in .mago-ski/certs/
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
On pull requests, `policy.json`, `trust-root.json` and `revocations.json` are read from the **base** revision. A pull request can add or update certificates (they are signed), but changes it makes to the trust documents take effect only after merge, so a pull request cannot add its own approver. Certificate directories and Skill directories are read from the pull request.
|
|
40
|
+
|
|
41
|
+
Symlinked Skill directories and symlinked `SKILL.md` files are reported as `UNVERIFIABLE`, not skipped (Pi follows such links). Discovery searches up to 16 directory levels below each `skill_dirs` entry. Anything deeper is reported as `UNVERIFIABLE` instead of being skipped: it fails the check in `enforce` mode and is reported in `shadow` mode. Discovery is bounded (1,000 Skills, 50,000 directory entries); a larger tree is reported as a single failure and no Skill is checked. Policy, trust and Skill directories must resolve inside their checkout.
|
|
42
|
+
|
|
43
|
+
The action writes a summary table to the job summary (at most 200 Skills, failures first) and an annotation on each failing Skill's `SKILL.md` (at most 50; a notice says when more were omitted, and the summary has the rest). In `shadow` mode it reports but does not fail. CI keeps no state file; rollback protection comes from reading trust documents from the protected base revision.
|
|
44
|
+
|
|
45
|
+
Run it alongside a capability scanner such as SkilLock: the scanner explains what changed, mago-ski makes sure someone approved exactly that.
|
package/docs/hosts/pi.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Pi host
|
|
2
|
+
|
|
3
|
+
The Pi extension (`hosts/pi/index.ts`) enforces certificates inside a normal [Pi](https://github.com/earendil-works/pi) session. Tested with Pi 1.0.4 and 1.1.0.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pi install npm:@mago-foundation/mago-ski # or for one session: pi -e npm:@mago-foundation/mago-ski
|
|
9
|
+
mkdir -p ~/.mago-ski
|
|
10
|
+
cp policy.json trust-root.json revocations.json ~/.mago-ski/
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The extension reads its policy from `MAGO_SKI_POLICY` or `~/.mago-ski/policy.json`. It never reads policy from the project directory, because project files may be controlled by the code under review. Without a policy it warns once and checks nothing. If a policy exists but cannot be loaded, it blocks every Skill.
|
|
14
|
+
|
|
15
|
+
`MAGO_SKI_POLICY` (and the `policyPath` option of the extension) must be an absolute path, or start with `~/`. A relative path is refused, because it would resolve against whatever directory Pi was started in. A path that resolves inside the project is refused too: the session directory, or the repository containing it (the nearest `.git`, searched up to but not including your home directory), after following symlinks. Paths under `~/.mago-ski/` are always accepted. A refused path is treated like a policy that cannot be loaded: every Skill is blocked and Pi shows an error naming the reason. It never falls back to the home default.
|
|
16
|
+
|
|
17
|
+
Start in `"mode": "shadow"` to see what would be blocked (the decision log records `would-block`), then switch to `"enforce"`.
|
|
18
|
+
|
|
19
|
+
## What it does
|
|
20
|
+
|
|
21
|
+
| Pi event | Enforce mode | Shadow mode |
|
|
22
|
+
|---|---|---|
|
|
23
|
+
| Before each run (`before_agent_start`) | Verifies every discovered Skill; only `VERIFIED` Skills stay in the list sent to the model. Status bar: `mago-ski enforce: 3/4 Skills verified` | Same check, nothing removed |
|
|
24
|
+
| `/skill:name` (`input`) | Verified: re-verified and expanded from the verified bytes, so Pi never re-reads the file. Unverified: refused, the model is not called. Not seen yet in this session: rewritten into a plain request, so Pi does not expand it unchecked | Allowed |
|
|
25
|
+
| `read`, `find`, `ls`, `edit`, `write` (`tool_call`) | Refused for paths in unverified Skills. Paths are resolved the way Pi's tools resolve them (a leading `@`, `file://` URLs, `~`, Unicode spaces, Windows shell paths, and the filename variants `read` tries), and matched both before and after resolving symlinks. Reads from verified Skills are re-hashed against the certificate; changed files, files not in the certificate, and paths that leave the Skill through a link are refused. Edits to verified Skills are refused. `find` and `ls` of verified Skills are allowed (names only) | Logged |
|
|
26
|
+
| `find`, `ls` over a directory that contains unverified Skills (`tool_result`) | The call runs, but entries that are, or are inside, an unverified Skill are removed from the result before the model sees it (links into one included), and a fixed note says how many were hidden. A line that does not name a real path (part of a file name containing a newline) is removed as well. Verified Skills and everything else stay listed | Result unchanged; logged |
|
|
27
|
+
| `grep` | Refused when it searches in or over an unverified Skill, or a verified Skill that has files excluded from its certificate (`.git`, ignored files, or an in-tree `.mago-ski-cert.json` that is not the certificate that verified the Skill). Otherwise the Skill is re-verified first and its exclusions checked again | Logged |
|
|
28
|
+
| `bash`, `powershell` | Refused when the command names an unverified Skill's directory; verified Skills it names are re-verified first. Best-effort (see the [threat model](../threat-model.md)) | Logged |
|
|
29
|
+
| Verification error | A Skill that cannot be checked is `UNVERIFIABLE`. If checking crashes outright, no Skills are advertised and file and shell tools touching any Skill path are blocked for that run | Logged |
|
|
30
|
+
|
|
31
|
+
Skill paths are shown to the model in Pi's Skill list and in the `/skill:` block, but the certificate does not cover the Skill directory, so a contributor can rename it. Below your working directory and your home directory, a Skill path may therefore use only `A-Z a-z 0-9 . _ / ~ @ + -`. A verified Skill with any other character in its path (spaces, non-ASCII letters, newlines, quotes, angle brackets) is not advertised and `/skill:` refuses it with a notice; rename or move the directory. The working and home directories themselves may contain spaces, but not control, quote or angle-bracket characters. Skill names are already limited to ASCII letters, digits, `.`, `_` and `-`.
|
|
32
|
+
|
|
33
|
+
When a verdict changes, it is written to the policy's `decision_log` (if configured and writable; it must be a regular file inside the policy's directory) with the event, verdict and reason. Verdicts other than `VERIFIED` are also shown as a notification when Pi has a UI.
|
|
34
|
+
|
|
35
|
+
## Limits
|
|
36
|
+
|
|
37
|
+
- Linux and macOS only. On Windows the extension shows a warning: executable bits are not covered, so Skills approved elsewhere may not verify.
|
|
38
|
+
- Standalone Markdown Skills (a `.md` file without its own directory and `SKILL.md`) cannot be certified and are always unverified.
|
|
39
|
+
- Pi runs extensions in load order; another extension can change the Skill list after mago-ski. Load mago-ski last.
|
|
40
|
+
- Custom tools and MCP tools that read files are not checked.
|
|
41
|
+
- Path matching mirrors Pi 1.1's tool path resolution (`hosts/pi/paths.ts`). A future Pi version that resolves paths differently needs a matching update; the Pi host tests pin the Pi version.
|