Skip to content

Commit a2d37a9

Browse files
elmigrantodougwilson
authored andcommitted
Fix array argument being altered
fixes #10 closes #11
1 parent 8481cbb commit a2d37a9

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Fix array argument being altered
45
* deps: ipaddr.js@1.4.0
56

67
1.1.4 / 2017-03-24

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ function compile(val) {
8686
throw new TypeError('argument is required');
8787
}
8888

89-
var trust = typeof val === 'string'
90-
? [val]
91-
: val;
89+
var trust;
9290

93-
if (!Array.isArray(trust)) {
91+
if (typeof val === 'string') {
92+
trust = [val];
93+
} else if (Array.isArray(val)) {
94+
trust = val.slice();
95+
} else {
9496
throw new TypeError('unsupported trust argument');
9597
}
9698

test/test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ describe('proxyaddr(req, trust)', function () {
6161
assert.doesNotThrow(proxyaddr.bind(null, req, ['loopback', '10.0.0.1']));
6262
});
6363

64+
it('should not alter input array', function () {
65+
var arr = ['loopback', '10.0.0.1'];
66+
var req = createReq('127.0.0.1');
67+
assert.doesNotThrow(proxyaddr.bind(null, req, arr));
68+
assert.deepEqual(arr, ['loopback', '10.0.0.1']);
69+
});
70+
6471
it('should reject non-IP', function () {
6572
var req = createReq('127.0.0.1');
6673
assert.throws(proxyaddr.bind(null, req, 'blargh'), /invalid IP address/);
@@ -481,6 +488,12 @@ describe('proxyaddr.compile(trust)', function () {
481488
assert.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/136'), /invalid range on address/);
482489
assert.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/-46'), /invalid range on address/);
483490
});
491+
492+
it('should not alter input array', function () {
493+
var arr = ['loopback', '10.0.0.1'];
494+
assert.equal(typeof proxyaddr.compile(arr), 'function');
495+
assert.deepEqual(arr, ['loopback', '10.0.0.1']);
496+
});
484497
});
485498
});
486499
});

0 commit comments

Comments
 (0)