@vitemcp/server 1.1.0 → 1.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.
@@ -1133,7 +1133,7 @@ var JWTIssuer = class {
1133
1133
  };
1134
1134
 
1135
1135
  // src/auth/utils/pkce.ts
1136
- import { createHash as createHash2, randomBytes as randomBytes2 } from "crypto";
1136
+ import { createHash as createHash2, randomBytes as randomBytes2, timingSafeEqual as timingSafeEqual3 } from "crypto";
1137
1137
  var PKCEUtils = class _PKCEUtils {
1138
1138
  /**
1139
1139
  * Generate a code challenge from a verifier
@@ -1190,11 +1190,11 @@ var PKCEUtils = class _PKCEUtils {
1190
1190
  return false;
1191
1191
  }
1192
1192
  if (method === "plain") {
1193
- return verifier === challenge;
1193
+ return equalsConstantTime(verifier, challenge);
1194
1194
  }
1195
1195
  if (method === "S256") {
1196
1196
  const computedChallenge = _PKCEUtils.generateChallenge(verifier, "S256");
1197
- return computedChallenge === challenge;
1197
+ return equalsConstantTime(computedChallenge, challenge);
1198
1198
  }
1199
1199
  return false;
1200
1200
  }
@@ -1207,6 +1207,11 @@ var PKCEUtils = class _PKCEUtils {
1207
1207
  return buffer.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
1208
1208
  }
1209
1209
  };
1210
+ function equalsConstantTime(a, b) {
1211
+ const bufA = Buffer.from(a, "utf8");
1212
+ const bufB = Buffer.from(b, "utf8");
1213
+ return bufA.length === bufB.length && timingSafeEqual3(bufA, bufB);
1214
+ }
1210
1215
 
1211
1216
  // src/auth/utils/tokenStore.ts
1212
1217
  import {
@@ -1389,6 +1394,14 @@ var OAuthProxy = class {
1389
1394
  consentManager;
1390
1395
  issuerNs;
1391
1396
  jwtIssuer;
1397
+ /**
1398
+ * The fallback storage this proxy created for itself, if any. It runs a
1399
+ * cleanup timer that keeps the event loop alive, so `destroy()` has to stop
1400
+ * it — and once the storage is wrapped for encryption it is reachable
1401
+ * through nothing else. A caller-supplied storage is deliberately not
1402
+ * recorded here: its lifetime belongs to the caller.
1403
+ */
1404
+ ownedTokenStorage = null;
1392
1405
  /**
1393
1406
  * Keyed by proxy-issued client_id for authorize/token-exchange lookups and
1394
1407
  * for the defence-in-depth callback checks. A registration never changes
@@ -1410,7 +1423,11 @@ var OAuthProxy = class {
1410
1423
  upstreamTokenEndpointAuthMethod: "client_secret_basic",
1411
1424
  ...config
1412
1425
  };
1413
- let storage = config.tokenStorage || new MemoryTokenStorage();
1426
+ let storage = config.tokenStorage;
1427
+ if (!storage) {
1428
+ this.ownedTokenStorage = new MemoryTokenStorage();
1429
+ storage = this.ownedTokenStorage;
1430
+ }
1414
1431
  const isAlreadyEncrypted = storage.constructor.name === "EncryptedTokenStorage";
1415
1432
  if (!isAlreadyEncrypted && config.encryptionKey !== false) {
1416
1433
  const encryptionKey = typeof config.encryptionKey === "string" ? config.encryptionKey : this.generateSigningKey();
@@ -1474,6 +1491,14 @@ var OAuthProxy = class {
1474
1491
  "code_challenge_method required when code_challenge is present"
1475
1492
  );
1476
1493
  }
1494
+ if (params.code_challenge && params.code_challenge_method && !this.supportedCodeChallengeMethods().includes(
1495
+ params.code_challenge_method
1496
+ )) {
1497
+ throw new OAuthProxyError(
1498
+ "invalid_request",
1499
+ `Unsupported code_challenge_method: ${params.code_challenge_method}`
1500
+ );
1501
+ }
1477
1502
  const transaction = await this.createTransaction(params);
1478
1503
  if (this.config.consentRequired && !transaction.consentGiven) {
1479
1504
  return this.consentManager.createConsentResponse(
@@ -1491,6 +1516,7 @@ var OAuthProxy = class {
1491
1516
  clearInterval(this.cleanupInterval);
1492
1517
  this.cleanupInterval = null;
1493
1518
  }
1519
+ this.ownedTokenStorage?.destroy();
1494
1520
  this.registeredClientsByClientId.clear();
1495
1521
  }
1496
1522
  /**
@@ -1590,7 +1616,7 @@ var OAuthProxy = class {
1590
1616
  authorizationResponseIssParameterSupported: true,
1591
1617
  // Clients prefer CIMD over DCR when both are offered.
1592
1618
  clientIdMetadataDocumentSupported: this.clientIdMetadata.enabled,
1593
- codeChallengeMethodsSupported: ["S256", "plain"],
1619
+ codeChallengeMethodsSupported: this.supportedCodeChallengeMethods(),
1594
1620
  grantTypesSupported: ["authorization_code", "refresh_token"],
1595
1621
  issuer: this.config.baseUrl,
1596
1622
  registrationEndpoint: `${this.config.baseUrl}/oauth/register`,
@@ -2475,6 +2501,19 @@ var OAuthProxy = class {
2475
2501
  this.cleanup();
2476
2502
  }, 6e4);
2477
2503
  }
2504
+ /**
2505
+ * PKCE challenge methods this proxy accepts, advertised in the authorization
2506
+ * server metadata and enforced at /oauth/authorize.
2507
+ *
2508
+ * S256 only by default. `plain` makes the challenge and the verifier the same
2509
+ * value, so the secret needed to redeem an authorization code is exposed
2510
+ * everywhere the authorization request is: browser history, referrer headers,
2511
+ * proxy logs. RFC 7636 §4.2 requires S256 of any client that can hash, and
2512
+ * OAuth 2.1 removes `plain` entirely.
2513
+ */
2514
+ supportedCodeChallengeMethods() {
2515
+ return this.config.allowPlainPkce ? ["S256", "plain"] : ["S256"];
2516
+ }
2478
2517
  /**
2479
2518
  * Atomically claim a token mapping, mirroring `OAuthProxyStateStore`: at most
2480
2519
  * one caller — across all processes sharing the storage — can receive a given
@@ -2565,13 +2604,27 @@ var AuthProvider = class {
2565
2604
  config;
2566
2605
  /**
2567
2606
  * Get the proxy, creating it lazily if needed.
2607
+ *
2608
+ * Refuses to rebuild after `destroy()`. Lazily replacing a torn-down proxy
2609
+ * would be silently wrong twice over: the replacement starts the very timers
2610
+ * `destroy()` released, and `ViteMCP` captured the original by reference when
2611
+ * it mounted the OAuth routes, so the token endpoint would keep answering
2612
+ * from the old instance while `authenticate()` verified against the new one —
2613
+ * with different auto-generated signing keys, every token minted by one is
2614
+ * invalid to the other.
2568
2615
  */
2569
2616
  get proxy() {
2617
+ if (this._destroyed) {
2618
+ throw new Error(
2619
+ "AuthProvider has been destroyed. Create a new provider rather than reusing this one."
2620
+ );
2621
+ }
2570
2622
  if (!this._proxy) {
2571
2623
  this._proxy = this.createProxy();
2572
2624
  }
2573
2625
  return this._proxy;
2574
2626
  }
2627
+ _destroyed = false;
2575
2628
  _proxy;
2576
2629
  constructor(config) {
2577
2630
  this.config = config;
@@ -2595,6 +2648,28 @@ var AuthProvider = class {
2595
2648
  }
2596
2649
  return this.createSession(upstreamTokens);
2597
2650
  }
2651
+ /**
2652
+ * Release the resources held by the proxy this provider built for itself.
2653
+ *
2654
+ * That proxy runs two cleanup timers — its own sweep and its token
2655
+ * storage's — and both keep the Node event loop alive, so a process that has
2656
+ * stopped serving does not exit until this runs. `getProxy().destroy()`
2657
+ * reaches the same teardown; this exists so a caller holding only the
2658
+ * provider does not have to know that.
2659
+ *
2660
+ * Terminal: the provider will not serve again afterwards, and any later use
2661
+ * throws rather than quietly building a replacement. Call it when the process
2662
+ * is shutting down, after the HTTP server has finished draining — a request
2663
+ * still in flight would otherwise resurrect what this just released.
2664
+ *
2665
+ * Safe to call more than once, and a no-op if the lazy proxy was never
2666
+ * created.
2667
+ */
2668
+ destroy() {
2669
+ this._destroyed = true;
2670
+ this._proxy?.destroy();
2671
+ this._proxy = void 0;
2672
+ }
2598
2673
  /**
2599
2674
  * Get the OAuth configuration object for ViteMCP ServerOptions.
2600
2675
  */
@@ -3121,4 +3196,4 @@ export {
3121
3196
  DiskStore,
3122
3197
  JWKSVerifier
3123
3198
  };
3124
- //# sourceMappingURL=chunk-WYT772UD.js.map
3199
+ //# sourceMappingURL=chunk-XZBHP34C.js.map