Unified · Split
| @@ -14,46 +14,8 @@ const callbackHandler = handleCallbackRoute({ · 13 unmodified lines | |||
| 14 | }, | 14 | }, |
| 15 | }); | 15 | }); |
| 16 | 16 | ||
| 17 | const WORKOS_CALLBACK_REPLAY_MS = 10 * 60 * 1000; | ||
| 18 | const CALLBACK_REPLAY_PRUNE_INTERVAL_MS = 30 * 1000; | ||
| 19 | const callbackCodeReplay = new Map<string, number>(); | ||
| 20 | let nextReplayPruneAt = 0; | ||
| 21 | |||
| 22 | type WorkOSCallbackInput = Parameters<typeof callbackHandler>[0]; | 17 | type WorkOSCallbackInput = Parameters<typeof callbackHandler>[0]; |
| 23 | 18 | ||
| 24 | function pruneCallbackReplays(now = Date.now()): void { | ||
| 25 | if (now < nextReplayPruneAt) { | ||
| 26 | return; | ||
| 27 | } | ||
| 28 | |||
| 29 | callbackCodeReplay.forEach((expiresAt, code) => { | ||
| 30 | if (expiresAt <= now) { | ||
| 31 | callbackCodeReplay.delete(code); | ||
| 32 | } | ||
| 33 | }); | ||
| 34 | |||
| 35 | nextReplayPruneAt = now + CALLBACK_REPLAY_PRUNE_INTERVAL_MS; | ||
| 36 | } | ||
| 37 | |||
| 38 | function isReplayedCode(code: string, now = Date.now()): boolean { | ||
| 39 | pruneCallbackReplays(now); | ||
| 40 | const expiresAt = callbackCodeReplay.get(code); | ||
| 41 | |||
| 42 | if (!expiresAt || expiresAt <= now) { | ||
| 43 | if (expiresAt) { | ||
| 44 | callbackCodeReplay.delete(code); | ||
| 45 | } | ||
| 46 | |||
| 47 | return false; | ||
| 48 | } | ||
| 49 | |||
| 50 | return true; | ||
| 51 | } | ||
| 52 | |||
| 53 | function markCodeAsReplayed(code: string, now = Date.now()): void { | ||
| 54 | callbackCodeReplay.set(code, now + WORKOS_CALLBACK_REPLAY_MS); | ||
| 55 | } | ||
| 56 | |||
| 57 | function getSetCookieValues(responseHeaders: Headers): string[] { | 19 | function getSetCookieValues(responseHeaders: Headers): string[] { |
| 58 | const getSetCookie = (responseHeaders as { getSetCookie?: () => string[] }).getSetCookie; | 20 | const getSetCookie = (responseHeaders as { getSetCookie?: () => string[] }).getSetCookie; |
| 59 | if (typeof getSetCookie === "function") { | 21 | if (typeof getSetCookie === "function") { |
| @@ -121,17 +83,6 @@ export async function handleWorkOSCallback(context: WorkOSCallbackInput): Promis · 61 unmodified lines | |||
| 121 | }); | 83 | }); |
| 122 | } | 84 | } |
| 123 | 85 | ||
| 124 | if (code && isReplayedCode(code)) { | ||
| 125 | if (isWorkosDebugEnabled()) { | ||
| 126 | logWorkosAuth("callback.replay-blocked", { | ||
| 127 | requestId, | ||
| 128 | code: redactAuthCode(code), | ||
| 129 | }); | ||
| 130 | } | ||
| 131 | |||
| 132 | return redirectResponse("/", 302); | ||
| 133 | } | ||
| 134 | |||
| 135 | try { | 86 | try { |
| 136 | const response = await callbackHandler(context); | 87 | const response = await callbackHandler(context); |
| 137 | 88 | ||
| @@ -171,8 +122,6 @@ export async function handleWorkOSCallback(context: WorkOSCallbackInput): Promis · 33 unmodified lines | |||
| 171 | } | 122 | } |
| 172 | 123 | ||
| 173 | if (code && response.status >= 300 && response.status < 400) { | 124 | if (code && response.status >= 300 && response.status < 400) { |
| 174 | markCodeAsReplayed(code); | ||
| 175 | |||
| 176 | if (isWorkosDebugEnabled()) { | 125 | if (isWorkosDebugEnabled()) { |
| 177 | logWorkosAuth("callback.code-marked-used", { | 126 | logWorkosAuth("callback.code-marked-used", { |
| 178 | requestId, | 127 | requestId, |
| @@ -212,8 +161,6 @@ export async function handleWorkOSCallback(context: WorkOSCallbackInput): Promis · 33 unmodified lines | |||
| 212 | } | 161 | } |
| 213 | 162 | ||
| 214 | if (code && invalidGrant) { | 163 | if (code && invalidGrant) { |
| 215 | markCodeAsReplayed(code); | ||
| 216 | |||
| 217 | return redirectResponse("/", 302); | 164 | return redirectResponse("/", 302); |
| 218 | } | 165 | } |
| 219 | 166 | ||
| @@ -35,7 +35,7 @@ test("uses explicit WORKOS_REDIRECT_URI when set", () => { · 34 unmodified lines | |||
| 35 | process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI = "https://public.example.com/callback"; | 35 | process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI = "https://public.example.com/callback"; |
| 36 | 36 | ||
| 37 | const request = new Request("https://request.example.com/sign-in"); | 37 | const request = new Request("https://request.example.com/sign-in"); |
| 38 | expect(resolveWorkosRedirectUri(request)).toBe("https://app.example.com/callback"); | 38 | expect(resolveWorkosRedirectUri(request)).toBe("https://app.example.com/api/auth/callback"); |
| 39 | }); | 39 | }); |
| 40 | 40 | ||
| 41 | test("falls back to NEXT_PUBLIC_WORKOS_REDIRECT_URI when server var is absent", () => { | 41 | test("falls back to NEXT_PUBLIC_WORKOS_REDIRECT_URI when server var is absent", () => { |
| @@ -45,7 +45,7 @@ test("falls back to NEXT_PUBLIC_WORKOS_REDIRECT_URI when server var is absent", · 3 unmodified lines | |||
| 45 | process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI = "https://public.example.com/callback"; | 45 | process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI = "https://public.example.com/callback"; |
| 46 | 46 | ||
| 47 | const request = new Request("https://request.example.com/sign-in"); | 47 | const request = new Request("https://request.example.com/sign-in"); |
| 48 | expect(resolveWorkosRedirectUri(request)).toBe("https://public.example.com/callback"); | 48 | expect(resolveWorkosRedirectUri(request)).toBe("https://public.example.com/api/auth/callback"); |
| 49 | }); | 49 | }); |
| 50 | 50 | ||
| 51 | test("derives from forwarded request origin when not explicitly configured", () => { | 51 | test("derives from forwarded request origin when not explicitly configured", () => { |
| @@ -61,7 +61,7 @@ test("derives from forwarded request origin when not explicitly configured", () · 9 unmodified lines | |||
| 61 | }, | 61 | }, |
| 62 | }); | 62 | }); |
| 63 | 63 | ||
| 64 | expect(resolveWorkosRedirectUri(request)).toBe("https://executor.sh/callback"); | 64 | expect(resolveWorkosRedirectUri(request)).toBe("https://executor.sh/api/auth/callback"); |
| 65 | }); | 65 | }); |
| 66 | 66 | ||
| 67 | test("falls back to localhost when host cannot be derived locally", () => { | 67 | test("falls back to localhost when host cannot be derived locally", () => { |
| @@ -76,7 +76,7 @@ test("falls back to localhost when host cannot be derived locally", () => { · 8 unmodified lines | |||
| 76 | process.env.NODE_ENV = "development"; | 76 | process.env.NODE_ENV = "development"; |
| 77 | 77 | ||
| 78 | const request = new Request("http://localhost:1234/sign-in"); | 78 | const request = new Request("http://localhost:1234/sign-in"); |
| 79 | expect(resolveWorkosRedirectUri(request)).toBe("http://localhost:1234/callback"); | 79 | expect(resolveWorkosRedirectUri(request)).toBe("http://localhost:1234/api/auth/callback"); |
| 80 | }); | 80 | }); |
| 81 | 81 | ||
| 82 | resetWorkosEnv(); | 82 | resetWorkosEnv(); |
| @@ -1,6 +1,21 @@ | |||
| 1 | import { externalOriginFromRequest } from "./http/request-origin"; | 1 | import { externalOriginFromRequest } from "./http/request-origin"; |
| 2 | 2 | ||
| 3 | const WORKOS_REDIRECT_PATH = "/callback"; | 3 | const WORKOS_REDIRECT_PATH = "/api/auth/callback"; |
| 4 | |||
| 5 | function normalizeWorkosRedirectUri(redirectUri: string): string { | ||
| 6 | try { | ||
| 7 | const parsed = new URL(redirectUri); | ||
| 8 | if (parsed.pathname === "/callback") { | ||
| 9 | parsed.pathname = WORKOS_REDIRECT_PATH; | ||
| 10 | parsed.search = ""; | ||
| 11 | parsed.hash = ""; | ||
| 12 | return parsed.toString(); | ||
| 13 | } | ||
| 14 | return parsed.toString(); | ||
| 15 | } catch { | ||
| 16 | return redirectUri; | ||
| 17 | } | ||
| 18 | } | ||
| 4 | 19 | ||
| 5 | function trim(value: string | undefined): string | undefined { | 20 | function trim(value: string | undefined): string | undefined { |
| 6 | const candidate = value?.trim(); | 21 | const candidate = value?.trim(); |
| @@ -32,17 +47,17 @@ function fallbackWorkosOriginFromServer(): string | undefined { · 25 unmodified lines | |||
| 32 | export function resolveWorkosRedirectUri(request?: Request): string | undefined { | 47 | export function resolveWorkosRedirectUri(request?: Request): string | undefined { |
| 33 | const explicitRedirect = trim(process.env.WORKOS_REDIRECT_URI); | 48 | const explicitRedirect = trim(process.env.WORKOS_REDIRECT_URI); |
| 34 | if (explicitRedirect) { | 49 | if (explicitRedirect) { |
| 35 | return explicitRedirect; | 50 | return normalizeWorkosRedirectUri(explicitRedirect); |
| 36 | } | 51 | } |
| 37 | 52 | ||
| 38 | const publicRedirect = trim(process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI); | 53 | const publicRedirect = trim(process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI); |
| 39 | if (publicRedirect) { | 54 | if (publicRedirect) { |
| 40 | return publicRedirect; | 55 | return normalizeWorkosRedirectUri(publicRedirect); |
| 41 | } | 56 | } |
| 42 | 57 | ||
| 43 | const origin = request | 58 | const origin = request |
| 44 | ? externalOriginFromRequest(request) | 59 | ? externalOriginFromRequest(request) |
| 45 | : fallbackWorkosOriginFromServer(); | 60 | : fallbackWorkosOriginFromServer(); |
| 46 | 61 | ||
| 47 | return origin ? `${origin}${WORKOS_REDIRECT_PATH}` : undefined; | 62 | return origin ? normalizeWorkosRedirectUri(`${origin}${WORKOS_REDIRECT_PATH}`) : undefined; |
| 48 | } | 63 | } |