@mago-foundation/mago-ski 0.1.0 → 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/README.md +25 -7
- package/action.yml +2 -2
- package/dist/hosts/github/run.d.ts +6 -0
- package/dist/hosts/github/run.js +38 -5
- package/dist/hosts/pi/index.d.ts +5 -3
- package/dist/hosts/pi/index.js +171 -9
- package/dist/src/admin.d.ts +4 -0
- package/dist/src/admin.js +178 -50
- package/dist/src/canonical-json.js +73 -28
- package/dist/src/certificate.js +23 -15
- package/dist/src/cli.js +256 -80
- package/dist/src/diff.d.ts +0 -4
- package/dist/src/diff.js +326 -70
- package/dist/src/dsse.d.ts +6 -4
- package/dist/src/dsse.js +22 -6
- package/dist/src/fs-safe.d.ts +17 -1
- package/dist/src/fs-safe.js +162 -29
- package/dist/src/keys.js +81 -35
- package/dist/src/main-module.js +1 -1
- package/dist/src/names.js +2 -4
- package/dist/src/policy.d.ts +28 -6
- package/dist/src/policy.js +134 -22
- package/dist/src/revocations.js +3 -1
- package/dist/src/time.d.ts +19 -3
- package/dist/src/time.js +50 -8
- package/dist/src/tree-digest.d.ts +3 -0
- package/dist/src/tree-digest.js +40 -15
- package/dist/src/trust-root.d.ts +5 -0
- package/dist/src/trust-root.js +34 -13
- package/dist/src/verify.d.ts +36 -13
- package/dist/src/verify.js +307 -61
- package/docs/hosts/github-action.md +3 -3
- package/docs/hosts/pi.md +8 -3
- package/docs/spec/root-recovery-v1.md +430 -0
- package/docs/spec/skill-certificate-v1.md +1 -1
- package/docs/spec/skill-tree-v2.md +2 -2
- package/docs/spec/trust-documents-v1.md +5 -5
- package/docs/spec/verification.md +11 -1
- package/docs/threat-model.md +6 -5
- package/package.json +47 -44
package/dist/src/diff.js
CHANGED
|
@@ -1,14 +1,54 @@
|
|
|
1
|
-
import { collectSkillTree } from "./tree-digest.js";
|
|
1
|
+
import { collectSkillTree, compareTreePaths, sha256Hex } from "./tree-digest.js";
|
|
2
2
|
const textDecoder = new TextDecoder('utf-8', { fatal: true });
|
|
3
3
|
const MAX_ANALYZED_TEXT_BYTES = 2 * 1024 * 1024;
|
|
4
|
+
/** Before + after bytes, summed over every file: per-file limits alone allow ~500 MiB of parsing. */
|
|
5
|
+
const MAX_SECTION_DIFF_BYTES = 16 * 1024 * 1024;
|
|
4
6
|
const MAX_REPORTED_FILES = 200;
|
|
5
7
|
const MAX_REPORTED_SECTIONS = 32;
|
|
8
|
+
const MAX_INDEXED_SECTIONS = 4_096;
|
|
6
9
|
const MAX_ORIGINS = 128;
|
|
10
|
+
/** Per tree. Each candidate costs a URL parse, and a 256 MiB tree can hold tens of millions. */
|
|
11
|
+
const MAX_URL_CANDIDATES = 100_000;
|
|
12
|
+
/** "https://", a 254-char DNS name (trailing dot included), ":65535". The URL parser accepts longer hosts. */
|
|
13
|
+
const MAX_ORIGIN_CHARS = 8 + 254 + 6;
|
|
14
|
+
const MAX_HEADING_LEVEL = 6;
|
|
15
|
+
/** One Markdown line can be megabytes long; reported labels must not be. */
|
|
16
|
+
const MAX_LABEL_TITLE_CHARS = 200;
|
|
17
|
+
const LABEL_DOCUMENT_BODY = '(document body)';
|
|
18
|
+
const LABEL_EMPTY_HEADING = '(empty heading)';
|
|
19
|
+
const LABEL_FILE_ADDED = '(file added)';
|
|
20
|
+
const LABEL_FILE_REMOVED = '(file removed)';
|
|
21
|
+
const LABEL_WHOLE_FILE = '(whole file)';
|
|
22
|
+
const LABEL_SIZE_LIMIT = '(whole file; size limit)';
|
|
23
|
+
const LABEL_TOTAL_SIZE_LIMIT = '(whole file; total size limit)';
|
|
24
|
+
const LABEL_NON_TEXT = '(whole file; non-text content)';
|
|
25
|
+
const LABEL_EXEC_BIT = '(executable bit)';
|
|
26
|
+
// Every pattern below runs on untrusted Skill content: keep each one free of overlapping quantifiers.
|
|
7
27
|
const urlPattern = /https?:\/\/[^\s"'`<>()[\]{}]+/giu;
|
|
28
|
+
const trailingPunctuation = '.,;:!?';
|
|
29
|
+
const fenceOpenPattern = /^ {0,3}(`{3,}|~{3,})/u;
|
|
30
|
+
const fenceClosePattern = /^ {0,3}(`{3,}|~{3,})\s*$/u;
|
|
31
|
+
const atxOpenPattern = /^ {0,3}(#{1,6})([ \t]+)/u;
|
|
32
|
+
const whitespacePattern = /\s/u;
|
|
33
|
+
/** What `.` refuses besides CR and LF, which never reach a line. */
|
|
34
|
+
const dotExcludedPattern = /[\u2028\u2029]/u;
|
|
35
|
+
// Sticky: a failed match must not rescan from every later offset (a lone CR made that quadratic).
|
|
36
|
+
const linePattern = /[^\r\n]*(?:\r\n|\r|\n|$)/uy;
|
|
37
|
+
const lineEndingPattern = /(?:\r\n|\r|\n)$/u;
|
|
38
|
+
const crlfPattern = /\r\n/gu;
|
|
39
|
+
/** Drops trailing sentence punctuation. A loop, because /[.,;:!?]+$/ backtracks quadratically on a long run. */
|
|
40
|
+
function trimTrailingPunctuation(value) {
|
|
41
|
+
let end = value.length;
|
|
42
|
+
while (end > 0 && trailingPunctuation.includes(value[end - 1])) {
|
|
43
|
+
end -= 1;
|
|
44
|
+
}
|
|
45
|
+
return value.slice(0, end);
|
|
46
|
+
}
|
|
8
47
|
function collectOrigins(contents) {
|
|
9
48
|
const origins = new Set();
|
|
10
49
|
let complete = true;
|
|
11
|
-
|
|
50
|
+
let candidates = 0;
|
|
51
|
+
scan: for (const content of contents.values()) {
|
|
12
52
|
if (content.length > MAX_ANALYZED_TEXT_BYTES || content.includes(0)) {
|
|
13
53
|
complete = false;
|
|
14
54
|
continue;
|
|
@@ -22,99 +62,312 @@ function collectOrigins(contents) {
|
|
|
22
62
|
continue;
|
|
23
63
|
}
|
|
24
64
|
for (const match of text.matchAll(urlPattern)) {
|
|
65
|
+
candidates += 1;
|
|
66
|
+
if (candidates > MAX_URL_CANDIDATES) {
|
|
67
|
+
complete = false;
|
|
68
|
+
break scan;
|
|
69
|
+
}
|
|
70
|
+
let origin;
|
|
25
71
|
try {
|
|
26
|
-
const parsed = new URL(match[0]
|
|
72
|
+
const parsed = new URL(trimTrailingPunctuation(match[0]));
|
|
27
73
|
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:')
|
|
28
74
|
continue;
|
|
29
|
-
|
|
30
|
-
if (origins.has(origin))
|
|
31
|
-
continue;
|
|
32
|
-
if (origins.size >= MAX_ORIGINS) {
|
|
33
|
-
complete = false;
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
origins.add(origin);
|
|
75
|
+
origin = parsed.origin.toLowerCase();
|
|
37
76
|
}
|
|
38
77
|
catch {
|
|
39
78
|
// URL-like text that does not parse is not capability evidence.
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
// No DNS name resolves past this length, and one such origin could be megabytes of report.
|
|
82
|
+
if (origin.length > MAX_ORIGIN_CHARS) {
|
|
83
|
+
complete = false;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (origins.has(origin))
|
|
87
|
+
continue;
|
|
88
|
+
// A full set cannot change any more, so scanning on would only burn CPU.
|
|
89
|
+
if (origins.size >= MAX_ORIGINS) {
|
|
90
|
+
complete = false;
|
|
91
|
+
break scan;
|
|
40
92
|
}
|
|
93
|
+
origins.add(origin);
|
|
41
94
|
}
|
|
42
95
|
}
|
|
43
|
-
return {
|
|
96
|
+
return {
|
|
97
|
+
origins: [...origins].sort(),
|
|
98
|
+
complete
|
|
99
|
+
};
|
|
44
100
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
101
|
+
/* ******************************************** */
|
|
102
|
+
/* Markdown Parsing */
|
|
103
|
+
/* ******************************************** */
|
|
104
|
+
/**
|
|
105
|
+
* Linear-time equivalent of /^ {0,3}(#{1,6})[ \t]+(.+?)\s*#*\s*$/u. That regex lets its lazy title
|
|
106
|
+
* overlap the optional closing sequence, and backtracks cubically on a long run of blanks.
|
|
107
|
+
*/
|
|
108
|
+
function atxHeading(line) {
|
|
109
|
+
const open = atxOpenPattern.exec(line);
|
|
110
|
+
if (!open)
|
|
51
111
|
return null;
|
|
112
|
+
const level = open[1].length;
|
|
113
|
+
const spacing = open[2].length;
|
|
114
|
+
const rest = line.slice(open[0].length);
|
|
115
|
+
// Walk back over the closing sequence the title leaves out: blanks, then '#'s, then blanks again.
|
|
116
|
+
let end = rest.length;
|
|
117
|
+
while (end > 0 && whitespacePattern.test(rest[end - 1]))
|
|
118
|
+
end -= 1;
|
|
119
|
+
while (end > 0 && rest[end - 1] === '#')
|
|
120
|
+
end -= 1;
|
|
121
|
+
while (end > 0 && whitespacePattern.test(rest[end - 1]))
|
|
122
|
+
end -= 1;
|
|
123
|
+
// The title needs one character `.` accepts; failing that, the regex borrows one blank from the spacing.
|
|
124
|
+
if (end === 0 && (rest === '' || dotExcludedPattern.test(rest[0]))) {
|
|
125
|
+
return spacing >= 2 ? { level, title: '' } : null;
|
|
126
|
+
}
|
|
127
|
+
const title = rest.slice(0, Math.max(1, end));
|
|
128
|
+
return dotExcludedPattern.test(title) ? null : { level, title: title.trim() };
|
|
129
|
+
}
|
|
130
|
+
function markdownSections(input) {
|
|
131
|
+
const sections = [];
|
|
132
|
+
let text;
|
|
133
|
+
let currentLevel = 0;
|
|
134
|
+
let currentTitle = '';
|
|
135
|
+
let sectionStart = 0;
|
|
136
|
+
let activeFence = null;
|
|
137
|
+
if (typeof input === 'string')
|
|
138
|
+
text = input;
|
|
139
|
+
else {
|
|
140
|
+
try {
|
|
141
|
+
text = textDecoder.decode(input);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
52
146
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
147
|
+
linePattern.lastIndex = 0;
|
|
148
|
+
while (linePattern.lastIndex < text.length) {
|
|
149
|
+
const lineStart = linePattern.lastIndex;
|
|
150
|
+
const match = linePattern.exec(text);
|
|
151
|
+
if (!match || match[0].length === 0)
|
|
152
|
+
break;
|
|
153
|
+
const line = match[0].replace(lineEndingPattern, '');
|
|
154
|
+
// 1. Track fenced code blocks (``` or ~~~) so '#' comments inside code are never mistaken for headings.
|
|
155
|
+
if (activeFence !== null) {
|
|
156
|
+
const close = fenceClosePattern.exec(line);
|
|
157
|
+
if (close && close[1][0] === activeFence.char && close[1].length >= activeFence.minLength) {
|
|
158
|
+
activeFence = null;
|
|
159
|
+
}
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const fenceMatch = fenceOpenPattern.exec(line);
|
|
163
|
+
if (fenceMatch) {
|
|
164
|
+
const fenceToken = fenceMatch[1];
|
|
165
|
+
// In CommonMark, backtick info strings cannot contain backticks (inline code on one line).
|
|
166
|
+
if (fenceToken[0] !== '`' || !line.slice(fenceMatch[0].length).includes('`')) {
|
|
167
|
+
activeFence = { char: fenceToken[0], minLength: fenceToken.length };
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
67
170
|
}
|
|
68
|
-
|
|
69
|
-
|
|
171
|
+
// 2. Outside code blocks, check for ATX headings (# through ######).
|
|
172
|
+
const heading = atxHeading(line);
|
|
173
|
+
if (!heading) {
|
|
174
|
+
continue;
|
|
70
175
|
}
|
|
176
|
+
if (lineStart > sectionStart || currentLevel > 0) {
|
|
177
|
+
sections.push({
|
|
178
|
+
level: currentLevel,
|
|
179
|
+
title: currentTitle,
|
|
180
|
+
content: text.slice(sectionStart, lineStart),
|
|
181
|
+
});
|
|
182
|
+
// indexSectionsByKey keeps only MAX_INDEXED_SECTIONS; one more is enough for it to report truncation.
|
|
183
|
+
if (sections.length > MAX_INDEXED_SECTIONS) {
|
|
184
|
+
return sections;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
currentLevel = heading.level;
|
|
188
|
+
currentTitle = heading.title;
|
|
189
|
+
sectionStart = lineStart;
|
|
190
|
+
}
|
|
191
|
+
if (text.length > sectionStart || currentLevel > 0) {
|
|
192
|
+
sections.push({
|
|
193
|
+
level: currentLevel,
|
|
194
|
+
title: currentTitle,
|
|
195
|
+
content: text.slice(sectionStart),
|
|
196
|
+
});
|
|
71
197
|
}
|
|
72
|
-
flush();
|
|
73
198
|
return sections;
|
|
74
199
|
}
|
|
75
|
-
function
|
|
200
|
+
function clampHeadingLevel(level) {
|
|
201
|
+
if (!Number.isFinite(level) || level <= 0) {
|
|
202
|
+
return 0;
|
|
203
|
+
}
|
|
204
|
+
return Math.min(MAX_HEADING_LEVEL, Math.floor(level));
|
|
205
|
+
}
|
|
206
|
+
function truncateTitle(title) {
|
|
207
|
+
if (title.length <= MAX_LABEL_TITLE_CHARS) {
|
|
208
|
+
return title;
|
|
209
|
+
}
|
|
210
|
+
let end = MAX_LABEL_TITLE_CHARS;
|
|
211
|
+
const last = title.charCodeAt(end - 1);
|
|
212
|
+
// Never split a surrogate pair.
|
|
213
|
+
if (last >= 0xd800 && last <= 0xdbff) {
|
|
214
|
+
end -= 1;
|
|
215
|
+
}
|
|
216
|
+
return `${title.slice(0, end)}...`;
|
|
217
|
+
}
|
|
218
|
+
function formatSectionLabel(section, occurrence) {
|
|
219
|
+
const safeLevel = clampHeadingLevel(section.level);
|
|
220
|
+
const safeTitle = truncateTitle(section.title.trim()) || LABEL_EMPTY_HEADING;
|
|
221
|
+
const baseLabel = safeLevel === 0
|
|
222
|
+
? LABEL_DOCUMENT_BODY
|
|
223
|
+
: `${'#'.repeat(safeLevel)} ${safeTitle}`;
|
|
224
|
+
return !Number.isFinite(occurrence) || occurrence <= 1
|
|
225
|
+
? baseLabel
|
|
226
|
+
: `${baseLabel} (${Math.floor(occurrence)})`;
|
|
227
|
+
}
|
|
228
|
+
/* ******************************************** */
|
|
229
|
+
/* Section Indexing */
|
|
230
|
+
/* ******************************************** */
|
|
231
|
+
function indexSectionsByKey(sections) {
|
|
232
|
+
const indexed = new Map();
|
|
233
|
+
const counts = new Map();
|
|
234
|
+
let truncated = false;
|
|
235
|
+
if (!Array.isArray(sections)) {
|
|
236
|
+
return { indexed, truncated };
|
|
237
|
+
}
|
|
238
|
+
for (const section of sections) {
|
|
239
|
+
if (!section || typeof section !== 'object')
|
|
240
|
+
continue;
|
|
241
|
+
if (indexed.size >= MAX_INDEXED_SECTIONS) {
|
|
242
|
+
truncated = true;
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
const safeLevel = clampHeadingLevel(section.level);
|
|
246
|
+
const rawTitle = typeof section.title === 'string' ? section.title : '';
|
|
247
|
+
const rawContent = typeof section.content === 'string' ? section.content : '';
|
|
248
|
+
const safeTitle = rawTitle.trim().normalize('NFC');
|
|
249
|
+
// A fixed-size key: V8 hashes strings over 16,383 chars by length alone, so raw long titles would all
|
|
250
|
+
// land in one Map bucket. The hex digest also has no delimiter for a crafted title to collide with.
|
|
251
|
+
const baseKey = `${safeLevel}:${sha256Hex(Buffer.from(safeTitle, 'utf8'))}`;
|
|
252
|
+
const occurrence = (counts.get(baseKey) ?? 0) + 1;
|
|
253
|
+
counts.set(baseKey, occurrence);
|
|
254
|
+
const uniqueKey = `${occurrence}:${baseKey}`;
|
|
255
|
+
indexed.set(uniqueKey, {
|
|
256
|
+
label: formatSectionLabel({ level: safeLevel, title: safeTitle, content: rawContent }, occurrence),
|
|
257
|
+
content: rawContent.replace(crlfPattern, '\n'),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return { indexed, truncated };
|
|
261
|
+
}
|
|
262
|
+
function changedSections(filePath, before, after, budget) {
|
|
76
263
|
if (!before || !after)
|
|
77
|
-
return { sections: [before ?
|
|
78
|
-
if (
|
|
264
|
+
return { sections: [before ? LABEL_FILE_REMOVED : LABEL_FILE_ADDED], truncated: false };
|
|
265
|
+
if (Buffer.compare(before, after) === 0)
|
|
79
266
|
return { sections: [], truncated: false };
|
|
80
267
|
if (!/\.md$/iu.test(filePath))
|
|
81
|
-
return { sections: [
|
|
82
|
-
if (before.
|
|
83
|
-
return { sections: [
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
268
|
+
return { sections: [LABEL_WHOLE_FILE], truncated: false };
|
|
269
|
+
if (before.byteLength > MAX_ANALYZED_TEXT_BYTES || after.byteLength > MAX_ANALYZED_TEXT_BYTES)
|
|
270
|
+
return { sections: [LABEL_SIZE_LIMIT], truncated: true };
|
|
271
|
+
const cost = before.byteLength + after.byteLength;
|
|
272
|
+
if (cost > budget.remainingBytes)
|
|
273
|
+
return { sections: [LABEL_TOTAL_SIZE_LIMIT], truncated: true };
|
|
274
|
+
budget.remainingBytes -= cost;
|
|
275
|
+
const beforeParsed = markdownSections(before);
|
|
276
|
+
const afterParsed = markdownSections(after);
|
|
277
|
+
if (!beforeParsed || !afterParsed)
|
|
278
|
+
return { sections: [LABEL_NON_TEXT], truncated: false };
|
|
279
|
+
const beforeResult = indexSectionsByKey(beforeParsed);
|
|
280
|
+
const afterResult = indexSectionsByKey(afterParsed);
|
|
281
|
+
const beforeMap = beforeResult.indexed;
|
|
282
|
+
const afterMap = afterResult.indexed;
|
|
283
|
+
const allKeys = new Set([...beforeMap.keys(), ...afterMap.keys()]);
|
|
284
|
+
const changed = [];
|
|
285
|
+
for (const key of allKeys) {
|
|
286
|
+
const beforeEntry = beforeMap.get(key);
|
|
287
|
+
const afterEntry = afterMap.get(key);
|
|
288
|
+
if (beforeEntry?.content !== afterEntry?.content) {
|
|
289
|
+
changed.push((afterEntry ?? beforeEntry).label);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// If bytes differed (e.g. CRLF vs LF) while normalized section contents matched, still report whole-file change.
|
|
293
|
+
if (changed.length === 0) {
|
|
294
|
+
return {
|
|
295
|
+
sections: [LABEL_WHOLE_FILE],
|
|
296
|
+
truncated: beforeResult.truncated || afterResult.truncated,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
changed.sort();
|
|
300
|
+
return {
|
|
301
|
+
sections: changed.slice(0, MAX_REPORTED_SECTIONS),
|
|
302
|
+
truncated: beforeResult.truncated ||
|
|
303
|
+
afterResult.truncated ||
|
|
304
|
+
changed.length > MAX_REPORTED_SECTIONS,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function classifyFileChange(before, after) {
|
|
308
|
+
if (!before) {
|
|
309
|
+
return 'added';
|
|
310
|
+
}
|
|
311
|
+
if (!after) {
|
|
312
|
+
return 'deleted';
|
|
313
|
+
}
|
|
314
|
+
if (before.sha256 === after.sha256) {
|
|
315
|
+
return 'mode-changed';
|
|
316
|
+
}
|
|
317
|
+
return 'modified';
|
|
91
318
|
}
|
|
92
319
|
function fileChanges(beforeFiles, afterFiles, beforeContents, afterContents) {
|
|
93
|
-
const beforeByPath = new Map(
|
|
94
|
-
const afterByPath = new Map(
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
320
|
+
const beforeByPath = new Map();
|
|
321
|
+
const afterByPath = new Map();
|
|
322
|
+
const pathSet = new Set();
|
|
323
|
+
const files = [];
|
|
324
|
+
const sectionBudget = { remainingBytes: MAX_SECTION_DIFF_BYTES };
|
|
325
|
+
let truncated = false;
|
|
326
|
+
for (const file of beforeFiles) {
|
|
327
|
+
beforeByPath.set(file.path, file);
|
|
328
|
+
pathSet.add(file.path);
|
|
329
|
+
}
|
|
330
|
+
for (const file of afterFiles) {
|
|
331
|
+
afterByPath.set(file.path, file);
|
|
332
|
+
pathSet.add(file.path);
|
|
333
|
+
}
|
|
334
|
+
const sortedPaths = [...pathSet].sort(compareTreePaths);
|
|
335
|
+
for (const filePath of sortedPaths) {
|
|
98
336
|
const before = beforeByPath.get(filePath);
|
|
99
337
|
const after = afterByPath.get(filePath);
|
|
100
|
-
if (before &&
|
|
338
|
+
if (before &&
|
|
339
|
+
after &&
|
|
340
|
+
before.sha256 === after.sha256 &&
|
|
341
|
+
before.size === after.size &&
|
|
342
|
+
before.exec === after.exec) {
|
|
101
343
|
continue;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
344
|
+
}
|
|
345
|
+
// Stop expensive Markdown section diffing once the reported file budget is reached.
|
|
346
|
+
if (files.length >= MAX_REPORTED_FILES) {
|
|
347
|
+
truncated = true;
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
const change = classifyFileChange(before, after);
|
|
351
|
+
const sectionDiff = change === 'mode-changed'
|
|
352
|
+
? { sections: [LABEL_EXEC_BIT], truncated: false }
|
|
353
|
+
: changedSections(filePath, beforeContents.get(filePath), afterContents.get(filePath), sectionBudget);
|
|
354
|
+
if (sectionDiff.truncated) {
|
|
355
|
+
truncated = true;
|
|
356
|
+
}
|
|
357
|
+
files.push({
|
|
105
358
|
path: filePath,
|
|
106
359
|
change,
|
|
107
360
|
before_bytes: before?.size ?? 0,
|
|
108
361
|
after_bytes: after?.size ?? 0,
|
|
109
362
|
exec_before: before?.exec ?? null,
|
|
110
363
|
exec_after: after?.exec ?? null,
|
|
111
|
-
changed_sections:
|
|
112
|
-
sections_truncated:
|
|
364
|
+
changed_sections: sectionDiff.sections,
|
|
365
|
+
sections_truncated: sectionDiff.truncated,
|
|
113
366
|
});
|
|
114
367
|
}
|
|
115
|
-
return
|
|
368
|
+
return { files, truncated };
|
|
116
369
|
}
|
|
117
|
-
|
|
370
|
+
/*
|
|
118
371
|
* Compares two Skill directories. Any digest difference requires a new certificate,
|
|
119
372
|
* whatever the capability evidence says: unchanged origins never waive a content change.
|
|
120
373
|
*/
|
|
@@ -122,13 +375,16 @@ export async function diffSkillTrees(baseRoot, candidateRoot) {
|
|
|
122
375
|
const base = await collectSkillTree(baseRoot, { includeContents: true });
|
|
123
376
|
const candidate = await collectSkillTree(candidateRoot, { includeContents: true });
|
|
124
377
|
const changed = base.digest !== candidate.digest;
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
378
|
+
const diff = changed
|
|
379
|
+
? fileChanges(base.files, candidate.files, base.contents, candidate.contents)
|
|
380
|
+
: { files: [], truncated: false };
|
|
128
381
|
const baseOrigins = collectOrigins(base.contents);
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
const
|
|
382
|
+
// Equal digests mean equal contents: a second scan could only repeat the first.
|
|
383
|
+
const candidateOrigins = changed ? collectOrigins(candidate.contents) : baseOrigins;
|
|
384
|
+
const baseOriginSet = new Set(baseOrigins.origins);
|
|
385
|
+
const candidateOriginSet = new Set(candidateOrigins.origins);
|
|
386
|
+
const added = candidateOrigins.origins.filter((origin) => !baseOriginSet.has(origin));
|
|
387
|
+
const removed = baseOrigins.origins.filter((origin) => !candidateOriginSet.has(origin));
|
|
132
388
|
const capabilityChanged = added.length > 0 || removed.length > 0;
|
|
133
389
|
return {
|
|
134
390
|
result: !changed ? 'UNCHANGED' : capabilityChanged ? 'CAPABILITY_CHANGED' : 'CONTENT_CHANGED',
|
|
@@ -137,12 +393,12 @@ export async function diffSkillTrees(baseRoot, candidateRoot) {
|
|
|
137
393
|
candidate_digest: candidate.digest,
|
|
138
394
|
capability_evidence: {
|
|
139
395
|
profile: 'mago.network-origin-scan/v1',
|
|
140
|
-
analysis_complete: baseOrigins.complete && candidateOrigins.complete && !truncated,
|
|
396
|
+
analysis_complete: baseOrigins.complete && candidateOrigins.complete && !diff.truncated,
|
|
141
397
|
added_network_origins: added,
|
|
142
398
|
removed_network_origins: removed,
|
|
143
399
|
note: 'Partial evidence: only HTTP(S) origins in text files are detected. No detected change does not make changed content safe or approved.',
|
|
144
400
|
},
|
|
145
|
-
diff_truncated: truncated,
|
|
146
|
-
files:
|
|
401
|
+
diff_truncated: diff.truncated,
|
|
402
|
+
files: diff.files,
|
|
147
403
|
};
|
|
148
404
|
}
|
package/dist/src/dsse.d.ts
CHANGED
|
@@ -2,10 +2,12 @@ import { type KeyObject } from 'node:crypto';
|
|
|
2
2
|
export interface Envelope {
|
|
3
3
|
payloadType: string;
|
|
4
4
|
payload: string;
|
|
5
|
-
signatures: [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
signatures: [
|
|
6
|
+
{
|
|
7
|
+
keyid: string;
|
|
8
|
+
sig: string;
|
|
9
|
+
}
|
|
10
|
+
];
|
|
9
11
|
}
|
|
10
12
|
/** Pre-Authentication Encoding: "DSSEv1" SP LEN(type) SP type SP LEN(body) SP body, lengths in bytes. */
|
|
11
13
|
export declare function preAuthEncoding(payloadType: string, payload: Uint8Array): Buffer;
|
package/dist/src/dsse.js
CHANGED
|
@@ -8,13 +8,22 @@ const BASE64_PATTERN = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/
|
|
|
8
8
|
export function preAuthEncoding(payloadType, payload) {
|
|
9
9
|
const type = Buffer.from(payloadType, 'utf8');
|
|
10
10
|
return Buffer.concat([
|
|
11
|
-
Buffer.from(`DSSEv1 ${type.length} `, 'ascii'),
|
|
12
|
-
|
|
11
|
+
Buffer.from(`DSSEv1 ${type.length} `, 'ascii'),
|
|
12
|
+
type,
|
|
13
|
+
Buffer.from(` ${payload.length} `, 'ascii'),
|
|
14
|
+
Buffer.from(payload),
|
|
13
15
|
]);
|
|
14
16
|
}
|
|
15
17
|
export function signEnvelope(payloadType, payload, keyId, privateKey) {
|
|
16
18
|
const sig = sign(null, preAuthEncoding(payloadType, payload), privateKey).toString('base64');
|
|
17
|
-
return {
|
|
19
|
+
return {
|
|
20
|
+
payloadType,
|
|
21
|
+
payload: Buffer.from(payload).toString('base64'),
|
|
22
|
+
signatures: [{
|
|
23
|
+
keyid: keyId,
|
|
24
|
+
sig
|
|
25
|
+
}]
|
|
26
|
+
};
|
|
18
27
|
}
|
|
19
28
|
export function envelopeBytes(envelope) {
|
|
20
29
|
return canonicalBytes(envelope);
|
|
@@ -34,15 +43,22 @@ export function parseEnvelope(bytes, label) {
|
|
|
34
43
|
const payloadType = requireString(value.payloadType, `${label} payloadType`, 256);
|
|
35
44
|
const payloadBytes = decodeBase64(value.payload, `${label} payload`, 6 * 1024 * 1024);
|
|
36
45
|
const signatures = requireArray(value.signatures, `${label} signatures`, 1);
|
|
37
|
-
if (signatures.length !== 1)
|
|
38
|
-
throw new Error(`${label} must carry exactly one signature`);
|
|
39
46
|
const signature = exactKeys(signatures[0], ['keyid', 'sig'], `${label} signature`);
|
|
40
47
|
const keyid = validateKeyId(signature.keyid, `${label} signature keyid`);
|
|
41
48
|
const sigBytes = decodeBase64(signature.sig, `${label} signature`, 128);
|
|
49
|
+
if (signatures.length !== 1)
|
|
50
|
+
throw new Error(`${label} must carry exactly one signature`);
|
|
42
51
|
if (sigBytes.length !== 64)
|
|
43
52
|
throw new Error(`${label} signature must be a 64-byte Ed25519 signature`);
|
|
44
53
|
return {
|
|
45
|
-
payloadType,
|
|
54
|
+
payloadType,
|
|
55
|
+
payload: value.payload,
|
|
56
|
+
signatures: [{
|
|
57
|
+
keyid,
|
|
58
|
+
sig: signature.sig
|
|
59
|
+
}],
|
|
60
|
+
payloadBytes,
|
|
61
|
+
sigBytes,
|
|
46
62
|
};
|
|
47
63
|
}
|
|
48
64
|
export function verifyEnvelopeSignature(envelope, publicKey) {
|
package/dist/src/fs-safe.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export declare function isInside(root: string, candidate: string): boolean;
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Reads a regular, non-symlink file, failing if it changes while being read.
|
|
4
|
+
* The type and size are checked before any content is read, and at most `maxBytes` (+1 to detect growth) are read.
|
|
5
|
+
*/
|
|
3
6
|
export declare function readRegularFile(filePath: string, label: string, maxBytes?: number): Promise<Buffer>;
|
|
4
7
|
/** Resolves a file path and requires it to be outside `root` (both canonicalized). */
|
|
5
8
|
export declare function resolveOutside(filePath: string, root: string, label: string, { allowMissing }?: {
|
|
@@ -9,3 +12,16 @@ export declare function resolveOutside(filePath: string, root: string, label: st
|
|
|
9
12
|
export declare function writeNewFile(filePath: string, bytes: Uint8Array, mode?: number): Promise<string>;
|
|
10
13
|
/** Writes via a temporary sibling and rename, so readers see the old or the new file, never a partial one. */
|
|
11
14
|
export declare function writeFileAtomic(filePath: string, bytes: Uint8Array, mode?: number): Promise<string>;
|
|
15
|
+
/** Canonical path of `target`, resolving symlinks on the longest existing prefix. A dangling link on the way is an error. */
|
|
16
|
+
export declare function realpathAllowingMissing(target: string): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Resolves a policy-supplied path against `base` and requires it to name something strictly inside `base`,
|
|
19
|
+
* after symlinks in its parent directories are resolved. An existing final component must be a regular,
|
|
20
|
+
* non-symlink file. Returns the canonical path.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveInsideDirectory(base: string, item: string, label: string): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Appends text to a log file, creating it (mode 600) if needed. Refuses symbolic links and anything that is not
|
|
25
|
+
* a regular file; O_NONBLOCK keeps a FIFO from hanging the open.
|
|
26
|
+
*/
|
|
27
|
+
export declare function appendToRegularFile(filePath: string, text: string, label: string, mode?: number): Promise<void>;
|