Skip to content

Commit

Permalink
dep update (#39)
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored May 25, 2022
1 parent aa06694 commit be76ea0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 54 deletions.
1 change: 0 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
esm: false
ts: false
jsx: false
flow: false
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
},
"homepage": "https://github.com/fastify/proxy-addr#readme",
"dependencies": {
"@fastify/forwarded": "^1.0.0",
"@fastify/forwarded": "^2.0.0",
"ipaddr.js": "^2.0.0"
},
"devDependencies": {
"beautify-benchmark": "0.2.4",
"benchmark": "2.1.4",
"standard": "^17.0.0",
"tap": "^14.11.0"
"tap": "^16.0.0"
},
"files": [
"LICENSE",
Expand Down
10 changes: 5 additions & 5 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ test('argument trustshould be optional', function (t) {

test('with no headers should return socket address', function (t) {
const req = createReq('127.0.0.1')
t.deepEqual(proxyaddr.all(req), ['127.0.0.1'])
t.same(proxyaddr.all(req), ['127.0.0.1'])
t.end()
})

test('with x-forwarded-for header should include x-forwarded-for', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1'
})
t.deepEqual(proxyaddr.all(req), ['127.0.0.1', '10.0.0.1'])
t.same(proxyaddr.all(req), ['127.0.0.1', '10.0.0.1'])
t.end()
})

test('with x-forwarded-for header should include x-forwarded-for in correct order', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2'
})
t.deepEqual(proxyaddr.all(req), ['127.0.0.1', '10.0.0.2', '10.0.0.1'])
t.same(proxyaddr.all(req), ['127.0.0.1', '10.0.0.2', '10.0.0.1'])
t.end()
})

test('with trust argument should stop at first untrusted', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2'
})
t.deepEqual(proxyaddr.all(req, '127.0.0.1'), ['127.0.0.1', '10.0.0.2'])
t.same(proxyaddr.all(req, '127.0.0.1'), ['127.0.0.1', '10.0.0.2'])
t.end()
})

test('with trust argument should be only socket address for no trust', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2'
})
t.deepEqual(proxyaddr.all(req, []), ['127.0.0.1'])
t.same(proxyaddr.all(req, []), ['127.0.0.1'])
t.end()
})

Expand Down
74 changes: 37 additions & 37 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ test('trust should not alter input array', function (t) {
const arr = ['loopback', '10.0.0.1']
const req = createReq('127.0.0.1')
t.doesNotThrow(proxyaddr.bind(null, req, arr))
t.deepEqual(arr, ['loopback', '10.0.0.1'])
t.same(arr, ['loopback', '10.0.0.1'])
t.end()
})

Expand Down Expand Up @@ -115,7 +115,7 @@ test('trust should be invoked as trust(addr, i)', function (t) {
return log.push(Array.prototype.slice.call(arguments))
})

t.deepEqual(log, [
t.same(log, [
['127.0.0.1', 0],
['10.0.0.1', 1]
])
Expand All @@ -125,249 +125,249 @@ test('trust should be invoked as trust(addr, i)', function (t) {

test('with all trusted should return socket address wtesth no headers', function (t) {
const req = createReq('127.0.0.1')
t.strictEqual(proxyaddr(req, all), '127.0.0.1')
t.equal(proxyaddr(req, all), '127.0.0.1')
t.end()
})

test('with all trusted should return header value', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1'
})
t.strictEqual(proxyaddr(req, all), '10.0.0.1')
t.equal(proxyaddr(req, all), '10.0.0.1')
t.end()
})

test('with all trusted should return furthest header value', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, all), '10.0.0.1')
t.equal(proxyaddr(req, all), '10.0.0.1')
t.end()
})

test('with none trusted should return socket address wtesth no headers', function (t) {
const req = createReq('127.0.0.1')
t.strictEqual(proxyaddr(req, none), '127.0.0.1')
t.equal(proxyaddr(req, none), '127.0.0.1')
t.end()
})

test('with none trusted should return socket address wtesth headers', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, none), '127.0.0.1')
t.equal(proxyaddr(req, none), '127.0.0.1')
t.end()
})

test('with some trusted should return socket address wtesth no headers', function (t) {
const req = createReq('127.0.0.1')
t.strictEqual(proxyaddr(req, trust10x), '127.0.0.1')
t.equal(proxyaddr(req, trust10x), '127.0.0.1')
t.end()
})

