Skip to content

Commit 84483c2

Browse files
brkalowjacekradkonikosdouvlis
authored
fix(backend): Do not throw on malformed host values (#7370)
Co-authored-by: Jacek Radko <jacek@clerk.dev> Co-authored-by: Nikos Douvlis <nikosdouvlis@gmail.com>
1 parent 2b1f9d1 commit 84483c2

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

.changeset/gentle-clouds-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/backend': patch
3+
---
4+
5+
Fixes an issue with host header parsing that would cause Clerk to throw an exception when receiving malformed host values.

packages/backend/src/tokens/__tests__/clerkRequest.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,39 @@ describe('createClerkRequest', () => {
158158
const req2 = new Request('http://localhost:3000////path');
159159
expect(createClerkRequest(req2).clerkUrl.toString()).toBe('http://localhost:3000////path');
160160
});
161+
162+
it('handles malicious host header with script injection gracefully', () => {
163+
const req = new Request('http://localhost:3000/path', {
164+
headers: {
165+
'x-forwarded-host': 'z2cgvm.xfh"></script><script>alert(document.domain);</script>/',
166+
'x-forwarded-proto': 'https',
167+
},
168+
});
169+
expect(() => createClerkRequest(req)).not.toThrow();
170+
expect(createClerkRequest(req).clerkUrl.toString()).toBe('http://localhost:3000/path');
171+
});
172+
173+
it('handles malicious host header with invalid characters gracefully', () => {
174+
const req = new Request('http://localhost:3000/path?foo=bar', {
175+
headers: {
176+
'x-forwarded-host': '<invalid>host',
177+
'x-forwarded-proto': 'https',
178+
},
179+
});
180+
expect(() => createClerkRequest(req)).not.toThrow();
181+
expect(createClerkRequest(req).clerkUrl.toString()).toBe('http://localhost:3000/path?foo=bar');
182+
});
183+
184+
it('handles empty forwarded headers gracefully', () => {
185+
const req = new Request('http://localhost:3000/path', {
186+
headers: {
187+
'x-forwarded-host': '',
188+
'x-forwarded-proto': '',
189+
},
190+
});
191+
expect(() => createClerkRequest(req)).not.toThrow();
192+
expect(createClerkRequest(req).clerkUrl.toString()).toBe('http://localhost:3000/path');
193+
});
161194
});
162195

163196
describe('toJSON', () => {

packages/backend/src/tokens/clerkRequest.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ class ClerkRequest extends Request {
5959
if (origin === initialUrl.origin) {
6060
return createClerkUrl(initialUrl);
6161
}
62-
return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
62+
63+
try {
64+
return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
65+
} catch {
66+
return createClerkUrl(initialUrl);
67+
}
6368
}
6469

6570
private getFirstValueFromHeader(value?: string | null) {

0 commit comments

Comments
 (0)