@vitemcp/server 1.8.4 → 1.8.5

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.
@@ -444,12 +444,6 @@ var OAuthProxyStateStore = class {
444
444
  }
445
445
  return parsed.data;
446
446
  }
447
- async isTransactionCallbackRegistered(transaction) {
448
- const registeredClient = await this.getRegisteredClientByClientId(
449
- transaction.clientId
450
- );
451
- return _nullishCoalesce(_optionalChain([registeredClient, 'optionalAccess', _3 => _3.redirectUris, 'access', _4 => _4.includes, 'call', _5 => _5(transaction.clientCallbackUrl)]), () => ( false));
452
- }
453
447
  /**
454
448
  * Record that an authorization code has been redeemed, so a later attempt
455
449
  * gets "already used" rather than looking like an unknown code. Keyed and
@@ -615,7 +609,7 @@ var ClaimsExtractor = (_class = class {
615
609
  if (this.PROTECTED_CLAIMS.has(key)) {
616
610
  continue;
617
611
  }
618
- if (_optionalChain([this, 'access', _6 => _6.config, 'access', _7 => _7.blockedClaims, 'optionalAccess', _8 => _8.includes, 'call', _9 => _9(key)])) {
612
+ if (_optionalChain([this, 'access', _3 => _3.config, 'access', _4 => _4.blockedClaims, 'optionalAccess', _5 => _5.includes, 'call', _6 => _6(key)])) {
619
613
  continue;
620
614
  }
621
615
  if (this.config.allowedClaims && !this.config.allowedClaims.includes(key)) {
@@ -1132,6 +1126,40 @@ var JWTIssuer = class {
1132
1126
  }
1133
1127
  };
1134
1128
 
1129
+ // src/auth/utils/loopbackRedirect.ts
1130
+ var LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["::1", "127.0.0.1", "localhost"]);
1131
+ var parse = (uri) => {
1132
+ try {
1133
+ return new URL(uri);
1134
+ } catch (e5) {
1135
+ return null;
1136
+ }
1137
+ };
1138
+ function loopbackRedirectMatches(declared, requested) {
1139
+ if (declared === requested) {
1140
+ return true;
1141
+ }
1142
+ const a = parse(declared);
1143
+ const b = parse(requested);
1144
+ if (!a || !b) {
1145
+ return false;
1146
+ }
1147
+ if (a.protocol !== "http:" || b.protocol !== "http:") {
1148
+ return false;
1149
+ }
1150
+ const host = a.hostname.replace(/^\[|\]$/g, "");
1151
+ if (!LOOPBACK_HOSTS.has(host) || a.hostname !== b.hostname) {
1152
+ return false;
1153
+ }
1154
+ if (a.port !== "") {
1155
+ return false;
1156
+ }
1157
+ if (a.username !== "" || a.password !== "" || b.username !== "" || b.password !== "") {
1158
+ return false;
1159
+ }
1160
+ return a.pathname === b.pathname && a.search === b.search && a.hash === b.hash;
1161
+ }
1162
+
1135
1163
  // src/auth/utils/pkce.ts
1136
1164
 
1137
1165
  var PKCEUtils = class _PKCEUtils {
@@ -1476,10 +1504,11 @@ var OAuthProxy = (_class4 = class {
1476
1504
  "Only 'code' response type is supported"
1477
1505
  );
1478
1506
  }
1479
- const registeredUris = await this.resolveClientRedirectUris(
1480
- params.client_id
1481
- );
1482
- if (!registeredUris.includes(params.redirect_uri)) {
1507
+ const registeredClient = await this.resolveClient(params.client_id);
1508
+ const registeredUris = registeredClient.redirectUris;
1509
+ if (!registeredUris.some(
1510
+ (uri) => loopbackRedirectMatches(uri, params.redirect_uri)
1511
+ )) {
1483
1512
  throw new OAuthProxyError(
1484
1513
  "invalid_request",
1485
1514
  "redirect_uri is not registered for this client"
@@ -1516,7 +1545,7 @@ var OAuthProxy = (_class4 = class {
1516
1545
  clearInterval(this.cleanupInterval);
1517
1546
  this.cleanupInterval = null;
1518
1547
  }
1519
- _optionalChain([this, 'access', _10 => _10.ownedTokenStorage, 'optionalAccess', _11 => _11.destroy, 'call', _12 => _12()]);
1548
+ _optionalChain([this, 'access', _7 => _7.ownedTokenStorage, 'optionalAccess', _8 => _8.destroy, 'call', _9 => _9()]);
1520
1549
  this.registeredClientsByClientId.clear();
1521
1550
  }
1522
1551
  /**
@@ -1529,10 +1558,7 @@ var OAuthProxy = (_class4 = class {
1529
1558
  "Only authorization_code grant type is supported"
1530
1559
  );
1531
1560
  }
1532
- const registeredClient = await this.stateStore.getRegisteredClientByClientId(request2.client_id);
1533
- if (!registeredClient) {
1534
- throw new OAuthProxyError("invalid_client", "Unknown client_id");
1535
- }
1561
+ await this.resolveClient(request2.client_id);
1536
1562
  const consumed = await this.stateStore.consumeClientCode(request2.code);
1537
1563
  if (!consumed) {
1538
1564
  throw new OAuthProxyError(
@@ -1623,9 +1649,15 @@ var OAuthProxy = (_class4 = class {
1623
1649
  responseTypesSupported: ["code"],
1624
1650
  scopesSupported: this.config.scopes || [],
1625
1651
  tokenEndpoint: `${this.config.baseUrl}/oauth/token`,
1652
+ // RFC 8414 §2. A CIMD client is a public client: it has no registration
1653
+ // response to carry a secret, and authenticates the exchange with PKCE
1654
+ // alone. A client that reads this list to pick an auth method finds
1655
+ // nothing it can use unless "none" is advertised alongside the
1656
+ // confidential methods DCR clients use.
1626
1657
  tokenEndpointAuthMethodsSupported: [
1627
1658
  "client_secret_basic",
1628
- "client_secret_post"
1659
+ "client_secret_post",
1660
+ ...this.clientIdMetadata.enabled ? ["none"] : []
1629
1661
  ]
1630
1662
  };
1631
1663
  }
@@ -1651,7 +1683,7 @@ var OAuthProxy = (_class4 = class {
1651
1683
  if (!transaction) {
1652
1684
  throw new OAuthProxyError("invalid_request", "Invalid or expired state");
1653
1685
  }
1654
- if (!await this.stateStore.isTransactionCallbackRegistered(transaction)) {
1686
+ if (!await this.isTransactionCallbackRegistered(transaction)) {
1655
1687
  throw new OAuthProxyError(
1656
1688
  "invalid_request",
1657
1689
  "Transaction callback URL is not registered"
@@ -1705,7 +1737,7 @@ var OAuthProxy = (_class4 = class {
1705
1737
  }
1706
1738
  if (action === "deny") {
1707
1739
  await this.stateStore.deleteTransaction(transactionId);
1708
- if (!await this.stateStore.isTransactionCallbackRegistered(transaction)) {
1740
+ if (!await this.isTransactionCallbackRegistered(transaction)) {
1709
1741
  throw new OAuthProxyError(
1710
1742
  "invalid_request",
1711
1743
  "Transaction callback URL is not registered"
@@ -1741,7 +1773,7 @@ var OAuthProxy = (_class4 = class {
1741
1773
  return null;
1742
1774
  }
1743
1775
  const result = await this.jwtIssuer.verify(vitemcpToken);
1744
- if (!result.valid || !_optionalChain([result, 'access', _13 => _13.claims, 'optionalAccess', _14 => _14.jti])) {
1776
+ if (!result.valid || !_optionalChain([result, 'access', _10 => _10.claims, 'optionalAccess', _11 => _11.jti])) {
1745
1777
  return null;
1746
1778
  }
1747
1779
  const mapping = await this.tokenStorage.get(
@@ -1914,7 +1946,7 @@ var OAuthProxy = (_class4 = class {
1914
1946
  const error = await tokenResponse.json();
1915
1947
  errorCode = error.error || "server_error";
1916
1948
  errorDescription = error.error_description;
1917
- } catch (e5) {
1949
+ } catch (e6) {
1918
1950
  errorDescription = `Upstream returned HTTP ${tokenResponse.status} ${tokenResponse.statusText}`;
1919
1951
  }
1920
1952
  throw new OAuthProxyError(errorCode, errorDescription);
@@ -1939,7 +1971,7 @@ var OAuthProxy = (_class4 = class {
1939
1971
  throw new Error("JWT issuer not initialized");
1940
1972
  }
1941
1973
  const result = await this.jwtIssuer.verify(token);
1942
- if (!result.valid || !_optionalChain([result, 'access', _15 => _15.claims, 'optionalAccess', _16 => _16.jti])) {
1974
+ if (!result.valid || !_optionalChain([result, 'access', _12 => _12.claims, 'optionalAccess', _13 => _13.jti])) {
1943
1975
  throw new Error("Failed to extract JTI from token");
1944
1976
  }
1945
1977
  return result.claims.jti;
@@ -2092,7 +2124,7 @@ var OAuthProxy = (_class4 = class {
2092
2124
  const error = await tokenResponse.json();
2093
2125
  errorCode = error.error || "invalid_grant";
2094
2126
  errorDescription = error.error_description;
2095
- } catch (e6) {
2127
+ } catch (e7) {
2096
2128
  errorDescription = `Upstream returned HTTP ${tokenResponse.status} ${tokenResponse.statusText}`;
2097
2129
  }
2098
2130
  throw new OAuthProxyError(errorCode, errorDescription);
@@ -2121,7 +2153,7 @@ var OAuthProxy = (_class4 = class {
2121
2153
  "Invalid or expired refresh token"
2122
2154
  );
2123
2155
  }
2124
- const jti = _optionalChain([verifyResult, 'access', _17 => _17.claims, 'optionalAccess', _18 => _18.jti]);
2156
+ const jti = _optionalChain([verifyResult, 'access', _14 => _14.claims, 'optionalAccess', _15 => _15.jti]);
2125
2157
  if (!jti) {
2126
2158
  throw new OAuthProxyError("invalid_grant", "Refresh token missing JTI");
2127
2159
  }
@@ -2317,6 +2349,27 @@ var OAuthProxy = (_class4 = class {
2317
2349
  }
2318
2350
  return response;
2319
2351
  }
2352
+ /**
2353
+ * Whether the callback URL a transaction carries is still one its client
2354
+ * declares.
2355
+ *
2356
+ * Resolved through `resolveClient` rather than a raw store lookup so a CIMD
2357
+ * client is visible here too, and so a document that has since dropped the
2358
+ * URI stops matching — the mid-flow revocation this check exists for.
2359
+ * Loopback ports are matched per RFC 8252 §7.3, exactly as `authorize()`
2360
+ * matched them, so the two legs cannot disagree about the same URI.
2361
+ */
2362
+ async isTransactionCallbackRegistered(transaction) {
2363
+ let client;
2364
+ try {
2365
+ client = await this.resolveClient(transaction.clientId);
2366
+ } catch (e8) {
2367
+ return false;
2368
+ }
2369
+ return client.redirectUris.some(
2370
+ (uri) => loopbackRedirectMatches(uri, transaction.clientCallbackUrl)
2371
+ );
2372
+ }
2320
2373
  /**
2321
2374
  * Match a parsed URI against an allow-list pattern, one URI component at a
2322
2375
  * time: scheme, host, port, then everything from the path onwards. A `*`
@@ -2465,7 +2518,7 @@ var OAuthProxy = (_class4 = class {
2465
2518
  const error = await tokenResponse.json();
2466
2519
  errorCode = error.error || "invalid_grant";
2467
2520
  errorDescription = error.error_description || "Upstream refresh failed";
2468
- } catch (e7) {
2521
+ } catch (e9) {
2469
2522
  errorDescription = `Upstream returned HTTP ${tokenResponse.status} ${tokenResponse.statusText}`;
2470
2523
  }
2471
2524
  throw new OAuthProxyError(errorCode, errorDescription);
@@ -2483,13 +2536,17 @@ var OAuthProxy = (_class4 = class {
2483
2536
  };
2484
2537
  }
2485
2538
  /**
2486
- * Redirect URIs the given client is allowed to use.
2539
+ * The client behind a client_id, on whichever leg of the flow asks.
2487
2540
  *
2488
2541
  * A URL-formatted client_id is resolved as a Client ID Metadata Document
2489
2542
  * (the preferred mechanism on this revision); anything else must have been
2490
- * registered through DCR.
2543
+ * registered through DCR. Throws `invalid_client` when it is neither.
2491
2544
  */
