@mago-foundation/mago-ski 0.1.0 → 0.3.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.
Files changed (91) hide show
  1. package/README.md +54 -9
  2. package/action.yml +2 -2
  3. package/dist/hosts/github/run.d.ts +9 -1
  4. package/dist/hosts/github/run.js +64 -11
  5. package/dist/hosts/pi/index.d.ts +5 -3
  6. package/dist/hosts/pi/index.js +219 -14
  7. package/dist/src/admin.d.ts +140 -1
  8. package/dist/src/admin.js +328 -63
  9. package/dist/src/canonical-json.js +73 -28
  10. package/dist/src/certificate.js +23 -15
  11. package/dist/src/cli.js +590 -89
  12. package/dist/src/diff.d.ts +0 -4
  13. package/dist/src/diff.js +326 -70
  14. package/dist/src/dsse.d.ts +6 -4
  15. package/dist/src/dsse.js +22 -6
  16. package/dist/src/fs-safe.d.ts +32 -3
  17. package/dist/src/fs-safe.js +193 -31
  18. package/dist/src/gate/adapters/claude-code-hook.d.ts +1 -0
  19. package/dist/src/gate/adapters/claude-code-hook.js +4 -0
  20. package/dist/src/gate/adapters/claude-code-settings.d.ts +13 -0
  21. package/dist/src/gate/adapters/claude-code-settings.js +49 -0
  22. package/dist/src/gate/adapters/claude-code.d.ts +33 -0
  23. package/dist/src/gate/adapters/claude-code.js +193 -0
  24. package/dist/src/gate/adapters/client.d.ts +13 -0
  25. package/dist/src/gate/adapters/client.js +57 -0
  26. package/dist/src/gate/adapters/codex-hook.d.ts +1 -0
  27. package/dist/src/gate/adapters/codex-hook.js +5 -0
  28. package/dist/src/gate/adapters/codex-requirements.d.ts +19 -0
  29. package/dist/src/gate/adapters/codex-requirements.js +56 -0
  30. package/dist/src/gate/adapters/codex.d.ts +16 -0
  31. package/dist/src/gate/adapters/codex.js +574 -0
  32. package/dist/src/gate/adapters/hook-main.d.ts +7 -0
  33. package/dist/src/gate/adapters/hook-main.js +35 -0
  34. package/dist/src/gate/adapters/host-pid.d.ts +1 -0
  35. package/dist/src/gate/adapters/host-pid.js +20 -0
  36. package/dist/src/gate/admission.d.ts +54 -0
  37. package/dist/src/gate/admission.js +490 -0
  38. package/dist/src/gate/contract.d.ts +51 -0
  39. package/dist/src/gate/contract.js +94 -0
  40. package/dist/src/gate/decision-log.d.ts +38 -0
  41. package/dist/src/gate/decision-log.js +251 -0
  42. package/dist/src/gate/generations.d.ts +67 -0
  43. package/dist/src/gate/generations.js +518 -0
  44. package/dist/src/gate/proc.d.ts +31 -0
  45. package/dist/src/gate/proc.js +79 -0
  46. package/dist/src/gate/refresh.d.ts +11 -0
  47. package/dist/src/gate/refresh.js +30 -0
  48. package/dist/src/gate/run-verifier.d.ts +23 -0
  49. package/dist/src/gate/run-verifier.js +89 -0
  50. package/dist/src/gate/service.d.ts +80 -0
  51. package/dist/src/gate/service.js +722 -0
  52. package/dist/src/gate/strict-json.d.ts +11 -0
  53. package/dist/src/gate/strict-json.js +190 -0
  54. package/dist/src/gate/trust-store.d.ts +36 -0
  55. package/dist/src/gate/trust-store.js +251 -0
  56. package/dist/src/gate/verifier-worker.d.ts +8 -0
  57. package/dist/src/gate/verifier-worker.js +66 -0
  58. package/dist/src/index.d.ts +2 -0
  59. package/dist/src/index.js +2 -0
  60. package/dist/src/keys.d.ts +4 -0
  61. package/dist/src/keys.js +85 -30
  62. package/dist/src/main-module.js +1 -1
  63. package/dist/src/names.js +2 -4
  64. package/dist/src/policy.d.ts +173 -6
  65. package/dist/src/policy.js +685 -54
  66. package/dist/src/recovery-admin.d.ts +173 -0
  67. package/dist/src/recovery-admin.js +400 -0
  68. package/dist/src/revocations.js +3 -1
  69. package/dist/src/root-events.d.ts +107 -0
  70. package/dist/src/root-events.js +376 -0
  71. package/dist/src/time.d.ts +19 -3
  72. package/dist/src/time.js +50 -8
  73. package/dist/src/timestamp.d.ts +61 -0
  74. package/dist/src/timestamp.js +107 -0
  75. package/dist/src/tree-digest.d.ts +4 -0
  76. package/dist/src/tree-digest.js +41 -16
  77. package/dist/src/trust-root.d.ts +5 -0
  78. package/dist/src/trust-root.js +34 -13
  79. package/dist/src/verify.d.ts +69 -13
  80. package/dist/src/verify.js +309 -61
  81. package/docs/hosts/adapter-interface.md +8 -2
  82. package/docs/hosts/github-action.md +3 -3
  83. package/docs/hosts/pi.md +10 -3
  84. package/docs/spec/gui-admin-rack-v1.md +289 -0
  85. package/docs/spec/root-recovery-v1.md +451 -0
  86. package/docs/spec/skill-certificate-v1.md +1 -1
  87. package/docs/spec/skill-tree-v2.md +2 -2
  88. package/docs/spec/trust-documents-v1.md +12 -6
  89. package/docs/spec/verification.md +20 -3
  90. package/docs/threat-model.md +9 -6
  91. package/package.json +49 -44
