@pomentum/syrmos-hub-kit 0.0.0-stage → 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.
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +23 -2
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/check.mjs +17006 -0
- package/kit.json +15 -0
- package/package.json +32 -4
- package/verify.mjs +271 -0
package/kit.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pomentum/syrmos-hub-kit",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"rulesCommit": "70a6d2bb80b249fe9b89d2c906a1b701933fea57",
|
|
5
|
+
"publicSuffixList": "2026-11-27_08-41-04_UTC",
|
|
6
|
+
"files": {
|
|
7
|
+
"LICENSE": "sha256:c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4",
|
|
8
|
+
"NOTICE": "sha256:bfd99454b732f13f9d5314d43efced39a1d753ad45c09ec587135f3b843dec43",
|
|
9
|
+
"README.md": "sha256:cff083033709eab8d3abc5bc3a8986b645460c6e518006492797de7350767dce",
|
|
10
|
+
"THIRD_PARTY_NOTICES.md": "sha256:c5a7e3b53f85ae8d1e50110fea3daf51e76f70a8022b9f2c570815e65c567bf1",
|
|
11
|
+
"check.mjs": "sha256:07e082ed8aa6c6075b3c87d93c9ae9b6636fd48db21fa31d2062e252d894fa97",
|
|
12
|
+
"package.json": "sha256:564e50b5565e4095211dc0b62bea7285d33c4102345148c252dd87702a15760d",
|
|
13
|
+
"verify.mjs": "sha256:bda0206acf231015d773a816d8baef0f294a1370b69969bb158fe6ed3eaa09c5"
|
|
14
|
+
}
|
|
15
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pomentum/syrmos-hub-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Part of Syrmos, by Pomentum: check an npm package of connectors, services or packs before you publish it for syrmos-hub, and verify a hub.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Pomentum",
|
|
7
|
+
"homepage": "https://www.pomentum.dev",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"bin": {
|
|
10
|
+
"syrmos-hub-check": "check.mjs",
|
|
11
|
+
"syrmos-hub-verify": "verify.mjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"check.mjs",
|
|
15
|
+
"verify.mjs",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"NOTICE",
|
|
18
|
+
"THIRD_PARTY_NOTICES.md",
|
|
19
|
+
"README.md",
|
|
20
|
+
"kit.json"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"syrmos",
|
|
30
|
+
"syrmos-hub",
|
|
31
|
+
"pomentum",
|
|
32
|
+
"connectors"
|
|
33
|
+
]
|
|
34
|
+
}
|
package/verify.mjs
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Checks a syrmos hub the way an installation does, from one file with no dependency but Node 20 or later.
|
|
3
|
+
// Read it before you run it: it is short, and it only reads from the address you give and writes to your screen.
|
|
4
|
+
//
|
|
5
|
+
// node verify.mjs <address> [--last-serial <number>]
|
|
6
|
+
//
|
|
7
|
+
// <address> where the hub is served, for example the address you read this on, with no path
|
|
8
|
+
// --last-serial the serial of the last index you took, if you keep one: a lower serial is a rollback and is refused.
|
|
9
|
+
//
|
|
10
|
+
// The hub's release key is built into this file (PINNED_KEY below) by the build of the npm package `@pomentum/syrmos-hub-kit`, so the file
|
|
11
|
+
// comes from npm and not from the host it checks, and the host is asked for nothing but what it signed: `index.json`, `index.json.sig` and
|
|
12
|
+
// the documents the index lists. A host holds no key and no checker of its own to be believed.
|
|
13
|
+
//
|
|
14
|
+
// It checks, in order: the signature is the key's over the exact bytes of index.json; the index is in
|
|
15
|
+
// canonical form; its format; that it has not expired and does not live past 45 days; that its serial is not lower than the last you
|
|
16
|
+
// saw; and that every document and fixtures file it lists matches its digest. Exit 0 means all held; exit 1 lists what did not.
|
|
17
|
+
import { createHash, createPublicKey, verify } from "node:crypto";
|
|
18
|
+
import { pathToFileURL } from "node:url";
|
|
19
|
+
|
|
20
|
+
/** The hub's release key itself (32 bytes, base64) that a build of this file puts in. The source holds none: only the package has it. */
|
|
21
|
+
export const PINNED_KEY = "QgasBCr9qGhiiNzywRKDILoTQTDbvYpZjck0Rzq+d20=";
|
|
22
|
+
|
|
23
|
+
/** An Ed25519 public key from its raw 32 bytes in base64. */
|
|
24
|
+
export const keyFromRaw = (raw) => createPublicKey({ key: Buffer.concat([Buffer.from("302a300506032b6570032100", "hex"), Buffer.from(raw, "base64")]), format: "der", type: "spki" });
|
|
25
|
+
|
|
26
|
+
const SAFE = Number.MAX_SAFE_INTEGER;
|
|
27
|
+
const DAY = 24 * 3600 * 1000;
|
|
28
|
+
const MAX_INDEX = 16 * 1024 * 1024;
|
|
29
|
+
const MAX_DOCUMENT = 4 * 1024 * 1024;
|
|
30
|
+
|
|
31
|
+
/** How deep a value may nest: far beyond any document of Syrmos, far below what overflows the stack. */
|
|
32
|
+
export const MAX_DEPTH = 64;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* JSON read strictly: the value, or an Error naming where it breaks RFC 8785's input rules (I-JSON). Also refused, since a reader that
|
|
36
|
+
* assigns instead of defining would read another value than the one digested: nesting past MAX_DEPTH, and any key an object already
|
|
37
|
+
* has (`__proto__`, `constructor`, `toString` and the rest of Object.prototype).
|
|
38
|
+
*/
|
|
39
|
+
export function parseStrict(text) {
|
|
40
|
+
let at = 0;
|
|
41
|
+
let depth = 0;
|
|
42
|
+
const fail = (why) => {
|
|
43
|
+
throw new Error(`${why} at character ${at}`);
|
|
44
|
+
};
|
|
45
|
+
const space = () => {
|
|
46
|
+
while (at < text.length && " \t\n\r".includes(text[at])) at += 1;
|
|
47
|
+
};
|
|
48
|
+
const string = () => {
|
|
49
|
+
const start = at;
|
|
50
|
+
at += 1;
|
|
51
|
+
let escaped = false;
|
|
52
|
+
while (at < text.length) {
|
|
53
|
+
const c = text[at];
|
|
54
|
+
if (escaped) escaped = false;
|
|
55
|
+
else if (c === "\\") escaped = true;
|
|
56
|
+
else if (c === '"') break;
|
|
57
|
+
at += 1;
|
|
58
|
+
}
|
|
59
|
+
if (text[at] !== '"') fail("an unclosed string");
|
|
60
|
+
at += 1;
|
|
61
|
+
const value = JSON.parse(text.slice(start, at));
|
|
62
|
+
if (/\p{Surrogate}/u.test(value.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ""))) fail("a lone surrogate");
|
|
63
|
+
return value;
|
|
64
|
+
};
|
|
65
|
+
const number = () => {
|
|
66
|
+
const found = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/.exec(text.slice(at));
|
|
67
|
+
if (!found) fail("a value that is not JSON");
|
|
68
|
+
at += found[0].length;
|
|
69
|
+
const value = Number(found[0]);
|
|
70
|
+
if (Object.is(value, -0)) fail("-0");
|
|
71
|
+
if (!Number.isFinite(value)) fail("a number out of range");
|
|
72
|
+
// An integer written out in full beyond 2^53 has already lost digits; 1e30 written as a number is just a number.
|
|
73
|
+
if (/^-?\d+$/.test(found[0]) && Math.abs(value) > SAFE) fail("an integer beyond 2^53");
|
|
74
|
+
return value;
|
|
75
|
+
};
|
|
76
|
+
const value = () => {
|
|
77
|
+
space();
|
|
78
|
+
const c = text[at];
|
|
79
|
+
if (c === "{" || c === "[") {
|
|
80
|
+
depth += 1;
|
|
81
|
+
if (depth > MAX_DEPTH) fail(`nesting deeper than ${MAX_DEPTH}`);
|
|
82
|
+
}
|
|
83
|
+
if (c === "{") {
|
|
84
|
+
at += 1;
|
|
85
|
+
const object = {};
|
|
86
|
+
const seen = new Set();
|
|
87
|
+
space();
|
|
88
|
+
if (text[at] === "}") {
|
|
89
|
+
at += 1;
|
|
90
|
+
depth -= 1;
|
|
91
|
+
return object;
|
|
92
|
+
}
|
|
93
|
+
for (;;) {
|
|
94
|
+
space();
|
|
95
|
+
if (text[at] !== '"') fail("a key that is not a string");
|
|
96
|
+
const key = string();
|
|
97
|
+
if (seen.has(key)) fail(`the key "${key}" twice`);
|
|
98
|
+
if (key in Object.prototype) fail(`the key "${key}", which every object already has`);
|
|
99
|
+
seen.add(key);
|
|
100
|
+
space();
|
|
101
|
+
if (text[at] !== ":") fail("a missing colon");
|
|
102
|
+
at += 1;
|
|
103
|
+
object[key] = value();
|
|
104
|
+
space();
|
|
105
|
+
if (text[at] === ",") at += 1;
|
|
106
|
+
else if (text[at] === "}") {
|
|
107
|
+
at += 1;
|
|
108
|
+
depth -= 1;
|
|
109
|
+
return object;
|
|
110
|
+
} else fail("a missing comma");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (c === "[") {
|
|
114
|
+
at += 1;
|
|
115
|
+
const list = [];
|
|
116
|
+
space();
|
|
117
|
+
if (text[at] === "]") {
|
|
118
|
+
at += 1;
|
|
119
|
+
depth -= 1;
|
|
120
|
+
return list;
|
|
121
|
+
}
|
|
122
|
+
for (;;) {
|
|
123
|
+
list.push(value());
|
|
124
|
+
space();
|
|
125
|
+
if (text[at] === ",") at += 1;
|
|
126
|
+
else if (text[at] === "]") {
|
|
127
|
+
at += 1;
|
|
128
|
+
depth -= 1;
|
|
129
|
+
return list;
|
|
130
|
+
} else fail("a missing comma");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (c === '"') return string();
|
|
134
|
+
for (const [word, result] of [
|
|
135
|
+
["true", true],
|
|
136
|
+
["false", false],
|
|
137
|
+
["null", null],
|
|
138
|
+
]) {
|
|
139
|
+
if (text.startsWith(word, at)) {
|
|
140
|
+
at += word.length;
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return number();
|
|
145
|
+
};
|
|
146
|
+
const result = value();
|
|
147
|
+
space();
|
|
148
|
+
if (at !== text.length) fail("text after the value");
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** RFC 8785: keys sorted by UTF-16 code units, no whitespace, numbers and strings as ECMAScript writes them. */
|
|
153
|
+
export function canonicalJson(value, depth = 0) {
|
|
154
|
+
if (depth > MAX_DEPTH) throw new Error(`nesting deeper than ${MAX_DEPTH}`);
|
|
155
|
+
if (Array.isArray(value)) return `[${value.map((v) => canonicalJson(v, depth + 1)).join(",")}]`;
|
|
156
|
+
if (value !== null && typeof value === "object") {
|
|
157
|
+
return `{${Object.keys(value)
|
|
158
|
+
.sort()
|
|
159
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key], depth + 1)}`)
|
|
160
|
+
.join(",")}}`;
|
|
161
|
+
}
|
|
162
|
+
if (typeof value === "number" && (!Number.isFinite(value) || Object.is(value, -0))) {
|
|
163
|
+
throw new Error("a number canonical JSON cannot hold");
|
|
164
|
+
}
|
|
165
|
+
return JSON.stringify(value);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** `sha256:` and the hex SHA-256 of a value's canonical JSON, as UTF-8: a document's digest in the index. */
|
|
169
|
+
export const digestOf = (value) => `sha256:${createHash("sha256").update(canonicalJson(value), "utf8").digest("hex")}`;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Checks the hub at `address` and returns what it found: `problems` (empty when everything held), the index's `serial` and `expires`,
|
|
173
|
+
* and how many documents matched. Stops at the first problem with the key or the signature, since nothing after it can be trusted.
|
|
174
|
+
*/
|
|
175
|
+
export async function verifyHub({ address, key, lastSerial, now = new Date(), fetchIt = fetch, log = () => {} }) {
|
|
176
|
+
const base = String(address).replace(/\/+$/, "");
|
|
177
|
+
const problems = [];
|
|
178
|
+
const result = { problems, serial: null, expires: null, documents: 0 };
|
|
179
|
+
const get = async (path, max) => {
|
|
180
|
+
const answer = await fetchIt(`${base}/${path}`, { redirect: "follow", signal: AbortSignal.timeout(30_000) });
|
|
181
|
+
if (!answer.ok) throw new Error(`${path} answered ${answer.status}`);
|
|
182
|
+
const bytes = Buffer.from(await answer.arrayBuffer());
|
|
183
|
+
if (bytes.length > max) throw new Error(`${path} is larger than ${max} bytes`);
|
|
184
|
+
return bytes;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
let indexBytes;
|
|
188
|
+
let signature;
|
|
189
|
+
try {
|
|
190
|
+
[indexBytes, signature] = await Promise.all([get("index.json", MAX_INDEX), get("index.json.sig", 1024)]);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
problems.push(`could not read the hub's files: ${error.message}`);
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (key.asymmetricKeyType !== "ed25519") problems.push("the key is not an Ed25519 key");
|
|
197
|
+
const found = createHash("sha256").update(key.export({ format: "der", type: "spki" })).digest("hex");
|
|
198
|
+
log(`the key is the one built into this copy (${found.slice(0, 16)}…)`);
|
|
199
|
+
|
|
200
|
+
if (!verify(null, indexBytes, key, signature)) {
|
|
201
|
+
problems.push("the signature is not the key's over this index.json: nothing else is checked");
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
log("the signature is the key's over index.json, byte for byte");
|
|
205
|
+
|
|
206
|
+
const text = indexBytes.toString("utf8");
|
|
207
|
+
let index;
|
|
208
|
+
try {
|
|
209
|
+
index = parseStrict(text);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
problems.push(`index.json is not strict JSON: ${error.message}`);
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
if (canonicalJson(index) !== text) problems.push("index.json is not in canonical form");
|
|
215
|
+
if (index.format !== "syrmos-index/1") problems.push(`an unknown format: ${index.format}`);
|
|
216
|
+
result.serial = index.serial;
|
|
217
|
+
result.expires = index.expires;
|
|
218
|
+
const expires = Date.parse(index.expires);
|
|
219
|
+
const issued = Date.parse(index.issued);
|
|
220
|
+
if (!(expires > now.getTime())) problems.push(`the index expired on ${index.expires}`);
|
|
221
|
+
else log(`not expired: it runs until ${index.expires}`);
|
|
222
|
+
if (!(expires - issued <= 45 * DAY)) problems.push("the index lives longer than 45 days");
|
|
223
|
+
if (!Number.isInteger(index.serial)) problems.push("the serial is not a whole number");
|
|
224
|
+
else if (lastSerial !== undefined && index.serial < lastSerial) problems.push(`the serial ${index.serial} is lower than the last you saw, ${lastSerial}: a rollback`);
|
|
225
|
+
else log(`serial ${index.serial}, issued ${index.issued}${lastSerial === undefined ? "" : `, not lower than ${lastSerial}`}`);
|
|
226
|
+
|
|
227
|
+
const entries = [];
|
|
228
|
+
for (const item of index.items ?? []) {
|
|
229
|
+
for (const version of item.versions ?? []) {
|
|
230
|
+
entries.push(version);
|
|
231
|
+
if (version.fixtures) entries.push(version.fixtures);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const queue = [...entries];
|
|
235
|
+
const work = async () => {
|
|
236
|
+
for (let entry = queue.shift(); entry; entry = queue.shift()) {
|
|
237
|
+
if (!/^(connector|service|pack)\/[a-z0-9-]+\/[a-z0-9-]+\/[0-9.]+(\.fixtures)?\.json$/.test(String(entry.path))) {
|
|
238
|
+
problems.push(`${entry.path}: a path outside the documents`);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
const document = parseStrict((await get(entry.path, MAX_DOCUMENT)).toString("utf8"));
|
|
243
|
+
if (digestOf(document) === entry.digest) result.documents += 1;
|
|
244
|
+
else problems.push(`${entry.path}: not the digest the index lists`);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
problems.push(`${entry.path}: ${error.message}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
await Promise.all(Array.from({ length: 8 }, work));
|
|
251
|
+
if (problems.length === 0 || result.documents === entries.length) log(`${result.documents} of ${entries.length} documents match their digests`);
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
256
|
+
const args = process.argv.slice(2);
|
|
257
|
+
const option = (name) => (args.includes(name) ? args[args.indexOf(name) + 1] : undefined);
|
|
258
|
+
const address = args.find((a) => /^https?:\/\//.test(a));
|
|
259
|
+
const pinned = PINNED_KEY === "" ? undefined : keyFromRaw(PINNED_KEY);
|
|
260
|
+
const last = option("--last-serial");
|
|
261
|
+
if (!address || !pinned) {
|
|
262
|
+
console.error(pinned ? "usage: node verify.mjs <address> [--last-serial <number>]" : "this file has no key built in: use the one in the npm package @pomentum/syrmos-hub-kit (syrmos-hub-verify)");
|
|
263
|
+
process.exit(2);
|
|
264
|
+
}
|
|
265
|
+
const result = await verifyHub({ address, key: pinned, lastSerial: last === undefined ? undefined : Number(last), log: (line) => console.log(`ok ${line}`) });
|
|
266
|
+
if (result.problems.length > 0) {
|
|
267
|
+
for (const problem of result.problems) console.error(`FAIL ${problem}`);
|
|
268
|
+
process.exit(1);
|
|
269
|
+
}
|
|
270
|
+
console.log(`\n${address}: all of it holds. Keep serial ${result.serial} to check the next index against.`);
|
|
271
|
+
}
|