Skip to content

Commit c9ef591

Browse files
mlafeldtwobsoriano
andauthored
fix(backend): Sanitize slashes in URL paths (#3982)
Co-authored-by: Robert Soriano <sorianorobertc@gmail.com>
1 parent 9d04777 commit c9ef591

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

.changeset/dull-goats-tie.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+
Fix error from duplicate leading slashes in URL path on Cloudflare Pages

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ export default (QUnit: QUnit) => {
151151
});
152152
assert.equal(createClerkRequest(req).clerkUrl.toString(), 'https://example.com/path?foo=bar');
153153
});
154+
155+
it('with duplicate leading slashes in URL path', assert => {
156+
const req1 = new Request('http://localhost:3000//path');
157+
assert.equal(createClerkRequest(req1).clerkUrl.toString(), 'http://localhost:3000//path');
158+
159+
const req2 = new Request('http://localhost:3000////path');
160+
assert.equal(createClerkRequest(req2).clerkUrl.toString(), 'http://localhost:3000////path');
161+
});
154162
});
155163

156164
module('toJSON', () => {

packages/backend/src/tokens/clerkRequest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class ClerkRequest extends Request {
5151
const resolvedProtocol = this.getFirstValueFromHeader(forwardedProto) ?? protocol?.replace(/[:/]/, '');
5252
const origin = resolvedHost && resolvedProtocol ? `${resolvedProtocol}://${resolvedHost}` : initialUrl.origin;
5353

54+
if (origin === initialUrl.origin) {
55+
return createClerkUrl(initialUrl);
56+
}
5457
return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
5558
}
5659

0 commit comments

Comments
 (0)