|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | + |
| 3 | +import { appConfigs } from '../presets'; |
| 4 | +import type { FakeUser } from '../testUtils'; |
| 5 | +import { createTestUtils, testAgainstRunningApps } from '../testUtils'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Tests Safari ITP (Intelligent Tracking Prevention) workaround |
| 9 | + * |
| 10 | + * Safari's ITP caps cookies set via fetch/XHR to 7 days. When the client cookie |
| 11 | + * is close to expiring (within 8 days), Clerk uses a full-page navigation through |
| 12 | + * the /v1/client/touch endpoint to refresh the cookie, bypassing the 7-day cap. |
| 13 | + * |
| 14 | + * The decorateUrl function in setActive() wraps redirect URLs with the touch |
| 15 | + * endpoint when the Safari ITP fix is needed. |
| 16 | + */ |
| 17 | +testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })('Safari ITP @generic @nextjs', ({ app }) => { |
| 18 | + test.describe.configure({ mode: 'serial' }); |
| 19 | + |
| 20 | + let fakeUser: FakeUser; |
| 21 | + |
| 22 | + test.beforeAll(async () => { |
| 23 | + const u = createTestUtils({ app }); |
| 24 | + fakeUser = u.services.users.createFakeUser(); |
| 25 | + await u.services.users.createBapiUser(fakeUser); |
| 26 | + }); |
| 27 | + |
| 28 | + test.afterAll(async () => { |
| 29 | + await fakeUser.deleteIfExists(); |
| 30 | + await app.teardown(); |
| 31 | + }); |
| 32 | + |
| 33 | + // Skip: Intercepting client responses breaks JWT signature validation |
| 34 | + // The decorateUrl functionality is tested in the tests below |
| 35 | + test.skip('navigates through touch endpoint when cookie is close to expiration', async ({ page, context }) => { |
| 36 | + const u = createTestUtils({ app, page, context }); |
| 37 | + |
| 38 | + // Intercept client responses and modify cookie_expires_at to be within 8 days |
| 39 | + // This makes isEligibleForTouch() return true |
| 40 | + await page.route('**/v1/client**', async route => { |
| 41 | + // Skip touch endpoint - we want to track that separately |
| 42 | + if (route.request().url().includes('/v1/client/touch')) { |
| 43 | + await route.continue(); |
| 44 | + return; |
| 45 | + } |
| 46 | + const response = await route.fetch(); |
| 47 | + const json = await response.json(); |
| 48 | + |
| 49 | + // Set cookie to expire in 2 days (within the 8-day threshold) |
| 50 | + // The API returns milliseconds since epoch |
| 51 | + const twoDaysFromNow = Date.now() + 2 * 24 * 60 * 60 * 1000; |
| 52 | + json.response.cookie_expires_at = twoDaysFromNow; |
| 53 | + |
| 54 | + await route.fulfill({ |
| 55 | + response, |
| 56 | + json, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + // Track if touch endpoint is called during navigation |
| 61 | + let touchEndpointCalled = false; |
| 62 | + let touchRedirectUrl: string | null = null; |
| 63 | + |
| 64 | + await page.route('**/v1/client/touch**', async route => { |
| 65 | + touchEndpointCalled = true; |
| 66 | + const url = new URL(route.request().url()); |
| 67 | + touchRedirectUrl = url.searchParams.get('redirect_url'); |
| 68 | + // Let the request continue normally |
| 69 | + await route.continue(); |
| 70 | + }); |
| 71 | + |
| 72 | + // Sign in |
| 73 | + await u.po.signIn.goTo(); |
| 74 | + await u.po.signIn.setIdentifier(fakeUser.email); |
| 75 | + await u.po.signIn.continue(); |
| 76 | + await u.po.signIn.setPassword(fakeUser.password); |
| 77 | + await u.po.signIn.continue(); |
| 78 | + |
| 79 | + // Wait for navigation to complete |
| 80 | + await u.po.expect.toBeSignedIn(); |
| 81 | + |
| 82 | + // Verify touch endpoint was called |
| 83 | + expect(touchEndpointCalled).toBe(true); |
| 84 | + expect(touchRedirectUrl).toBeTruthy(); |
| 85 | + }); |
| 86 | + |
| 87 | + // Skip: Intercepting client responses breaks JWT signature validation |
| 88 | + // The decorateUrl functionality is tested in the tests below |
| 89 | + test.skip('does not use touch endpoint when cookie is not close to expiration', async ({ page, context }) => { |
| 90 | + const u = createTestUtils({ app, page, context }); |
| 91 | + |
| 92 | + // Intercept client responses and set cookie_expires_at to be far in the future |
| 93 | + // This makes isEligibleForTouch() return false |
| 94 | + await page.route('**/v1/client**', async route => { |
| 95 | + // Skip touch endpoint - we want to track that separately |
| 96 | + if (route.request().url().includes('/v1/client/touch')) { |
| 97 | + await route.continue(); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const response = await route.fetch(); |
| 102 | + const json = await response.json(); |
| 103 | + |
| 104 | + // Set cookie to expire in 30 days (outside the 8-day threshold) |
| 105 | + // The API returns milliseconds since epoch |
| 106 | + const thirtyDaysFromNow = Date.now() + 30 * 24 * 60 * 60 * 1000; |
| 107 | + json.response.cookie_expires_at = thirtyDaysFromNow; |
| 108 | + |
| 109 | + await route.fulfill({ |
| 110 | + response, |
| 111 | + json, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + // Track if touch endpoint is called |
| 116 | + let touchEndpointCalled = false; |
| 117 | + |
| 118 | + await page.route('**/v1/client/touch**', async route => { |
| 119 | + touchEndpointCalled = true; |
| 120 | + await route.continue(); |
| 121 | + }); |
| 122 | + |
| 123 | + // Sign in |
| 124 | + await u.po.signIn.goTo(); |
| 125 | + await u.po.signIn.setIdentifier(fakeUser.email); |
| 126 | + await u.po.signIn.continue(); |
| 127 | + await u.po.signIn.setPassword(fakeUser.password); |
| 128 | + await u.po.signIn.continue(); |
| 129 | + |
| 130 | + // Wait for navigation to complete |
| 131 | + await u.po.expect.toBeSignedIn(); |
| 132 | + |
| 133 | + // Verify touch endpoint was NOT called |
| 134 | + expect(touchEndpointCalled).toBe(false); |
| 135 | + }); |
| 136 | + |
| 137 | + test('decorateUrl returns touch URL when client is eligible for touch', async ({ page, context }) => { |
| 138 | + const u = createTestUtils({ app, page, context }); |
| 139 | + |
| 140 | + // Sign in first without mocking to get a valid session |
| 141 | + await u.po.signIn.goTo(); |
| 142 | + await u.po.signIn.setIdentifier(fakeUser.email); |
| 143 | + await u.po.signIn.continue(); |
| 144 | + await u.po.signIn.setPassword(fakeUser.password); |
| 145 | + await u.po.signIn.continue(); |
| 146 | + await u.po.expect.toBeSignedIn(); |
| 147 | + |
| 148 | + // Now test setActive with a navigate callback that captures decorateUrl behavior |
| 149 | + const result = await page.evaluate(async () => { |
| 150 | + const clerk = (window as any).Clerk; |
| 151 | + |
| 152 | + // Mock isEligibleForTouch to return true |
| 153 | + const originalIsEligibleForTouch = clerk.client.isEligibleForTouch.bind(clerk.client); |
| 154 | + clerk.client.isEligibleForTouch = () => true; |
| 155 | + |
| 156 | + let capturedDecorateUrl: ((url: string) => string) | undefined; |
| 157 | + let decoratedUrl: string | undefined; |
| 158 | + |
| 159 | + try { |
| 160 | + await clerk.setActive({ |
| 161 | + session: clerk.session.id, |
| 162 | + navigate: ({ decorateUrl }: { decorateUrl: (url: string) => string }) => { |
| 163 | + capturedDecorateUrl = decorateUrl; |
| 164 | + decoratedUrl = decorateUrl('/dashboard'); |
| 165 | + }, |
| 166 | + }); |
| 167 | + } finally { |
| 168 | + // Restore original |
| 169 | + clerk.client.isEligibleForTouch = originalIsEligibleForTouch; |
| 170 | + } |
| 171 | + |
| 172 | + return { |
| 173 | + decorateUrlCaptured: !!capturedDecorateUrl, |
| 174 | + decoratedUrl, |
| 175 | + containsTouch: decoratedUrl?.includes('/v1/client/touch') ?? false, |
| 176 | + containsRedirectUrl: decoratedUrl?.includes('redirect_url=') ?? false, |
| 177 | + }; |
| 178 | + }); |
| 179 | + |
| 180 | + expect(result.decorateUrlCaptured).toBe(true); |
| 181 | + expect(result.containsTouch).toBe(true); |
| 182 | + expect(result.containsRedirectUrl).toBe(true); |
| 183 | + }); |
| 184 | + |
| 185 | + test('decorateUrl returns original URL when client is not eligible for touch', async ({ page, context }) => { |
| 186 | + const u = createTestUtils({ app, page, context }); |
| 187 | + |
| 188 | + // Sign in first |
| 189 | + await u.po.signIn.goTo(); |
| 190 | + await u.po.signIn.setIdentifier(fakeUser.email); |
| 191 | + await u.po.signIn.continue(); |
| 192 | + await u.po.signIn.setPassword(fakeUser.password); |
| 193 | + await u.po.signIn.continue(); |
| 194 | + await u.po.expect.toBeSignedIn(); |
| 195 | + |
| 196 | + // Test setActive with navigate callback when isEligibleForTouch is false |
| 197 | + const result = await page.evaluate(async () => { |
| 198 | + const clerk = (window as any).Clerk; |
| 199 | + |
| 200 | + // Ensure isEligibleForTouch returns false |
| 201 | + const originalIsEligibleForTouch = clerk.client.isEligibleForTouch.bind(clerk.client); |
| 202 | + clerk.client.isEligibleForTouch = () => false; |
| 203 | + |
| 204 | + let decoratedUrl: string | undefined; |
| 205 | + |
| 206 | + try { |
| 207 | + await clerk.setActive({ |
| 208 | + session: clerk.session.id, |
| 209 | + navigate: ({ decorateUrl }: { decorateUrl: (url: string) => string }) => { |
| 210 | + decoratedUrl = decorateUrl('/dashboard'); |
| 211 | + }, |
| 212 | + }); |
| 213 | + } finally { |
| 214 | + // Restore original |
| 215 | + clerk.client.isEligibleForTouch = originalIsEligibleForTouch; |
| 216 | + } |
| 217 | + |
| 218 | + return { |
| 219 | + decoratedUrl, |
| 220 | + isOriginalUrl: decoratedUrl === '/dashboard', |
| 221 | + containsTouch: decoratedUrl?.includes('/v1/client/touch') ?? false, |
| 222 | + }; |
| 223 | + }); |
| 224 | + |
| 225 | + expect(result.isOriginalUrl).toBe(true); |
| 226 | + expect(result.containsTouch).toBe(false); |
| 227 | + }); |
| 228 | +}); |
0 commit comments