mycomesh-consumer 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/consumer.mjs +8 -2
- package/src/tlspin.mjs +19 -21
package/package.json
CHANGED
package/src/consumer.mjs
CHANGED
|
@@ -160,11 +160,17 @@ export class Consumer {
|
|
|
160
160
|
/** Manifest Relays first, then directory Relays whose /health proves the announced signer. */
|
|
161
161
|
async relays() {
|
|
162
162
|
if (this.relayCache && Date.now() - this.relayCache.at < RELAY_CACHE_MS) return this.relayCache.relays;
|
|
163
|
-
const relays =
|
|
163
|
+
const relays = this.network.relays.map((relay) => ({ ...relay }));
|
|
164
164
|
const known = new Set(relays.map((relay) => relay.signer.toLowerCase()));
|
|
165
165
|
try {
|
|
166
166
|
for (const relay of await directoryRelays(this.network)) {
|
|
167
|
-
if (known.has(relay.signer))
|
|
167
|
+
if (known.has(relay.signer)) {
|
|
168
|
+
// A manifest Relay that pinned its certificate on-chain is checked by the pin, not the CA.
|
|
169
|
+
const listed = relays.find((entry) => entry.signer.toLowerCase() === relay.signer);
|
|
170
|
+
listed.pin = relay.pin || listed.pin;
|
|
171
|
+
listed.owner = relay.owner;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
168
174
|
try {
|
|
169
175
|
const { status, body } = await this.fetchJson(`${relay.url}/health`, { ca: this.network.tls_ca, pin: relay.pin, timeoutMs: 5_000 });
|
|
170
176
|
if (status === 200 && String(body.relay_signer).toLowerCase() === relay.signer
|
package/src/tlspin.mjs
CHANGED
|
@@ -1,36 +1,34 @@
|
|
|
1
|
-
// TLS without certificate authorities: a Relay pins its
|
|
1
|
+
// TLS without certificate authorities: a Relay pins its certificate on-chain
|
|
2
2
|
// (https://IP:PORT#sha256=<hex> in the RelayDirectoryV11); mirrors mycomesh/tlspin.py.
|
|
3
|
+
// The handshake completes, the fingerprint is compared, and only then does HTTP write a byte,
|
|
4
|
+
// whether the certificate is self-signed or issued by some CA.
|
|
3
5
|
import { createHash } from "node:crypto";
|
|
4
6
|
import { connect } from "node:tls";
|
|
5
7
|
|
|
6
8
|
const PIN_PREFIX = "#sha256=";
|
|
7
|
-
const trusted = new Map(); // pin -> PEM of the certificate that matched it
|
|
8
9
|
|
|
9
10
|
export function splitPin(endpoint) {
|
|
10
11
|
const index = String(endpoint).indexOf(PIN_PREFIX);
|
|
11
12
|
return index < 0 ? [String(endpoint), null] : [endpoint.slice(0, index), endpoint.slice(index + PIN_PREFIX.length).toLowerCase()];
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const socket = connect({ host: target.hostname, port: Number(target.port || 443), rejectUnauthorized: false }, () => {
|
|
23
|
-
const der = socket.getPeerCertificate(true).raw;
|
|
24
|
-
socket.end();
|
|
15
|
+
/** Request options whose connection is handed to HTTP only after the certificate matches the pin. */
|
|
16
|
+
export function pinnedOptions(url, pin) {
|
|
17
|
+
const target = new URL(url);
|
|
18
|
+
return {
|
|
19
|
+
createConnection: (_options, ready) => {
|
|
20
|
+
const socket = connect({ host: target.hostname, port: Number(target.port || 443), rejectUnauthorized: false });
|
|
21
|
+
socket.once("secureConnect", () => {
|
|
22
|
+
const der = socket.getPeerCertificate(false)?.raw;
|
|
25
23
|
if (!der || createHash("sha256").update(der).digest("hex") !== pin) {
|
|
26
|
-
|
|
24
|
+
const error = new Error(`Relay ${target.host} certificate does not match its on-chain pin`);
|
|
25
|
+
socket.destroy(error);
|
|
26
|
+
return ready(error);
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
return ready(null, socket);
|
|
29
29
|
});
|
|
30
|
-
socket.
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
return { ca: [trusted.get(pin)], checkServerIdentity: () => undefined };
|
|
30
|
+
socket.once("error", (error) => ready(error));
|
|
31
|
+
return undefined;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
36
34
|
}
|