-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core-api): get client IP from
x-forwarded-for
header (#4527)
* chore(core-api): get ip from x-forwarded-for header * chore(core-api): pass trustProxy option to plugins * chore(core-api): resolve ip method * chore(core-api): resolve ip in plugins * test(core-api): getIp
- Loading branch information
1 parent
58789ed
commit f85b927
Showing
6 changed files
with
69 additions
and
7 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,34 @@ | ||
import { getIp } from "@packages/core-api/src/utils/get-ip"; | ||
|
||
const clientIp = "127.0.0.1"; | ||
const proxyIp = "127.0.0.2"; | ||
|
||
const request = { | ||
info: { | ||
remoteAddress: proxyIp, | ||
}, | ||
headers: { | ||
["x-forwarded-for"]: clientIp, | ||
}, | ||
}; | ||
|
||
describe("getIp", () => { | ||
it("should return IP from remoteAddress if trustProxy = false", () => { | ||
expect(getIp(request, false)).toEqual(proxyIp); | ||
}); | ||
|
||
it("should return IP from headers if trustProxy = true", () => { | ||
expect(getIp(request, true)).toEqual(clientIp); | ||
}); | ||
|
||
it("should return IP remoteAddress if trustProxy = true and x-forwarded-for headers are not set", () => { | ||
const requestWithoutHeaders = { | ||
info: { | ||
remoteAddress: clientIp, | ||
}, | ||
headers: {}, | ||
}; | ||
|
||
expect(getIp(requestWithoutHeaders, true)).toEqual(clientIp); | ||
}); | ||
}); |
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,9 @@ | ||
import { Request } from "@hapi/hapi"; | ||
|
||
export const getIp = (request: Request, trustProxy: boolean): string => { | ||
if (trustProxy) { | ||
return request.headers["x-forwarded-for"]?.split(",")[0]?.trim() ?? request.info.remoteAddress; | ||
} | ||
|
||
return request.info.remoteAddress; | ||
}; |
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 @@ | ||
export * from "./get-ip" |