@cosyte/mllp 0.1.12 → 0.1.13

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.13
4
+ ### Patch Changes
5
+
6
+ - c81ac75: **The `tls` options are copied when a client or server is created.** Both kept the caller's `tls` object, and a client read it again on every connect and reconnect, so a `pfx` added to that object after construction reached the socket without the `MLLP_TLS_CREDENTIAL_CONFLICT` check and its certificate was presented in place of `cert`. Now `createClient`, `createServer` and both constructors take a copy, check it, and use only it. The copy reads each TLS option by name, so options a class instance supplies through getters, or an object inherits from its prototype, are kept (a server given `clientAuth: "MUST"` that way still requires a client certificate); it takes new arrays and new bytes for every `Buffer`, so bytes overwritten in place after construction no longer change the identity a client presents on reconnect. A caller who changed their `tls` object, its `Buffer` bytes, or put a new one in the options, to have a running client pick up new TLS settings must create a new client instead. A `tls` that is `null` or not an object now throws `MllpTlsConfigurationError` with the new exported code `MLLP_TLS_OPTIONS_NOT_AN_OBJECT` at creation, where it threw a bare `TypeError` (or, for a value such as `false` on a client, was taken as TLS with default options). The opening of the TLS section in the warning and error codes page now says which codes are thrown at creation rather than rejecting `connect()` or `listen()`.
7
+ - 697f397: **The `'tlsNegotiated'` record now names the key-exchange group, so a post-quantum link can be seen in your logs.** `NegotiatedTlsParameters` gains an optional `keyExchangeGroup`, the group the handshake's key exchange used as the TLS library names it: `'X25519'`, `'prime256v1'`, or a post-quantum hybrid such as `'X25519MLKEM768'`. The key is on the record only where Node.js reports the group, and missing everywhere else: Node.js reports it on a client socket alone, so a server record never carries it; a classic elliptic-curve group is reported on every supported Node.js line, while a group with no classic key, such as a post-quantum hybrid, is reported from Node.js 24.19.0 and 26.5.0 and is missing on Node.js 22; and a TLS 1.2 `DHE` suite has no group name to report. Like the other fields it is a name out of a closed registry, never caller data. Nothing changes for code that reads the record; code that checks its exact set of keys has to allow `keyExchangeGroup` on a client record.
8
+ - 1f8661d: **Every byte array in the `tls` options is copied, and a `tls` that is an array or another kind of object is refused.** The copy taken at creation gave new bytes only to a `Buffer`, so a `cert`, `key`, `ca` or `pfx` passed as a plain `Uint8Array` (outside the declared type, but Node.js accepts it) stayed the caller's: bytes overwritten in it after creation changed what a client presented on reconnect. Now every `ArrayBuffer` view (a `Buffer`, a `Uint8Array`, any other typed array, a `DataView`) is copied the same way, as a `Buffer` of the bytes it covers. A `tls` that is an array, a byte array (a `Buffer`, any other typed array, a `DataView`, an `ArrayBuffer` or a `SharedArrayBuffer`), a `Map`, a `Set`, a `Date`, a `Promise` or a boxed primitive now throws `MllpTlsConfigurationError` with `MLLP_TLS_OPTIONS_NOT_AN_OBJECT` from `createClient`, `createServer` and both constructors; before, a client given an array connected with TLS defaults and a server threw a `TypeError` asking for `cert` and `key`. Only those kinds are refused, each recognised by what it is rather than by its `Symbol.toStringTag`: a literal, a class instance (one that sets its own `Symbol.toStringTag` included), an `Object.create` object and a module namespace object are still accepted and read by name, as before. A caller who passed one of the refused kinds must pass an object of options instead. A client now loads its `pfx` once, when it is created, rather than on every connect and reconnect: a file that cannot be loaded still rejects every `connect()` with `MLLP_TLS_PFX_REJECTED` before a socket is opened, and a reconnect no longer spends the time to load the same bytes again.
9
+
3
10
  ## 0.1.12