2492
- async resolveClientRedirectUris(clientId) {
2545
+ async resolveClient(clientId) {
2546
+ const registered = await this.stateStore.getRegisteredClientByClientId(clientId);
2547
+ if (registered) {
2548
+ return registered;
2549
+ }
2493
2550
  if (this.clientIdMetadata.enabled && isClientIdMetadataUrl(clientId)) {
2494
2551
  let metadata;
2495
2552
  try {
@@ -2508,13 +2565,20 @@ var OAuthProxy = (_class4 = class {
2508
2565
  );
2509
2566
  }
2510
2567
  }
2511
- return metadata.redirect_uris;
2512
- }
2513
- const registeredClient = await this.stateStore.getRegisteredClientByClientId(clientId);
2514
- if (!registeredClient) {
2515
- throw new OAuthProxyError("invalid_client", "Unknown client_id");
2568
+ return {
2569
+ callbackUrl: metadata.redirect_uris[0],
2570
+ clientId,
2571
+ clientSecret: void 0,
2572
+ metadata: {
2573
+ client_name: metadata.client_name,
2574
+ client_uri: metadata.client_uri,
2575
+ redirect_uris: metadata.redirect_uris
2576
+ },
2577
+ redirectUris: metadata.redirect_uris,
2578
+ registeredAt: /* @__PURE__ */ new Date()
2579
+ };
2516
2580
  }
2517
- return registeredClient.redirectUris;
2581
+ throw new OAuthProxyError("invalid_client", "Unknown client_id");
2518
2582
  }
2519
2583
  /**
2520
2584
  * Put a claimed refresh-token mapping back after a rotation failed, keeping
@@ -2601,7 +2665,7 @@ var OAuthProxy = (_class4 = class {
2601
2665
  let url;
2602
2666
  try {
2603
2667
  url = new URL(uri);
2604
- } catch (e8) {
2668
+ } catch (e10) {
2605
2669
  return false;
2606
2670
  }
2607
2671
  if (url.username !== "" || url.password !== "") {
@@ -2666,7 +2730,7 @@ var isLoopbackRedirectUri = (uri) => {
2666
2730
  return ["::1", "127.0.0.1", "localhost"].includes(url.hostname);
2667
2731
  }
2668
2732
  return url.protocol !== "";
2669
- } catch (e9) {
2733
+ } catch (e11) {
2670
2734
  return false;
2671
2735
  }
2672
2736
  };
@@ -2760,7 +2824,7 @@ var AuthProvider = (_class5 = class {
2760
2824
  */
2761
2825
  destroy() {
2762
2826
  this._destroyed = true;
2763
- _optionalChain([this, 'access', _19 => _19._proxy, 'optionalAccess', _20 => _20.destroy, 'call', _21 => _21()]);
2827
+ _optionalChain([this, 'access', _16 => _16._proxy, 'optionalAccess', _17 => _17.destroy, 'call', _18 => _18()]);
2764
2828
  this._proxy = void 0;
2765
2829
  }
2766
2830
  /**
@@ -3053,7 +3117,7 @@ var DiskStore = (_class6 = class {
3053
3117
  await this.ensureDirectory();
3054
3118
  const files = await _promises.readdir.call(void 0, this.directory);
3055
3119
  return files.filter((f) => f.endsWith(this.fileExtension)).length;
3056
- } catch (e10) {
3120
+ } catch (e12) {
3057
3121
  return 0;
3058
3122
  }
3059
3123
  }
@@ -3135,7 +3199,7 @@ var DiskStore = (_class6 = class {
3135
3199
  console.warn(`Failed to read/parse file ${file}, deleting:`, error);
3136
3200
  try {
3137
3201
  await _promises.rm.call(void 0, _path.join.call(void 0, this.directory, file));
3138
- } catch (e11) {
3202
+ } catch (e13) {
3139
3203
  }
3140
3204
  }
3141
3205
  }
@@ -3309,4 +3373,4 @@ Original error: ${error.message}`
3309
3373
 
3310
3374
 
3311
3375
  exports.getAuthSession = getAuthSession; exports.requireAll = requireAll; exports.requireAny = requireAny; exports.requireAuth = requireAuth; exports.requireRole = requireRole; exports.requireScopes = requireScopes; exports.DEFAULT_ACCESS_TOKEN_TTL = DEFAULT_ACCESS_TOKEN_TTL; exports.DEFAULT_ACCESS_TOKEN_TTL_NO_REFRESH = DEFAULT_ACCESS_TOKEN_TTL_NO_REFRESH; exports.DEFAULT_REFRESH_TOKEN_TTL = DEFAULT_REFRESH_TOKEN_TTL; exports.DEFAULT_AUTHORIZATION_CODE_TTL = DEFAULT_AUTHORIZATION_CODE_TTL; exports.DEFAULT_TRANSACTION_TTL = DEFAULT_TRANSACTION_TTL; exports.ConsentManager = ConsentManager; exports.JWTIssuer = JWTIssuer; exports.PKCEUtils = PKCEUtils; exports.EncryptedTokenStorage = EncryptedTokenStorage; exports.MemoryTokenStorage = MemoryTokenStorage; exports.OAuthProxy = OAuthProxy; exports.OAuthProxyError = OAuthProxyError; exports.AuthProvider = AuthProvider; exports.AzureProvider = AzureProvider; exports.GitHubProvider = GitHubProvider; exports.GoogleProvider = GoogleProvider; exports.OAuthProvider = OAuthProvider; exports.DiskStore = DiskStore; exports.JWKSVerifier = JWKSVerifier;
3312
- //# sourceMappingURL=chunk-VTEMGFQZ.cjs.map
3376
+ //# sourceMappingURL=chunk-Y3GRHB77.cjs.map