-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
270 lines (229 loc) · 7.58 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* istanbul ignore next */
describe('forwarded-for', function () {
'use strict';
var middleware = require('./middleware')
, assume = require('assume')
, parser = require('./');
it('is exposed as function', function () {
assume(parser).is.a('function');
});
it('exposes the proxy array', function () {
assume(parser.proxies).to.be.a('array');
});
it('exposes the Forwarded class', function () {
assume(parser.Forwarded).to.be.a('function');
});
it('sorts the proxy array and sets the last match as first item', function () {
var last = parser.proxies[parser.proxies.length - 1]
, spec = { ip: '222.1.2.242', port: '4900', proto: 'https' };
assume(parser.proxies[0].ip).to.not.equal(last.ip);
var forwarded = parser({
remoteAddress: '127.1.2.0',
remotePort: 490
}, Object.keys(spec).reduce(function reduce(headers, key) {
headers[last[key]] = spec[key];
return headers;
}, {}));
assume(parser.proxies[0].ip).to.equal(last.ip);
assume(forwarded.ip).to.equal('222.1.2.242');
assume(forwarded.port).to.equal(4900);
assume(forwarded.secure).to.equal(true);
});
it('extracts the `remoteAdress` and port from the given object', function() {
var forwarded = parser({
remoteAddress: '222.1.2.242',
remotePort: 490
});
assume(forwarded.ip).to.equal('222.1.2.242');
assume(forwarded.port).to.equal(490);
assume(forwarded.secure).to.equal(false);
});
it('`encrypted` is used to check connection is secure', function () {
var forwarded = parser({
remoteAddress: '222.1.2.242',
remotePort: 490,
encrypted: true
});
assume(forwarded.ip).to.equal('222.1.2.242');
assume(forwarded.port).to.equal(490);
assume(forwarded.secure).to.equal(true);
});
describe('x-forwarded-* headers', function () {
it('prefer over the supplied object', function () {
var forwarded = parser({
remoteAddress: '127.1.2.0',
remotePort: 490
}, {
'x-forwarded-for': '72.1.80.224',
'x-forwarded-port': '9093',
'x-forwarded-proto': 'https'
});
assume(forwarded.ip).to.equal('72.1.80.224');
assume(forwarded.port).to.equal(9093);
assume(forwarded.secure).to.equal(true);
});
it('ensure header supports list with no spaces', function () {
var forwarded = parser({
remoteAddress: '127.1.2.0'
}, {
'x-forwarded-for': '72.1.80.224,10.0.1.1'
});
assume(forwarded.ip).to.equal('72.1.80.224');
});
it('ensure header supports list with spaces around commas', function () {
var forwarded = parser({
remoteAddress: '127.1.2.0'
}, {
'x-forwarded-for': '72.1.80.224, 10.0.1.1, 222.1.2.242'
});
assume(forwarded.ip).to.equal('72.1.80.224');
});
it('ensure header supports ip:port syntax', function () {
var forwarded = parser({
remoteAddress: '127.1.2.0'
}, {
'x-forwarded-for': '72.1.80.224:3000,10.0.1.1:8000,172.16.10.1'
});
assume(forwarded.ip).to.equal('72.1.80.224');
assume(forwarded.port).to.equal(3000);
});
it('ensure header is disqualified if invalid', function () {
var forwarded = parser({
remoteAddress: '127.1.2.0'
}, {
'x-forwarded-for': '72.1.80.224,999.0.1.1'
});
assume(forwarded.ip).to.equal('127.1.2.0');
});
});
it('works when shuffling the proxies array', function() {
var i = 0, forwarded, hs = [
{headers: {'fastly-client-ip': '1.2.3.4'}, expected: '1.2.3.4'},
{headers: {'x-forwarded-for': '9.9.9.9'}, expected: '9.9.9.9'},
{headers: {'forwarded': '3.4.5.6'}, expected: '3.4.5.6'},
{headers: {'x-real-ip': '7.8.9.10'}, expected: '7.8.9.10'}
];
// Fisher-Yates shuffle
function shuffle(o) { //v1.0
for (var j, x, i = o.length; i;
j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
for (; i < 8; i++) {
hs = shuffle(hs);
hs.forEach(function(v) {
forwarded = parser({}, v.headers);
assume(forwarded.ip).to.equal(v.expected);
});
}
});
describe('fastly.com', function () {
it('extracts information from fastly headers', function () {
var forwarded = parser({}, {
'fastly-client-ip': '1.2.3.4',
'fastly-ssl': '1'
});
assume(forwarded.ip).to.equal('1.2.3.4');
assume(forwarded.secure).to.equal(true);
forwarded = parser({}, { 'fastly-client-ip': '1.2.3.4' });
assume(forwarded.secure).to.equal(false);
});
});
describe('zscaler', function () {
it('extracts information from the zeus/zscaler headers', function () {
var forwarded = parser({}, {
'z-forwarded-for': '1.2.3.4'
});
assume(forwarded.ip).to.equal('1.2.3.4');
});
});
describe('socket.io', function () {
//
// Copy of the data structure that Socket.IO 0.9 supplies to the user during
// authorization and which is available as `socket.handshake`
//
var handshakeData ={
headers: {}
, address: {
address: '1.2.3.4',
port: 1234
}
, time: (new Date()).toString()
, query: { query: 'string' }
, url: '/socket.io/1/websocket'
, xdomain: false
, secure: true
, issued: Date.now()
};
it('extracts the data from the handshake', function () {
var forwarded = parser(handshakeData, {});
assume(forwarded.ip).to.equal('1.2.3.4');
assume(forwarded.port).to.equal(1234);
assume(forwarded.secure).to.equal(true);
});
});
describe('sockjs', function () {
var socket = {
_session: {
session_id: undefined,
heartbeat_delay: 25000,
disconnect_delay: 5000,
prefix: '/primus(?:[^/]+)?',
send_buffer: [],
is_closing: false,
readyState: 1,
timeout_cb: [Function],
to_tref:{
_idleTimeout: 25000,
_idlePrev: [Object],
_idleNext: [Object],
_idleStart: 1408442593896,
_onTimeout: [Function],
_repeat: false
},
connection: [socket],
emit_open: null,
recv: {
ws: [Object],
connection: [Object],
thingy: [Object],
thingy_end_cb: [Function],
session: [socket]
}
},
id: 'b44a71e0-3f95-42c6-a04e-648ea2db8ae4',
headers: { via: null, host: 'localhost:1113' },
prefix: '/primus(?:[^/]+)?',
remoteAddress: '1.2.3.4',
remotePort: 62191,
address: { address: '127.0.0.1', family: 'IPv4', port: 1113 },
url: '/primus/139/3v_ublnj/websocket',
pathname: '/primus/139/3v_ublnj/websocket',
protocol: 'websocket'
};
it('extracts the data from the socket', function () {
var forwarded = parser(socket, {});
assume(forwarded.ip).to.equal('1.2.3.4');
assume(forwarded.port).to.equal(62191);
assume(forwarded.secure).to.equal(false);
});
});
describe('middleware', function () {
it('is exported a function', function () {
assume(middleware).is.a('function');
});
it('returns a middleware layer', function () {
assume(middleware()).is.a('function');
assume(middleware().length).equals(3);
});
it('adds the ip information as `forwarded` property', function (next) {
var req = { remoteAddress: '127.1.2.0', remotePort: 490 }
, m = middleware();
m(req, {}, function () {
assume(req.forwarded).is.a('object');
assume(req.forwarded.port).equals(490);
next();
});
});
});
});