-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(backend): Refactor existing cross-origin check - drop forwarded…
…Port
- Loading branch information
Showing
5 changed files
with
40 additions
and
77 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
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 |
---|---|---|
@@ -1,18 +1,36 @@ | ||
import { constants } from './constants'; | ||
|
||
const getHeader = (req: Request, key: string) => req.headers.get(key); | ||
const getFirstValueFromHeader = (req: Request, key: string) => getHeader(req, key)?.split(',')[0]; | ||
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 = getFirstValueFromHeader(request, constants.Headers.ForwardedProto); | ||
const forwardedHost = getFirstValueFromHeader(request, constants.Headers.ForwardedHost); | ||
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 resolvedHost = forwardedHost ?? host ?? initialUrl.host; | ||
const resolvedProtocol = forwardedProto ?? initialUrl.protocol.replace(/[:/]/, ''); | ||
const base = buildOrigin({ protocol, forwardedProto, forwardedHost, host: host || initialUrl.host }); | ||
|
||
return new URL(path || initialUrl.pathname, `${resolvedProtocol}://${resolvedHost}`); | ||
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}`; | ||
}; |