Skip to content

Commit

Permalink
Trust proxy may be provided from the environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Apr 25, 2022
1 parent 8e36507 commit b38358f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/node/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ export class NodeRequest<T> extends Request<T> {
*
* If trustProxy is set to true, it means this server is running behind a
* proxy, and the X-Forwarded-For header should be used instead.
*
* If trustProxy is not set, it defaults to false unless the
* CURVEBALL_TRUSTPROXY environment variable is set. Using an environment
* variable is a good idea for this as having a proxy may be environment
* dependent.
*/
ip(trustProxy: boolean = false): string {

if (trustProxy) {
if (trustProxy ?? process.env.CURVEBALL_TRUSTPROXY) {
const forwardedForHeader = this.headers.get('X-Forwarded-For');
if (forwardedForHeader) {
return forwardedForHeader.split(',')[0].trim();
Expand Down
17 changes: 14 additions & 3 deletions test/node/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ describe('NodeRequest', () => {
});

server.close();
expect(ip).to.eql('::ffff:127.0.0.1');
expect(ip).to.be.oneOf([
'::ffff:127.0.0.1',
'::1',
]);

});

Expand Down Expand Up @@ -264,7 +267,10 @@ describe('NodeRequest', () => {
});

server.close();
expect(ip).to.eql('::ffff:127.0.0.1');
expect(ip).to.be.oneOf([
'::ffff:127.0.0.1',
'::1',
]);

});

Expand All @@ -289,7 +295,12 @@ describe('NodeRequest', () => {
});

server.close();
expect(ip).to.eql('::ffff:127.0.0.1');


expect(ip).to.be.oneOf([
'::ffff:127.0.0.1',
'::1',
]);

});
});
Expand Down

0 comments on commit b38358f

Please sign in to comment.