-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): Introduce buildRequestUrl utility and enable CLERK_TRU…
…ST_HOST by default * chore(nextjs): Trust Clerk host by default * chore(nextjs): Drop using forwarded-port from clerkUrl * feat(backend): Introduce buildRequestUrl utility The buildRequestUrl utility will be used to construct the request url using a request and optional path parameter (used to overrided the path of the request url). This utility will use the forwarded headers values and the request.url to generate and return the actual request URL * chore(shared): Depreacate getRequestUrl in favor of buildRequestUrl in backend * chore(clerk-sdk-node): Use buildRequestUrl from backend package * chore(nextjs): Use buildRequestUrl from backend package * chore(remix): Use buildRequestUrl from backend package * chore(remix): Drop createIsomorphicRequest usage since Remix request is already of type Request * chore(nextjs): Add changeset * chore(backend): Refactor existing cross-origin check - drop forwardedPort * chore(backend): Add tests for buildRequestUrl and buildOrigin
- Loading branch information
Showing
23 changed files
with
242 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@clerk/clerk-sdk-node': minor | ||
'@clerk/backend': minor | ||
'@clerk/nextjs': minor | ||
'@clerk/remix': minor | ||
--- | ||
|
||
Support hosting NextJs apps on non-Vercel platforms by constructing req.url using host-related headers instead of using on req.url directly. CLERK_TRUST_HOST is now enabled by default. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import type QUnit from 'qunit'; | ||
|
||
import { buildOrigin, buildRequestUrl } from './utils'; | ||
|
||
export default (QUnit: QUnit) => { | ||
const { module, test } = QUnit; | ||
|
||
module('buildOrigin({ protocol, forwardedProto, forwardedHost, host })', () => { | ||
test('without any param', assert => { | ||
assert.equal(buildOrigin({}), ''); | ||
}); | ||
|
||
test('with protocol', assert => { | ||
assert.equal(buildOrigin({ protocol: 'http' }), ''); | ||
}); | ||
|
||
test('with host', assert => { | ||
assert.equal(buildOrigin({ host: 'localhost:3000' }), ''); | ||
}); | ||
|
||
test('with protocol and host', assert => { | ||
assert.equal(buildOrigin({ protocol: 'http', host: 'localhost:3000' }), 'http://localhost:3000'); | ||
}); | ||
|
||
test('with forwarded proto', assert => { | ||
assert.equal( | ||
buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedProto: 'https' }), | ||
'https://localhost:3000', | ||
); | ||
}); | ||
|
||
test('with forwarded proto - with multiple values', assert => { | ||
assert.equal( | ||
buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedProto: 'https,http' }), | ||
'https://localhost:3000', | ||
); | ||
}); | ||
|
||
test('with forwarded host', assert => { | ||
assert.equal( | ||
buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedHost: 'example.com' }), | ||
'http://example.com', | ||
); | ||
}); | ||
|
||
test('with forwarded host - with multiple values', assert => { | ||
assert.equal( | ||
buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedHost: 'example.com,example-2.com' }), | ||
'http://example.com', | ||
); | ||
}); | ||
|
||
test('with forwarded proto and host', assert => { | ||
assert.equal( | ||
buildOrigin({ | ||
protocol: 'http', | ||
host: 'localhost:3000', | ||
forwardedProto: 'https', | ||
forwardedHost: 'example.com', | ||
}), | ||
'https://example.com', | ||
); | ||
}); | ||
|
||
test('with forwarded proto and host - without protocol', assert => { | ||
assert.equal( | ||
buildOrigin({ host: 'localhost:3000', forwardedProto: 'https', forwardedHost: 'example.com' }), | ||
'https://example.com', | ||
); | ||
}); | ||
|
||
test('with forwarded proto and host - without host', assert => { | ||
assert.equal( | ||
buildOrigin({ protocol: 'http', forwardedProto: 'https', forwardedHost: 'example.com' }), | ||
'https://example.com', | ||
); | ||
}); | ||
|
||
test('with forwarded proto and host - without host and protocol', assert => { | ||
assert.equal(buildOrigin({ forwardedProto: 'https', forwardedHost: 'example.com' }), 'https://example.com'); | ||
}); | ||
}); | ||
|
||
module('buildRequestUrl({ request, path })', () => { | ||
test('without headers', assert => { | ||
const req = new Request('http://localhost:3000/path'); | ||
assert.equal(buildRequestUrl(req), 'http://localhost:3000/path'); | ||
}); | ||
|
||
test('with forwarded proto / host headers', assert => { | ||
const req = new Request('http://localhost:3000/path', { | ||
headers: { 'x-forwarded-host': 'example.com', 'x-forwarded-proto': 'https,http' }, | ||
}); | ||
assert.equal(buildRequestUrl(req), 'https://example.com/path'); | ||
}); | ||
|
||
test('with forwarded proto / host and host headers', assert => { | ||
const req = new Request('http://localhost:3000/path', { | ||
headers: { | ||
'x-forwarded-host': 'example.com', | ||
'x-forwarded-proto': 'https,http', | ||
host: 'example-host.com', | ||
}, | ||
}); | ||
assert.equal(buildRequestUrl(req), 'https://example.com/path'); | ||
}); | ||
|
||
test('with path', assert => { | ||
const req = new Request('http://localhost:3000/path'); | ||
assert.equal(buildRequestUrl(req, '/other-path'), 'http://localhost:3000/other-path'); | ||
}); | ||
|
||
test('with query params in request', assert => { | ||
const req = new Request('http://localhost:3000/path'); | ||
assert.equal(buildRequestUrl(req), 'http://localhost:3000/path'); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { constants } from './constants'; | ||
|
||
const getHeader = (req: Request, key: string) => req.headers.get(key); | ||
const getFirstValueFromHeader = (value?: string | null) => value?.split(',')[0]; | ||
|
||
type BuildRequestUrl = (request: Request, path?: string) => URL; | ||
export const buildRequestUrl: BuildRequestUrl = (request, path) => { | ||
const initialUrl = new URL(request.url); | ||
|
||
const forwardedProto = getHeader(request, constants.Headers.ForwardedProto); | ||
const forwardedHost = getHeader(request, constants.Headers.ForwardedHost); | ||
const host = getHeader(request, constants.Headers.Host); | ||
const protocol = initialUrl.protocol; | ||
|
||
const base = buildOrigin({ protocol, forwardedProto, forwardedHost, host: host || initialUrl.host }); | ||
|
||
return new URL(path || initialUrl.pathname, base); | ||
}; | ||
|
||
type BuildOriginParams = { | ||
protocol?: string; | ||
forwardedProto?: string | null; | ||
forwardedHost?: string | null; | ||
host?: string | null; | ||
}; | ||
type BuildOrigin = (params: BuildOriginParams) => string; | ||
export const buildOrigin: BuildOrigin = ({ protocol, forwardedProto, forwardedHost, host }) => { | ||
const resolvedHost = getFirstValueFromHeader(forwardedHost) ?? host; | ||
const resolvedProtocol = getFirstValueFromHeader(forwardedProto) ?? protocol?.replace(/[:/]/, ''); | ||
|
||
if (!resolvedHost || !resolvedProtocol) { | ||
return ''; | ||
} | ||
|
||
return `${resolvedProtocol}://${resolvedHost}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.