-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(remix): Attempt to extract user IP from request headers. #6263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,79 @@ | ||
// Vendored / modified from @sergiodxa/remix-utils | ||
// https://github.com/sergiodxa/remix-utils/blob/02af80e12829a53696bfa8f3c2363975cf59f55e/src/server/get-client-ip-address.ts | ||
|
||
import { isIP } from 'net'; | ||
|
||
/** | ||
* This is the list of headers, in order of preference, that will be used to | ||
* determine the client's IP address. | ||
*/ | ||
const headerNames = [ | ||
'X-Client-IP', | ||
'X-Forwarded-For', | ||
'Fly-Client-IP', | ||
'CF-Connecting-IP', | ||
'Fastly-Client-Ip', | ||
'True-Client-Ip', | ||
'X-Real-IP', | ||
'X-Cluster-Client-IP', | ||
'X-Forwarded', | ||
'Forwarded-For', | ||
'Forwarded', | ||
]; | ||
/** | ||
* Get the IP address of the client sending a request. | ||
* | ||
* It receives a Request headers object and use it to get the | ||
* IP address from one of the following headers in order. | ||
* | ||
* - X-Client-IP | ||
* - X-Forwarded-For | ||
* - Fly-Client-IP | ||
* - CF-Connecting-IP | ||
* - Fastly-Client-Ip | ||
* - True-Client-Ip | ||
* - X-Real-IP | ||
* - X-Cluster-Client-IP | ||
* - X-Forwarded | ||
* - Forwarded-For | ||
* - Forwarded | ||
* | ||
* If the IP address is valid, it will be returned. Otherwise, null will be | ||
* returned. | ||
* | ||
* If the header values contains more than one IP address, the first valid one | ||
* will be returned. | ||
*/ | ||
export function getClientIPAddress(headers: Headers): string | null { | ||
const ipAddress = headerNames | ||
.map((headerName: string) => { | ||
const value = headers.get(headerName); | ||
if (headerName === 'Forwarded') { | ||
return parseForwardedHeader(value); | ||
} | ||
if (!value?.includes(', ')) return value; | ||
return value.split(', '); | ||
}) | ||
.reduce((acc: string[], val) => { | ||
if (!val) { | ||
return acc; | ||
} | ||
|
||
return acc.concat(val); | ||
}, []) | ||
.find(ip => { | ||
if (ip === null) return false; | ||
return isIP(ip); | ||
}); | ||
|
||
return ipAddress ?? null; | ||
} | ||
|
||
function parseForwardedHeader(value: string | null): string | null { | ||
if (!value) return null; | ||
for (const part of value.split(';')) { | ||
if (part.startsWith('for=')) return part.slice(4); | ||
continue; | ||
} | ||
return null; | ||
} | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.