test('with some trusted should return socket address when not trusted', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, trust10x), '127.0.0.1')
t.equal(proxyaddr(req, trust10x), '127.0.0.1')
t.end()
})

test('with some trusted should return header when socket trusted', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1'
})
t.strictEqual(proxyaddr(req, trust10x), '192.168.0.1')
t.equal(proxyaddr(req, trust10x), '192.168.0.1')
t.end()
})

test('with some trusted should return first untrusted after trusted', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, trust10x), '192.168.0.1')
t.equal(proxyaddr(req, trust10x), '192.168.0.1')
t.end()
})

test('with some trusted should not skip untrusted', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '10.0.0.3, 192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, trust10x), '192.168.0.1')
t.equal(proxyaddr(req, trust10x), '192.168.0.1')
t.end()
})

test('when given array should accept ltesteral IP addresses', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1')
t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1')
t.end()
})

test('when given array should not trust non-IP addresses', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2, localhost'
})
t.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), 'localhost')
t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), 'localhost')
t.end()
})

test('when given array should return socket address if none match', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, ['127.0.0.1', '192.168.0.100']), '10.0.0.1')
t.equal(proxyaddr(req, ['127.0.0.1', '192.168.0.100']), '10.0.0.1')
t.end()
})

test('when array empty should return socket address ', function (t) {
const req = createReq('127.0.0.1')
t.strictEqual(proxyaddr(req, []), '127.0.0.1')
t.equal(proxyaddr(req, []), '127.0.0.1')
t.end()
})

test('when array empty should return socket address wtesth headers', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, []), '127.0.0.1')
t.equal(proxyaddr(req, []), '127.0.0.1')
t.end()
})

test('when given IPv4 addresses should accept ltesteral IP addresses', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1')
t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1')
t.end()
})

test('when given IPv4 addresses should accept CIDR notation', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.200'
})
t.strictEqual(proxyaddr(req, '10.0.0.2/26'), '10.0.0.200')
t.equal(proxyaddr(req, '10.0.0.2/26'), '10.0.0.200')
t.end()
})

test('when given IPv4 addresses should accept netmask notation', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.200'
})
t.strictEqual(proxyaddr(req, '10.0.0.2/255.255.255.192'), '10.0.0.200')
t.equal(proxyaddr(req, '10.0.0.2/255.255.255.192'), '10.0.0.200')
t.end()
})

test('when given IPv6 addresses should accept ltesteral IP addresses', function (t) {
const req = createReq('fe80::1', {
'x-forwarded-for': '2002:c000:203::1, fe80::2'
})
t.strictEqual(proxyaddr(req, ['fe80::1', 'fe80::2']), '2002:c000:203::1')
t.equal(proxyaddr(req, ['fe80::1', 'fe80::2']), '2002:c000:203::1')
t.end()
})

test('when given IPv6 addresses should accept CIDR notation', function (t) {
const req = createReq('fe80::1', {
'x-forwarded-for': '2002:c000:203::1, fe80::ff00'
})
t.strictEqual(proxyaddr(req, 'fe80::/125'), 'fe80::ff00')
t.equal(proxyaddr(req, 'fe80::/125'), 'fe80::ff00')
t.end()
})

test('with IP version mixed should match respective versions', function (t) {
const req = createReq('::1', {
'x-forwarded-for': '2002:c000:203::1'
})
t.strictEqual(proxyaddr(req, ['127.0.0.1', '::1']), '2002:c000:203::1')
t.equal(proxyaddr(req, ['127.0.0.1', '::1']), '2002:c000:203::1')
t.end()
})

test('with IP version mixed should not match IPv4 to IPv6', function (t) {
const req = createReq('::1', {
'x-forwarded-for': '2002:c000:203::1'
})
t.strictEqual(proxyaddr(req, '127.0.0.1'), '::1')
t.equal(proxyaddr(req, '127.0.0.1'), '::1')
t.end()
})

test('when IPv4-mapped IPv6 addresses should match IPv4 trust to IPv6 request', function (t) {
const req = createReq('::ffff:a00:1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1')
t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1')
t.end()
})

