SessionTests.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import { expect } from 'chai'
  2. import { setTimeout } from 'node:timers/promises'
  3. import UserHelper from './helpers/User.mjs'
  4. import redis from './helpers/redis.mjs'
  5. import UserSessionsRedis from '../../../app/src/Features/User/UserSessionsRedis.mjs'
  6. const rclient = UserSessionsRedis.client()
  7. const UserPromises = UserHelper.promises
  8. describe('Sessions', function () {
  9. beforeEach(async function () {
  10. this.timeout(20000)
  11. this.user1 = new UserPromises()
  12. this.site_admin = new UserPromises({ email: 'admin@example.com' })
  13. await this.user1.login()
  14. await this.user1.logout()
  15. })
  16. describe('one session', function () {
  17. it('should have one session in UserSessions set', async function () {
  18. await redis.clearUserSessions(this.user1)
  19. // login, should add session to set
  20. await this.user1.login()
  21. const sessions = await redis.getUserSessions(this.user1)
  22. expect(sessions.length).to.equal(1)
  23. expect(sessions[0].slice(0, 5)).to.equal('sess:')
  24. // should be able to access project list page
  25. const statusCode = await this.user1.getProjectListPage()
  26. expect(statusCode).to.equal(200)
  27. // logout, should remove session from set
  28. await this.user1.logout()
  29. const sessionsAfterLogout = await redis.getUserSessions(this.user1)
  30. expect(sessionsAfterLogout.length).to.equal(0)
  31. })
  32. it('should update audit log on logout', async function () {
  33. await redis.clearUserSessions(this.user1)
  34. // login
  35. await this.user1.login()
  36. // logout, should add logout audit log entry (happens in background)
  37. await this.user1.logout()
  38. // poll for audit log entry since it's written in the background
  39. const findAuditLogEntry = async () => {
  40. for (let attempts = 0; attempts < 10; attempts++) {
  41. const auditLog = await this.user1.getAuditLogWithoutNoise()
  42. const logoutEntries = auditLog.filter(
  43. entry => entry.operation === 'logout'
  44. )
  45. // If we found the logout entry, we're done
  46. if (logoutEntries.length > 0) {
  47. return logoutEntries
  48. }
  49. await setTimeout(25)
  50. }
  51. throw new Error('Logout audit log entry not found after 10 attempts')
  52. }
  53. const logoutEntries = await findAuditLogEntry()
  54. expect(logoutEntries.length).to.be.greaterThan(0)
  55. const lastLogout = logoutEntries[logoutEntries.length - 1]
  56. expect(lastLogout.operation).to.equal('logout')
  57. expect(lastLogout.ipAddress).to.exist
  58. expect(lastLogout.initiatorId).to.exist
  59. expect(lastLogout.timestamp).to.exist
  60. })
  61. })
  62. describe('two sessions', function () {
  63. beforeEach(function () {
  64. // set up second session for this user
  65. this.user2 = new UserPromises()
  66. this.user2.email = this.user1.email
  67. this.user2.emails = this.user1.emails
  68. this.user2.password = this.user1.password
  69. })
  70. it('should have two sessions in UserSessions set', async function () {
  71. await redis.clearUserSessions(this.user1)
  72. // login, should add session to set
  73. await this.user1.login()
  74. const sessions1 = await redis.getUserSessions(this.user1)
  75. expect(sessions1.length).to.equal(1)
  76. expect(sessions1[0].slice(0, 5)).to.equal('sess:')
  77. // login again, should add the second session to set
  78. await this.user2.login()
  79. const sessions2 = await redis.getUserSessions(this.user1)
  80. expect(sessions2.length).to.equal(2)
  81. expect(sessions2[0].slice(0, 5)).to.equal('sess:')
  82. expect(sessions2[1].slice(0, 5)).to.equal('sess:')
  83. // both should be able to access project list page
  84. const statusCode1 = await this.user1.getProjectListPage()
  85. expect(statusCode1).to.equal(200)
  86. const statusCode2 = await this.user2.getProjectListPage()
  87. expect(statusCode2).to.equal(200)
  88. // logout first session, should remove session from set
  89. await this.user1.logout()
  90. const sessions3 = await redis.getUserSessions(this.user1)
  91. expect(sessions3.length).to.equal(1)
  92. // first session should not have access to project list page
  93. const statusCode3 = await this.user1.getProjectListPage()
  94. expect(statusCode3).to.equal(302)
  95. // second session should still have access to settings
  96. const statusCode4 = await this.user2.getProjectListPage()
  97. expect(statusCode4).to.equal(200)
  98. // logout second session, should remove last session from set
  99. await this.user2.logout()
  100. const sessions4 = await redis.getUserSessions(this.user1)
  101. expect(sessions4.length).to.equal(0)
  102. // second session should not have access to project list page
  103. const statusCode5 = await this.user2.getProjectListPage()
  104. expect(statusCode5).to.equal(302)
  105. })
  106. })
  107. describe('three sessions, password reset', function () {
  108. beforeEach(function () {
  109. // set up second session for this user
  110. this.user2 = new UserPromises()
  111. this.user2.email = this.user1.email
  112. this.user2.emails = this.user1.emails
  113. this.user2.password = this.user1.password
  114. this.user3 = new UserPromises()
  115. this.user3.email = this.user1.email
  116. this.user3.emails = this.user1.emails
  117. this.user3.password = this.user1.password
  118. })
  119. it('should erase both sessions when password is reset', async function () {
  120. await redis.clearUserSessions(this.user1)
  121. // login, should add session to set
  122. await this.user1.login()
  123. const sessions1 = await redis.getUserSessions(this.user1)
  124. expect(sessions1.length).to.equal(1)
  125. expect(sessions1[0].slice(0, 5)).to.equal('sess:')
  126. // login again, should add the second session to set
  127. await this.user2.login()
  128. const sessions2 = await redis.getUserSessions(this.user1)
  129. expect(sessions2.length).to.equal(2)
  130. expect(sessions2[0].slice(0, 5)).to.equal('sess:')
  131. expect(sessions2[1].slice(0, 5)).to.equal('sess:')
  132. // login third session, should add the second session to set
  133. await this.user3.login()
  134. const sessions3 = await redis.getUserSessions(this.user1)
  135. expect(sessions3.length).to.equal(3)
  136. expect(sessions3[0].slice(0, 5)).to.equal('sess:')
  137. expect(sessions3[1].slice(0, 5)).to.equal('sess:')
  138. // password reset from second session, should erase two of the three sessions
  139. await this.user2.changePassword(`password${Date.now()}`)
  140. const sessions4 = await redis.getUserSessions(this.user2)
  141. expect(sessions4.length).to.equal(1)
  142. // users one and three should not be able to access project list page
  143. const statusCode1 = await this.user1.getProjectListPage()
  144. expect(statusCode1).to.equal(302)
  145. const statusCode3 = await this.user3.getProjectListPage()
  146. expect(statusCode3).to.equal(302)
  147. // user two should still be logged in, and able to access project list page
  148. const statusCode2 = await this.user2.getProjectListPage()
  149. expect(statusCode2).to.equal(200)
  150. // logout second session, should remove last session from set
  151. await this.user2.logout()
  152. const sessions5 = await redis.getUserSessions(this.user1)
  153. expect(sessions5.length).to.equal(0)
  154. })
  155. })
  156. describe('three sessions, sessions page', function () {
  157. beforeEach(async function () {
  158. // set up second session for this user
  159. this.user2 = new UserPromises()
  160. this.user2.email = this.user1.email
  161. this.user2.emails = this.user1.emails
  162. this.user2.password = this.user1.password
  163. this.user3 = new UserPromises()
  164. this.user3.email = this.user1.email
  165. this.user3.emails = this.user1.emails
  166. this.user3.password = this.user1.password
  167. await this.user2.login()
  168. })
  169. it('should allow the user to erase the other two sessions', async function () {
  170. await redis.clearUserSessions(this.user1)
  171. // login, should add session to set
  172. await this.user1.login()
  173. const sessions1 = await redis.getUserSessions(this.user1)
  174. expect(sessions1.length).to.equal(1)
  175. expect(sessions1[0].slice(0, 5)).to.equal('sess:')
  176. // login again, should add the second session to set
  177. await this.user2.login()
  178. const sessions2 = await redis.getUserSessions(this.user1)
  179. expect(sessions2.length).to.equal(2)
  180. expect(sessions2[0].slice(0, 5)).to.equal('sess:')
  181. expect(sessions2[1].slice(0, 5)).to.equal('sess:')
  182. // login third session, should add the second session to set
  183. await this.user3.login()
  184. const sessions3 = await redis.getUserSessions(this.user1)
  185. expect(sessions3.length).to.equal(3)
  186. expect(sessions3[0].slice(0, 5)).to.equal('sess:')
  187. expect(sessions3[1].slice(0, 5)).to.equal('sess:')
  188. // check the sessions page
  189. const { response: sessionsPageResponse } = await this.user2.doRequest(
  190. 'GET',
  191. '/user/sessions'
  192. )
  193. expect(sessionsPageResponse.statusCode).to.equal(200)
  194. // clear sessions from second session, should erase two of the three sessions
  195. await this.user2.getCsrfToken()
  196. await this.user2.doRequest('POST', '/user/sessions/clear')
  197. const sessions4 = await redis.getUserSessions(this.user2)
  198. expect(sessions4.length).to.equal(1)
  199. // users one and three should not be able to access project list page
  200. const statusCode1 = await this.user1.getProjectListPage()
  201. expect(statusCode1).to.equal(302)
  202. const statusCode3 = await this.user3.getProjectListPage()
  203. expect(statusCode3).to.equal(302)
  204. // user two should still be logged in, and able to access project list page
  205. const statusCode2 = await this.user2.getProjectListPage()
  206. expect(statusCode2).to.equal(200)
  207. // logout second session, should remove last session from set
  208. await this.user2.logout()
  209. const sessions5 = await redis.getUserSessions(this.user1)
  210. expect(sessions5.length).to.equal(0)
  211. // the user audit log should have been updated
  212. const auditLog = await this.user1.getAuditLogWithoutNoise()
  213. expect(auditLog).to.exist
  214. // find the clear-sessions entry
  215. const clearSessionsEntries = auditLog.filter(
  216. entry => entry.operation === 'clear-sessions'
  217. )
  218. expect(clearSessionsEntries.length).to.equal(1)
  219. expect(clearSessionsEntries[0].operation).to.equal('clear-sessions')
  220. expect(clearSessionsEntries[0].ipAddress).to.exist
  221. expect(clearSessionsEntries[0].initiatorId).to.exist
  222. expect(clearSessionsEntries[0].timestamp).to.exist
  223. expect(clearSessionsEntries[0].info.sessions.length).to.equal(2)
  224. })
  225. })
  226. describe('validationToken', function () {
  227. const User = UserHelper.promises
  228. async function tryWithValidationToken(validationToken) {
  229. const user = new User()
  230. await user.login()
  231. await checkSessionIsValid(user)
  232. const [, sid] = user.sessionCookie().value.match(/^s:(.+?)\./)
  233. const key = `sess:${sid}`
  234. const sess = JSON.parse(await rclient.get(key))
  235. expect(sess.validationToken).to.equal('v1:' + sid.slice(-4))
  236. sess.validationToken = validationToken
  237. await rclient.set(key, JSON.stringify(sess))
  238. {
  239. // The current code destroys the session and throws an error/500.
  240. // Check for login redirect on page reload.
  241. await user.doRequest('GET', '/project')
  242. const { response } = await user.doRequest('GET', '/project')
  243. expect(response.statusCode).to.equal(302)
  244. expect(response.headers.location).to.equal('/login?')
  245. }
  246. }
  247. async function getOtherUsersValidationToken() {
  248. const otherUser = new User()
  249. await otherUser.login()
  250. await checkSessionIsValid(otherUser)
  251. const { validationToken } = await otherUser.getSession()
  252. expect(validationToken).to.match(/^v1:.{4}$/)
  253. return validationToken
  254. }
  255. async function checkSessionIsValid(user) {
  256. const { response } = await user.doRequest('GET', '/project')
  257. expect(response.statusCode).to.equal(200)
  258. }
  259. it('should reject the redis value when missing', async function () {
  260. await tryWithValidationToken(undefined)
  261. })
  262. it('should reject the redis value when empty', async function () {
  263. await tryWithValidationToken('')
  264. })
  265. it('should reject the redis value when out of sync', async function () {
  266. await tryWithValidationToken(await getOtherUsersValidationToken())
  267. })
  268. it('should ignore overwrites in app code', async function () {
  269. const otherUsersValidationToken = await getOtherUsersValidationToken()
  270. const user = new User()
  271. await user.login()
  272. await checkSessionIsValid(user)
  273. const { validationToken: token1 } = await user.getSession()
  274. const allowedUpdateValue = 'allowed-update-value'
  275. await user.setInSession({
  276. validationToken: otherUsersValidationToken,
  277. // also update another field to check that the write operation went through
  278. allowedUpdate: allowedUpdateValue,
  279. })
  280. const { validationToken: token2, allowedUpdate } = await user.getSession()
  281. expect(allowedUpdate).to.equal(allowedUpdateValue)
  282. expect(token1).to.equal(token2)
  283. await checkSessionIsValid(user)
  284. })
  285. })
  286. })