CustomSessionStore.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const session = require('express-session')
  2. const RedisStore = require('connect-redis')(session)
  3. const metrics = require('@overleaf/metrics')
  4. const logger = require('@overleaf/logger')
  5. const SessionManager = require('../Features/Authentication/SessionManager')
  6. const MAX_SESSION_SIZE_THRESHOLD = 4096
  7. // Define a custom session store to record session metrics and log large
  8. // anonymous sessions for debugging purposes
  9. class CustomSessionStore extends RedisStore {
  10. static largestSessionSize = 3 * 1024 // ignore sessions smaller than 3KB
  11. static metric(method, sess) {
  12. let type // type of session: 'logged-in', 'anonymous', or 'na' (not available)
  13. if (sess) {
  14. type = SessionManager.isUserLoggedIn(sess) ? 'logged-in' : 'anonymous'
  15. } else {
  16. type = 'na'
  17. }
  18. const size = sess ? JSON.stringify(sess).length : 0
  19. // record the number of redis session operations
  20. metrics.inc('session.store.count', 1, {
  21. method,
  22. type,
  23. status: size > MAX_SESSION_SIZE_THRESHOLD ? 'oversize' : 'normal',
  24. })
  25. // record the redis session bandwidth for get/set operations
  26. if (method === 'get' || method === 'set') {
  27. metrics.count('session.store.bytes', size, { method, type })
  28. }
  29. // log the largest anonymous session seen so far
  30. if (type === 'anonymous' && size > CustomSessionStore.largestSessionSize) {
  31. CustomSessionStore.largestSessionSize = size
  32. logger.warn(
  33. { redactedSession: redactSession(sess), largestSessionSize: size },
  34. 'largest session size seen'
  35. )
  36. }
  37. }
  38. // Override the get, set, touch, and destroy methods to record metrics
  39. get(sid, cb) {
  40. super.get(sid, (err, ...args) => {
  41. if (args[0]) {
  42. CustomSessionStore.metric('get', args[0])
  43. }
  44. cb(err, ...args)
  45. })
  46. }
  47. set(sid, sess, cb) {
  48. CustomSessionStore.metric('set', sess)
  49. super.set(sid, sess, cb)
  50. }
  51. touch(sid, sess, cb) {
  52. CustomSessionStore.metric('touch', sess)
  53. super.touch(sid, sess, cb)
  54. }
  55. destroy(sid, cb) {
  56. // for the destroy method we don't have access to the session object itself
  57. CustomSessionStore.metric('destroy')
  58. super.destroy(sid, cb)
  59. }
  60. }
  61. // Helper function to return a redacted version of session object
  62. // so we can identify the largest keys without exposing sensitive
  63. // data
  64. function redactSession(sess) {
  65. // replace all string values with '***' of the same length
  66. return JSON.parse(
  67. JSON.stringify(sess, (key, value) => {
  68. if (typeof value === 'string') {
  69. return '*'.repeat(value.length)
  70. }
  71. return value
  72. })
  73. )
  74. }
  75. module.exports = CustomSessionStore