4
11
  ### Patch Changes
5
12
 
package/dist/index.cjs CHANGED
@@ -4,6 +4,7 @@ var tls = require('tls');
4
4
  var events = require('events');
5
5
  var crypto = require('crypto');
6
6
  var net = require('net');
7
+ var util = require('util');
7
8
 
8
9
  // src/framing/registry.ts
9
10
  function createWarning(code, byteOffset, message) {
@@ -650,6 +651,7 @@ var MLLP_TLS_CIPHER_LIST_UNENFORCEABLE = "MLLP_TLS_CIPHER_LIST_UNENFORCEABLE";
650
651
  var MLLP_TLS_DH_PARAMETERS_UNENFORCEABLE = "MLLP_TLS_DH_PARAMETERS_UNENFORCEABLE";
651
652
  var MLLP_TLS_PFX_REJECTED = "MLLP_TLS_PFX_REJECTED";
652
653
  var MLLP_TLS_CREDENTIAL_CONFLICT = "MLLP_TLS_CREDENTIAL_CONFLICT";
654
+ var MLLP_TLS_OPTIONS_NOT_AN_OBJECT = "MLLP_TLS_OPTIONS_NOT_AN_OBJECT";
653
655
  function tlsConfigurationMessage(code) {
654
656
  const messages = {
655
657
  [MLLP_TLS_CIPHER_LIST_REJECTED]: "the TLS library rejected the configured cipher-suite list; no connection was opened and no fallback to the runtime default list was made",
@@ -658,7 +660,8 @@ function tlsConfigurationMessage(code) {
658
660
  [MLLP_TLS_CIPHER_LIST_UNENFORCEABLE]: "this runtime's TLS library does not enforce a configured cipher-suite list; no connection was opened, rather than one offering a list other than the one configured",
659
661
  [MLLP_TLS_DH_PARAMETERS_UNENFORCEABLE]: "this runtime's TLS library does not enforce server Diffie-Hellman parameters; nothing was bound, rather than a server running without the parameters configured",
660
662
  [MLLP_TLS_PFX_REJECTED]: "tls.pfx could not be loaded with tls.passphrase: the passphrase is wrong, the file is not PKCS#12, or it uses a legacy encryption (RC2-40, as `openssl pkcs12 -export -legacy` and older exports write it) that OpenSSL 3 does not load, so re-export it with AES-256; no socket was opened",
661
- [MLLP_TLS_CREDENTIAL_CONFLICT]: "tls.pfx was set together with tls.cert or tls.key; the PKCS#12 file carries its own certificate and key, so set either cert and key or pfx, not both"
663
+ [MLLP_TLS_CREDENTIAL_CONFLICT]: "tls.pfx was set together with tls.cert or tls.key; the PKCS#12 file carries its own certificate and key, so set either cert and key or pfx, not both",
664
+ [MLLP_TLS_OPTIONS_NOT_AN_OBJECT]: "tls must be an object of TLS options, or true for a client's defaults; it was null, an array, another kind of object or a value that is no object, and no client or server was created"
662
665
  };
663
666
  return messages[code];
664
667
  }
@@ -926,14 +929,23 @@ function readNegotiatedTlsParameters(socket, host, port) {
926
929
  const cipher = socket.getCipher();
927
930
  const cipherSuite = cipher.standardName === "" ? cipher.name : cipher.standardName;
928
931
  if (cipherSuite === "") return null;
932
+ const keyExchangeGroup = readKeyExchangeGroup(socket);
929
933
  return Object.freeze({
930
934
  protocolVersion,
931
935
  cipherSuite,
936
+ ...keyExchangeGroup === void 0 ? {} : { keyExchangeGroup },
932
937
  host,
933
938
  port,
934
939
  timestamp: /* @__PURE__ */ new Date()
935
940
  });
936
941
  }
942
+ function readKeyExchangeGroup(socket) {
943
+ if (typeof socket.getEphemeralKeyInfo !== "function") return void 0;
944
+ const info = socket.getEphemeralKeyInfo();
945
+ if (typeof info !== "object" || info === null || !("name" in info)) return void 0;
946
+ const { name } = info;
947
+ return typeof name === "string" && name !== "" ? name : void 0;
948
+ }
937
949
 
938
950
  // src/connection/error.ts
939
951
  var MllpConnectionError = class _MllpConnectionError extends Error {
@@ -1431,14 +1443,59 @@ function checkCredentialMix(tls) {
1431
1443
  throw new MllpTlsConfigurationError(MLLP_TLS_CREDENTIAL_CONFLICT);
1432
1444
  }
1433
1445
  }
1434
- function checkPfx(pfx, passphrase) {
1435
- if (pfx === void 0) return;
1446
+ function pfxRefusal(pfx, passphrase) {
1447
+ if (pfx === void 0) return void 0;
1436
1448
  try {
1437
1449
  tls.createSecureContext({ pfx, passphrase });
1450
+ return void 0;
1438
1451
  } catch (err) {
1439
- throw new MllpTlsConfigurationError(MLLP_TLS_PFX_REJECTED, { cause: err });
1452
+ return new MllpTlsConfigurationError(MLLP_TLS_PFX_REJECTED, { cause: err });
1440
1453
  }
1441
1454
  }
1455
+ function checkPfx(pfx, passphrase) {
1456
+ const refusal = pfxRefusal(pfx, passphrase);
1457
+ if (refusal !== void 0) throw refusal;
1458
+ }
1459
+ function tlsOptionNames() {
1460
+ const names = {
1461
+ ca: "ca",
1462
+ cert: "cert",
1463
+ key: "key",
1464
+ passphrase: "passphrase",
1465
+ pfx: "pfx",
1466
+ servername: "servername",
1467
+ minVersion: "minVersion",
1468
+ maxVersion: "maxVersion",
1469
+ atnaTransportSecurity: "atnaTransportSecurity",
1470
+ ciphers: "ciphers",
1471
+ allowUnverified: "allowUnverified",
1472
+ clientAuth: "clientAuth",
1473
+ dhParameters: "dhParameters"
1474
+ };
1475
+ return Object.values(names);
1476
+ }
1477
+ function copyValue(value) {
1478
+ return ArrayBuffer.isView(value) ? Buffer.from(new Uint8Array(value.buffer, value.byteOffset, value.byteLength)) : value;
1479
+ }
1480
+ function isOptionsObject(value) {
1481
+ if (typeof value !== "object" || value === null) return false;
1482
+ return !(Array.isArray(value) || ArrayBuffer.isView(value) || util.types.isAnyArrayBuffer(value) || util.types.isMap(value) || util.types.isSet(value) || util.types.isDate(value) || util.types.isPromise(value) || util.types.isBoxedPrimitive(value));
1483
+ }
1484
+ function copyTlsOptions(tls) {
1485
+ if (tls === void 0 || tls === true) return tls;
1486
+ const given = tls;
1487
+ if (!isOptionsObject(given)) {
1488
+ throw new MllpTlsConfigurationError(MLLP_TLS_OPTIONS_NOT_AN_OBJECT);
1489
+ }
1490
+ const source = given;
1491
+ const copy = {};
1492
+ for (const name of tlsOptionNames()) {
1493
+ const value = source[name];
1494
+ if (value === void 0) continue;
1495
+ copy[name] = Array.isArray(value) ? value.map(copyValue) : copyValue(value);
1496
+ }
1497
+ return copy;
1498
+ }
1442
1499
 
1443
1500
  // src/internal/control-id.ts
1444
1501
  var ASCII_M = 77;
@@ -1825,6 +1882,8 @@ var MllpServer = class extends events.EventEmitter {
1825
1882
  _netServer;
1826
1883
  _connections = /* @__PURE__ */ new Set();
1827
1884
  _opts;
1885
+ /** The TLS options as they were at construction; never read `_opts.tls`, which the caller owns. */
1886
+ _tls;
1828
1887
  /** `true` when `ServerOptions.tls` is set, `_netServer` is actually a `tls.Server`. */
1829
1888
  _isTls;
1830
1889
  _listening = false;
@@ -1879,27 +1938,33 @@ var MllpServer = class extends events.EventEmitter {
1879
1938
  "ServerOptions.maxConnections must be a positive integer, or left unset for no cap"
1880
1939
  );
1881
1940
  }
1882
- const tlsInput = opts.tls;
1941
+ const tlsInput = copyTlsOptions(opts.tls);
1883
1942
  checkCredentialMix(tlsInput);
1884
1943
  if (tlsInput !== void 0 && tlsInput.pfx === void 0 && (tlsInput.cert === void 0 || tlsInput.key === void 0)) {
1885
1944
  throw new TypeError("ServerOptions.tls needs cert and key, or pfx");
1886
1945
  }
1887
1946
  this._opts = opts;
1888
- this._isTls = opts.tls !== void 0;
1947
+ this._tls = tlsInput;
1948
+ this._isTls = tlsInput !== void 0;
1889
1949
  let cipherPolicy = null;
1890
- if (opts.tls !== void 0) {
1950
+ if (tlsInput !== void 0) {
1891
1951
  try {
1892
- checkPfx(opts.tls.pfx, opts.tls.passphrase);
1893
- cipherPolicy = resolveTlsCipherPolicy(opts.tls, "server");
1952
+ checkPfx(tlsInput.pfx, tlsInput.passphrase);
1953
+ cipherPolicy = resolveTlsCipherPolicy(tlsInput, "server");
1894
1954
  } catch (err) {
1895
1955
  if (!(err instanceof MllpTlsConfigurationError)) throw err;
1896
1956
  this._tlsConfigError = err;
1897
1957
  }
1898
1958
  }
1899
- if (opts.tls !== void 0 && cipherPolicy === null) {
1959
+ if (tlsInput === void 0) {
1900
1960
  this._netServer = net.createServer();
1901
- } else if (opts.tls !== void 0 && cipherPolicy !== null) {
1902
- const tlsOpts = opts.tls;
1961
+ this._netServer.on("connection", (socket) => {
1962
+ this._onSocketAccepted(socket);
1963
+ });
1964
+ } else if (cipherPolicy === null) {
1965
+ this._netServer = net.createServer();
1966
+ } else {
1967
+ const tlsOpts = tlsInput;
1903
1968
  const clientAuth = tlsOpts.clientAuth ?? "NONE";
1904
1969
  const tlsServer = tls.createServer({
1905
1970
  cert: tlsOpts.cert,
@@ -1930,11 +1995,6 @@ var MllpServer = class extends events.EventEmitter {
1930
1995
  });
1931
1996
  this._emitContained("tlsClientError", event);
1932
1997
  });
1933
- } else {
1934
- this._netServer = net.createServer();
1935
- this._netServer.on("connection", (socket) => {
1936
- this._onSocketAccepted(socket);
1937
- });
1938
1998
  }
1939
1999
  this._netServer.on("error", (err) => {
1940
2000
  if (this.listenerCount("error") > 0) {
@@ -2446,7 +2506,7 @@ var MllpServer = class extends events.EventEmitter {
2446
2506
  }
2447
2507
  }
2448
2508
  );
2449
- const peerCertificate = this._isTls && (this._opts.tls?.clientAuth ?? "NONE") !== "NONE" ? this._extractPeerCertificateSummary(socket) : null;
2509
+ const peerCertificate = (this._tls?.clientAuth ?? "NONE") !== "NONE" ? this._extractPeerCertificateSummary(socket) : null;
2450
2510
  this._emitContained(
2451
2511
  "connection",
2452
2512
  Object.freeze({
@@ -3411,6 +3471,10 @@ function identityOptions(host, servername) {
3411
3471
  }
3412
3472
  var MllpClient = class extends events.EventEmitter {
3413
3473
  _opts;
3474
+ /** The TLS options as they were at construction; never read `_opts.tls`, which the caller owns. */
3475
+ _tls;
3476
+ /** Why `tls.pfx` cannot be loaded, found once at construction; `undefined` when it loads or is unset. */
3477
+ _pfxRefusal;
3414
3478
  _connection = null;
3415
3479
  _socket = null;
3416
3480
  /**
@@ -3546,7 +3610,10 @@ var MllpClient = class extends events.EventEmitter {
3546
3610
  "ClientOptions.connectTimeoutMs must be an integer from 1 to 2147483647, or left unset for no bound"
3547
3611
  );
3548
3612
  }
3549
- checkCredentialMix(opts.tls);
3613
+ const tls = copyTlsOptions(opts.tls);
3614
+ checkCredentialMix(tls);
3615
+ this._pfxRefusal = typeof tls === "object" ? pfxRefusal(tls.pfx, tls.passphrase) : void 0;
3616
+ this._tls = tls;
3550
3617
  this._opts = opts;
3551
3618
  this._ackTimeoutMs = opts.ackTimeoutMs ?? 3e4;
3552
3619
  this._applicationAckTimeoutMs = opts.applicationAckTimeoutMs;
@@ -3664,7 +3731,7 @@ var MllpClient = class extends events.EventEmitter {
3664
3731
  }
3665
3732
  const conn = new Connection(connOpts);
3666
3733
  this._attachConnection(conn);
3667
- const connectEventName = this._opts.tls !== void 0 ? "secureConnect" : "connect";
3734
+ const connectEventName = this._tls !== void 0 ? "secureConnect" : "connect";
3668
3735
  this._armConnectTimeout(socket, connectEventName);
3669
3736
  const cleanup = () => {
3670
3737
  if (signal !== void 0) {
@@ -3734,14 +3801,14 @@ var MllpClient = class extends events.EventEmitter {
3734
3801
  * against it.
3735
3802
  */
3736
3803
  _createSocketAndTransport() {
3737
- const tlsOpt = this._opts.tls;
3804
+ const tlsOpt = this._tls;
3738
3805
  if (tlsOpt === void 0) {
3739
3806
  const socket2 = net.createConnection({ host: this._opts.host, port: this._opts.port });
3740
3807
  return { socket: socket2, transport: new NetTransport(socket2) };
3741
3808
  }
3742
3809
  const tlsOpts = tlsOpt === true ? {} : tlsOpt;
3743
3810
  const cipherPolicy = resolveTlsCipherPolicy(tlsOpts, "client");
3744
- checkPfx(tlsOpts.pfx, tlsOpts.passphrase);
3811
+ if (this._pfxRefusal !== void 0) throw this._pfxRefusal;
3745
3812
  const socket = tls.connect({
3746
3813
  host: this._opts.host,
3747
3814
  port: this._opts.port,
@@ -3802,7 +3869,7 @@ var MllpClient = class extends events.EventEmitter {
3802
3869
  connectionCause: "connect-timeout"
3803
3870
  });
3804
3871
  }
3805
- if (this._opts.tls === void 0) {
3872
+ if (this._tls === void 0) {
3806
3873
  return new MllpConnectionError(err.message, { cause: err, phase });
3807
3874
  }
3808
3875
  const code = err.code;
@@ -3911,7 +3978,7 @@ var MllpClient = class extends events.EventEmitter {
3911
3978
  * connections or when verification is on.
3912
3979
  */
3913
3980
  _emitInsecureWarningIfNeeded() {
3914
- const tlsOpt = this._opts.tls;
3981
+ const tlsOpt = this._tls;
3915
3982
  if (tlsOpt === void 0 || tlsOpt === true) return;
3916
3983
  if (tlsOpt.allowUnverified !== true) return;
3917
3984
  const message = "MLLP TLS certificate verification is DISABLED (allowUnverified: true), this connection does not authenticate the peer.";
@@ -3936,7 +4003,7 @@ var MllpClient = class extends events.EventEmitter {
3936
4003
  * built at handshake-completion time, before any HL7 byte crosses the link.
3937
4004
  */
3938
4005
  _emitNegotiatedTlsParameters(socket) {
3939
- if (this._opts.tls === void 0) return;
4006
+ if (this._tls === void 0) return;
3940
4007
  const params = readNegotiatedTlsParameters(socket, this._opts.host, this._opts.port);
3941
4008
  if (params === null) return;
3942
4009
  this._emitContained("tlsNegotiated", params);
@@ -4179,7 +4246,7 @@ var MllpClient = class extends events.EventEmitter {
4179
4246
  }
4180
4247
  }
4181
4248
  }
4182
- const tlsProtocolShaped = this._opts.tls !== void 0 && isTlsProtocolError(err);
4249
+ const tlsProtocolShaped = this._tls !== void 0 && isTlsProtocolError(err);
4183
4250
  const classifiedAs = !tlsProtocolShaped && isTransientConnectionError(err) ? "transient" : "permanent";
4184
4251
  if (classifiedAs === "permanent") {
4185
4252
  this._userClosed = true;
@@ -4294,7 +4361,7 @@ var MllpClient = class extends events.EventEmitter {
4294
4361
  this._emitInsecureWarningIfNeeded();
4295
4362
  this._emitNegotiatedTlsParameters(socket);
4296
4363
  };
4297
- const connectEventName = this._opts.tls !== void 0 ? "secureConnect" : "connect";
4364
+ const connectEventName = this._tls !== void 0 ? "secureConnect" : "connect";
4298
4365
  this._armConnectTimeout(socket, connectEventName);
4299
4366
  socket.once("error", (sErr) => {
4300
4367
  if (this._lastError === null || this._lastError.message !== sErr.message) {
@@ -5102,7 +5169,7 @@ var MllpClient = class extends events.EventEmitter {
5102
5169
  reconnectAttempts: this._reconnectAttempts,
5103
5170
  lastConnectedAt: this._lastConnectedAt,
5104
5171
  lastAckAt: this._lastAckAt,
5105
- tls: this._opts.tls !== void 0
5172
+ tls: this._tls !== void 0
5106
5173
  };
5107
5174
  }
5108
5175
  /**
@@ -5459,7 +5526,7 @@ async function runDifferential(options = {}) {
5459
5526
  }
5460
5527
 
5461
5528
  // src/index.ts
5462
- var VERSION = "0.1.12";
5529
+ var VERSION = "0.1.13";
5463
5530
 
5464
5531
  exports.ATNA_CIPHER_LIST = ATNA_CIPHER_LIST;
5465
5532
  exports.ATNA_CIPHER_SUITES = ATNA_CIPHER_SUITES;
@@ -5474,6 +5541,7 @@ exports.MLLP_TLS_CIPHER_OPTION_CONFLICT = MLLP_TLS_CIPHER_OPTION_CONFLICT;
5474
5541
  exports.MLLP_TLS_CREDENTIAL_CONFLICT = MLLP_TLS_CREDENTIAL_CONFLICT;
5475
5542
  exports.MLLP_TLS_DH_PARAMETERS_REJECTED = MLLP_TLS_DH_PARAMETERS_REJECTED;
5476
5543
  exports.MLLP_TLS_DH_PARAMETERS_UNENFORCEABLE = MLLP_TLS_DH_PARAMETERS_UNENFORCEABLE;
5544
+ exports.MLLP_TLS_OPTIONS_NOT_AN_OBJECT = MLLP_TLS_OPTIONS_NOT_AN_OBJECT;
5477
5545
  exports.MLLP_TLS_PFX_REJECTED = MLLP_TLS_PFX_REJECTED;
5478
5546
  exports.MLLP_TLS_VERIFY_DISABLED = MLLP_TLS_VERIFY_DISABLED;
5479
5547
  exports.MllpAckError = MllpAckError;