Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/backend/src/tokens/__tests__/clerkRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ describe('createClerkRequest', () => {
const req2 = new Request('http://localhost:3000////path');
expect(createClerkRequest(req2).clerkUrl.toString()).toBe('http://localhost:3000////path');
});

it('should handle malformed x-forwarded-host header gracefully', () => {
const req = new Request('http://localhost:3000/path', {
headers: {
'x-forwarded-host': 'z2cgvm.xfh"></script><script>alert(document.domain);</script>',
'x-forwarded-proto': 'https',
},
});
// Should fall back to original URL instead of throwing
expect(createClerkRequest(req).clerkUrl.toString()).toBe('http://localhost:3000/path');
});

it('should handle malformed host header gracefully', () => {
const req = new Request('http://localhost:3000/path', {
headers: {
host: 'z2cgvm.xfh"></script><script>alert(document.domain);</script>',
},
});
// Should fall back to original URL instead of throwing
expect(createClerkRequest(req).clerkUrl.toString()).toBe('http://localhost:3000/path');
});
});

describe('toJSON', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/backend/src/tokens/clerkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ class ClerkRequest extends Request {
if (origin === initialUrl.origin) {
return createClerkUrl(initialUrl);
}
return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
try {
return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
} catch {
// If the origin contains invalid characters (e.g., from malicious headers),
// fall back to using the original request URL to prevent the application from crashing
return createClerkUrl(initialUrl);
}
}

private getFirstValueFromHeader(value?: string | null) {
Expand Down
Loading