@ubercode/channelvault 0.3.0-rc.2 → 0.3.0-rc.4

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/dist/index.d.ts CHANGED
@@ -69,6 +69,8 @@ interface ClientConfig {
69
69
  https?: boolean;
70
70
  /** default false; set true for self-signed certs */
71
71
  disableTlsCheck?: boolean;
72
+ /** SHA-256 of the only certificate accepted, 64 lowercase hex digits; replaces CA verification. */
73
+ tlsFingerprint?: string;
72
74
  /**
73
75
  * default false. Put the server's response body in error messages. A body
74
76
  * can echo the credentials that were sent; it is decoded and has credential
package/dist/index.js CHANGED
@@ -924,7 +924,9 @@ function createExplodeEngine() {
924
924
 
925
925
  // src/client/index.ts
926
926
  import { CookieJar } from "tough-cookie";
927
- import { Agent } from "undici";
927
+ import { isIP } from "net";
928
+ import { connect as tlsConnect } from "tls";
929
+ import { Agent, buildConnector } from "undici";
928
930
 
929
931
  // src/secrets/detect.ts
930
932
  import { createHash } from "crypto";
@@ -1144,6 +1146,35 @@ function connectionError(url, err) {
1144
1146
  error.code = code;
1145
1147
  return error;
1146
1148
  }
1149
+ function fingerprintOf(socket) {
1150
+ return (socket.getPeerCertificate().fingerprint256 ?? "").replace(/:/g, "").toLowerCase();
1151
+ }
1152
+ function pinnedConnector(pin) {
1153
+ const base = buildConnector({ rejectUnauthorized: false, maxCachedSessions: 0 });
1154
+ return (options, callback) => {
1155
+ base(options, (err, socket) => {
1156
+ if (err) return callback(err, null);
1157
+ const presented = fingerprintOf(socket);
1158
+ if (presented === pin) return callback(null, socket);
1159
+ socket.destroy();
1160
+ const mismatch = new Error(
1161
+ `the server presented certificate SHA-256 ${presented || "(none)"}, but the pinned fingerprint is ${pin}; nothing was sent`
1162
+ );
1163
+ mismatch.code = "TLS_PIN_MISMATCH";
1164
+ mismatch.presented = presented;
1165
+ callback(mismatch, null);
1166
+ });
1167
+ };
1168
+ }
1169
+ function redirectTarget(url, response) {
1170
+ const location = response.headers.get("location");
1171
+ if (!location) return "";
1172
+ try {
1173
+ return `, to ${new URL(location, url).origin}`;
1174
+ } catch {
1175
+ return "";
1176
+ }
1177
+ }
1147
1178
  var SERVER_CONFIG_KEY = "serverConfiguration";
1148
1179
  function unwrapSingleKey(value) {
1149
1180
  const keys = Object.keys(value);
@@ -1166,7 +1197,7 @@ var MirthClientImpl = class {
1166
1197
  const protocol = config.https !== false ? "https" : "http";
1167
1198
  this.baseUrl = `${protocol}://${config.host}:${config.port}/api`;
1168
1199
  this.cookieJar = new CookieJar();
1169
- this.dispatcher = new Agent(config.disableTlsCheck ? { connect: { rejectUnauthorized: false } } : {});
1200
+ this.dispatcher = new Agent(config.tlsFingerprint ? { connect: pinnedConnector(config.tlsFingerprint) } : config.disableTlsCheck ? { connect: { rejectUnauthorized: false } } : {});
1170
1201
  }
1171
1202
  isAuthenticated() {
1172
1203
  return this.authenticated;
@@ -1388,17 +1419,28 @@ var MirthClientImpl = class {
1388
1419
  }
1389
1420
  return response;
1390
1421
  }
1391
- /** fetch through this client's pool, with network failures explained. */
1422
+ /**
1423
+ * fetch through this client's pool, with network failures explained. Never
1424
+ * follows a redirect: fetch re-sends a 307/308 body, so a redirected login
1425
+ * would post the username and password to wherever the server points.
1426
+ */
1392
1427
  async send(url, init) {
1428
+ let response;
1393
1429
  try {
1394
- return await fetch(url, {
1430
+ response = await fetch(url, {
1395
1431
  ...init,
1432
+ redirect: "manual",
1396
1433
  // @ts-expect-error - dispatcher is a Node.js/undici specific option
1397
1434
  dispatcher: this.dispatcher
1398
1435
  });
1399
1436
  } catch (err) {
1400
1437
  throw connectionError(url, err);
1401
1438
  }
1439
+ if (response.status >= 300 && response.status < 400) {
1440
+ await response.body?.cancel();
1441
+ throw new Error(`the server answered ${new URL(url).pathname} with a redirect (HTTP ${response.status}${redirectTarget(url, response)}); channelvault does not follow redirects, so credentials and session cookies are never sent elsewhere. Connect to the address it redirects to: check https vs --no-https, --host and --port (or MIRTH_HOST/MIRTH_PORT)`);
1442
+ }
1443
+ return response;
1402
1444
  }
1403
1445
  /** Build an absolute URL with an optional query string. */
1404
1446
  buildUrl(path2, query) {