test('when IPv4-mapped IPv6 addresses should match IPv4 netmask trust to IPv6 request', function (t) {
const req = createReq('::ffff:a00:1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, ['10.0.0.1/16']), '192.168.0.1')
t.equal(proxyaddr(req, ['10.0.0.1/16']), '192.168.0.1')
t.end()
})

test('when IPv4-mapped IPv6 addresses should match IPv6 trust to IPv4 request', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.2'
})
t.strictEqual(proxyaddr(req, ['::ffff:a00:1', '::ffff:a00:2']), '192.168.0.1')
t.equal(proxyaddr(req, ['::ffff:a00:1', '::ffff:a00:2']), '192.168.0.1')
t.end()
})

test('when IPv4-mapped IPv6 addresses should match CIDR notation for IPv4-mapped address', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.200'
})
t.strictEqual(proxyaddr(req, '::ffff:a00:2/122'), '10.0.0.200')
t.equal(proxyaddr(req, '::ffff:a00:2/122'), '10.0.0.200')
t.end()
})

test('when IPv4-mapped IPv6 addresses should match CIDR notation for IPv4-mapped address mixed wtesth IPv6 CIDR', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.200'
})
t.strictEqual(proxyaddr(req, ['::ffff:a00:2/122', 'fe80::/125']), '10.0.0.200')
t.equal(proxyaddr(req, ['::ffff:a00:2/122', 'fe80::/125']), '10.0.0.200')
t.end()
})

test('when IPv4-mapped IPv6 addresses should match CIDR notation for IPv4-mapped address mixed wtesth IPv4 addresses', function (t) {
const req = createReq('10.0.0.1', {
'x-forwarded-for': '192.168.0.1, 10.0.0.200'
})
t.strictEqual(proxyaddr(req, ['::ffff:a00:2/122', '127.0.0.1']), '10.0.0.200')
t.equal(proxyaddr(req, ['::ffff:a00:2/122', '127.0.0.1']), '10.0.0.200')
t.end()
})

test('when given predefined names should accept single pre-defined name', function (t) {
const req = createReq('fe80::1', {
'x-forwarded-for': '2002:c000:203::1, fe80::2'
})
t.strictEqual(proxyaddr(req, 'linklocal'), '2002:c000:203::1')
t.equal(proxyaddr(req, 'linklocal'), '2002:c000:203::1')
t.end()
})

test('when given predefined names should accept multiple pre-defined names', function (t) {
const req = createReq('::1', {
'x-forwarded-for': '2002:c000:203::1, fe80::2'
})
t.strictEqual(proxyaddr(req, ['loopback', 'linklocal']), '2002:c000:203::1')
t.equal(proxyaddr(req, ['loopback', 'linklocal']), '2002:c000:203::1')
t.end()
})

test('when header contains non-ip addresses should stop at first non-ip after trusted', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': 'myrouter, 127.0.0.1, proxy'
})
t.strictEqual(proxyaddr(req, '127.0.0.1'), 'proxy')
t.equal(proxyaddr(req, '127.0.0.1'), 'proxy')
t.end()
})

test('when header contains non-ip addresses should stop at first malformed ip after trusted', function (t) {
const req = createReq('127.0.0.1', {
'x-forwarded-for': 'myrouter, 127.0.0.1, ::8:8:8:8:8:8:8:8:8'
})
t.strictEqual(proxyaddr(req, '127.0.0.1'), '::8:8:8:8:8:8:8:8:8')
t.equal(proxyaddr(req, '127.0.0.1'), '::8:8:8:8:8:8:8:8:8')
t.end()
})

Expand All @@ -381,7 +381,7 @@ test('when header contains non-ip addresses should provide all values to functio
return log.push(Array.prototype.slice.call(arguments))
})

t.deepEqual(log, [
t.same(log, [
['127.0.0.1', 0],
['proxy', 1],
['127.0.0.1', 2]
Expand All @@ -391,15 +391,15 @@ test('when header contains non-ip addresses should provide all values to functio

test('when socket address undefined should return undefined as address', function (t) {
const req = createReq(undefined)
t.strictEqual(proxyaddr(req, '127.0.0.1'), undefined)
t.equal(proxyaddr(req, '127.0.0.1'), undefined)
t.end()
})

test('when socket address undefined should return undefined even wtesth trusted headers', function (t) {
const req = createReq(undefined, {
'x-forwarded-for': '127.0.0.1, 10.0.0.1'
})
t.strictEqual(proxyaddr(req, '127.0.0.1'), undefined)
t.equal(proxyaddr(req, '127.0.0.1'), undefined)
t.end()
})

Expand Down
Loading

0 comments on commit be76ea0

Please sign in to comment.