Skip to content

Commit

Permalink
fix(backend): Sanitize slashes in URL paths (#3982)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Soriano <sorianorobertc@gmail.com>
  • Loading branch information
mlafeldt and wobsoriano authored Aug 29, 2024
1 parent 9d04777 commit c9ef591
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-goats-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/backend": patch
---

Fix error from duplicate leading slashes in URL path on Cloudflare Pages
8 changes: 8 additions & 0 deletions packages/backend/src/tokens/__tests__/clerkRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ export default (QUnit: QUnit) => {
});
assert.equal(createClerkRequest(req).clerkUrl.toString(), 'https://example.com/path?foo=bar');
});

it('with duplicate leading slashes in URL path', assert => {
const req1 = new Request('http://localhost:3000//path');
assert.equal(createClerkRequest(req1).clerkUrl.toString(), 'http://localhost:3000//path');

const req2 = new Request('http://localhost:3000////path');
assert.equal(createClerkRequest(req2).clerkUrl.toString(), 'http://localhost:3000////path');
});
});

module('toJSON', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/src/tokens/clerkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class ClerkRequest extends Request {
const resolvedProtocol = this.getFirstValueFromHeader(forwardedProto) ?? protocol?.replace(/[:/]/, '');
const origin = resolvedHost && resolvedProtocol ? `${resolvedProtocol}://${resolvedHost}` : initialUrl.origin;

if (origin === initialUrl.origin) {
return createClerkUrl(initialUrl);
}
return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
}

Expand Down

0 comments on commit c9ef591

Please sign in to comment.