Skip to content

Commit 5414a91

Browse files
committed
fix(ttl): fixing infinit looping on etcd3 client with a max ttl
1 parent 3ce0148 commit 5414a91

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/express-session-etcd3.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import debug from 'debug'
77
*/
88
export const oneDay = 86400
99

10+
/**
11+
* Max TTL time to leasing on ETCD3 Client in seconds.
12+
*/
13+
export const maxTTL = 6442450
14+
1015
/**
1116
* Configuration options for the etcd v3
1217
*/
@@ -193,6 +198,14 @@ export default class Etcd3Store extends Store {
193198
* Get the Time to Live (`ttl`) of the session
194199
*/
195200
private getTTL(sess: Express.SessionData, sid: string): number {
201+
const rawTTL = this.getRawTTL(sess, sid)
202+
return rawTTL > maxTTL ? maxTTL : rawTTL
203+
}
204+
205+
/**
206+
* Get the raw Time to Live (`ttl`) of the session from the data sources
207+
*/
208+
private getRawTTL(sess: Express.SessionData, sid: string): number {
196209
const storeTtl: any = (this as any)['ttl']
197210
if (typeof storeTtl === 'number') return storeTtl
198211
if (typeof storeTtl === 'string') return Number(storeTtl)

test/express-session-etcd3.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { DeleteBuilder, MultiRangeBuilder, SingleRangeBuilder } from 'etcd3/lib/src'
2-
import Etcd3Store, { Etcd3StoreOptions, defaultOptions, oneDay } from '../src/express-session-etcd3'
2+
import Etcd3Store, {
3+
Etcd3StoreOptions,
4+
defaultOptions,
5+
oneDay,
6+
maxTTL
7+
} from '../src/express-session-etcd3'
38
import { Etcd3 } from 'etcd3'
49
import { PutBuilder } from 'etcd3/lib/src/builder'
510
import { anotherPrefix, createTestClientAndKeys, sessionData, tearDownTestClient } from './utils'
@@ -450,5 +455,14 @@ describe('Etcd3Store test suit', () => {
450455
const session = { ...sessionData, cookie: { ...sessionData.cookie, maxAge: 'what?' as any } }
451456
expect(subject['getTTL'](session, null)).toBe(oneDay)
452457
})
458+
459+
it('should use max ttl value if the configured value is greater than it', async () => {
460+
const { subject } = await createSubject()
461+
const session = {
462+
...sessionData,
463+
cookie: { ...sessionData.cookie, maxAge: maxTTL * 1000 * 2 }
464+
}
465+
expect(subject['getTTL'](session, null)).toBe(maxTTL)
466+
})
453467
})
454468
})

0 commit comments

Comments
 (0)