SessionSockets.test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { vi, expect, describe, beforeEach, it } from 'vitest'
  2. import { EventEmitter } from 'node:events'
  3. import sinon from 'sinon'
  4. const modulePath = '../../../app/js/SessionSockets'
  5. describe('SessionSockets', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.metrics = { inc: sinon.stub() }
  8. vi.doMock('@overleaf/metrics', () => ({
  9. default: ctx.metrics,
  10. }))
  11. ctx.SessionSocketsModule = (await import(modulePath)).default
  12. ctx.io = new EventEmitter()
  13. ctx.id1 = Math.random().toString()
  14. ctx.id2 = Math.random().toString()
  15. const redisResponses = {
  16. error: [new Error('Redis: something went wrong'), null],
  17. unknownId: [null, null],
  18. }
  19. redisResponses[ctx.id1] = [null, { user: { _id: '123' } }]
  20. redisResponses[ctx.id2] = [null, { user: { _id: 'abc' } }]
  21. ctx.sessionStore = {
  22. get: sinon
  23. .stub()
  24. .callsFake((id, fn) => fn.apply(null, redisResponses[id])),
  25. }
  26. ctx.cookieParser = function (req, res, next) {
  27. req.signedCookies = req._signedCookies
  28. return next()
  29. }
  30. ctx.SessionSockets = ctx.SessionSocketsModule(
  31. ctx.io,
  32. ctx.sessionStore,
  33. ctx.cookieParser,
  34. 'ol.sid'
  35. )
  36. ctx.checkSocket = (socket, fn) => {
  37. ctx.SessionSockets.once('connection', fn)
  38. return ctx.io.emit('connection', socket)
  39. }
  40. })
  41. describe('without cookies', function () {
  42. beforeEach(function (ctx) {
  43. ctx.socket = { handshake: {} }
  44. })
  45. it('should return a lookup error', async function (ctx) {
  46. await new Promise((resolve, reject) => {
  47. ctx.checkSocket(ctx.socket, error => {
  48. expect(error).to.exist
  49. expect(error.message).to.equal('could not look up session by key')
  50. resolve()
  51. })
  52. })
  53. })
  54. it('should not query redis', async function (ctx) {
  55. await new Promise((resolve, reject) => {
  56. ctx.checkSocket(ctx.socket, () => {
  57. expect(ctx.sessionStore.get.called).to.equal(false)
  58. resolve()
  59. })
  60. })
  61. })
  62. it('should increment the session.cookie metric with status "none"', async function (ctx) {
  63. await new Promise((resolve, reject) => {
  64. ctx.checkSocket(ctx.socket, () => {
  65. expect(ctx.metrics.inc).to.be.calledWith('session.cookie', 1, {
  66. status: 'none',
  67. })
  68. resolve()
  69. })
  70. })
  71. })
  72. })
  73. describe('with a different cookie', function () {
  74. beforeEach(function (ctx) {
  75. ctx.socket = { handshake: { _signedCookies: { other: 1 } } }
  76. })
  77. it('should return a lookup error', async function (ctx) {
  78. await new Promise((resolve, reject) => {
  79. ctx.checkSocket(ctx.socket, error => {
  80. expect(error).to.exist
  81. expect(error.message).to.equal('could not look up session by key')
  82. resolve()
  83. })
  84. })
  85. })
  86. it('should not query redis', async function (ctx) {
  87. await new Promise((resolve, reject) => {
  88. ctx.checkSocket(ctx.socket, () => {
  89. expect(ctx.sessionStore.get.called).to.equal(false)
  90. resolve()
  91. })
  92. })
  93. })
  94. })
  95. describe('with a cookie with an invalid signature', function () {
  96. beforeEach(function (ctx) {
  97. ctx.socket = {
  98. handshake: { _signedCookies: { 'ol.sid': false } },
  99. }
  100. })
  101. it('should return a lookup error', async function (ctx) {
  102. await new Promise((resolve, reject) => {
  103. ctx.checkSocket(ctx.socket, error => {
  104. expect(error).to.exist
  105. expect(error.message).to.equal('could not look up session by key')
  106. resolve()
  107. })
  108. })
  109. })
  110. it('should not query redis', async function (ctx) {
  111. await new Promise((resolve, reject) => {
  112. ctx.checkSocket(ctx.socket, () => {
  113. expect(ctx.sessionStore.get.called).to.equal(false)
  114. resolve()
  115. })
  116. })
  117. })
  118. it('should increment the session.cookie metric with status=bad-signature', async function (ctx) {
  119. await new Promise((resolve, reject) => {
  120. ctx.checkSocket(ctx.socket, () => {
  121. expect(ctx.metrics.inc).to.be.calledWith('session.cookie', 1, {
  122. status: 'bad-signature',
  123. })
  124. resolve()
  125. })
  126. })
  127. })
  128. })
  129. describe('with a valid cookie and a failing session lookup', function () {
  130. beforeEach(function (ctx) {
  131. ctx.socket = {
  132. handshake: { _signedCookies: { 'ol.sid': 'error' } },
  133. }
  134. })
  135. it('should query redis', async function (ctx) {
  136. await new Promise((resolve, reject) => {
  137. ctx.checkSocket(ctx.socket, () => {
  138. expect(ctx.sessionStore.get.called).to.equal(true)
  139. resolve()
  140. })
  141. })
  142. })
  143. it('should return a redis error', async function (ctx) {
  144. await new Promise((resolve, reject) => {
  145. ctx.checkSocket(ctx.socket, error => {
  146. expect(error).to.exist
  147. expect(error.message).to.equal('Redis: something went wrong')
  148. resolve()
  149. })
  150. })
  151. })
  152. it('should increment the session.cookie metric with status=error', async function (ctx) {
  153. await new Promise((resolve, reject) => {
  154. ctx.checkSocket(ctx.socket, () => {
  155. expect(ctx.metrics.inc).to.be.calledWith('session.cookie', 1, {
  156. status: 'error',
  157. })
  158. resolve()
  159. })
  160. })
  161. })
  162. })
  163. describe('with a valid cookie and no matching session', function () {
  164. beforeEach(function (ctx) {
  165. ctx.socket = {
  166. handshake: { _signedCookies: { 'ol.sid': 'unknownId' } },
  167. }
  168. })
  169. it('should query redis', async function (ctx) {
  170. await new Promise((resolve, reject) => {
  171. ctx.checkSocket(ctx.socket, () => {
  172. expect(ctx.sessionStore.get.called).to.equal(true)
  173. resolve()
  174. })
  175. })
  176. })
  177. it('should return a lookup error', async function (ctx) {
  178. await new Promise((resolve, reject) => {
  179. ctx.checkSocket(ctx.socket, error => {
  180. expect(error).to.exist
  181. expect(error.message).to.equal('could not look up session by key')
  182. resolve()
  183. })
  184. })
  185. })
  186. it('should increment the session.cookie metric with status=missing', async function (ctx) {
  187. await new Promise((resolve, reject) => {
  188. ctx.checkSocket(ctx.socket, () => {
  189. expect(ctx.metrics.inc).to.be.calledWith('session.cookie', 1, {
  190. status: 'missing',
  191. })
  192. resolve()
  193. })
  194. })
  195. })
  196. })
  197. describe('with a valid cookie and a matching session', function () {
  198. beforeEach(function (ctx) {
  199. ctx.socket = {
  200. handshake: { _signedCookies: { 'ol.sid': ctx.id1 } },
  201. }
  202. })
  203. it('should query redis', async function (ctx) {
  204. await new Promise((resolve, reject) => {
  205. ctx.checkSocket(ctx.socket, () => {
  206. expect(ctx.sessionStore.get.called).to.equal(true)
  207. resolve()
  208. })
  209. })
  210. })
  211. it('should not return an error', async function (ctx) {
  212. await new Promise((resolve, reject) => {
  213. ctx.checkSocket(ctx.socket, error => {
  214. expect(error).to.not.exist
  215. resolve()
  216. })
  217. })
  218. })
  219. it('should return the session', async function (ctx) {
  220. await new Promise((resolve, reject) => {
  221. ctx.checkSocket(ctx.socket, (error, s, session) => {
  222. if (error) return reject(error)
  223. expect(session).to.deep.equal({ user: { _id: '123' } })
  224. resolve()
  225. })
  226. })
  227. })
  228. it('should increment the session.cookie metric with status=signed', async function (ctx) {
  229. await new Promise((resolve, reject) => {
  230. ctx.checkSocket(ctx.socket, () => {
  231. expect(ctx.metrics.inc).to.be.calledWith('session.cookie', 1, {
  232. status: 'signed',
  233. })
  234. resolve()
  235. })
  236. })
  237. })
  238. })
  239. describe('with a different valid cookie and matching session', function () {
  240. beforeEach(function (ctx) {
  241. ctx.socket = {
  242. handshake: { _signedCookies: { 'ol.sid': ctx.id2 } },
  243. }
  244. })
  245. it('should query redis', async function (ctx) {
  246. await new Promise((resolve, reject) => {
  247. ctx.checkSocket(ctx.socket, () => {
  248. expect(ctx.sessionStore.get.called).to.equal(true)
  249. resolve()
  250. })
  251. })
  252. })
  253. it('should not return an error', async function (ctx) {
  254. await new Promise((resolve, reject) => {
  255. ctx.checkSocket(ctx.socket, error => {
  256. expect(error).to.not.exist
  257. resolve()
  258. })
  259. })
  260. })
  261. it('should return the other session', async function (ctx) {
  262. await new Promise((resolve, reject) => {
  263. ctx.checkSocket(ctx.socket, (error, s, session) => {
  264. if (error) return reject(error)
  265. expect(session).to.deep.equal({ user: { _id: 'abc' } })
  266. resolve()
  267. })
  268. })
  269. })
  270. it('should increment the session.cookie metric with status=error', async function (ctx) {
  271. await new Promise((resolve, reject) => {
  272. ctx.checkSocket(ctx.socket, () => {
  273. expect(ctx.metrics.inc).to.be.calledWith('session.cookie', 1, {
  274. status: 'signed',
  275. })
  276. resolve()
  277. })
  278. })
  279. })
  280. })
  281. })