@@ -50,8 +50,13 @@ function fastifySecureSession (fastify, options, next) {
5050 for ( const sessionOptions of options ) {
5151 const sessionName = sessionOptions . sessionName || 'session'
5252 const cookieName = sessionOptions . cookieName || sessionName
53+ const expiry = sessionOptions . expiry || 86401 // 24 hours
5354 const cookieOptions = sessionOptions . cookieOptions || sessionOptions . cookie || { }
5455
56+ if ( cookieOptions . httpOnly === undefined ) {
57+ cookieOptions . httpOnly = true
58+ }
59+
5560 let key
5661 if ( sessionOptions . secret ) {
5762 if ( Buffer . byteLength ( sessionOptions . secret ) < 32 ) {
@@ -120,7 +125,8 @@ function fastifySecureSession (fastify, options, next) {
120125 sessionNames . set ( sessionName , {
121126 cookieName,
122127 cookieOptions,
123- key
128+ key,
129+ expiry
124130 } )
125131
126132 if ( ! defaultSessionName ) {
@@ -139,7 +145,7 @@ function fastifySecureSession (fastify, options, next) {
139145 throw new Error ( 'Unknown session key.' )
140146 }
141147
142- const { key } = sessionNames . get ( sessionName )
148+ const { key, expiry } = sessionNames . get ( sessionName )
143149
144150 // do not use destructuring or it will deopt
145151 const split = cookie . split ( ';' )
@@ -184,8 +190,15 @@ function fastifySecureSession (fastify, options, next) {
184190 return null
185191 }
186192
193+ const parsed = JSON . parse ( msg )
194+ if ( ( parsed . __ts + expiry ) * 1000 - Date . now ( ) <= 0 ) {
195+ // maximum validity is reached, resetting
196+ log . debug ( '@fastify/secure-session: expiry reached' )
197+ return null
198+ }
187199 const session = new Proxy ( new Session ( JSON . parse ( msg ) ) , sessionProxyHandler )
188200 session . changed = signingKeyRotated
201+
189202 return session
190203 } )
191204
@@ -228,7 +241,7 @@ function fastifySecureSession (fastify, options, next) {
228241 const cookie = request . cookies [ cookieName ]
229242 const result = fastify . decodeSecureSession ( cookie , request . log , sessionName )
230243
231- request [ sessionName ] = new Proxy ( ( result || new Session ( { } ) ) , sessionProxyHandler )
244+ request [ sessionName ] = result || new Proxy ( new Session ( { } ) , sessionProxyHandler )
232245 }
233246
234247 next ( )
@@ -275,6 +288,10 @@ class Session {
275288 this [ kCookieOptions ] = null
276289 this . changed = false
277290 this . deleted = false
291+
292+ if ( this [ kObj ] . __ts === undefined ) {
293+ this [ kObj ] . __ts = Math . round ( Date . now ( ) / 1000 )
294+ }
278295 }
279296
280297 get ( key ) {
@@ -296,7 +313,12 @@ class Session {
296313 }
297314
298315 data ( ) {
299- return this [ kObj ]
316+ const copy = {
317+ ...this [ kObj ]
318+ }
319+
320+ delete copy . __ts
321+ return copy
300322 }
301323
302324 touch ( ) {
0 commit comments