@@ -2,33 +2,56 @@
2
2
  // Parsing rejects any byte sequence that is not exactly the canonical form, so
3
3
  // duplicate keys, alternate escapes and reordered fields cannot change meaning.
4
4
  export const MAX_CANONICAL_JSON_BYTES = 4 * 1024 * 1024;
5
+ const MAX_CANONICAL_JSON_NODES = 200_000;
6
+ const MAX_CANONICAL_JSON_DEPTH = 64;
7
+ const DEFAULT_MAX_STRING_LENGTH = 1_024;
8
+ const UTF8_BOM_CHAR_CODE = 0xfeff;
5
9
  const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
10
+ const controlCharPattern = /[\u0000-\u001f\u007f]/u;
11
+ function errorMessage(error) {
12
+ return error instanceof Error ? error.message : String(error);
13
+ }
6
14
  export function isRecord(value) {
7
- return value !== null && typeof value === 'object' && !Array.isArray(value);
15
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
16
+ return false;
17
+ }
18
+ const proto = Object.getPrototypeOf(value);
19
+ return proto === Object.prototype || proto === null;
8
20
  }
9
21
  function canonicalValue(value, depth, state) {
10
22
  state.nodes += 1;
11
- if (state.nodes > 200_000 || depth > 64)
23
+ if (state.nodes > MAX_CANONICAL_JSON_NODES || depth > MAX_CANONICAL_JSON_DEPTH) {
12
24
  throw new Error('Canonical JSON exceeds structural limits');
13
- if (value === null)
25
+ }
26
+ if (value === null) {
14
27
  return 'null';
15
- if (typeof value === 'string')
28
+ }
29
+ if (typeof value === 'string') {
16
30
  return JSON.stringify(value);
17
- if (typeof value === 'boolean')
31
+ }
32
+ if (typeof value === 'boolean') {
18
33
  return value ? 'true' : 'false';
34
+ }
19
35
  if (typeof value === 'number') {
20
- if (!Number.isSafeInteger(value))
36
+ if (!Number.isSafeInteger(value)) {
21
37
  throw new Error('Canonical JSON numbers must be safe integers');
22
- return JSON.stringify(value);
38
+ }
39
+ return Object.is(value, -0) ? '0' : JSON.stringify(value);
23
40
  }
24
41
  if (Array.isArray(value)) {
25
- return `[${value.map((item) => canonicalValue(item, depth + 1, state)).join(',')}]`;
42
+ const elements = value.map((item) => canonicalValue(item, depth + 1, state));
43
+ return `[${elements.join(',')}]`;
26
44
  }
27
45
  if (isRecord(value)) {
28
- const entries = Object.keys(value).sort().map((key) => {
46
+ const sortedKeys = Object.keys(value).sort();
47
+ const entries = sortedKeys.map((key) => {
48
+ if (key === '__proto__') {
49
+ throw new Error('Canonical JSON forbids "__proto__" object keys');
50
+ }
29
51
  const item = value[key];
30
- if (item === undefined)
52
+ if (item === undefined) {
31
53
  throw new Error(`Canonical JSON field ${JSON.stringify(key)} is undefined`);
54
+ }
32
55
  return `${JSON.stringify(key)}:${canonicalValue(item, depth + 1, state)}`;
33
56
  });
34
57
  return `{${entries.join(',')}}`;
@@ -36,64 +59,86 @@ function canonicalValue(value, depth, state) {
36
59
  throw new Error('Canonical JSON contains an unsupported value');
37
60
  }
38
61
  export function serializeCanonicalJson(value) {
39
- return `${canonicalValue(value, 0, { nodes: 0 })}\n`;
62
+ const state = { nodes: 0 };
63
+ return `${canonicalValue(value, 0, state)}\n`;
40
64
  }
41
65
  export function canonicalBytes(value) {
42
66
  return Buffer.from(serializeCanonicalJson(value), 'utf8');
43
67
  }
44
68
  export function parseCanonicalJsonBytes(input, label = 'JSON document') {
45
- const bytes = Buffer.from(input);
46
- if (bytes.length === 0 || bytes.length > MAX_CANONICAL_JSON_BYTES) {
69
+ let text;
70
+ let value;
71
+ let canonical;
72
+ if (!(input instanceof Uint8Array) ||
73
+ input.byteLength === 0 ||
74
+ input.byteLength > MAX_CANONICAL_JSON_BYTES) {
47
75
  throw new Error(`${label} must be between 1 byte and ${MAX_CANONICAL_JSON_BYTES} bytes`);
48
76
  }
49
- let text;
50
77
  try {
51
- text = utf8Decoder.decode(bytes);
78
+ text = utf8Decoder.decode(input);
52
79
  }
53
80
  catch {
54
81
  throw new Error(`${label} is not valid UTF-8`);
55
82
  }
56
- if (text.charCodeAt(0) === 0xfeff || !text.endsWith('\n')) {
83
+ if (text.charCodeAt(0) === UTF8_BOM_CHAR_CODE || !text.endsWith('\n')) {
57
84
  throw new Error(`${label} must be canonical UTF-8 JSON with one trailing LF`);
58
85
  }
59
- let value;
60
86
  try {
61
- value = JSON.parse(text.slice(0, -1));
87
+ value = JSON.parse(text.slice(0, -1), (key, parsedValue) => {
88
+ if (key === '__proto__') {
89
+ throw new Error('__proto__ key is forbidden');
90
+ }
91
+ if (typeof parsedValue === 'number' && Object.is(parsedValue, -0)) {
92
+ throw new Error('negative zero is not canonical');
93
+ }
94
+ return parsedValue;
95
+ });
62
96
  }
63
97
  catch {
64
98
  throw new Error(`${label} is not valid JSON`);
65
99
  }
66
- let canonical;
67
100
  try {
68
101
  canonical = serializeCanonicalJson(value);
69
102
  }
70
103
  catch (error) {
71
- throw new Error(`${label} is structurally invalid: ${error.message}`);
104
+ throw new Error(`${label} is structurally invalid: ${errorMessage(error)}`);
72
105
  }
73
- if (!bytes.equals(Buffer.from(canonical, 'utf8'))) {
106
+ if (canonical !== text) {
74
107
  throw new Error(`${label} is not canonical JSON (duplicate keys and alternate encodings are rejected)`);
75
108
  }
76
109
  return value;
77
110
  }
78
111
  export function exactKeys(value, keys, label) {
79
- if (!isRecord(value))
112
+ if (!isRecord(value)) {
80
113
  throw new Error(`${label} must be a JSON object`);
81
- const actual = Object.keys(value).sort();
82
- const expected = [...keys].sort();
83
- if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
114
+ }
115
+ const actualKeys = Object.keys(value).sort();
116
+ const expectedKeys = [...new Set(keys)].sort();
117
+ if (actualKeys.length !== expectedKeys.length ||
118
+ actualKeys.some((key, index) => key !== expectedKeys[index])) {
84
119
  throw new Error(`${label} has missing or unsupported fields`);
85
120
  }
86
121
  return value;
87
122
  }
88
- export function requireString(value, label, maxLength = 1024) {
89
- if (typeof value !== 'string' || value.length === 0 || value.length > maxLength || /[\u0000-\u001f\u007f]/u.test(value)) {
123
+ export function requireString(value, label, maxLength = DEFAULT_MAX_STRING_LENGTH) {
124
+ if (!Number.isSafeInteger(maxLength) || maxLength < 1) {
125
+ throw new Error(`Invalid maxLength limit for ${label}`);
126
+ }
127
+ if (typeof value !== 'string' ||
128
+ value.length === 0 ||
129
+ value.length > maxLength ||
130
+ controlCharPattern.test(value)) {
90
131
  throw new Error(`${label} must be a non-empty string of at most ${maxLength} characters without control characters`);
91
132
  }
92
133
  return value;
93
134
  }
94
135
  export function requireArray(value, label, maxLength) {
95
- if (!Array.isArray(value) || value.length > maxLength)
136
+ if (!Number.isSafeInteger(maxLength) || maxLength < 0) {
137
+ throw new Error(`Invalid maxLength limit for ${label}`);
138
+ }
139
+ if (!Array.isArray(value) || value.length > maxLength) {
96
140
  throw new Error(`${label} must be an array of at most ${maxLength} items`);
141
+ }
97
142
  return value;
98
143
  }
99
144
  export function requirePositiveInteger(value, label) {
@@ -18,11 +18,19 @@ export function issueCertificate(options) {
18
18
  throw new Error('Certificate must expire after it is issued');
19
19
  const statement = {
20
20
  _type: STATEMENT_TYPE,
21
- subject: [{ name: options.skillName, digest: { [SUBJECT_DIGEST_ALGORITHM]: options.digest.slice('sha256:'.length) } }],
21
+ subject: [{
22
+ name: options.skillName,
23
+ digest: { [SUBJECT_DIGEST_ALGORITHM]: options.digest.slice('sha256:'.length) }
24
+ }],
22
25
  predicateType: CERTIFICATE_PREDICATE_TYPE,
23
26
  predicate: {
24
27
  tree_profile: TREE_PROFILE,
25
- files: options.files.map((file) => ({ path: file.path, size: file.size, sha256: file.sha256, exec: file.exec })),
28
+ files: options.files.map((file) => ({
29
+ path: file.path,
30
+ size: file.size,
31
+ sha256: file.sha256,
32
+ exec: file.exec
33
+ })),
26
34
  approval: {
27
35
  approver_key_id: options.approver.keyId,
28
36
  issued_at: formatTimestamp(options.issuedAt),
@@ -56,37 +64,37 @@ function parseFiles(value, label) {
56
64
  export function parseCertificate(bytes) {
57
65
  const label = 'certificate';
58
66
  const envelope = parseEnvelope(bytes, label);
67
+ const statement = exactKeys(parseCanonicalJsonBytes(envelope.payloadBytes, `${label} statement`), ['_type', 'subject', 'predicateType', 'predicate'], `${label} statement`);
68
+ const subjects = requireArray(statement.subject, `${label} subject`, 1);
69
+ const subject = exactKeys(subjects[0], ['name', 'digest'], `${label} subject`);
70
+ const skillName = validateSkillName(subject.name, `${label} subject name`);
71
+ const digestSet = exactKeys(subject.digest, [SUBJECT_DIGEST_ALGORITHM], `${label} subject digest`);
72
+ const digest = validateDigest(`sha256:${String(digestSet[SUBJECT_DIGEST_ALGORITHM])}`, `${label} subject digest`);
73
+ const predicate = exactKeys(statement.predicate, ['tree_profile', 'files', 'approval', 'previous_digest'], `${label} predicate`);
74
+ const files = parseFiles(predicate.files, `${label} files`);
75
+ const approval = exactKeys(predicate.approval, ['approver_key_id', 'issued_at', 'expires_at', 'reason'], `${label} approval`);
76
+ const approverKeyId = String(approval.approver_key_id);
77
+ const issuedAt = parseTimestamp(approval.issued_at, `${label} issued_at`);
78
+ const expiresAt = parseTimestamp(approval.expires_at, `${label} expires_at`);
79
+ const previous = predicate.previous_digest;
59
80
  if (envelope.payloadType !== STATEMENT_PAYLOAD_TYPE)
60
81
  throw new Error(`${label} has the wrong payload type`);
61
- const statement = exactKeys(parseCanonicalJsonBytes(envelope.payloadBytes, `${label} statement`), ['_type', 'subject', 'predicateType', 'predicate'], `${label} statement`);
62
82
  if (statement._type !== STATEMENT_TYPE)
63
83
  throw new Error(`${label} is not an in-toto v1 statement`);
64
84
  if (statement.predicateType !== CERTIFICATE_PREDICATE_TYPE)
65
85
  throw new Error(`${label} has an unsupported predicateType`);
66
- const subjects = requireArray(statement.subject, `${label} subject`, 1);
67
86
  if (subjects.length !== 1)
68
87
  throw new Error(`${label} must have exactly one subject`);
69
- const subject = exactKeys(subjects[0], ['name', 'digest'], `${label} subject`);
70
- const skillName = validateSkillName(subject.name, `${label} subject name`);
71
- const digestSet = exactKeys(subject.digest, [SUBJECT_DIGEST_ALGORITHM], `${label} subject digest`);
72
- const digest = validateDigest(`sha256:${String(digestSet[SUBJECT_DIGEST_ALGORITHM])}`, `${label} subject digest`);
73
- const predicate = exactKeys(statement.predicate, ['tree_profile', 'files', 'approval', 'previous_digest'], `${label} predicate`);
74
88
  if (predicate.tree_profile !== TREE_PROFILE)
75
89
  throw new Error(`${label} uses unsupported tree profile ${String(predicate.tree_profile)}`);
76
- const files = parseFiles(predicate.files, `${label} files`);
77
90
  if (!files.some((file) => file.path === SKILL_FILENAME))
78
91
  throw new Error(`${label} file list does not include ${SKILL_FILENAME}`);
79
92
  if (digestFileList(files) !== digest)
80
93
  throw new Error(`${label} file list does not match its subject digest`);
81
- const approval = exactKeys(predicate.approval, ['approver_key_id', 'issued_at', 'expires_at', 'reason'], `${label} approval`);
82
- const approverKeyId = String(approval.approver_key_id);
83
94
  if (approverKeyId !== envelope.signatures[0].keyid)
84
95
  throw new Error(`${label} approver_key_id does not match the signing key`);
85
- const issuedAt = parseTimestamp(approval.issued_at, `${label} issued_at`);
86
- const expiresAt = parseTimestamp(approval.expires_at, `${label} expires_at`);
87
96
  if (expiresAt <= issuedAt)
88
97
  throw new Error(`${label} expires before it is issued`);
89
- const previous = predicate.previous_digest;
90
98
  if (previous !== null && isRecord(previous))
91
99
  throw new Error(`${label} previous_digest is invalid`);
92
100
  return {