Skip to content

Commit 801291b

Browse files
committedJun 15, 2019
fix linting error
1 parent f928547 commit 801291b

File tree

3 files changed

+99
-100
lines changed

3 files changed

+99
-100
lines changed
 

‎src/crypto.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Crypto {
99
this.iv_size = options.iv_size || 16
1010
this.at_size = options.at_size || 16
1111
this.key_size = options.key_size || 32
12-
this.secret = this._derive_key(options.secret) || false
12+
this.secret = this._deriveKey(options.secret) || false
1313
}
1414

1515
set(plaintext) {
@@ -42,7 +42,7 @@ class Crypto {
4242
}
4343

4444
get(ciphertext) {
45-
let ct, hmac, pt, sid, session
45+
let ct
4646

4747
if (ciphertext) {
4848
try {
@@ -52,17 +52,17 @@ class Crypto {
5252
}
5353
}
5454

55-
hmac = this._digest(this.secret, ct.ct, this.hashing, this.encodeas)
55+
const hmac = this._digest(this.secret, ct.ct, this.hashing, this.encodeas)
5656

57-
if (hmac != ct.hmac) {
58-
throw 'Encrypted session was tampered with!'
57+
if (hmac !== ct.hmac) {
58+
throw Error('Encrypted session was tampered with!')
5959
}
6060

6161
if (ct.at) {
6262
ct.at = Buffer.from(ct.at)
6363
}
6464

65-
pt = this._decrypt(
65+
const pt = this._decrypt(
6666
this.secret,
6767
ct.ct,
6868
this.algorithm,
@@ -140,14 +140,12 @@ class Crypto {
140140
return pt
141141
}
142142

143-
_derive_key(secret) {
144-
let key, hash, salt
145-
146-
hash = this.crypto.createHash(this.hashing)
143+
_deriveKey(secret) {
144+
const hash = this.crypto.createHash(this.hashing)
147145
hash.update(secret)
148-
salt = hash.digest(this.encodeas).substr(0, 16)
146+
const salt = hash.digest(this.encodeas).substr(0, 16)
149147

150-
key = this.crypto.pbkdf2Sync(secret, salt, 10000, 64, this.hashing)
148+
const key = this.crypto.pbkdf2Sync(secret, salt, 10000, 64, this.hashing)
151149

152150
return key.toString(this.encodeas).substr(0, this.key_size)
153151
}

‎src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ module.exports = function(connect) {
236236
if (session) {
237237
if (this.Crypto) {
238238
try {
239-
const tmp_session = this.transformFunctions.unserialize(
239+
const tmpSession = this.transformFunctions.unserialize(
240240
session.session
241241
)
242-
session.session = this.Crypto.get(tmp_session)
242+
session.session = this.Crypto.get(tmpSession)
243243
} catch (error) {
244244
return callback(error)
245245
}

‎test/legacy-tests.js

+87-86
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable handle-callback-err */
12
'use strict'
23

34
const expressSession = require('express-session')
@@ -11,7 +12,7 @@ const mongo = require('mongodb')
1112
const mongoose = require('mongoose')
1213

1314
// Create a connect cookie instance
14-
const make_cookie = function() {
15+
const makeCookie = function() {
1516
const cookie = new expressSession.Cookie()
1617
cookie.maxAge = 10000 // This sets cookie.expire through a setter
1718
cookie.secure = true
@@ -41,19 +42,19 @@ function getClientPromise() {
4142
}
4243

4344
// Create session data
44-
const make_data = function() {
45+
const makeData = function() {
4546
return {
4647
foo: 'bar',
4748
baz: {
4849
cow: 'moo',
4950
chicken: 'cluck',
5051
},
5152
num: 1,
52-
cookie: make_cookie(),
53+
cookie: makeCookie(),
5354
}
5455
}
5556

56-
const make_data_no_cookie = function() {
57+
const makeDataNoCookie = function() {
5758
return {
5859
foo: 'bar',
5960
baz: {
@@ -66,7 +67,7 @@ const make_data_no_cookie = function() {
6667
}
6768

6869
// Given a session id, input data, and session, make sure the stored data matches in the input data
69-
const assert_session_equals = function(sid, data, session) {
70+
const assertSessionEquals = function(sid, data, session) {
7071
if (typeof session.session === 'string') {
7172
// Compare stringified JSON
7273
assert.strictEqual(session.session, JSON.stringify(data))
@@ -76,9 +77,9 @@ const assert_session_equals = function(sid, data, session) {
7677
for (const prop in session.session) {
7778
if (prop === 'cookie') {
7879
// Make sure the cookie is intact
79-
assert.deepEqual(session.session.cookie, data.cookie.toJSON())
80+
assert.deepStrictEqual(session.session.cookie, data.cookie.toJSON())
8081
} else {
81-
assert.deepEqual(session.session[prop], data[prop])
82+
assert.deepStrictEqual(session.session[prop], data[prop])
8283
}
8384
}
8485
}
@@ -87,20 +88,20 @@ const assert_session_equals = function(sid, data, session) {
8788
assert.strictEqual(session._id, sid)
8889
}
8990

90-
const open_db = function(options, callback) {
91+
const openDb = function(options, callback) {
9192
const store = new MongoStore(options)
9293
store.once('connected', function() {
9394
callback(this, this.db, this.collection)
9495
})
9596
}
9697

97-
const cleanup_store = function(store) {
98+
const cleanupStore = function(store) {
9899
store.close()
99100
}
100101

101102
const cleanup = function(store, db, collection, callback) {
102103
collection.drop(() => {
103-
cleanup_store(store)
104+
cleanupStore(store)
104105
callback()
105106
})
106107
}
@@ -117,22 +118,22 @@ function getNativeDbConnection(options, done) {
117118
if (err) {
118119
return done(err)
119120
}
120-
open_db(Object.assign(options, { client }), done)
121+
openDb(Object.assign(options, { client }), done)
121122
}
122123
)
123124
}
124125

125126
exports.test_set = function(done) {
126127
getNativeDbConnection((store, db, collection) => {
127128
const sid = 'test_set-sid'
128-
const data = make_data()
129+
const data = makeData()
129130

130131
store.set(sid, data, err => {
131-
assert.equal(err, null)
132+
assert.strictEqual(err, null)
132133

133134
// Verify it was saved
134135
collection.findOne({ _id: sid }, (err, session) => {
135-
assert_session_equals(sid, data, session)
136+
assertSessionEquals(sid, data, session)
136137

137138
cleanup(store, db, collection, () => {
138139
done()
@@ -145,14 +146,14 @@ exports.test_set = function(done) {
145146
exports.test_set_promise = function(done) {
146147
getNativeDbConnection((store, db, collection) => {
147148
const sid = 'test_set_promise-sid'
148-
const data = make_data()
149+
const data = makeData()
149150

150151
store
151152
.set(sid, data)
152153
.then(() => {
153154
// Verify it was saved
154155
collection.findOne({ _id: sid }, (err, session) => {
155-
assert_session_equals(sid, data, session)
156+
assertSessionEquals(sid, data, session)
156157

157158
cleanup(store, db, collection, () => {
158159
done()
@@ -166,14 +167,14 @@ exports.test_set_promise = function(done) {
166167
exports.test_set_no_stringify = function(done) {
167168
getNativeDbConnection({ stringify: false }, (store, db, collection) => {
168169
const sid = 'test_set-sid'
169-
const data = make_data()
170+
const data = makeData()
170171

171172
store.set(sid, data, err => {
172-
assert.equal(err, null)
173+
assert.strictEqual(err, null)
173174

174175
// Verify it was saved
175176
collection.findOne({ _id: sid }, (err, session) => {
176-
assert_session_equals(sid, data, session)
177+
assertSessionEquals(sid, data, session)
177178

178179
cleanup(store, db, collection, () => {
179180
done()
@@ -184,24 +185,24 @@ exports.test_set_no_stringify = function(done) {
184185
}
185186

186187
exports.test_session_cookie_overwrite_no_stringify = function(done) {
187-
const origSession = make_data()
188+
const origSession = makeData()
188189
const cookie = origSession.cookie
189190

190191
getNativeDbConnection({ stringify: false }, (store, db, collection) => {
191192
const sid = 'test_set-sid'
192193
store.set(sid, origSession, err => {
193-
assert.equal(err, null)
194+
assert.strictEqual(err, null)
194195

195196
collection.findOne({ _id: sid }, (err, session) => {
196197
// Make sure cookie came out intact
197198
assert.strictEqual(origSession.cookie, cookie)
198199

199200
// Make sure the fields made it back intact
200-
assert.equal(
201+
assert.strictEqual(
201202
cookie.expires.toJSON(),
202203
session.session.cookie.expires.toJSON()
203204
)
204-
assert.equal(cookie.secure, session.session.cookie.secure)
205+
assert.strictEqual(cookie.secure, session.session.cookie.secure)
205206

206207
cleanup(store, db, collection, () => {
207208
done()
@@ -214,14 +215,14 @@ exports.test_session_cookie_overwrite_no_stringify = function(done) {
214215
exports.test_set_expires = function(done) {
215216
getNativeDbConnection((store, db, collection) => {
216217
const sid = 'test_set_expires-sid'
217-
const data = make_data()
218+
const data = makeData()
218219

219220
store.set(sid, data, err => {
220-
assert.equal(err, null)
221+
assert.strictEqual(err, null)
221222

222223
// Verify it was saved
223224
collection.findOne({ _id: sid }, (err, session) => {
224-
assert_session_equals(sid, data, session)
225+
assertSessionEquals(sid, data, session)
225226

226227
cleanup(store, db, collection, () => {
227228
done()
@@ -234,14 +235,14 @@ exports.test_set_expires = function(done) {
234235
exports.test_set_expires_no_stringify = function(done) {
235236
getNativeDbConnection({ stringify: false }, (store, db, collection) => {
236237
const sid = 'test_set_expires-sid'
237-
const data = make_data()
238+
const data = makeData()
238239

239240
store.set(sid, data, err => {
240-
assert.equal(err, null)
241+
assert.strictEqual(err, null)
241242

242243
// Verify it was saved
243244
collection.findOne({ _id: sid }, (err, session) => {
244-
assert_session_equals(sid, data, session)
245+
assertSessionEquals(sid, data, session)
245246

246247
cleanup(store, db, collection, () => {
247248
done()
@@ -258,7 +259,7 @@ exports.test_get = function(done) {
258259
{ _id: sid, session: JSON.stringify({ key1: 1, key2: 'two' }) },
259260
() => {
260261
store.get(sid, (err, session) => {
261-
assert.deepEqual(session, { key1: 1, key2: 'two' })
262+
assert.deepStrictEqual(session, { key1: 1, key2: 'two' })
262263
cleanup(store, db, collection, () => {
263264
done()
264265
})
@@ -277,7 +278,7 @@ exports.test_get_promise = function(done) {
277278
store
278279
.get(sid)
279280
.then(session => {
280-
assert.deepEqual(session, { key1: 1, key2: 'two' })
281+
assert.deepStrictEqual(session, { key1: 1, key2: 'two' })
281282
cleanup(store, db, collection, () => {
282283
done()
283284
})
@@ -295,9 +296,9 @@ exports.test_all = function(done) {
295296
{ _id: sid, session: JSON.stringify({ key1: 1, key2: 'two' }) },
296297
() => {
297298
store.all((err, sessions) => {
298-
assert.equal(err, null)
299+
assert.strictEqual(err, null)
299300
assert.strictEqual(sessions.length, 1)
300-
assert.deepEqual(sessions[0], { key1: 1, key2: 'two' })
301+
assert.deepStrictEqual(sessions[0], { key1: 1, key2: 'two' })
301302
cleanup(store, db, collection, () => {
302303
done()
303304
})
@@ -317,7 +318,7 @@ exports.test_all_promise = function(done) {
317318
.all()
318319
.then(sessions => {
319320
assert.strictEqual(sessions.length, 1)
320-
assert.deepEqual(sessions[0], { key1: 1, key2: 'two' })
321+
assert.deepStrictEqual(sessions[0], { key1: 1, key2: 'two' })
321322
cleanup(store, db, collection, () => {
322323
done()
323324
})
@@ -335,7 +336,7 @@ exports.test_length = function(done) {
335336
{ _id: sid, session: JSON.stringify({ key1: 1, key2: 'two' }) },
336337
() => {
337338
store.length((err, length) => {
338-
assert.equal(err, null)
339+
assert.strictEqual(err, null)
339340
assert.strictEqual(length, 1)
340341
cleanup(store, db, collection, () => {
341342
done()
@@ -373,7 +374,7 @@ exports.test_destroy_ok = function(done) {
373374
{ _id: sid, session: JSON.stringify({ key1: 1, key2: 'two' }) },
374375
() => {
375376
store.destroy(sid, err => {
376-
assert.equal(err, null)
377+
assert.strictEqual(err, null)
377378
cleanup(store, db, collection, () => {
378379
done()
379380
})
@@ -447,9 +448,9 @@ exports.test_options_url = function(done) {
447448
store.once('connected', function() {
448449
assert.strictEqual(store.db.databaseName, 'connect-mongo-test')
449450
assert.strictEqual(store.db.serverConfig.host, 'localhost')
450-
assert.equal(store.db.serverConfig.port, 27017)
451-
assert.equal(store.collection.collectionName, 'sessions-test')
452-
cleanup_store(store)
451+
assert.strictEqual(store.db.serverConfig.port, 27017)
452+
assert.strictEqual(store.collection.collectionName, 'sessions-test')
453+
cleanupStore(store)
453454
done()
454455
})
455456
}
@@ -471,7 +472,7 @@ exports.new_connection_failure = function(done) {
471472

472473
exports.test_options_no_db = function(done) {
473474
assert.throws(() => {
474-
new MongoStore({})
475+
return new MongoStore({})
475476
}, Error)
476477

477478
done()
@@ -480,18 +481,18 @@ exports.test_options_no_db = function(done) {
480481
/* Options.mongooseConnection tests */
481482

482483
exports.test_set_with_mongoose_db = function(done) {
483-
open_db(
484+
openDb(
484485
{ mongooseConnection: getMongooseConnection() },
485486
(store, db, collection) => {
486487
const sid = 'test_set-sid'
487-
const data = make_data()
488+
const data = makeData()
488489

489490
store.set(sid, data, err => {
490-
assert.equal(err, null)
491+
assert.strictEqual(err, null)
491492

492493
// Verify it was saved
493494
collection.findOne({ _id: sid }, (err, session) => {
494-
assert_session_equals(sid, data, session)
495+
assertSessionEquals(sid, data, session)
495496

496497
cleanup(store, db, collection, () => {
497498
done()
@@ -505,16 +506,16 @@ exports.test_set_with_mongoose_db = function(done) {
505506
/* Options.clientPromise tests */
506507

507508
exports.test_set_with_promise_db = function(done) {
508-
open_db({ clientPromise: getClientPromise() }, (store, db, collection) => {
509+
openDb({ clientPromise: getClientPromise() }, (store, db, collection) => {
509510
const sid = 'test_set-sid'
510-
const data = make_data()
511+
const data = makeData()
511512

512513
store.set(sid, data, err => {
513-
assert.equal(err, null)
514+
assert.strictEqual(err, null)
514515

515516
// Verify it was saved
516517
collection.findOne({ _id: sid }, (err, session) => {
517-
assert_session_equals(sid, data, session)
518+
assertSessionEquals(sid, data, session)
518519

519520
cleanup(store, db, collection, () => {
520521
done()
@@ -529,14 +530,14 @@ exports.test_set_with_promise_db = function(done) {
529530
exports.test_set_with_native_db = function(done) {
530531
getNativeDbConnection((store, db, collection) => {
531532
const sid = 'test_set-sid'
532-
const data = make_data()
533+
const data = makeData()
533534

534535
store.set(sid, data, err => {
535-
assert.equal(err, null)
536+
assert.strictEqual(err, null)
536537

537538
// Verify it was saved
538539
collection.findOne({ _id: sid }, (err, session) => {
539-
assert_session_equals(sid, data, session)
540+
assertSessionEquals(sid, data, session)
540541

541542
cleanup(store, db, collection, () => {
542543
done()
@@ -550,18 +551,18 @@ exports.test_set_default_expiration = function(done) {
550551
const defaultTTL = 10
551552
getNativeDbConnection({ ttl: defaultTTL }, (store, db, collection) => {
552553
const sid = 'test_set_expires-sid'
553-
const data = make_data_no_cookie()
554+
const data = makeDataNoCookie()
554555

555556
const timeBeforeSet = new Date().valueOf()
556557

557558
store.set(sid, data, err => {
558-
assert.equal(err, null)
559+
assert.strictEqual(err, null)
559560

560561
// Verify it was saved
561562
collection.findOne({ _id: sid }, (err, session) => {
562-
assert.deepEqual(session.session, JSON.stringify(data))
563+
assert.deepStrictEqual(session.session, JSON.stringify(data))
563564
assert.strictEqual(session._id, sid)
564-
assert.notEqual(session.expires, null)
565+
assert.notStrictEqual(session.expires, null)
565566

566567
const timeAfterSet = new Date().valueOf()
567568

@@ -582,18 +583,18 @@ exports.test_set_without_default_expiration = function(done) {
582583
const defaultExpirationTime = 1000 * 60 * 60 * 24 * 14
583584
getNativeDbConnection((store, db, collection) => {
584585
const sid = 'test_set_expires-sid'
585-
const data = make_data_no_cookie()
586+
const data = makeDataNoCookie()
586587

587588
const timeBeforeSet = new Date().valueOf()
588589

589590
store.set(sid, data, err => {
590-
assert.equal(err, null)
591+
assert.strictEqual(err, null)
591592

592593
// Verify it was saved
593594
collection.findOne({ _id: sid }, (err, session) => {
594-
assert.deepEqual(session.session, JSON.stringify(data))
595+
assert.deepStrictEqual(session.session, JSON.stringify(data))
595596
assert.strictEqual(session._id, sid)
596-
assert.notEqual(session.expires, null)
597+
assert.notStrictEqual(session.expires, null)
597598

598599
const timeAfterSet = new Date().valueOf()
599600

@@ -622,15 +623,15 @@ exports.test_set_custom_serializer = function(done) {
622623
},
623624
(store, db, collection) => {
624625
const sid = 'test_set_custom_serializer-sid'
625-
const data = make_data()
626+
const data = makeData()
626627
const dataWithIce = JSON.parse(JSON.stringify(data))
627628

628629
dataWithIce.ice = 'test-1'
629630
store.set(sid, data, err => {
630-
assert.equal(err, null)
631+
assert.strictEqual(err, null)
631632

632633
collection.findOne({ _id: sid }, (err, session) => {
633-
assert.deepEqual(session.session, JSON.stringify(dataWithIce))
634+
assert.deepStrictEqual(session.session, JSON.stringify(dataWithIce))
634635
assert.strictEqual(session._id, sid)
635636

636637
cleanup(store, db, collection, done)
@@ -650,14 +651,14 @@ exports.test_get_custom_unserializer = function(done) {
650651
},
651652
(store, db, collection) => {
652653
const sid = 'test_get_custom_unserializer-sid'
653-
const data = make_data()
654+
const data = makeData()
654655
store.set(sid, data, err => {
655-
assert.equal(err, null)
656+
assert.strictEqual(err, null)
656657
store.get(sid, (err, session) => {
657658
data.ice = 'test-2'
658659
data.cookie = data.cookie.toJSON()
659-
assert.equal(err, null)
660-
assert.deepEqual(session, data)
660+
assert.strictEqual(err, null)
661+
assert.deepStrictEqual(session, data)
661662
cleanup(store, db, collection, done)
662663
})
663664
})
@@ -668,23 +669,23 @@ exports.test_get_custom_unserializer = function(done) {
668669
exports.test_session_touch = function(done) {
669670
getNativeDbConnection((store, db, collection) => {
670671
const sid = 'test_touch-sid'
671-
const data = make_data()
672+
const data = makeData()
672673

673674
store.set(sid, data, err => {
674-
assert.equal(err, null)
675+
assert.strictEqual(err, null)
675676

676677
// Verify it was saved
677678
collection.findOne({ _id: sid }, (err, session) => {
678-
assert.equal(err, null)
679-
assert_session_equals(sid, data, session)
679+
assert.strictEqual(err, null)
680+
assertSessionEquals(sid, data, session)
680681

681682
// Touch the session
682683
store.touch(sid, session.session, err => {
683-
assert.equal(err, null)
684+
assert.strictEqual(err, null)
684685

685686
// Find the touched session
686687
collection.findOne({ _id: sid }, (err, session2) => {
687-
assert.equal(err, null)
688+
assert.strictEqual(err, null)
688689

689690
// Check if both expiry date are different
690691
assert.ok(session2.expires.getTime() > session.expires.getTime())
@@ -702,23 +703,23 @@ exports.test_session_touch = function(done) {
702703
exports.test_session_touch_promise = function(done) {
703704
getNativeDbConnection((store, db, collection) => {
704705
const sid = 'test_touch_promise-sid'
705-
const data = make_data()
706+
const data = makeData()
706707

707708
store
708709
.set(sid, data)
709710
.then(() => {
710711
// Verify it was saved
711712
collection.findOne({ _id: sid }, (err, session) => {
712-
assert.equal(err, null)
713-
assert_session_equals(sid, data, session)
713+
assert.strictEqual(err, null)
714+
assertSessionEquals(sid, data, session)
714715

715716
// Touch the session
716717
store
717718
.touch(sid, session.session)
718719
.then(() => {
719720
// Find the touched session
720721
collection.findOne({ _id: sid }, (err, session2) => {
721-
assert.equal(err, null)
722+
assert.strictEqual(err, null)
722723

723724
// Check if both expiry date are different
724725
assert.ok(
@@ -740,25 +741,25 @@ exports.test_session_touch_promise = function(done) {
740741
exports.test_session_lazy_touch_sync = function(done) {
741742
getNativeDbConnection({ touchAfter: 2 }, (store, db, collection) => {
742743
const sid = 'test_lazy_touch-sid-sync'
743-
const data = make_data()
744+
const data = makeData()
744745
let lastModifiedBeforeTouch
745746
let lastModifiedAfterTouch
746747

747748
store.set(sid, data, err => {
748-
assert.equal(err, null)
749+
assert.strictEqual(err, null)
749750

750751
// Verify it was saved
751752
collection.findOne({ _id: sid }, (err, session) => {
752-
assert.equal(err, null)
753+
assert.strictEqual(err, null)
753754

754755
lastModifiedBeforeTouch = session.lastModified.getTime()
755756

756757
// Touch the session
757758
store.touch(sid, session, err => {
758-
assert.equal(err, null)
759+
assert.strictEqual(err, null)
759760

760761
collection.findOne({ _id: sid }, (err, session2) => {
761-
assert.equal(err, null)
762+
assert.strictEqual(err, null)
762763

763764
lastModifiedAfterTouch = session2.lastModified.getTime()
764765

@@ -777,26 +778,26 @@ exports.test_session_lazy_touch_sync = function(done) {
777778
exports.test_session_lazy_touch_async = function(done) {
778779
getNativeDbConnection({ touchAfter: 2 }, (store, db, collection) => {
779780
const sid = 'test_lazy_touch-sid'
780-
const data = make_data()
781+
const data = makeData()
781782
let lastModifiedBeforeTouch
782783
let lastModifiedAfterTouch
783784

784785
store.set(sid, data, err => {
785-
assert.equal(err, null)
786+
assert.strictEqual(err, null)
786787

787788
// Verify it was saved
788789
collection.findOne({ _id: sid }, (err, session) => {
789-
assert.equal(err, null)
790+
assert.strictEqual(err, null)
790791

791792
lastModifiedBeforeTouch = session.lastModified.getTime()
792793

793794
setTimeout(() => {
794795
// Touch the session
795796
store.touch(sid, session, err => {
796-
assert.equal(err, null)
797+
assert.strictEqual(err, null)
797798

798799
collection.findOne({ _id: sid }, (err, session2) => {
799-
assert.equal(err, null)
800+
assert.strictEqual(err, null)
800801

801802
lastModifiedAfterTouch = session2.lastModified.getTime()
802803

0 commit comments

Comments
 (0)
Please sign in to comment.