|
@@ -4,6 +4,7 @@ const metrics = require('@overleaf/metrics')
|
|
|
const logger = require('@overleaf/logger')
|
|
const logger = require('@overleaf/logger')
|
|
|
const Settings = require('@overleaf/settings')
|
|
const Settings = require('@overleaf/settings')
|
|
|
const SessionManager = require('../Features/Authentication/SessionManager')
|
|
const SessionManager = require('../Features/Authentication/SessionManager')
|
|
|
|
|
+const Metrics = require('@overleaf/metrics')
|
|
|
|
|
|
|
|
const MAX_SESSION_SIZE_THRESHOLD = 4096
|
|
const MAX_SESSION_SIZE_THRESHOLD = 4096
|
|
|
|
|
|
|
@@ -55,22 +56,30 @@ class CustomSessionStore extends RedisStore {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Override the get, set, touch, and destroy methods to record metrics
|
|
|
|
|
get(sid, cb) {
|
|
get(sid, cb) {
|
|
|
- super.get(sid, (err, ...args) => {
|
|
|
|
|
- if (args[0]) {
|
|
|
|
|
- CustomSessionStore.metric('get', args[0])
|
|
|
|
|
- }
|
|
|
|
|
- cb(err, ...args)
|
|
|
|
|
|
|
+ super.get(sid, (err, sess) => {
|
|
|
|
|
+ if (err || !sess || !checkValidationToken(sid, sess)) return cb(err, null)
|
|
|
|
|
+ CustomSessionStore.metric('get', sess)
|
|
|
|
|
+ cb(null, sess)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
set(sid, sess, cb) {
|
|
set(sid, sess, cb) {
|
|
|
|
|
+ // Refresh the validation token just before writing to Redis
|
|
|
|
|
+ // This will ensure that the token is always matching to the sessionID that we write the session value for.
|
|
|
|
|
+ // Potential reasons for missing/mismatching token:
|
|
|
|
|
+ // - brand-new session
|
|
|
|
|
+ // - cycling of the sessionID as part of the login flow
|
|
|
|
|
+ // - upgrade from a client side session to a redis session
|
|
|
|
|
+ // - accidental writes in the app code
|
|
|
|
|
+ sess.validationToken = computeValidationToken(sid)
|
|
|
|
|
+
|
|
|
CustomSessionStore.metric('set', sess)
|
|
CustomSessionStore.metric('set', sess)
|
|
|
const originalId = sess.req.signedCookies[Settings.cookieName]
|
|
const originalId = sess.req.signedCookies[Settings.cookieName]
|
|
|
if (sid === originalId || sid === sess.req.newSessionId) {
|
|
if (sid === originalId || sid === sess.req.newSessionId) {
|
|
|
this.#updateInPlaceStore.set(sid, sess, cb)
|
|
this.#updateInPlaceStore.set(sid, sess, cb)
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+ Metrics.inc('security.session', 1, { status: 'new' })
|
|
|
// Multiple writes can get issued with the new sid. Keep track of it.
|
|
// Multiple writes can get issued with the new sid. Keep track of it.
|
|
|
Object.defineProperty(sess.req, 'newSessionId', { value: sid })
|
|
Object.defineProperty(sess.req, 'newSessionId', { value: sid })
|
|
|
this.#initialSetStore.set(sid, sess, cb)
|
|
this.#initialSetStore.set(sid, sess, cb)
|
|
@@ -89,6 +98,35 @@ class CustomSessionStore extends RedisStore {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function computeValidationToken(sid) {
|
|
|
|
|
+ // This should be a deterministic function of the client-side sessionID,
|
|
|
|
|
+ // prepended with a version number in case we want to change it later.
|
|
|
|
|
+ return 'v1:' + sid.slice(-4)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function checkValidationToken(sid, sess) {
|
|
|
|
|
+ const sessionToken = sess.validationToken
|
|
|
|
|
+ if (sessionToken) {
|
|
|
|
|
+ const clientToken = computeValidationToken(sid)
|
|
|
|
|
+ // Reject sessions where the validation token is out of sync with the sessionID.
|
|
|
|
|
+ // If you change the method for computing the token (above) then you need to either check or ignore previous versions of the token.
|
|
|
|
|
+ if (sessionToken === clientToken) {
|
|
|
|
|
+ Metrics.inc('security.session', 1, { status: 'ok' })
|
|
|
|
|
+ return true
|
|
|
|
|
+ } else {
|
|
|
|
|
+ logger.warn(
|
|
|
|
|
+ { sid, sessionToken, clientToken },
|
|
|
|
|
+ 'session token validation failed'
|
|
|
|
|
+ )
|
|
|
|
|
+ Metrics.inc('security.session', 1, { status: 'error' })
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Metrics.inc('security.session', 1, { status: 'missing' })
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Helper function to return a redacted version of session object
|
|
// Helper function to return a redacted version of session object
|
|
|
// so we can identify the largest keys without exposing sensitive
|
|
// so we can identify the largest keys without exposing sensitive
|
|
|
// data
|
|
// data
|