fastmcp 4.28.0 → 4.28.2

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.
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6;// src/auth/helpers.ts
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6;// src/auth/helpers.ts
2
2
  function getAuthSession(session) {
3
3
  if (!session) {
4
4
  throw new Error("Session is not authenticated");
@@ -1322,6 +1322,7 @@ var MemoryTokenStorage = (_class3 = class {
1322
1322
  () => void this.cleanup(),
1323
1323
  cleanupIntervalMs
1324
1324
  );
1325
+ _optionalChain([this, 'access', _9 => _9.cleanupInterval, 'access', _10 => _10.unref, 'optionalCall', _11 => _11()]);
1325
1326
  }
1326
1327
  async cleanup() {
1327
1328
  const now = Date.now();
@@ -1394,7 +1395,8 @@ var DEFAULT_PORTS = {
1394
1395
  "ws:": "80",
1395
1396
  "wss:": "443"
1396
1397
  };
1397
- var CONSENT_COOKIE_VALUE = /^[\w-]{43}$/;
1398
+ var GENERATED_ID = /^[\w-]{43}$/;
1399
+ var OAUTH_ERROR_TEXT = /^[\x20-\x21\x23-\x5b\x5d-\x7e]+$/;
1398
1400
  var MAX_CLIENT_REGISTRATION_BYTES = 32 * 1024;
1399
1401
  var RESERVED_AUTHORIZATION_PARAMS = /* @__PURE__ */ new Set([
1400
1402
  "client_id",
@@ -1416,11 +1418,9 @@ var OAuthProxy = (_class4 = class {
1416
1418
 
1417
1419
 
1418
1420
  /**
1419
- * The fallback storage this proxy created for itself, if any. It runs a
1420
- * cleanup timer that keeps the event loop alive, so `destroy()` has to stop
1421
- * it — and once the storage is wrapped for encryption it is reachable
1422
- * through nothing else. A caller-supplied storage is deliberately not
1423
- * recorded here: its lifetime belongs to the caller.
1421
+ * The fallback storage this proxy created for itself, if any. Keep a
1422
+ * reference so `destroy()` can stop its cleanup timer even when the storage
1423
+ * is wrapped for encryption. Caller-supplied storage has its own lifetime.
1424
1424
  */
1425
1425
  __init7() {this.ownedTokenStorage = null}
1426
1426
  /**
@@ -1556,7 +1556,7 @@ var OAuthProxy = (_class4 = class {
1556
1556
  const consentBinding = this.config.consentRequired ? _nullishCoalesce((request && this.consentCookieValue(request)), () => ( this.generateId())) : void 0;
1557
1557
  const transaction = await this.createTransaction(params, consentBinding);
1558
1558
  if (consentBinding && !transaction.consentGiven) {
1559
- const clientName = _optionalChain([registeredClient, 'access', _9 => _9.metadata, 'optionalAccess', _10 => _10.client_name]);
1559
+ const clientName = _optionalChain([registeredClient, 'access', _12 => _12.metadata, 'optionalAccess', _13 => _13.client_name]);
1560
1560
  return this.consentManager.createConsentResponse(
1561
1561
  transaction,
1562
1562
  this.getProviderName(),
@@ -1576,7 +1576,7 @@ var OAuthProxy = (_class4 = class {
1576
1576
  clearInterval(this.cleanupInterval);
1577
1577
  this.cleanupInterval = null;
1578
1578
  }
1579
- _optionalChain([this, 'access', _11 => _11.ownedTokenStorage, 'optionalAccess', _12 => _12.destroy, 'call', _13 => _13()]);
1579
+ _optionalChain([this, 'access', _14 => _14.ownedTokenStorage, 'optionalAccess', _15 => _15.destroy, 'call', _16 => _16()]);
1580
1580
  this.registeredClientsByClientId.clear();
1581
1581
  }
1582
1582
  /**
@@ -1739,20 +1739,24 @@ var OAuthProxy = (_class4 = class {
1739
1739
  */
1740
1740
  async handleCallback(request) {
1741
1741
  const url = new URL(request.url);
1742
- const code = url.searchParams.get("code");
1742
+ const code = _nullishCoalesce(url.searchParams.get("code"), () => ( ""));
1743
1743
  const state = url.searchParams.get("state");
1744
1744
  const error = url.searchParams.get("error");
1745
+ const description = url.searchParams.get("error_description") || void 0;
1745
1746
  if (error) {
1746
- const errorDescription = url.searchParams.get("error_description");
1747
- throw new OAuthProxyError(error, errorDescription || void 0);
1748
- }
1749
- if (!code || !state) {
1747
+ if (!state || url.searchParams.getAll("state").length !== 1 || !GENERATED_ID.test(state) || !OAUTH_ERROR_TEXT.test(error) || description && !OAUTH_ERROR_TEXT.test(description) || await _asyncOptionalChain([(await this.stateStore.getTransaction(state)), 'optionalAccess', async _17 => _17.id]) !== state) {
1748
+ throw new OAuthProxyError(error, description);
1749
+ }
1750
+ } else if (!code || !state) {
1750
1751
  throw new OAuthProxyError(
1751
1752
  "invalid_request",
1752
1753
  "Missing code or state parameter"
1753
1754
  );
1754
1755
  }
1755
1756
  const transaction = await this.stateStore.consumeTransaction(state);
1757
+ if (error && (_optionalChain([transaction, 'optionalAccess', _18 => _18.id]) !== state || transaction.expiresAt.getTime() <= Date.now())) {
1758
+ throw new OAuthProxyError(error, description);
1759
+ }
1756
1760
  if (!transaction) {
1757
1761
  throw new OAuthProxyError("invalid_request", "Invalid or expired state");
1758
1762
  }
@@ -1768,23 +1772,18 @@ var OAuthProxy = (_class4 = class {
1768
1772
  "Transaction callback URL is not registered"
1769
1773
  );
1770
1774
  }
1775
+ if (error) {
1776
+ return this.redirectToClient(transaction, {
1777
+ error,
1778
+ error_description: description
1779
+ });
1780
+ }
1771
1781
  const upstreamTokens = await this.exchangeUpstreamCode(code, transaction);
1772
1782
  const clientCode = await this.generateAuthorizationCode(
1773
1783
  transaction,
1774
1784
  upstreamTokens
1775
1785
  );
1776
- const redirectUrl = new URL(transaction.clientCallbackUrl);
1777
- redirectUrl.searchParams.set("code", clientCode);
1778
- redirectUrl.searchParams.set("state", transaction.state);
1779
- if (this.config.authorizationResponseIss) {
1780
- redirectUrl.searchParams.set("iss", this.config.baseUrl);
1781
- }
1782
- return new Response(null, {
1783
- headers: {
1784
- Location: redirectUrl.toString()
1785
- },
1786
- status: 302
1787
- });
1786
+ return this.redirectToClient(transaction, { code: clientCode });
1788
1787
  }
1789
1788
  /**
1790
1789
  * Handle consent form submission
@@ -1823,21 +1822,9 @@ var OAuthProxy = (_class4 = class {
1823
1822
  "Transaction callback URL is not registered"
1824
1823
  );
1825
1824
  }
1826
- const redirectUrl = new URL(transaction.clientCallbackUrl);
1827
- redirectUrl.searchParams.set("error", "access_denied");
1828
- redirectUrl.searchParams.set(
1829
- "error_description",
1830
- "User denied authorization"
1831
- );
1832
- redirectUrl.searchParams.set("state", transaction.state);
1833
- if (this.config.authorizationResponseIss) {
1834
- redirectUrl.searchParams.set("iss", this.config.baseUrl);
1835
- }
1836
- return new Response(null, {
1837
- headers: {
1838
- Location: redirectUrl.toString()
1839
- },
1840
- status: 302
1825
+ return this.redirectToClient(transaction, {
1826
+ error: "access_denied",
1827
+ error_description: "User denied authorization"
1841
1828
  });
1842
1829
  }
1843
1830
  const approvedTransaction = {
@@ -1855,7 +1842,7 @@ var OAuthProxy = (_class4 = class {
1855
1842
  return null;
1856
1843
  }
1857
1844
  const result = await this.jwtIssuer.verify(fastmcpToken);
1858
- if (!result.valid || !_optionalChain([result, 'access', _14 => _14.claims, 'optionalAccess', _15 => _15.jti])) {
1845
+ if (!result.valid || !_optionalChain([result, 'access', _19 => _19.claims, 'optionalAccess', _20 => _20.jti])) {
1859
1846
  return null;
1860
1847
  }
1861
1848
  if (result.claims.token_use !== "access") {
@@ -2065,7 +2052,7 @@ var OAuthProxy = (_class4 = class {
2065
2052
  const separator = pair.indexOf("=");
2066
2053
  if (separator !== -1 && withoutOptionalWhitespace(pair.slice(0, separator)) === name) {
2067
2054
  const value = withoutOptionalWhitespace(pair.slice(separator + 1));
2068
- return CONSENT_COOKIE_VALUE.test(value) ? value : void 0;
2055
+ return GENERATED_ID.test(value) ? value : void 0;
2069
2056
  }
2070
2057
  }
2071
2058
  return void 0;
@@ -2149,7 +2136,7 @@ var OAuthProxy = (_class4 = class {
2149
2136
  throw new Error("JWT issuer not initialized");
2150
2137
  }
2151
2138
  const result = await this.jwtIssuer.verify(token);
2152
- if (!result.valid || !_optionalChain([result, 'access', _16 => _16.claims, 'optionalAccess', _17 => _17.jti])) {
2139
+ if (!result.valid || !_optionalChain([result, 'access', _21 => _21.claims, 'optionalAccess', _22 => _22.jti])) {
2153
2140
  throw new Error("Failed to extract JTI from token");
2154
2141
  }
2155
2142
  return result.claims.jti;
@@ -2411,7 +2398,7 @@ var OAuthProxy = (_class4 = class {
2411
2398
  refreshedUpstreamTokens.scope = upstreamTokens.scope;
2412
2399
  }
2413
2400
  const requestedScope = [
2414
- ...new Set(_optionalChain([request, 'access', _18 => _18.scope, 'optionalAccess', _19 => _19.split, 'call', _20 => _20(" "), 'access', _21 => _21.filter, 'call', _22 => _22(Boolean)]))
2401
+ ...new Set(_optionalChain([request, 'access', _23 => _23.scope, 'optionalAccess', _24 => _24.split, 'call', _25 => _25(" "), 'access', _26 => _26.filter, 'call', _27 => _27(Boolean)]))
2415
2402
  ];
2416
2403
  const accessTokenScope = requestedScope.length > 0 && requestedScope.every(
2417
2404
  (scope) => refreshedUpstreamTokens.scope.includes(scope)
@@ -2610,7 +2597,7 @@ var OAuthProxy = (_class4 = class {
2610
2597
  transaction.clientId,
2611
2598
  transaction.clientCallbackUrl
2612
2599
  );
2613
- return _nullishCoalesce(_optionalChain([registeredClient, 'optionalAccess', _23 => _23.redirectUris, 'access', _24 => _24.includes, 'call', _25 => _25(transaction.clientCallbackUrl)]), () => ( false));
2600
+ return _nullishCoalesce(_optionalChain([registeredClient, 'optionalAccess', _28 => _28.redirectUris, 'access', _29 => _29.includes, 'call', _30 => _30(transaction.clientCallbackUrl)]), () => ( false));
2614
2601
  }
2615
2602
  /**
2616
2603
  * Whether a redirect URI matches a pattern.
@@ -2696,6 +2683,20 @@ var OAuthProxy = (_class4 = class {
2696
2683
  passthroughRefreshKey(refreshToken) {
2697
2684
  return `passthrough-refresh:${sha256(refreshToken)}`;
2698
2685
  }
2686
+ redirectToClient(transaction, params) {
2687
+ const url = new URL(transaction.clientCallbackUrl);
2688
+ for (const [name, value] of Object.entries(params)) {
2689
+ if (value !== void 0) url.searchParams.set(name, value);
2690
+ }
2691
+ url.searchParams.set("state", transaction.state);
2692
+ if (this.config.authorizationResponseIss) {
2693
+ url.searchParams.set("iss", this.config.baseUrl);
2694
+ }
2695
+ return new Response(null, {
2696
+ headers: { Location: url.toString() },
2697
+ status: 302
2698
+ });
2699
+ }
2699
2700
  /**
2700
2701
  * Redirect to upstream OAuth provider
2701
2702
  */
@@ -2873,6 +2874,7 @@ var OAuthProxy = (_class4 = class {
2873
2874
  this.cleanupInterval = setInterval(() => {
2874
2875
  this.cleanup();
2875
2876
  }, 6e4);
2877
+ _optionalChain([this, 'access', _31 => _31.cleanupInterval, 'access', _32 => _32.unref, 'optionalCall', _33 => _33()]);
2876
2878
  }
2877
2879
  /**
2878
2880
  * Atomically claim a token mapping, mirroring `OAuthProxyStateStore`: at most
@@ -3041,8 +3043,8 @@ var AuthProvider = class {
3041
3043
  if (!request) {
3042
3044
  return void 0;
3043
3045
  }
3044
- const authHeader = _optionalChain([request, 'access', _26 => _26.headers, 'optionalAccess', _27 => _27.authorization]);
3045
- if (!authHeader || !authHeader.startsWith("Bearer ")) {
3046
+ const authHeader = _optionalChain([request, 'access', _34 => _34.headers, 'optionalAccess', _35 => _35.authorization]);
3047
+ if (!authHeader || !/^Bearer /i.test(authHeader)) {
3046
3048
  return void 0;
3047
3049
  }
3048
3050
  const token = authHeader.slice(7);
@@ -3259,6 +3261,7 @@ var DiskStore = (_class5 = class {
3259
3261
  this.cleanupInterval = setInterval(() => {
3260
3262
  void this.cleanup();
3261
3263
  }, cleanupIntervalMs);
3264
+ _optionalChain([this, 'access', _36 => _36.cleanupInterval, 'access', _37 => _37.unref, 'optionalCall', _38 => _38()]);
3262
3265
  }
3263
3266
  /**
3264
3267
  * Clean up expired entries
@@ -3435,7 +3438,7 @@ var DiskStore = (_class5 = class {
3435
3438
  await _promises.rename.call(void 0, source, destination);
3436
3439
  return;
3437
3440
  } catch (error) {
3438
- const code = _optionalChain([error, 'optionalAccess', _28 => _28.code]);
3441
+ const code = _optionalChain([error, 'optionalAccess', _39 => _39.code]);
3439
3442
  if (process.platform !== "win32" || attempt >= 5 || !["EACCES", "EBUSY", "EPERM"].includes(_nullishCoalesce(code, () => ( "")))) {
3440
3443
  throw error;
3441
3444
  }
@@ -3611,4 +3614,4 @@ Original error: ${error.message}`
3611
3614
 
3612
3615
 
3613
3616
  exports.cancelResponseBody = cancelResponseBody; 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.TOKEN_ENDPOINT_AUTH_METHODS = TOKEN_ENDPOINT_AUTH_METHODS; 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;
3614
- //# sourceMappingURL=chunk-VVAKRLCY.cjs.map
3617
+ //# sourceMappingURL=chunk-HZJE6ZGH.cjs.map