forked from caolan/cookie-sessions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-cookie-sessions.js
797 lines (724 loc) · 23.5 KB
/
test-cookie-sessions.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
var sessions = require('cookie-sessions');
exports['split'] = function(test){
var hmac_sig = 'c82d1eacb4adb15a3250a6df7c8f190b586ab6b9',
timestamp = 1264710287440,
data_blob = 'somedata';
var serialized_cookie = hmac_sig + timestamp + data_blob;
test.same(
sessions.split(serialized_cookie),
{hmac_signature: hmac_sig, timestamp: timestamp, data_blob: data_blob},
'split correctly seperates sig, timestamp and data blob'
);
test.done();
};
exports['valid'] = function(test){
var secret = 'secret';
current_valid_sig = '5eaaa22480acefd8b18d67bb194573dc1b75d9db',
expired_valid_sig = '9c7ad86913ceeced1f6f249ba52868006c8dfdab',
invalid_sig = '51a2a32485a6e7d8b9810711112513d14b15d16b',
expired_timestamp = 1264700000000,
current_timestamp = 1264710287440,
session_timeout = 54000,
data_blob = 'somedata';
var Date_copy = global.Date;
global.Date = function(){this.getTime = function(){return 1264710287000}};
test.ok(
sessions.valid(
secret, session_timeout,
current_valid_sig + current_timestamp + data_blob
) === true,
'returns true for valid hmac sig within timeout'
);
test.ok(
sessions.valid(
secret, session_timeout,
expired_valid_sig + expired_timestamp + data_blob
) === false,
'returns false for valid hmac sig past timeout'
);
test.ok(
sessions.valid(
secret, session_timeout,
invalid_sig + current_timestamp + data_blob
) === false,
'returns false for invalid hmac sig within timeout'
);
test.ok(
sessions.valid(
secret, session_timeout,
invalid_sig + expired_timestamp + data_blob
) === false,
'returns false for invalid hmac sig past timeout'
);
// restore Date
global.Date = Date_copy;
test.done();
};
exports['decrypt'] = function(test){
var r = sessions.decrypt(
'secret', '686734eb9e0fff9adea53983210825ef'
);
test.same(r, 'somedata', 'decrypt sucessfully returns decrypted data');
test.done();
};
exports['encrypt'] = function(test){
var r = sessions.encrypt('secret', 'somedata');
test.same(
r, '686734eb9e0fff9adea53983210825ef',
'encrypt sucessfully returns encrypted data'
);
test.done();
};
exports['deserialize valid cookie'] = function(test){
test.expect(8);
// copy some functions
var valid = sessions.valid;
var decrypt = sessions.decrypt;
var parse = JSON.parse;
sessions.valid = function(secret, timeout, str){
test.equals(secret, 'secret', 'valid called with secret');
test.equals(timeout, 123, 'valid called with timeout');
test.equals(str, 'cookiestring', 'valid called with cookie string');
return true;
};
sessions.split = function(str){
test.equals(str, 'cookiestring', 'split called with cookie string');
return {data_blob: 'datastr'};
};
sessions.decrypt = function(secret, str){
test.equals(secret, 'secret', 'decrypt called with secret');
test.equals(str, 'datastr', 'decrypt called with data string');
return 'decrypted_data';
};
JSON.parse = function(str){
test.equals(
str, 'decrypted_data', 'JSON.parse called with decrypted data'
);
return {test:'test'};
};
var r = sessions.deserialize('secret', 123, 'cookiestring');
test.same(r, {test:'test'}, 'deserialize returns parsed json data');
// restore copied functions:
sessions.valid = valid;
sessions.decrypt = decrypt;
JSON.parse = parse;
test.done();
};
exports['deserialize invalid cookie'] = function(test){
test.expect(1);
// copy some functions
var valid = sessions.valid;
var decrypt = sessions.decrypt;
var parse = JSON.parse;
sessions.valid = function(secret, timeout, str){
return false;
};
sessions.decrypt = function(secret, str){
test.ok(false, 'should not attempt to decrypt invalid cookie');
};
JSON.parse = function(str){
test.ok(false, 'should not attempt to parse invalid cookie');
};
try {
sessions.deserialize('secret', 123, 'cookiestring');
}
catch(e){
test.ok(true, 'throw exception on invalid cookie');
}
// restore copied functions:
sessions.valid = valid;
sessions.decrypt = decrypt;
JSON.parse = parse;
test.done();
};
exports['serialize'] = function(test){
test.expect(7);
// copy some functions
var encrypt = sessions.encrypt;
var hmac_signature = sessions.hmac_signature;
var stringify= JSON.stringify;
var Date_copy = global.Date;
global.Date = function(){this.getTime = function(){return 1234;}};
JSON.stringify = function(obj){
test.same(
obj, {test:'test'}, 'JSON.stringify called with cookie data obj'
);
return 'data';
};
sessions.encrypt = function(secret, str){
test.equals(secret, 'secret', 'encrypt called with secret');
test.equals(str, 'data', 'encrypt called with stringified data');
return 'encrypted_data';
};
sessions.hmac_signature = function(secret, timestamp, data_str){
test.equals(secret, 'secret', 'hmac_signature called with secret');
test.equals(timestamp, 1234, 'hmac_signature called with timestamp');
test.equals(
data_str, 'encrypted_data',
'hmac_signature called with encrypted data string'
);
return 'hmac';
};
var r = sessions.serialize('secret', {test:'test'});
test.equals(
r, 'hmac1234encrypted_data', 'serialize returns correct string'
);
// restore copied functions:
sessions.encrypt = encrypt;
sessions.hmac_signature = hmac_signature;
JSON.stringify = stringify;
global.Date = Date_copy;
test.done();
};
exports['serialize data over 4096 chars'] = function(test){
test.expect(1);
// copy some functions
var encrypt = sessions.encrypt;
var hmac_signature = sessions.hmac_signature;
var stringify= JSON.stringify;
var Date_copy = global.Date;
global.Date = function(){this.getTime = function(){return 1234;}};
JSON.stringify = function(obj){
return 'data';
};
sessions.encrypt = function(secret, str){
// lets make this too long!
var r = '';
for(var i=0; i<4089; i++){
r = r + 'x';
};
return r;
};
sessions.hmac_signature = function(secret, timestamp, data_str){
return 'hmac';
};
try {
var r = sessions.serialize('secret', {test:'test'});
}
catch(e){
test.ok(
true, 'serializing a cookie over 4096 chars throws an exception'
);
}
// restore copied functions:
sessions.encrypt = encrypt;
sessions.hmac_signature = hmac_signature;
JSON.stringify = stringify;
global.Date = Date_copy;
test.done();
};
exports['readCookies'] = function(test){
var req = {
headers: {cookie: "name1=data1; test=\"abcXYZ%20123\""},
url: '/'
};
var r = sessions.readCookies(req);
test.same(r, {name1: 'data1', test: '"abcXYZ 123"'}, 'test header read ok');
test.done();
};
exports['readCookies alternate format'] = function(test){
var req = {
headers: {cookie: "name1=data1;test=\"abcXYZ%20123\""},
url: '/'
};
var r = sessions.readCookies(req);
test.same(r, {name1: 'data1', test: '"abcXYZ 123"'}, 'test header read ok');
test.done();
};
exports['readCookies no cookie in headers'] = function(test){
var req = {headers: {}, url: '/'};
var r = sessions.readCookies(req);
test.same(r, {}, 'returns empty object');
test.done();
};
exports['readCookies from Connect cookieDecoder'] = function(test){
var req = {headers: {}, cookies: {'test':'cookie'}, url: '/'};
test.same(sessions.readCookies(req), {'test': 'cookie'});
test.done();
};
exports['readSession'] = function(test){
test.expect(5);
var readCookies = sessions.readCookies;
var deserialize = sessions.deserialize;
sessions.readCookies = function(r){
test.equals(r, 'request_obj', 'readCookies called with request object');
return {'node_session': 'cookie_data'};
};
sessions.deserialize = function(secret, timeout, str){
test.equals(secret, 'secret', 'readCookies called with secret');
test.equals(timeout, 12, 'readCookies called with timeout');
test.equals(str, 'cookie_data', 'readCookies called with cookie data');
return {test: 'test'};
};
var r = sessions.readSession(
'node_session', 'secret', 12, 'request_obj'
);
test.same(r, {test: 'test'}, 'session with key node_session read ok');
// restore copied functions
sessions.readCookies = readCookies;
sessions.deserialize = deserialize;
test.done();
};
exports['readSession no cookie'] = function(test){
test.expect(2);
var readCookies = sessions.readCookies;
var deserialize = sessions.deserialize;
sessions.readCookies = function(r){
test.equals(r, 'request_obj', 'readCookies called with request object');
return {};
};
sessions.deserialize = function(secret, timeout, str){
test.ok(false, 'should not call deserialize');
};
var r = sessions.readSession(
'node_session', 'secret', 12, 'request_obj'
);
test.same(r, undefined, 'return empty session');
// restore copied functions
sessions.readCookies = readCookies;
sessions.deserialize = deserialize;
test.done();
};
exports['onRequest'] = function(test){
test.expect(5);
var readSession = sessions.readSession;
var s = {
session_key:'_node',
secret: 'secret',
timeout: 86400
};
var req = {url: '/'};
sessions.readSession = function(key, secret, timeout, req){
test.equals(key, '_node', 'readSession called with session key');
test.equals(secret, 'secret', 'readSession called with secret');
test.equals(timeout, 86400, 'readSession called with timeout');
return 'testsession';
};
var next = function(){
test.ok(true, 'chain.next called');
test.equals(
req.session, 'testsession', 'req.session equals session data'
);
};
sessions(s)(req, 'res', next);
// restore copied functions
sessions.readSession = readSession;
test.done();
};
exports['writeHead'] = function(test){
test.expect(6);
var s = {
session_key:'_node',
secret: 'secret',
timeout: 86400
};
var req = {headers: {cookie: "_node="}, url: '/'};
var res = {
writeHead: function(code, headers){
test.equals(
headers['Set-Cookie'],
'_node=serialized_session; ' +
'expires=expiry_date; ' +
'max-age=86400; ' +
'path=/; HttpOnly'
);
test.equals(headers['original'], 'header');
}
};
var serialize = sessions.serialize;
sessions.serialize = function(secret, data){
test.equals(secret, 'secret', 'serialize called with secret');
test.same(data, {test:'test'}, 'serialize called with session data');
return 'serialized_session';
};
var expires = sessions.expires;
sessions.expires = function(timeout){
test.equals(timeout, s.timeout);
return 'expiry_date';
};
var next = function(){
test.ok(true, 'chain.next called');
req.session = {test:'test'};
res.writeHead(200, {'original':'header'});
// restore copied functions
sessions.serialize = serialize;
sessions.expires = expires;
test.done();
};
sessions(s)(req, res, next);
};
exports['writeHead doesnt write cookie if none exists and session is undefined'] = function(test){
test.expect(3);
var s = {
session_key:'_node',
secret: 'secret',
timeout: 86400
};
var req = {headers: {}, url: '/'};
var res = {
writeHead: function(code, headers){
test.ok(!("Set-Cookie" in headers));
test.equals(headers['original'], 'header');
}
};
var next = function(){
test.ok(true, 'chain.next called');
req.session = undefined;
res.writeHead(200, {'original':'header'});
test.done();
};
sessions(s)(req, res, next);
};
exports['writeHead writes empty cookie with immediate expiration if session is undefined'] = function(test){
test.expect(4);
var s = {
session_key:'_node',
secret: 'secret',
timeout: 86400
};
var req = {headers: {cookie: "_node=Blah"}, url: '/'};
var res = {
writeHead: function(code, headers){
test.equals(
headers['Set-Cookie'],
'_node=; ' +
'expires=now; ' +
'max-age=0; ' +
'path=/; HttpOnly'
);
test.equals(headers['original'], 'header');
}
};
var expires = sessions.expires;
sessions.expires = function(timeout){
test.equals(timeout, 0);
return 'now';
};
var readSession = sessions.readSession;
sessions.readSession = function(key, secret, timeout, req) {
return {"username": "Bob"};
};
var next = function(){
test.ok(true, 'chain.next called');
req.session = undefined;
res.writeHead(200, {'original':'header'});
// restore copied functions
sessions.expires = expires;
sessions.readSession = readSession;
test.done();
};
sessions(s)(req, res, next);
};
exports['onInit secret set'] = function(test){
test.expect(0);
var s = {secret: 'secret'};
try {
sessions({secret: 'secret'});
}
catch(e){
test.ok(false, 'do nothing if secret set in server settings');
}
test.done();
};
exports['onInit no secret set'] = function(test){
test.expect(1);
try {
sessions({});
}
catch(e){
test.ok(true, 'throw exception if no secret set in server settings');
}
test.done();
};
exports['set multiple cookies'] = function(test){
test.expect(3);
var _serialize = sessions.serialize;
sessions.serialize = function(){
return 'session_data';
};
var _expires = sessions.expires;
sessions.expires = function(timeout){
test.equals(timeout, 12345);
return 'expiry_date';
};
var req = {headers: {cookie:''}, url: '/'};
var res = {writeHead: function(statusCode, headers){
test.equals(statusCode, 200);
test.same(headers, [
['other_header', 'val'],
['Set-Cookie', 'testcookie=testvalue'],
['Set-Cookie', '_node=session_data; ' +
'expires=expiry_date; ' +
'max-age=12345; ' +
'path=/; HttpOnly']
]);
sessions.serialize = _serialize;
sessions.expires = _expires;
test.done();
}};
sessions({secret: 'secret', timeout: 12345})(req, res, function(){
req.session = {test: 'test'};
res.writeHead(200, {
'other_header': 'val',
'Set-Cookie':'testcookie=testvalue'
});
});
};
exports['set single cookie'] = function(test){
test.expect(3);
var _serialize = sessions.serialize;
sessions.serialize = function(){
return 'session_data';
};
var _expires = sessions.expires;
sessions.expires = function(timeout){
test.equals(timeout, 12345);
return 'expiry_date';
};
var req = {headers: {cookie:''}, url: '/'};
var res = {writeHead: function(statusCode, headers){
test.equals(statusCode, 200);
test.same(headers, {
'other_header': 'val',
'Set-Cookie': '_node=session_data; ' +
'expires=expiry_date; ' +
'max-age=12345; ' +
'path=/; HttpOnly'
});
sessions.serialize = _serialize;
sessions.expires = _expires;
test.done();
}};
sessions({secret: 'secret', timeout: 12345})(req, res, function(){
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};
exports['handle headers as array'] = function(test){
test.expect(3);
var _serialize = sessions.serialize;
sessions.serialize = function(){
return 'session_data';
};
var _expires = sessions.expires;
sessions.expires = function(timeout){
test.equals(timeout, 12345);
return 'expiry_date';
};
var req = {headers: {cookie:''}, url: '/'};
var res = {writeHead: function(statusCode, headers){
test.equals(statusCode, 200);
test.same(headers, [
['header1', 'val1'],
['header2', 'val2'],
['Set-Cookie', '_node=session_data; ' +
'expires=expiry_date; ' +
'max-age=12345; ' +
'path=/; HttpOnly']
]);
sessions.serialize = _serialize;
test.done();
}};
sessions({secret: 'secret', timeout: 12345})(req, res, function(){
req.session = {test: 'test'};
res.writeHead(200, [['header1', 'val1'],['header2', 'val2']]);
});
};
exports['convert headers to array'] = function(test){
test.same(
sessions.headersToArray({'key1':'val1','key2':'val2'}),
[['key1','val1'],['key2','val2']]
);
test.same(
sessions.headersToArray([['key1','val1'],['key2','val2']]),
[['key1','val1'],['key2','val2']]
);
test.done();
};
exports['send cookies even if there are no headers'] = function (test) {
test.expect(2);
var req = {headers: {cookie:''}, url: '/'};
var res = {
writeHead: function (code, headers) {
test.equal(code, 200);
test.ok(headers['Set-Cookie']);
test.done();
}
};
sessions({secret: 'secret', timeout: 12345})(req, res, function () {
req.session = {test: 'test'};
res.writeHead(200);
});
};
exports['send cookies when no headers but reason_phrase'] = function (test) {
test.expect(3);
var req = {headers: {cookie:''}, url: '/'};
var res = {
writeHead: function (code, reason_phrase, headers) {
test.equal(code, 200);
test.equal(reason_phrase, 'reason');
test.ok(headers['Set-Cookie']);
test.done();
}
};
sessions({secret: 'secret', timeout: 12345})(req, res, function () {
req.session = {test: 'test'};
res.writeHead(200, 'reason');
});
};
exports['custom path'] = function (test) {
test.expect(2);
var req = {headers: {cookie:''}, url: '/test/path'};
var res = {
writeHead: function (code, headers) {
test.equal(code, 200);
test.ok(/path=\/test\/path/.test(headers['Set-Cookie']));
test.done();
}
};
sessions({
secret: 'secret',
timeout: 12345,
path: '/test/path'
})(req, res, function () {
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};
exports['don\'t set cookie if incorrect path'] = function (test) {
test.expect(2);
var req = {headers: {cookie:''}, url: '/other/path'};
var res = {
writeHead: function (code, headers) {
test.equal(code, 200);
test.equal(headers['Set-Cookie'], undefined);
test.done();
}
};
sessions({
secret: 'secret',
timeout: 12345,
path: '/test/path'
})(req, res, function () {
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};
exports['custom domain'] = function (test) {
test.expect(2);
var req = {headers: {cookie:''}, url: '/'};
var res = {
writeHead: function (code, headers) {
test.equal(code, 200);
test.ok(/domain=testdomain.com/.test(headers['Set-Cookie']));
test.done();
}
};
sessions({
secret: 'secret',
domain: 'testdomain.com'
})(req, res, function () {
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};
exports['secure'] = function (test) {
test.expect(2);
var req = {headers: {cookie:''}, url: '/'};
var res = {
writeHead: function (code, headers) {
test.equal(code, 200);
test.ok(/secure;/.test(headers['Set-Cookie']));
test.done();
}
};
sessions({
secret: 'secret',
secure: true
})(req, res, function () {
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};
exports['useExpires: false'] = function(test){
test.expect(2);
var _serialize = sessions.serialize;
sessions.serialize = function(){
return 'session_data';
};
var req = {headers: {cookie:''}, url: '/'};
var res = {writeHead: function(statusCode, headers){
test.equals(statusCode, 200);
test.same(headers, {
'other_header': 'val',
'Set-Cookie': '_node=session_data; ' +
'max-age=12345; ' +
'path=/; HttpOnly'
});
sessions.serialize = _serialize;
test.done();
}};
sessions({secret: 'secret', timeout: 12345, useExpires: false})(req, res, function(){
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};
exports['useMaxAge: false'] = function(test){
test.expect(3);
var _serialize = sessions.serialize;
sessions.serialize = function(){
return 'session_data';
};
var _expires = sessions.expires;
sessions.expires = function(timeout){
test.equals(timeout, 12345);
return 'expiry_date';
};
var req = {headers: {cookie:''}, url: '/'};
var res = {writeHead: function(statusCode, headers){
test.equals(statusCode, 200);
test.same(headers, {
'other_header': 'val',
'Set-Cookie': '_node=session_data; ' +
'expires=expiry_date; ' +
'path=/; HttpOnly'
});
sessions.serialize = _serialize;
sessions.expires = _expires;
test.done();
}};
sessions({secret: 'secret', timeout: 12345, useMaxAge: false})(req, res, function(){
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};
exports['useHttpOnly: false'] = function(test){
test.expect(3);
var _serialize = sessions.serialize;
sessions.serialize = function(){
return 'session_data';
};
var _expires = sessions.expires;
sessions.expires = function(timeout){
test.equals(timeout, 12345);
return 'expiry_date';
};
var req = {headers: {cookie:''}, url: '/'};
var res = {writeHead: function(statusCode, headers){
test.equals(statusCode, 200);
test.same(headers, {
'other_header': 'val',
'Set-Cookie': '_node=session_data; ' +
'expires=expiry_date; ' +
'max-age=12345; ' +
'path=/'
});
sessions.serialize = _serialize;
sessions.expires = _expires;
test.done();
}};
sessions({secret: 'secret', timeout: 12345, useHttpOnly: false})(req, res, function(){
req.session = {test: 'test'};
res.writeHead(200, {'other_header': 'val'});
});
};