Skip to content

Commit 03cf6a6

Browse files
devinledevinleclaudenicholasio
authored
fix: shouldSkipRedirect incorrectly skips cross-domain redirects (#942)
Co-authored-by: devinle <devin.leggett@get10up.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Nícholas André <nicholas.andre@fueled.com>
1 parent 209fd26 commit 03cf6a6

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@headstartwp/core": patch
3+
---
4+
5+
Fix: shouldSkipRedirect incorrectly skipping cross-domain redirects when pathnames match. Fixes #941

packages/core/src/utils/__tests__/fetchRedirect.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,18 @@ describe('fetchRedirect', () => {
5151

5252
global.fetch = originalFetch;
5353
});
54+
55+
it('handles cross-domain redirect with same pathname', async () => {
56+
const result = await fetchRedirect('/recipe/my-recipe/', 'http://example.com/');
57+
58+
expect(result.location).toBe('https://www.external-domain.com/recipe/my-recipe/');
59+
expect(result.status).toBe(302);
60+
});
61+
62+
it('handles cross-domain redirect with different pathname', async () => {
63+
const result = await fetchRedirect('/old-recipe/', 'http://example.com/');
64+
65+
expect(result.location).toBe('https://www.external-domain.com/new-recipe/');
66+
expect(result.status).toBe(301);
67+
});
5468
});

packages/core/src/utils/fetchRedirect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ function shouldSkipRedirect(link: string, redirect: string, sourceUrl: string) {
2828
return true;
2929
}
3030

31+
// Cross-domain redirects should never be skipped
32+
if (linkURL.host !== redirectURL.host) {
33+
return false;
34+
}
35+
3136
const linkParams = linkURL.searchParams;
3237
const redirectParams = redirectURL.searchParams;
3338

packages/core/test/server-handlers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ const handlers = [
3636
return res(redirect('http://example.com/redirect-test-missing-slash', 301));
3737
}),
3838

39+
// Cross-domain redirect with same pathname
40+
rest.head('http://example.com/recipe/my-recipe/', (req, res) => {
41+
return res(redirect('https://www.external-domain.com/recipe/my-recipe/', 302));
42+
}),
43+
44+
// Cross-domain redirect with different pathname
45+
rest.head('http://example.com/old-recipe/', (req, res) => {
46+
return res(redirect('https://www.external-domain.com/new-recipe/', 301));
47+
}),
48+
3949
rest.get<DefaultRequestBody, TestEndpointResponse>(/\/test-endpoint/, (req, res, ctx) => {
4050
return res(ctx.json({ ok: true }));
4151
}),

0 commit comments

Comments
 (0)