diff --git a/src/node/request.ts b/src/node/request.ts index 9581b29..3cac050 100644 --- a/src/node/request.ts +++ b/src/node/request.ts @@ -72,10 +72,15 @@ export class NodeRequest extends Request { * * 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(); diff --git a/test/node/request.ts b/test/node/request.ts index 4687777..358cb10 100644 --- a/test/node/request.ts +++ b/test/node/request.ts @@ -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', + ]); }); @@ -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', + ]); }); @@ -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', + ]); }); });