@clerk/backend 3.2.3-snapshot.v20260325172303 → 3.2.3

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/dist/internal.mjs CHANGED
@@ -22,7 +22,7 @@ import {
22
22
  stripPrivateDataFromObject,
23
23
  unauthenticatedMachineObject,
24
24
  verifyMachineAuthToken
25
- } from "./chunk-CNS7DTD3.mjs";
25
+ } from "./chunk-4ITBPQ5U.mjs";
26
26
  import "./chunk-YBVFDYDR.mjs";
27
27
  import "./chunk-7KLH7JRZ.mjs";
28
28
  import "./chunk-I4B6KCGC.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,2BAA2B,GAAG,qBAAqB,GAAG,sBAAsB,CAAC;AAE1G;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,cAAc,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB;AAcD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAcxE;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAKxD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,uBAAuB,EAAE,WAAW,CAAC,GAAG,OAAO,CAI9G;AAsDD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmIlH"}
1
+ {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,2BAA2B,GAAG,qBAAqB,GAAG,sBAAsB,CAAC;AAE1G;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,cAAc,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB;AAqBD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAcxE;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAKxD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,uBAAuB,EAAE,WAAW,CAAC,GAAG,OAAO,CAI9G;AAsDD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,CAAC,QAAQ,CAAC,CA4JlH"}
package/dist/proxy.js CHANGED
@@ -40,6 +40,7 @@ var HOP_BY_HOP_HEADERS = [
40
40
  "transfer-encoding",
41
41
  "upgrade"
42
42
  ];
43
+ var RESPONSE_HEADERS_TO_STRIP = ["content-encoding", "content-length"];
43
44
  function fapiUrlFromPublishableKey(publishableKey) {
44
45
  const frontendApi = (0, import_keys.parsePublishableKey)(publishableKey)?.frontendApi;
45
46
  if (frontendApi?.startsWith("clerk.") && import_constants.LEGACY_DEV_INSTANCE_SUFFIXES.some((suffix) => frontendApi?.endsWith(suffix))) {
@@ -124,9 +125,13 @@ async function clerkFrontendApiProxy(request, options) {
124
125
  );
125
126
  }
126
127
  const fapiBaseUrl = fapiUrlFromPublishableKey(publishableKey);
128
+ const fapiHost = new URL(fapiBaseUrl).host;
127
129
  const targetPath = requestUrl.pathname.slice(proxyPath.length) || "/";
128
- const targetUrl = new URL(targetPath, fapiBaseUrl);
130
+ const targetUrl = new URL(`${fapiBaseUrl}${targetPath}`);
129
131
  targetUrl.search = requestUrl.search;
132
+ if (targetUrl.host !== fapiHost) {
133
+ return createErrorResponse("proxy_request_failed", "Resolved target does not match the expected host", 400);
134
+ }
130
135
  const headers = new Headers();
131
136
  request.headers.forEach((value, key) => {
132
137
  if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {
@@ -137,8 +142,8 @@ async function clerkFrontendApiProxy(request, options) {
137
142
  const proxyUrl = `${publicOrigin}${proxyPath}`;
138
143
  headers.set("Clerk-Proxy-Url", proxyUrl);
139
144
  headers.set("Clerk-Secret-Key", secretKey);
140
- const fapiHost = new URL(fapiBaseUrl).host;
141
145
  headers.set("Host", fapiHost);
146
+ headers.set("Accept-Encoding", "identity");
142
147
  if (!headers.has("X-Forwarded-Host")) {
143
148
  headers.set("X-Forwarded-Host", requestUrl.host);
144
149
  }
@@ -163,8 +168,13 @@ async function clerkFrontendApiProxy(request, options) {
163
168
  const response = await fetch(targetUrl.toString(), fetchOptions);
164
169
  const responseHeaders = new Headers();
165
170
  response.headers.forEach((value, key) => {
166
- if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {
167
- responseHeaders.set(key, value);
171
+ const lower = key.toLowerCase();
172
+ if (!HOP_BY_HOP_HEADERS.includes(lower) && !RESPONSE_HEADERS_TO_STRIP.includes(lower)) {
173
+ if (lower === "set-cookie") {
174
+ responseHeaders.append(key, value);
175
+ } else {
176
+ responseHeaders.set(key, value);
177
+ }
168
178
  }
169
179
  });
170
180
  const locationHeader = response.headers.get("location");
@@ -178,11 +188,15 @@ async function clerkFrontendApiProxy(request, options) {
178
188
  } catch {
179
189
  }
180
190
  }
181
- return new Response(response.body, {
191
+ const proxyResponse = new Response(response.body, {
182
192
  status: response.status,
183
193
  statusText: response.statusText,
184
194
  headers: responseHeaders
185
195
  });
196
+ for (const header of RESPONSE_HEADERS_TO_STRIP) {
197
+ proxyResponse.headers.delete(header);
198
+ }
199
+ return proxyResponse;
186
200
  } catch (error) {
187
201
  const message = error instanceof Error ? error.message : "Unknown error";
188
202
  return createErrorResponse("proxy_request_failed", `Failed to proxy request to Clerk FAPI: ${message}`, 502);
package/dist/proxy.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import {\n DEFAULT_PROXY_PATH,\n LEGACY_DEV_INSTANCE_SUFFIXES,\n LOCAL_ENV_SUFFIXES,\n LOCAL_FAPI_URL,\n PROD_FAPI_URL,\n STAGING_ENV_SUFFIXES,\n STAGING_FAPI_URL,\n} from '@clerk/shared/constants';\nimport { parsePublishableKey } from '@clerk/shared/keys';\n\nexport { DEFAULT_PROXY_PATH } from '@clerk/shared/constants';\n\n/**\n * Options for the Frontend API proxy\n */\nexport interface FrontendApiProxyOptions {\n /**\n * The path prefix for proxy requests. Defaults to `/__clerk`.\n */\n proxyPath?: string;\n /**\n * The Clerk publishable key. Falls back to CLERK_PUBLISHABLE_KEY env var.\n */\n publishableKey?: string;\n /**\n * The Clerk secret key. Falls back to CLERK_SECRET_KEY env var.\n */\n secretKey?: string;\n}\n\n/**\n * Error codes for proxy errors\n */\nexport type ProxyErrorCode = 'proxy_configuration_error' | 'proxy_path_mismatch' | 'proxy_request_failed';\n\n/**\n * Error response structure for proxy errors\n */\nexport interface ProxyError {\n code: ProxyErrorCode;\n message: string;\n}\n\n// Hop-by-hop headers that should not be forwarded\nconst HOP_BY_HOP_HEADERS = [\n 'connection',\n 'keep-alive',\n 'proxy-authenticate',\n 'proxy-authorization',\n 'te',\n 'trailer',\n 'transfer-encoding',\n 'upgrade',\n];\n\n/**\n * Derives the Frontend API URL from a publishable key.\n * @param publishableKey - The Clerk publishable key\n * @returns The Frontend API URL for the environment\n */\nexport function fapiUrlFromPublishableKey(publishableKey: string): string {\n const frontendApi = parsePublishableKey(publishableKey)?.frontendApi;\n\n if (frontendApi?.startsWith('clerk.') && LEGACY_DEV_INSTANCE_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return PROD_FAPI_URL;\n }\n\n if (LOCAL_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return LOCAL_FAPI_URL;\n }\n if (STAGING_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return STAGING_FAPI_URL;\n }\n return PROD_FAPI_URL;\n}\n\n/**\n * Removes trailing slashes from a string without using regex\n * to avoid potential ReDoS concerns flagged by security scanners.\n */\nexport function stripTrailingSlashes(str: string): string {\n while (str.endsWith('/')) {\n str = str.slice(0, -1);\n }\n return str;\n}\n\n/**\n * Checks if a request path matches the proxy path.\n * @param request - The incoming request\n * @param options - Proxy options including the proxy path\n * @returns True if the request matches the proxy path\n */\nexport function matchProxyPath(request: Request, options?: Pick<FrontendApiProxyOptions, 'proxyPath'>): boolean {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const url = new URL(request.url);\n return url.pathname === proxyPath || url.pathname.startsWith(proxyPath + '/');\n}\n\n/**\n * Creates a JSON error response\n */\nfunction createErrorResponse(code: ProxyErrorCode, message: string, status: number): Response {\n const error: ProxyError = { code, message };\n return new Response(JSON.stringify({ errors: [error] }), {\n status,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n}\n\n/**\n * Derives the public-facing origin from forwarded headers, falling back to the raw request URL.\n * Behind a reverse proxy, request.url is typically localhost, but the Clerk-Proxy-Url header\n * and Location rewrites must use the origin visible to the browser.\n */\nfunction derivePublicOrigin(request: Request, requestUrl: URL): string {\n const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim();\n const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim();\n\n if (forwardedProto && forwardedHost) {\n return `${forwardedProto}://${forwardedHost}`;\n }\n\n return requestUrl.origin;\n}\n\n/**\n * Gets the client IP address from various headers\n */\nfunction getClientIp(request: Request): string | undefined {\n const cfConnectingIp = request.headers.get('cf-connecting-ip');\n if (cfConnectingIp) {\n return cfConnectingIp;\n }\n\n const xRealIp = request.headers.get('x-real-ip');\n if (xRealIp) {\n return xRealIp;\n }\n\n const xForwardedFor = request.headers.get('x-forwarded-for');\n if (xForwardedFor) {\n // Take the first IP in the chain\n return xForwardedFor.split(',')[0]?.trim();\n }\n\n return undefined;\n}\n\n/**\n * Proxies a request to Clerk's Frontend API.\n *\n * This function handles forwarding requests from your application to Clerk's\n * Frontend API, enabling scenarios where direct communication with Clerk's API\n * is blocked or needs to go through your application server.\n *\n * @param request - The incoming request to proxy\n * @param options - Proxy configuration options\n * @returns A Response from Clerk's Frontend API\n *\n * @example\n * ```typescript\n * import { clerkFrontendApiProxy } from '@clerk/backend/proxy';\n *\n * // In a route handler\n * const response = await clerkFrontendApiProxy(request, {\n * proxyPath: '/__clerk',\n * publishableKey: process.env.CLERK_PUBLISHABLE_KEY,\n * secretKey: process.env.CLERK_SECRET_KEY,\n * });\n * ```\n */\nexport async function clerkFrontendApiProxy(request: Request, options?: FrontendApiProxyOptions): Promise<Response> {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const publishableKey =\n options?.publishableKey || (typeof process !== 'undefined' ? process.env?.CLERK_PUBLISHABLE_KEY : undefined);\n const secretKey = options?.secretKey || (typeof process !== 'undefined' ? process.env?.CLERK_SECRET_KEY : undefined);\n\n // Validate configuration\n if (!publishableKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing publishableKey. Provide it in options or set CLERK_PUBLISHABLE_KEY environment variable.',\n 500,\n );\n }\n\n if (!secretKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing secretKey. Provide it in options or set CLERK_SECRET_KEY environment variable.',\n 500,\n );\n }\n\n // Get the request URL and validate path\n const requestUrl = new URL(request.url);\n const pathMatches = requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/');\n if (!pathMatches) {\n return createErrorResponse(\n 'proxy_path_mismatch',\n `Request path \"${requestUrl.pathname}\" does not match proxy path \"${proxyPath}\"`,\n 400,\n );\n }\n\n // Derive the FAPI URL and construct the target URL\n const fapiBaseUrl = fapiUrlFromPublishableKey(publishableKey);\n const targetPath = requestUrl.pathname.slice(proxyPath.length) || '/';\n const targetUrl = new URL(targetPath, fapiBaseUrl);\n targetUrl.search = requestUrl.search;\n\n // Build headers for the proxied request\n const headers = new Headers();\n\n // Copy original headers, excluding hop-by-hop headers\n request.headers.forEach((value, key) => {\n if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {\n headers.set(key, value);\n }\n });\n\n // Set required Clerk proxy headers\n // Use the public origin (from forwarded headers) so the Clerk-Proxy-Url\n // points to the browser-visible host, not localhost behind a reverse proxy.\n const publicOrigin = derivePublicOrigin(request, requestUrl);\n const proxyUrl = `${publicOrigin}${proxyPath}`;\n headers.set('Clerk-Proxy-Url', proxyUrl);\n headers.set('Clerk-Secret-Key', secretKey);\n\n // Set the host header to the FAPI host\n const fapiHost = new URL(fapiBaseUrl).host;\n headers.set('Host', fapiHost);\n\n // Set X-Forwarded-* headers for proxy awareness\n // Only set these if not already present (preserve values from upstream proxies)\n if (!headers.has('X-Forwarded-Host')) {\n headers.set('X-Forwarded-Host', requestUrl.host);\n }\n if (!headers.has('X-Forwarded-Proto')) {\n headers.set('X-Forwarded-Proto', requestUrl.protocol.replace(':', ''));\n }\n\n // Set X-Forwarded-For to the client IP\n // In multi-proxy scenarios, we prefer authoritative headers (CF-Connecting-IP, X-Real-IP)\n // over the existing X-Forwarded-For chain, as they provide the true client IP\n const clientIp = getClientIp(request);\n if (clientIp) {\n headers.set('X-Forwarded-For', clientIp);\n }\n\n // Determine if request has a body\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(request.method);\n\n try {\n // Make the proxied request\n const fetchOptions: RequestInit = {\n method: request.method,\n headers,\n // @ts-expect-error - duplex is required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n };\n\n // Only include body for methods that support it\n if (hasBody && request.body) {\n fetchOptions.body = request.body;\n }\n\n const response = await fetch(targetUrl.toString(), fetchOptions);\n\n // Build response headers, excluding hop-by-hop headers\n const responseHeaders = new Headers();\n response.headers.forEach((value, key) => {\n if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {\n responseHeaders.set(key, value);\n }\n });\n\n // Rewrite Location header for redirects to go through the proxy\n const locationHeader = response.headers.get('location');\n if (locationHeader) {\n try {\n const locationUrl = new URL(locationHeader, fapiBaseUrl);\n // Check if the redirect points to the FAPI host\n if (locationUrl.host === fapiHost) {\n // Rewrite to go through the proxy\n const rewrittenLocation = `${proxyUrl}${locationUrl.pathname}${locationUrl.search}${locationUrl.hash}`;\n responseHeaders.set('Location', rewrittenLocation);\n }\n } catch {\n // If URL parsing fails, leave the Location header as-is (could be a relative URL)\n }\n }\n\n return new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers: responseHeaders,\n });\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Unknown error';\n return createErrorResponse('proxy_request_failed', `Failed to proxy request to Clerk FAPI: ${message}`, 502);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAQO;AACP,kBAAoC;AAEpC,IAAAA,oBAAmC;AAkCnC,IAAM,qBAAqB;AAAA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ACnD,wBAAgB,IAAI,KAAK,KAAK;AAAA,MAChC;AAAA,IACF,CAAC;AAGD,UAAM,iBAAiB,SAAS,QAAQ,IAAI,UAAU;AACtD,QAAI,gBAAgB;AAClB,UAAI;AACF,cAAM,cAAc,IAAI,IAAI,gBAAgB,WAAW;AAEvD,YAAI,YAAY,SAAS,UAAU;AAEjC,gBAAM,oBAAoB,GAAG,QAAQ,GAAG,YAAY,QAAQ,GAAG,YAAY,MAAM,GAAG,YAAY,IAAI;AACpG,0BAAgB,IAAI,YAAY,iBAAiB;AAAA,QACnD;AAAA,MACF,QAAQ;AAAA,MAER;AAAA,IACF;AAEA,WAAO,IAAI,SAAS,SAAS,MAAM;AAAA,MACjC,QAAQ,SAAS;AAAA,MACjB,YAAY,SAAS;AAAA,MACrB,SAAS;AAAA,IACX,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AACzD,WAAO,oBAAoB,wBAAwB,0CAA0C,OAAO,IAAI,GAAG;AAAA,EAC7G;AACF;","names":["import_constants"]}
1
+ {"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import {\n DEFAULT_PROXY_PATH,\n LEGACY_DEV_INSTANCE_SUFFIXES,\n LOCAL_ENV_SUFFIXES,\n LOCAL_FAPI_URL,\n PROD_FAPI_URL,\n STAGING_ENV_SUFFIXES,\n STAGING_FAPI_URL,\n} from '@clerk/shared/constants';\nimport { parsePublishableKey } from '@clerk/shared/keys';\n\nexport { DEFAULT_PROXY_PATH } from '@clerk/shared/constants';\n\n/**\n * Options for the Frontend API proxy\n */\nexport interface FrontendApiProxyOptions {\n /**\n * The path prefix for proxy requests. Defaults to `/__clerk`.\n */\n proxyPath?: string;\n /**\n * The Clerk publishable key. Falls back to CLERK_PUBLISHABLE_KEY env var.\n */\n publishableKey?: string;\n /**\n * The Clerk secret key. Falls back to CLERK_SECRET_KEY env var.\n */\n secretKey?: string;\n}\n\n/**\n * Error codes for proxy errors\n */\nexport type ProxyErrorCode = 'proxy_configuration_error' | 'proxy_path_mismatch' | 'proxy_request_failed';\n\n/**\n * Error response structure for proxy errors\n */\nexport interface ProxyError {\n code: ProxyErrorCode;\n message: string;\n}\n\n// Hop-by-hop headers that should not be forwarded\nconst HOP_BY_HOP_HEADERS = [\n 'connection',\n 'keep-alive',\n 'proxy-authenticate',\n 'proxy-authorization',\n 'te',\n 'trailer',\n 'transfer-encoding',\n 'upgrade',\n];\n\n// Headers to strip from proxied responses. fetch() auto-decompresses\n// response bodies, so Content-Encoding no longer describes the body\n// and Content-Length reflects the compressed size. We request identity\n// encoding upstream to avoid the double compression pass, but strip\n// these defensively since servers may ignore Accept-Encoding: identity.\nconst RESPONSE_HEADERS_TO_STRIP = ['content-encoding', 'content-length'];\n\n/**\n * Derives the Frontend API URL from a publishable key.\n * @param publishableKey - The Clerk publishable key\n * @returns The Frontend API URL for the environment\n */\nexport function fapiUrlFromPublishableKey(publishableKey: string): string {\n const frontendApi = parsePublishableKey(publishableKey)?.frontendApi;\n\n if (frontendApi?.startsWith('clerk.') && LEGACY_DEV_INSTANCE_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return PROD_FAPI_URL;\n }\n\n if (LOCAL_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return LOCAL_FAPI_URL;\n }\n if (STAGING_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return STAGING_FAPI_URL;\n }\n return PROD_FAPI_URL;\n}\n\n/**\n * Removes trailing slashes from a string without using regex\n * to avoid potential ReDoS concerns flagged by security scanners.\n */\nexport function stripTrailingSlashes(str: string): string {\n while (str.endsWith('/')) {\n str = str.slice(0, -1);\n }\n return str;\n}\n\n/**\n * Checks if a request path matches the proxy path.\n * @param request - The incoming request\n * @param options - Proxy options including the proxy path\n * @returns True if the request matches the proxy path\n */\nexport function matchProxyPath(request: Request, options?: Pick<FrontendApiProxyOptions, 'proxyPath'>): boolean {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const url = new URL(request.url);\n return url.pathname === proxyPath || url.pathname.startsWith(proxyPath + '/');\n}\n\n/**\n * Creates a JSON error response\n */\nfunction createErrorResponse(code: ProxyErrorCode, message: string, status: number): Response {\n const error: ProxyError = { code, message };\n return new Response(JSON.stringify({ errors: [error] }), {\n status,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n}\n\n/**\n * Derives the public-facing origin from forwarded headers, falling back to the raw request URL.\n * Behind a reverse proxy, request.url is typically localhost, but the Clerk-Proxy-Url header\n * and Location rewrites must use the origin visible to the browser.\n */\nfunction derivePublicOrigin(request: Request, requestUrl: URL): string {\n const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim();\n const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim();\n\n if (forwardedProto && forwardedHost) {\n return `${forwardedProto}://${forwardedHost}`;\n }\n\n return requestUrl.origin;\n}\n\n/**\n * Gets the client IP address from various headers\n */\nfunction getClientIp(request: Request): string | undefined {\n const cfConnectingIp = request.headers.get('cf-connecting-ip');\n if (cfConnectingIp) {\n return cfConnectingIp;\n }\n\n const xRealIp = request.headers.get('x-real-ip');\n if (xRealIp) {\n return xRealIp;\n }\n\n const xForwardedFor = request.headers.get('x-forwarded-for');\n if (xForwardedFor) {\n // Take the first IP in the chain\n return xForwardedFor.split(',')[0]?.trim();\n }\n\n return undefined;\n}\n\n/**\n * Proxies a request to Clerk's Frontend API.\n *\n * This function handles forwarding requests from your application to Clerk's\n * Frontend API, enabling scenarios where direct communication with Clerk's API\n * is blocked or needs to go through your application server.\n *\n * @param request - The incoming request to proxy\n * @param options - Proxy configuration options\n * @returns A Response from Clerk's Frontend API\n *\n * @example\n * ```typescript\n * import { clerkFrontendApiProxy } from '@clerk/backend/proxy';\n *\n * // In a route handler\n * const response = await clerkFrontendApiProxy(request, {\n * proxyPath: '/__clerk',\n * publishableKey: process.env.CLERK_PUBLISHABLE_KEY,\n * secretKey: process.env.CLERK_SECRET_KEY,\n * });\n * ```\n */\nexport async function clerkFrontendApiProxy(request: Request, options?: FrontendApiProxyOptions): Promise<Response> {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const publishableKey =\n options?.publishableKey || (typeof process !== 'undefined' ? process.env?.CLERK_PUBLISHABLE_KEY : undefined);\n const secretKey = options?.secretKey || (typeof process !== 'undefined' ? process.env?.CLERK_SECRET_KEY : undefined);\n\n // Validate configuration\n if (!publishableKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing publishableKey. Provide it in options or set CLERK_PUBLISHABLE_KEY environment variable.',\n 500,\n );\n }\n\n if (!secretKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing secretKey. Provide it in options or set CLERK_SECRET_KEY environment variable.',\n 500,\n );\n }\n\n // Get the request URL and validate path\n const requestUrl = new URL(request.url);\n const pathMatches = requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/');\n if (!pathMatches) {\n return createErrorResponse(\n 'proxy_path_mismatch',\n `Request path \"${requestUrl.pathname}\" does not match proxy path \"${proxyPath}\"`,\n 400,\n );\n }\n\n // Derive the FAPI URL and construct the target URL.\n // Use string concatenation instead of `new URL(path, base)` to avoid\n // protocol-relative resolution (e.g., \"//evil.com\" resolving to a different host).\n const fapiBaseUrl = fapiUrlFromPublishableKey(publishableKey);\n const fapiHost = new URL(fapiBaseUrl).host;\n const targetPath = requestUrl.pathname.slice(proxyPath.length) || '/';\n const targetUrl = new URL(`${fapiBaseUrl}${targetPath}`);\n targetUrl.search = requestUrl.search;\n\n if (targetUrl.host !== fapiHost) {\n return createErrorResponse('proxy_request_failed', 'Resolved target does not match the expected host', 400);\n }\n\n // Build headers for the proxied request\n const headers = new Headers();\n\n // Copy original headers, excluding hop-by-hop headers\n request.headers.forEach((value, key) => {\n if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {\n headers.set(key, value);\n }\n });\n\n // Set required Clerk proxy headers\n // Use the public origin (from forwarded headers) so the Clerk-Proxy-Url\n // points to the browser-visible host, not localhost behind a reverse proxy.\n const publicOrigin = derivePublicOrigin(request, requestUrl);\n const proxyUrl = `${publicOrigin}${proxyPath}`;\n headers.set('Clerk-Proxy-Url', proxyUrl);\n headers.set('Clerk-Secret-Key', secretKey);\n\n // Set the host header to the FAPI host\n headers.set('Host', fapiHost);\n\n // Request uncompressed responses to avoid a double compression pass.\n // fetch() auto-decompresses, so without this FAPI compresses → fetch\n // decompresses → the serving layer re-compresses for the browser.\n headers.set('Accept-Encoding', 'identity');\n\n // Set X-Forwarded-* headers for proxy awareness\n // Only set these if not already present (preserve values from upstream proxies)\n if (!headers.has('X-Forwarded-Host')) {\n headers.set('X-Forwarded-Host', requestUrl.host);\n }\n if (!headers.has('X-Forwarded-Proto')) {\n headers.set('X-Forwarded-Proto', requestUrl.protocol.replace(':', ''));\n }\n\n // Set X-Forwarded-For to the client IP\n // In multi-proxy scenarios, we prefer authoritative headers (CF-Connecting-IP, X-Real-IP)\n // over the existing X-Forwarded-For chain, as they provide the true client IP\n const clientIp = getClientIp(request);\n if (clientIp) {\n headers.set('X-Forwarded-For', clientIp);\n }\n\n // Determine if request has a body\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(request.method);\n\n try {\n // Make the proxied request\n const fetchOptions: RequestInit = {\n method: request.method,\n headers,\n // @ts-expect-error - duplex is required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n };\n\n // Only include body for methods that support it\n if (hasBody && request.body) {\n fetchOptions.body = request.body;\n }\n\n const response = await fetch(targetUrl.toString(), fetchOptions);\n\n // Build response headers, excluding hop-by-hop and encoding headers\n const responseHeaders = new Headers();\n response.headers.forEach((value, key) => {\n const lower = key.toLowerCase();\n if (!HOP_BY_HOP_HEADERS.includes(lower) && !RESPONSE_HEADERS_TO_STRIP.includes(lower)) {\n if (lower === 'set-cookie') {\n responseHeaders.append(key, value);\n } else {\n responseHeaders.set(key, value);\n }\n }\n });\n\n // Rewrite Location header for redirects to go through the proxy\n const locationHeader = response.headers.get('location');\n if (locationHeader) {\n try {\n const locationUrl = new URL(locationHeader, fapiBaseUrl);\n // Check if the redirect points to the FAPI host\n if (locationUrl.host === fapiHost) {\n // Rewrite to go through the proxy\n const rewrittenLocation = `${proxyUrl}${locationUrl.pathname}${locationUrl.search}${locationUrl.hash}`;\n responseHeaders.set('Location', rewrittenLocation);\n }\n } catch {\n // If URL parsing fails, leave the Location header as-is (could be a relative URL)\n }\n }\n\n const proxyResponse = new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers: responseHeaders,\n });\n\n // Some runtimes may re-add Content-Length when constructing the Response.\n // Delete explicitly since fetch() decoded the body and the original values\n // no longer reflect the actual content.\n for (const header of RESPONSE_HEADERS_TO_STRIP) {\n proxyResponse.headers.delete(header);\n }\n\n return proxyResponse;\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Unknown error';\n return createErrorResponse('proxy_request_failed', `Failed to proxy request to Clerk FAPI: ${message}`, 502);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAQO;AACP,kBAAoC;AAEpC,IAAAA,oBAAmC;AAkCnC,IAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOA,IAAM,4BAA4B,CAAC,oBAAoB,gBAAgB;AAOhE,SAAS,0BAA0B,gBAAgC;AACxE,QAAM,kBAAc,iCAAoB,cAAc,GAAG;AAEzD,MAAI,aAAa,WAAW,QAAQ,KAAK,8CAA6B,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACnH,WAAO;AAAA,EACT;AAEA,MAAI,oCAAmB,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACpE,WAAO;AAAA,EACT;AACA,MAAI,sCAAq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,IAAI,UAAU;AACtD,QAAI,gBAAgB;AAClB,UAAI;AACF,cAAM,cAAc,IAAI,IAAI,gBAAgB,WAAW;AAEvD,YAAI,YAAY,SAAS,UAAU;AAEjC,gBAAM,oBAAoB,GAAG,QAAQ,GAAG,YAAY,QAAQ,GAAG,YAAY,MAAM,GAAG,YAAY,IAAI;AACpG,0BAAgB,IAAI,YAAY,iBAAiB;AAAA,QACnD;AAAA,MACF,QAAQ;AAAA,MAER;AAAA,IACF;AAEA,UAAM,gBAAgB,IAAI,SAAS,SAAS,MAAM;AAAA,MAChD,QAAQ,SAAS;AAAA,MACjB,YAAY,SAAS;AAAA,MACrB,SAAS;AAAA,IACX,CAAC;AAKD,eAAW,UAAU,2BAA2B;AAC9C,oBAAc,QAAQ,OAAO,MAAM;AAAA,IACrC;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,UAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AACzD,WAAO,oBAAoB,wBAAwB,0CAA0C,OAAO,IAAI,GAAG;AAAA,EAC7G;AACF;","names":["import_constants"]}
package/dist/proxy.mjs CHANGED
@@ -22,6 +22,7 @@ var HOP_BY_HOP_HEADERS = [
22
22
  "transfer-encoding",
23
23
  "upgrade"
24
24
  ];
25
+ var RESPONSE_HEADERS_TO_STRIP = ["content-encoding", "content-length"];
25
26
  function fapiUrlFromPublishableKey(publishableKey) {
26
27
  const frontendApi = parsePublishableKey(publishableKey)?.frontendApi;
27
28
  if (frontendApi?.startsWith("clerk.") && LEGACY_DEV_INSTANCE_SUFFIXES.some((suffix) => frontendApi?.endsWith(suffix))) {
@@ -106,9 +107,13 @@ async function clerkFrontendApiProxy(request, options) {
106
107
  );
107
108
  }
108
109
  const fapiBaseUrl = fapiUrlFromPublishableKey(publishableKey);
110
+ const fapiHost = new URL(fapiBaseUrl).host;
109
111
  const targetPath = requestUrl.pathname.slice(proxyPath.length) || "/";
110
- const targetUrl = new URL(targetPath, fapiBaseUrl);
112
+ const targetUrl = new URL(`${fapiBaseUrl}${targetPath}`);
111
113
  targetUrl.search = requestUrl.search;
114
+ if (targetUrl.host !== fapiHost) {
115
+ return createErrorResponse("proxy_request_failed", "Resolved target does not match the expected host", 400);
116
+ }
112
117
  const headers = new Headers();
113
118
  request.headers.forEach((value, key) => {
114
119
  if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {
@@ -119,8 +124,8 @@ async function clerkFrontendApiProxy(request, options) {
119
124
  const proxyUrl = `${publicOrigin}${proxyPath}`;
120
125
  headers.set("Clerk-Proxy-Url", proxyUrl);
121
126
  headers.set("Clerk-Secret-Key", secretKey);
122
- const fapiHost = new URL(fapiBaseUrl).host;
123
127
  headers.set("Host", fapiHost);
128
+ headers.set("Accept-Encoding", "identity");
124
129
  if (!headers.has("X-Forwarded-Host")) {
125
130
  headers.set("X-Forwarded-Host", requestUrl.host);
126
131
  }
@@ -145,8 +150,13 @@ async function clerkFrontendApiProxy(request, options) {
145
150
  const response = await fetch(targetUrl.toString(), fetchOptions);
146
151
  const responseHeaders = new Headers();
147
152
  response.headers.forEach((value, key) => {
148
- if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {
149
- responseHeaders.set(key, value);
153
+ const lower = key.toLowerCase();
154
+ if (!HOP_BY_HOP_HEADERS.includes(lower) && !RESPONSE_HEADERS_TO_STRIP.includes(lower)) {
155
+ if (lower === "set-cookie") {
156
+ responseHeaders.append(key, value);
157
+ } else {
158
+ responseHeaders.set(key, value);
159
+ }
150
160
  }
151
161
  });
152
162
  const locationHeader = response.headers.get("location");
@@ -160,11 +170,15 @@ async function clerkFrontendApiProxy(request, options) {
160
170
  } catch {
161
171
  }
162
172
  }
163
- return new Response(response.body, {
173
+ const proxyResponse = new Response(response.body, {
164
174
  status: response.status,
165
175
  statusText: response.statusText,
166
176
  headers: responseHeaders
167
177
  });
178
+ for (const header of RESPONSE_HEADERS_TO_STRIP) {
179
+ proxyResponse.headers.delete(header);
180
+ }
181
+ return proxyResponse;
168
182
  } catch (error) {
169
183
  const message = error instanceof Error ? error.message : "Unknown error";
170
184
  return createErrorResponse("proxy_request_failed", `Failed to proxy request to Clerk FAPI: ${message}`, 502);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import {\n DEFAULT_PROXY_PATH,\n LEGACY_DEV_INSTANCE_SUFFIXES,\n LOCAL_ENV_SUFFIXES,\n LOCAL_FAPI_URL,\n PROD_FAPI_URL,\n STAGING_ENV_SUFFIXES,\n STAGING_FAPI_URL,\n} from '@clerk/shared/constants';\nimport { parsePublishableKey } from '@clerk/shared/keys';\n\nexport { DEFAULT_PROXY_PATH } from '@clerk/shared/constants';\n\n/**\n * Options for the Frontend API proxy\n */\nexport interface FrontendApiProxyOptions {\n /**\n * The path prefix for proxy requests. Defaults to `/__clerk`.\n */\n proxyPath?: string;\n /**\n * The Clerk publishable key. Falls back to CLERK_PUBLISHABLE_KEY env var.\n */\n publishableKey?: string;\n /**\n * The Clerk secret key. Falls back to CLERK_SECRET_KEY env var.\n */\n secretKey?: string;\n}\n\n/**\n * Error codes for proxy errors\n */\nexport type ProxyErrorCode = 'proxy_configuration_error' | 'proxy_path_mismatch' | 'proxy_request_failed';\n\n/**\n * Error response structure for proxy errors\n */\nexport interface ProxyError {\n code: ProxyErrorCode;\n message: string;\n}\n\n// Hop-by-hop headers that should not be forwarded\nconst HOP_BY_HOP_HEADERS = [\n 'connection',\n 'keep-alive',\n 'proxy-authenticate',\n 'proxy-authorization',\n 'te',\n 'trailer',\n 'transfer-encoding',\n 'upgrade',\n];\n\n/**\n * Derives the Frontend API URL from a publishable key.\n * @param publishableKey - The Clerk publishable key\n * @returns The Frontend API URL for the environment\n */\nexport function fapiUrlFromPublishableKey(publishableKey: string): string {\n const frontendApi = parsePublishableKey(publishableKey)?.frontendApi;\n\n if (frontendApi?.startsWith('clerk.') && LEGACY_DEV_INSTANCE_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return PROD_FAPI_URL;\n }\n\n if (LOCAL_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return LOCAL_FAPI_URL;\n }\n if (STAGING_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return STAGING_FAPI_URL;\n }\n return PROD_FAPI_URL;\n}\n\n/**\n * Removes trailing slashes from a string without using regex\n * to avoid potential ReDoS concerns flagged by security scanners.\n */\nexport function stripTrailingSlashes(str: string): string {\n while (str.endsWith('/')) {\n str = str.slice(0, -1);\n }\n return str;\n}\n\n/**\n * Checks if a request path matches the proxy path.\n * @param request - The incoming request\n * @param options - Proxy options including the proxy path\n * @returns True if the request matches the proxy path\n */\nexport function matchProxyPath(request: Request, options?: Pick<FrontendApiProxyOptions, 'proxyPath'>): boolean {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const url = new URL(request.url);\n return url.pathname === proxyPath || url.pathname.startsWith(proxyPath + '/');\n}\n\n/**\n * Creates a JSON error response\n */\nfunction createErrorResponse(code: ProxyErrorCode, message: string, status: number): Response {\n const error: ProxyError = { code, message };\n return new Response(JSON.stringify({ errors: [error] }), {\n status,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n}\n\n/**\n * Derives the public-facing origin from forwarded headers, falling back to the raw request URL.\n * Behind a reverse proxy, request.url is typically localhost, but the Clerk-Proxy-Url header\n * and Location rewrites must use the origin visible to the browser.\n */\nfunction derivePublicOrigin(request: Request, requestUrl: URL): string {\n const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim();\n const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim();\n\n if (forwardedProto && forwardedHost) {\n return `${forwardedProto}://${forwardedHost}`;\n }\n\n return requestUrl.origin;\n}\n\n/**\n * Gets the client IP address from various headers\n */\nfunction getClientIp(request: Request): string | undefined {\n const cfConnectingIp = request.headers.get('cf-connecting-ip');\n if (cfConnectingIp) {\n return cfConnectingIp;\n }\n\n const xRealIp = request.headers.get('x-real-ip');\n if (xRealIp) {\n return xRealIp;\n }\n\n const xForwardedFor = request.headers.get('x-forwarded-for');\n if (xForwardedFor) {\n // Take the first IP in the chain\n return xForwardedFor.split(',')[0]?.trim();\n }\n\n return undefined;\n}\n\n/**\n * Proxies a request to Clerk's Frontend API.\n *\n * This function handles forwarding requests from your application to Clerk's\n * Frontend API, enabling scenarios where direct communication with Clerk's API\n * is blocked or needs to go through your application server.\n *\n * @param request - The incoming request to proxy\n * @param options - Proxy configuration options\n * @returns A Response from Clerk's Frontend API\n *\n * @example\n * ```typescript\n * import { clerkFrontendApiProxy } from '@clerk/backend/proxy';\n *\n * // In a route handler\n * const response = await clerkFrontendApiProxy(request, {\n * proxyPath: '/__clerk',\n * publishableKey: process.env.CLERK_PUBLISHABLE_KEY,\n * secretKey: process.env.CLERK_SECRET_KEY,\n * });\n * ```\n */\nexport async function clerkFrontendApiProxy(request: Request, options?: FrontendApiProxyOptions): Promise<Response> {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const publishableKey =\n options?.publishableKey || (typeof process !== 'undefined' ? process.env?.CLERK_PUBLISHABLE_KEY : undefined);\n const secretKey = options?.secretKey || (typeof process !== 'undefined' ? process.env?.CLERK_SECRET_KEY : undefined);\n\n // Validate configuration\n if (!publishableKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing publishableKey. Provide it in options or set CLERK_PUBLISHABLE_KEY environment variable.',\n 500,\n );\n }\n\n if (!secretKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing secretKey. Provide it in options or set CLERK_SECRET_KEY environment variable.',\n 500,\n );\n }\n\n // Get the request URL and validate path\n const requestUrl = new URL(request.url);\n const pathMatches = requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/');\n if (!pathMatches) {\n return createErrorResponse(\n 'proxy_path_mismatch',\n `Request path \"${requestUrl.pathname}\" does not match proxy path \"${proxyPath}\"`,\n 400,\n );\n }\n\n // Derive the FAPI URL and construct the target URL\n const fapiBaseUrl = fapiUrlFromPublishableKey(publishableKey);\n const targetPath = requestUrl.pathname.slice(proxyPath.length) || '/';\n const targetUrl = new URL(targetPath, fapiBaseUrl);\n targetUrl.search = requestUrl.search;\n\n // Build headers for the proxied request\n const headers = new Headers();\n\n // Copy original headers, excluding hop-by-hop headers\n request.headers.forEach((value, key) => {\n if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {\n headers.set(key, value);\n }\n });\n\n // Set required Clerk proxy headers\n // Use the public origin (from forwarded headers) so the Clerk-Proxy-Url\n // points to the browser-visible host, not localhost behind a reverse proxy.\n const publicOrigin = derivePublicOrigin(request, requestUrl);\n const proxyUrl = `${publicOrigin}${proxyPath}`;\n headers.set('Clerk-Proxy-Url', proxyUrl);\n headers.set('Clerk-Secret-Key', secretKey);\n\n // Set the host header to the FAPI host\n const fapiHost = new URL(fapiBaseUrl).host;\n headers.set('Host', fapiHost);\n\n // Set X-Forwarded-* headers for proxy awareness\n // Only set these if not already present (preserve values from upstream proxies)\n if (!headers.has('X-Forwarded-Host')) {\n headers.set('X-Forwarded-Host', requestUrl.host);\n }\n if (!headers.has('X-Forwarded-Proto')) {\n headers.set('X-Forwarded-Proto', requestUrl.protocol.replace(':', ''));\n }\n\n // Set X-Forwarded-For to the client IP\n // In multi-proxy scenarios, we prefer authoritative headers (CF-Connecting-IP, X-Real-IP)\n // over the existing X-Forwarded-For chain, as they provide the true client IP\n const clientIp = getClientIp(request);\n if (clientIp) {\n headers.set('X-Forwarded-For', clientIp);\n }\n\n // Determine if request has a body\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(request.method);\n\n try {\n // Make the proxied request\n const fetchOptions: RequestInit = {\n method: request.method,\n headers,\n // @ts-expect-error - duplex is required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n };\n\n // Only include body for methods that support it\n if (hasBody && request.body) {\n fetchOptions.body = request.body;\n }\n\n const response = await fetch(targetUrl.toString(), fetchOptions);\n\n // Build response headers, excluding hop-by-hop headers\n const responseHeaders = new Headers();\n response.headers.forEach((value, key) => {\n if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {\n responseHeaders.set(key, value);\n }\n });\n\n // Rewrite Location header for redirects to go through the proxy\n const locationHeader = response.headers.get('location');\n if (locationHeader) {\n try {\n const locationUrl = new URL(locationHeader, fapiBaseUrl);\n // Check if the redirect points to the FAPI host\n if (locationUrl.host === fapiHost) {\n // Rewrite to go through the proxy\n const rewrittenLocation = `${proxyUrl}${locationUrl.pathname}${locationUrl.search}${locationUrl.hash}`;\n responseHeaders.set('Location', rewrittenLocation);\n }\n } catch {\n // If URL parsing fails, leave the Location header as-is (could be a relative URL)\n }\n }\n\n return new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers: responseHeaders,\n });\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Unknown error';\n return createErrorResponse('proxy_request_failed', `Failed to proxy request to Clerk FAPI: ${message}`, 502);\n }\n}\n"],"mappings":";;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,2BAA2B;AAEpC,SAAS,sBAAAA,2BAA0B;AAkCnC,IAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOO,SAAS,0BAA0B,gBAAgC;AACxE,QAAM,cAAc,oBAAoB,cAAc,GAAG;AAEzD,MAAI,aAAa,WAAW,QAAQ,KAAK,6BAA6B,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACnH,WAAO;AAAA,EACT;AAEA,MAAI,mBAAmB,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACpE,WAAO;AAAA,EACT;AACA,MAAI,qBAAqB,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACtE,WAAO;AAAA,EACT;AACA,SA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,SAAS;AAAA,MACjB,YAAY,SAAS;AAAA,MACrB,SAAS;AAAA,IACX,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AACzD,WAAO,oBAAoB,wBAAwB,0CAA0C,OAAO,IAAI,GAAG;AAAA,EAC7G;AACF;","names":["DEFAULT_PROXY_PATH"]}
1
+ {"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import {\n DEFAULT_PROXY_PATH,\n LEGACY_DEV_INSTANCE_SUFFIXES,\n LOCAL_ENV_SUFFIXES,\n LOCAL_FAPI_URL,\n PROD_FAPI_URL,\n STAGING_ENV_SUFFIXES,\n STAGING_FAPI_URL,\n} from '@clerk/shared/constants';\nimport { parsePublishableKey } from '@clerk/shared/keys';\n\nexport { DEFAULT_PROXY_PATH } from '@clerk/shared/constants';\n\n/**\n * Options for the Frontend API proxy\n */\nexport interface FrontendApiProxyOptions {\n /**\n * The path prefix for proxy requests. Defaults to `/__clerk`.\n */\n proxyPath?: string;\n /**\n * The Clerk publishable key. Falls back to CLERK_PUBLISHABLE_KEY env var.\n */\n publishableKey?: string;\n /**\n * The Clerk secret key. Falls back to CLERK_SECRET_KEY env var.\n */\n secretKey?: string;\n}\n\n/**\n * Error codes for proxy errors\n */\nexport type ProxyErrorCode = 'proxy_configuration_error' | 'proxy_path_mismatch' | 'proxy_request_failed';\n\n/**\n * Error response structure for proxy errors\n */\nexport interface ProxyError {\n code: ProxyErrorCode;\n message: string;\n}\n\n// Hop-by-hop headers that should not be forwarded\nconst HOP_BY_HOP_HEADERS = [\n 'connection',\n 'keep-alive',\n 'proxy-authenticate',\n 'proxy-authorization',\n 'te',\n 'trailer',\n 'transfer-encoding',\n 'upgrade',\n];\n\n// Headers to strip from proxied responses. fetch() auto-decompresses\n// response bodies, so Content-Encoding no longer describes the body\n// and Content-Length reflects the compressed size. We request identity\n// encoding upstream to avoid the double compression pass, but strip\n// these defensively since servers may ignore Accept-Encoding: identity.\nconst RESPONSE_HEADERS_TO_STRIP = ['content-encoding', 'content-length'];\n\n/**\n * Derives the Frontend API URL from a publishable key.\n * @param publishableKey - The Clerk publishable key\n * @returns The Frontend API URL for the environment\n */\nexport function fapiUrlFromPublishableKey(publishableKey: string): string {\n const frontendApi = parsePublishableKey(publishableKey)?.frontendApi;\n\n if (frontendApi?.startsWith('clerk.') && LEGACY_DEV_INSTANCE_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return PROD_FAPI_URL;\n }\n\n if (LOCAL_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return LOCAL_FAPI_URL;\n }\n if (STAGING_ENV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) {\n return STAGING_FAPI_URL;\n }\n return PROD_FAPI_URL;\n}\n\n/**\n * Removes trailing slashes from a string without using regex\n * to avoid potential ReDoS concerns flagged by security scanners.\n */\nexport function stripTrailingSlashes(str: string): string {\n while (str.endsWith('/')) {\n str = str.slice(0, -1);\n }\n return str;\n}\n\n/**\n * Checks if a request path matches the proxy path.\n * @param request - The incoming request\n * @param options - Proxy options including the proxy path\n * @returns True if the request matches the proxy path\n */\nexport function matchProxyPath(request: Request, options?: Pick<FrontendApiProxyOptions, 'proxyPath'>): boolean {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const url = new URL(request.url);\n return url.pathname === proxyPath || url.pathname.startsWith(proxyPath + '/');\n}\n\n/**\n * Creates a JSON error response\n */\nfunction createErrorResponse(code: ProxyErrorCode, message: string, status: number): Response {\n const error: ProxyError = { code, message };\n return new Response(JSON.stringify({ errors: [error] }), {\n status,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n}\n\n/**\n * Derives the public-facing origin from forwarded headers, falling back to the raw request URL.\n * Behind a reverse proxy, request.url is typically localhost, but the Clerk-Proxy-Url header\n * and Location rewrites must use the origin visible to the browser.\n */\nfunction derivePublicOrigin(request: Request, requestUrl: URL): string {\n const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim();\n const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim();\n\n if (forwardedProto && forwardedHost) {\n return `${forwardedProto}://${forwardedHost}`;\n }\n\n return requestUrl.origin;\n}\n\n/**\n * Gets the client IP address from various headers\n */\nfunction getClientIp(request: Request): string | undefined {\n const cfConnectingIp = request.headers.get('cf-connecting-ip');\n if (cfConnectingIp) {\n return cfConnectingIp;\n }\n\n const xRealIp = request.headers.get('x-real-ip');\n if (xRealIp) {\n return xRealIp;\n }\n\n const xForwardedFor = request.headers.get('x-forwarded-for');\n if (xForwardedFor) {\n // Take the first IP in the chain\n return xForwardedFor.split(',')[0]?.trim();\n }\n\n return undefined;\n}\n\n/**\n * Proxies a request to Clerk's Frontend API.\n *\n * This function handles forwarding requests from your application to Clerk's\n * Frontend API, enabling scenarios where direct communication with Clerk's API\n * is blocked or needs to go through your application server.\n *\n * @param request - The incoming request to proxy\n * @param options - Proxy configuration options\n * @returns A Response from Clerk's Frontend API\n *\n * @example\n * ```typescript\n * import { clerkFrontendApiProxy } from '@clerk/backend/proxy';\n *\n * // In a route handler\n * const response = await clerkFrontendApiProxy(request, {\n * proxyPath: '/__clerk',\n * publishableKey: process.env.CLERK_PUBLISHABLE_KEY,\n * secretKey: process.env.CLERK_SECRET_KEY,\n * });\n * ```\n */\nexport async function clerkFrontendApiProxy(request: Request, options?: FrontendApiProxyOptions): Promise<Response> {\n const proxyPath = stripTrailingSlashes(options?.proxyPath || DEFAULT_PROXY_PATH);\n const publishableKey =\n options?.publishableKey || (typeof process !== 'undefined' ? process.env?.CLERK_PUBLISHABLE_KEY : undefined);\n const secretKey = options?.secretKey || (typeof process !== 'undefined' ? process.env?.CLERK_SECRET_KEY : undefined);\n\n // Validate configuration\n if (!publishableKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing publishableKey. Provide it in options or set CLERK_PUBLISHABLE_KEY environment variable.',\n 500,\n );\n }\n\n if (!secretKey) {\n return createErrorResponse(\n 'proxy_configuration_error',\n 'Missing secretKey. Provide it in options or set CLERK_SECRET_KEY environment variable.',\n 500,\n );\n }\n\n // Get the request URL and validate path\n const requestUrl = new URL(request.url);\n const pathMatches = requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/');\n if (!pathMatches) {\n return createErrorResponse(\n 'proxy_path_mismatch',\n `Request path \"${requestUrl.pathname}\" does not match proxy path \"${proxyPath}\"`,\n 400,\n );\n }\n\n // Derive the FAPI URL and construct the target URL.\n // Use string concatenation instead of `new URL(path, base)` to avoid\n // protocol-relative resolution (e.g., \"//evil.com\" resolving to a different host).\n const fapiBaseUrl = fapiUrlFromPublishableKey(publishableKey);\n const fapiHost = new URL(fapiBaseUrl).host;\n const targetPath = requestUrl.pathname.slice(proxyPath.length) || '/';\n const targetUrl = new URL(`${fapiBaseUrl}${targetPath}`);\n targetUrl.search = requestUrl.search;\n\n if (targetUrl.host !== fapiHost) {\n return createErrorResponse('proxy_request_failed', 'Resolved target does not match the expected host', 400);\n }\n\n // Build headers for the proxied request\n const headers = new Headers();\n\n // Copy original headers, excluding hop-by-hop headers\n request.headers.forEach((value, key) => {\n if (!HOP_BY_HOP_HEADERS.includes(key.toLowerCase())) {\n headers.set(key, value);\n }\n });\n\n // Set required Clerk proxy headers\n // Use the public origin (from forwarded headers) so the Clerk-Proxy-Url\n // points to the browser-visible host, not localhost behind a reverse proxy.\n const publicOrigin = derivePublicOrigin(request, requestUrl);\n const proxyUrl = `${publicOrigin}${proxyPath}`;\n headers.set('Clerk-Proxy-Url', proxyUrl);\n headers.set('Clerk-Secret-Key', secretKey);\n\n // Set the host header to the FAPI host\n headers.set('Host', fapiHost);\n\n // Request uncompressed responses to avoid a double compression pass.\n // fetch() auto-decompresses, so without this FAPI compresses → fetch\n // decompresses → the serving layer re-compresses for the browser.\n headers.set('Accept-Encoding', 'identity');\n\n // Set X-Forwarded-* headers for proxy awareness\n // Only set these if not already present (preserve values from upstream proxies)\n if (!headers.has('X-Forwarded-Host')) {\n headers.set('X-Forwarded-Host', requestUrl.host);\n }\n if (!headers.has('X-Forwarded-Proto')) {\n headers.set('X-Forwarded-Proto', requestUrl.protocol.replace(':', ''));\n }\n\n // Set X-Forwarded-For to the client IP\n // In multi-proxy scenarios, we prefer authoritative headers (CF-Connecting-IP, X-Real-IP)\n // over the existing X-Forwarded-For chain, as they provide the true client IP\n const clientIp = getClientIp(request);\n if (clientIp) {\n headers.set('X-Forwarded-For', clientIp);\n }\n\n // Determine if request has a body\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(request.method);\n\n try {\n // Make the proxied request\n const fetchOptions: RequestInit = {\n method: request.method,\n headers,\n // @ts-expect-error - duplex is required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n };\n\n // Only include body for methods that support it\n if (hasBody && request.body) {\n fetchOptions.body = request.body;\n }\n\n const response = await fetch(targetUrl.toString(), fetchOptions);\n\n // Build response headers, excluding hop-by-hop and encoding headers\n const responseHeaders = new Headers();\n response.headers.forEach((value, key) => {\n const lower = key.toLowerCase();\n if (!HOP_BY_HOP_HEADERS.includes(lower) && !RESPONSE_HEADERS_TO_STRIP.includes(lower)) {\n if (lower === 'set-cookie') {\n responseHeaders.append(key, value);\n } else {\n responseHeaders.set(key, value);\n }\n }\n });\n\n // Rewrite Location header for redirects to go through the proxy\n const locationHeader = response.headers.get('location');\n if (locationHeader) {\n try {\n const locationUrl = new URL(locationHeader, fapiBaseUrl);\n // Check if the redirect points to the FAPI host\n if (locationUrl.host === fapiHost) {\n // Rewrite to go through the proxy\n const rewrittenLocation = `${proxyUrl}${locationUrl.pathname}${locationUrl.search}${locationUrl.hash}`;\n responseHeaders.set('Location', rewrittenLocation);\n }\n } catch {\n // If URL parsing fails, leave the Location header as-is (could be a relative URL)\n }\n }\n\n const proxyResponse = new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers: responseHeaders,\n });\n\n // Some runtimes may re-add Content-Length when constructing the Response.\n // Delete explicitly since fetch() decoded the body and the original values\n // no longer reflect the actual content.\n for (const header of RESPONSE_HEADERS_TO_STRIP) {\n proxyResponse.headers.delete(header);\n }\n\n return proxyResponse;\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Unknown error';\n return createErrorResponse('proxy_request_failed', `Failed to proxy request to Clerk FAPI: ${message}`, 502);\n }\n}\n"],"mappings":";;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,2BAA2B;AAEpC,SAAS,sBAAAA,2BAA0B;AAkCnC,IAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOA,IAAM,4BAA4B,CAAC,oBAAoB,gBAAgB;AAOhE,SAAS,0BAA0B,gBAAgC;AACxE,QAAM,cAAc,oBAAoB,cAAc,GAAG;AAEzD,MAAI,aAAa,WAAW,QAAQ,KAAK,6BAA6B,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACnH,WAAO;AAAA,EACT;AAEA,MAAI,mBAAmB,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACpE,WAAO;AAAA,EACT;AACA,MAAI,qBAAqB,KAAK,YAAU,aAAa,SAAS,MAAM,CAAC,GAAG;AACtE,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAMO,SAAS,qBAAqB,KAAqB;AACxD,SAAO,IAAI,SAAS,GAAG,GAAG;AACxB,UAAM,IAAI,MAAM,GAAG,EAAE;AAAA,EACvB;AACA,SAAO;AACT;AAQO,SAAS,eAAe,SAAkB,SAA+D;AAC9G,QAAM,YAAY,qBAAqB,SAAS,aAAa,kBAAkB;AAC/E,QAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAC/B,SAAO,IAAI,aAAa,aAAa,IAAI,SAAS,WAAW,YAAY,GAAG;AAC9E;AAKA,SAAS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A,IACX,CAAC;AAKD,eAAW,UAAU,2BAA2B;AAC9C,oBAAc,QAAQ,OAAO,MAAM;AAAA,IACrC;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,UAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AACzD,WAAO,oBAAoB,wBAAwB,0CAA0C,OAAO,IAAI,GAAG;AAAA,EAC7G;AACF;","names":["DEFAULT_PROXY_PATH"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerk/backend",
3
- "version": "3.2.3-snapshot.v20260325172303",
3
+ "version": "3.2.3",
4
4
  "description": "Clerk Backend SDK - REST Client for Backend API & JWT verification utilities",
5
5
  "homepage": "https://clerk.com/",
6
6
  "bugs": {
@@ -99,7 +99,7 @@
99
99
  "dependencies": {
100
100
  "standardwebhooks": "^1.0.0",
101
101
  "tslib": "2.8.1",
102
- "@clerk/shared": "4.3.3-snapshot.v20260325172303"
102
+ "@clerk/shared": "^4.3.2"
103
103
  },
104
104
  "devDependencies": {
105
105
  "@edge-runtime/vm": "5.0.0",