client-certificate-auth 1.3.6 → 1.3.7

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.
@@ -32,6 +32,19 @@ function isThenable(value) {
32
32
  && typeof value.then === 'function';
33
33
  }
34
34
 
35
+ /**
36
+ * Wrap primitive values thrown or rejected by the validation callback in an
37
+ * Error so `.message` and `.status` access is safe; objects pass through.
38
+ * @param {unknown} err
39
+ * @returns {any}
40
+ */
41
+ function normalizeCallbackError(err) {
42
+ if (err !== null && (typeof err === 'object' || typeof err === 'function')) {
43
+ return err;
44
+ }
45
+ return new Error(err === null || err === undefined ? '' : String(err));
46
+ }
47
+
35
48
  /**
36
49
  * Options not supported by the sync CJS wrapper.
37
50
  * These require the ESM module's async header parsing.
@@ -132,7 +145,8 @@ function clientCertificateAuth(callback, options = {}) {
132
145
  try {
133
146
  const result = callback(cert, req);
134
147
  if (isThenable(result)) {
135
- Promise.resolve(result).then(doneAuthorizing).catch((err) => {
148
+ Promise.resolve(result).then(doneAuthorizing).catch((rejection) => {
149
+ const err = normalizeCallbackError(rejection);
136
150
  safeCallHook(onRejected, cert, req, err.message || 'callback_threw');
137
151
  if (err.status === undefined) {
138
152
  err.status = 401;
@@ -142,7 +156,8 @@ function clientCertificateAuth(callback, options = {}) {
142
156
  } else {
143
157
  doneAuthorizing(result);
144
158
  }
145
- } catch (err) {
159
+ } catch (thrown) {
160
+ const err = normalizeCallbackError(thrown);
146
161
  safeCallHook(onRejected, cert, req, err.message || 'callback_threw');
147
162
  if (err.status === undefined) {
148
163
  err.status = 401;
@@ -20,6 +20,19 @@ function isThenable(value) {
20
20
  && typeof /** @type {{then?: unknown}} */ (value).then === 'function';
21
21
  }
22
22
 
23
+ /**
24
+ * Wrap primitive values thrown or rejected by the validation callback in an
25
+ * Error so `.message` and `.status` access is safe; objects pass through.
26
+ * @param {unknown} err
27
+ * @returns {any}
28
+ */
29
+ function normalizeCallbackError(err) {
30
+ if (err !== null && (typeof err === 'object' || typeof err === 'function')) {
31
+ return err;
32
+ }
33
+ return new Error(err === null || err === undefined ? '' : String(err));
34
+ }
35
+
23
36
  /**
24
37
  * @typedef {import('http').IncomingMessage & { secure?: boolean; socket: import('tls').TLSSocket & { authorized?: boolean; authorizationError?: string }; clientCertificate?: import('tls').PeerCertificate }} ClientCertRequest
25
38
  * @typedef {import('http').ServerResponse & { redirect: (statusOrUrl: number | string, url?: string) => void }} ClientCertResponse
@@ -191,7 +204,8 @@ export default function clientCertificateAuth(callback, options = {}) {
191
204
  try {
192
205
  const callbackResult = callback(cert, req);
193
206
  if (isThenable(callbackResult)) {
194
- Promise.resolve(callbackResult).then(doneAuthorizing).catch((err) => {
207
+ Promise.resolve(callbackResult).then(doneAuthorizing).catch((rejection) => {
208
+ const err = normalizeCallbackError(rejection);
195
209
  safeCallHook(onRejected, cert, req, err.message || 'callback_threw');
196
210
  if (err.status === undefined) {
197
211
  err.status = 401;
@@ -201,7 +215,8 @@ export default function clientCertificateAuth(callback, options = {}) {
201
215
  } else {
202
216
  doneAuthorizing(callbackResult);
203
217
  }
204
- } catch (err) {
218
+ } catch (thrown) {
219
+ const err = normalizeCallbackError(thrown);
205
220
  safeCallHook(onRejected, cert, req, err.message || 'callback_threw');
206
221
  if (err.status === undefined) {
207
222
  err.status = 401;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "client-certificate-auth",
3
- "version": "1.3.6",
3
+ "version": "1.3.7",
4
4
  "description": "Express/Connect middleware for mTLS client certificate authentication with reverse proxy support (AWS ALB, Envoy, Cloudflare, Traefik)",
5
5
  "homepage": "https://github.com/tgies/client-certificate-auth",
6
6
  "bugs": {