CookieMetricsTests.mjs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import Settings from '@overleaf/settings'
  2. import { expect } from 'chai'
  3. import UserHelper from './helpers/User.mjs'
  4. import MetricsHelper from './helpers/metrics.mjs'
  5. import cookieSignature from 'cookie-signature'
  6. const User = UserHelper.promises
  7. const getMetric = MetricsHelper.promises.getMetric
  8. const resetMetrics = MetricsHelper.resetMetrics
  9. async function getSessionCookieMetric(status) {
  10. return getMetric(
  11. line =>
  12. line.includes('session_cookie') && line.includes(`status="${status}"`)
  13. )
  14. }
  15. /*
  16. * Modifies the session cookie by removing the existing signature and signing
  17. * the cookie with a new secret.
  18. */
  19. function modifyCookieSignature(originalCookie, newSecret) {
  20. const [sessionKey] = originalCookie.value.slice(2).split('.')
  21. return cookieSignature.sign(sessionKey, newSecret)
  22. }
  23. describe('Session cookie', function () {
  24. before(async function () {
  25. this.user = new User()
  26. })
  27. describe('with no session cookie', function () {
  28. before(async function () {
  29. resetMetrics()
  30. const { response } = await this.user.doRequest('GET', '/login')
  31. this.response = response
  32. })
  33. after(function () {
  34. this.user.resetCookies()
  35. })
  36. it('should accept the request', function () {
  37. expect(this.response.statusCode).to.equal(200)
  38. })
  39. it('should return a signed cookie', async function () {
  40. const cookie = this.user.sessionCookie()
  41. expect(cookie).to.exist
  42. expect(cookie.key).to.equal(Settings.cookieName)
  43. expect(cookie.value).to.match(/^s:/)
  44. })
  45. it('should sign the cookie with the current session secret', function () {
  46. const cookie = this.user.sessionCookie()
  47. const unsigned = cookieSignature.unsign(
  48. cookie.value.slice(2), // strip the 's:' prefix
  49. Settings.security.sessionSecret
  50. )
  51. expect(unsigned).not.to.be.false
  52. expect(unsigned).to.match(/^[a-zA-Z0-9_-]+$/)
  53. })
  54. it('should record a "none" cookie metric', async function () {
  55. const count = await getSessionCookieMetric('none')
  56. expect(count).to.equal(1)
  57. })
  58. })
  59. describe('with a signed session cookie', function () {
  60. before(async function () {
  61. // get the first cookie
  62. await this.user.doRequest('GET', '/login')
  63. this.firstCookie = this.user.sessionCookie()
  64. // make a subsequent request
  65. resetMetrics()
  66. const { response } = await this.user.doRequest('GET', '/login')
  67. this.response = response
  68. })
  69. after(function () {
  70. this.user.resetCookies()
  71. })
  72. it('should accept the request', function () {
  73. expect(this.response.statusCode).to.equal(200)
  74. })
  75. it('should return the same signed cookie', async function () {
  76. const cookie = this.user.sessionCookie()
  77. expect(cookie).to.exist
  78. expect(cookie.key).to.equal(Settings.cookieName)
  79. expect(cookie.value).to.equal(this.firstCookie.value)
  80. })
  81. it('should record a "signed" cookie metric', async function () {
  82. const count = await getSessionCookieMetric('signed')
  83. expect(count).to.equal(1)
  84. })
  85. })
  86. describe('with a session cookie signed with the fallback session secret', function () {
  87. before(async function () {
  88. // get the first cookie
  89. await this.user.doRequest('GET', '/login')
  90. this.firstCookie = this.user.sessionCookie()
  91. // sign the session key with the fallback secret
  92. this.user.setSessionCookie(
  93. 's:' +
  94. modifyCookieSignature(
  95. this.firstCookie,
  96. Settings.security.sessionSecretFallback
  97. )
  98. )
  99. // make a subsequent request
  100. resetMetrics()
  101. const { response } = await this.user.doRequest('GET', '/login')
  102. this.response = response
  103. })
  104. after(function () {
  105. this.user.resetCookies()
  106. })
  107. it('should accept the request', async function () {
  108. expect(this.response.statusCode).to.equal(200)
  109. })
  110. it('should return the cookie signed with the current secret', function () {
  111. const cookie = this.user.sessionCookie()
  112. expect(cookie).to.exist
  113. expect(cookie.key).to.equal(Settings.cookieName)
  114. expect(cookie.value).to.equal(this.firstCookie.value)
  115. })
  116. it('should record a "signed" cookie metric', async function () {
  117. const count = await getSessionCookieMetric('signed')
  118. expect(count).to.equal(1)
  119. })
  120. })
  121. describe('with a session cookie signed with the upcoming session secret', function () {
  122. before(async function () {
  123. // get the first cookie
  124. await this.user.doRequest('GET', '/login')
  125. this.firstCookie = this.user.sessionCookie()
  126. // sign the session key with the upcoming secret
  127. this.user.setSessionCookie(
  128. 's:' +
  129. modifyCookieSignature(
  130. this.firstCookie,
  131. Settings.security.sessionSecretUpcoming
  132. )
  133. )
  134. // make a subsequent request
  135. resetMetrics()
  136. const { response } = await this.user.doRequest('GET', '/login')
  137. this.response = response
  138. })
  139. after(function () {
  140. this.user.resetCookies()
  141. })
  142. it('should accept the request', async function () {
  143. expect(this.response.statusCode).to.equal(200)
  144. })
  145. it('should return the cookie signed with the current secret', function () {
  146. const cookie = this.user.sessionCookie()
  147. expect(cookie).to.exist
  148. expect(cookie.key).to.equal(Settings.cookieName)
  149. expect(cookie.value).to.equal(this.firstCookie.value)
  150. })
  151. it('should record a "signed" cookie metric', async function () {
  152. const count = await getSessionCookieMetric('signed')
  153. expect(count).to.equal(1)
  154. })
  155. })
  156. describe('with a session cookie signed with an invalid secret', function () {
  157. before(async function () {
  158. // get the first cookie
  159. await this.user.doRequest('GET', '/login')
  160. this.firstCookie = this.user.sessionCookie()
  161. // sign the session key with an invalid secret
  162. this.user.setSessionCookie(
  163. 's:' + modifyCookieSignature(this.firstCookie, 'invalid-secret')
  164. )
  165. // make a subsequent request
  166. resetMetrics()
  167. const { response } = await this.user.doRequest('GET', '/login')
  168. this.response = response
  169. })
  170. after(function () {
  171. this.user.resetCookies()
  172. })
  173. it('should not reject the request', async function () {
  174. expect(this.response.statusCode).to.equal(200)
  175. })
  176. it('should return a new cookie signed with the current secret', function () {
  177. const cookie = this.user.sessionCookie()
  178. expect(cookie).to.exist
  179. expect(cookie.key).to.equal(Settings.cookieName)
  180. const [sessionKey] = cookie.value.slice(2).split('.')
  181. expect(sessionKey).not.to.equal(this.firstSessionKey)
  182. })
  183. it('should record a "bad-signature" cookie metric', async function () {
  184. const count = await getSessionCookieMetric('bad-signature')
  185. expect(count).to.equal(1)
  186. })
  187. })
  188. describe('with an unsigned session cookie', function () {
  189. before(async function () {
  190. // get the first cookie
  191. await this.user.doRequest('GET', '/login')
  192. this.firstCookie = this.user.sessionCookie()
  193. // use the session key without signing it
  194. const [sessionKey] = this.firstCookie.value.slice(2).split('.')
  195. this.firstSessionKey = sessionKey
  196. this.user.setSessionCookie(sessionKey)
  197. // make a subsequent request
  198. resetMetrics()
  199. const { response } = await this.user.doRequest('GET', '/login')
  200. this.response = response
  201. })
  202. after(function () {
  203. this.user.resetCookies()
  204. })
  205. it('should not reject the request', async function () {
  206. expect(this.response.statusCode).to.equal(200)
  207. })
  208. it('should return a new cookie signed with the current secret', function () {
  209. const cookie = this.user.sessionCookie()
  210. expect(cookie).to.exist
  211. expect(cookie.key).to.equal(Settings.cookieName)
  212. const [sessionKey] = cookie.value.slice(2).split('.')
  213. expect(sessionKey).not.to.equal(this.firstSessionKey)
  214. })
  215. it('should record an "unsigned" cookie metric', async function () {
  216. const count = await getSessionCookieMetric('unsigned')
  217. expect(count).to.equal(1)
  218. })
  219. })
  220. })