HaveIBeenPwnedApiTests.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const Settings = require('@overleaf/settings')
  2. const { expect } = require('chai')
  3. const User = require('./helpers/User').promises
  4. const MockHaveIBeenPwnedApiClass = require('./mocks/MockHaveIBeenPwnedApi')
  5. const { db } = require('../../../app/src/infrastructure/mongodb')
  6. const { getMetric } = require('./helpers/metrics').promises
  7. const sleep = require('util').promisify(setTimeout)
  8. let MockHaveIBeenPwnedApi
  9. before(function () {
  10. MockHaveIBeenPwnedApi = MockHaveIBeenPwnedApiClass.instance()
  11. })
  12. async function letPasswordCheckRunInBackground() {
  13. await sleep(200)
  14. }
  15. async function getMetricReUsed() {
  16. return getMetric(
  17. line => line.includes('password_re_use') && line.includes('re-used')
  18. )
  19. }
  20. async function getMetricUnique() {
  21. return getMetric(
  22. line => line.includes('password_re_use') && line.includes('unique')
  23. )
  24. }
  25. async function getMetricFailure() {
  26. return getMetric(
  27. line => line.includes('password_re_use') && line.includes('failure')
  28. )
  29. }
  30. let user, previous
  31. async function resetPassword(password) {
  32. await user.getCsrfToken()
  33. await user.doRequest('POST', {
  34. url: '/user/password/reset',
  35. form: {
  36. email: user.email,
  37. },
  38. })
  39. const token = (
  40. await db.tokens.findOne({
  41. 'data.user_id': user._id.toString(),
  42. })
  43. ).token
  44. await user.doRequest('GET', {
  45. url: `/user/password/set?passwordResetToken=${token}&email=${user.email}`,
  46. })
  47. await user.doRequest('POST', {
  48. url: '/user/password/set',
  49. form: {
  50. passwordResetToken: token,
  51. password,
  52. },
  53. })
  54. }
  55. describe('HaveIBeenPwnedApi', function () {
  56. before(function () {
  57. Settings.apis.haveIBeenPwned.enabled = true
  58. })
  59. after(function () {
  60. Settings.apis.haveIBeenPwned.enabled = false
  61. })
  62. describe('login with weak password', function () {
  63. beforeEach(function () {
  64. user = new User()
  65. user.password = 'aLeakedPassword42'
  66. // echo -n aLeakedPassword42 | sha1sum
  67. MockHaveIBeenPwnedApi.addPasswordByHash(
  68. 'D1ABBDEEE70CBE8BBCE5D9D039C53C0CE91C0C16'
  69. )
  70. })
  71. beforeEach('create the user', async function () {
  72. await user.ensureUserExists()
  73. await letPasswordCheckRunInBackground()
  74. })
  75. beforeEach('fetch previous count', async function () {
  76. previous = await getMetricReUsed()
  77. })
  78. beforeEach('login', async function () {
  79. await user.loginNoUpdate()
  80. await letPasswordCheckRunInBackground()
  81. })
  82. it('should track the weak password', async function () {
  83. const after = await getMetricReUsed()
  84. expect(after).to.equal(previous + 1)
  85. })
  86. })
  87. describe('login with strong password', function () {
  88. beforeEach(function () {
  89. user = new User()
  90. user.password = 'this-is-a-strong-password'
  91. })
  92. beforeEach('create the user', async function () {
  93. await user.ensureUserExists()
  94. await letPasswordCheckRunInBackground()
  95. })
  96. beforeEach('fetch previous count', async function () {
  97. previous = await getMetricUnique()
  98. })
  99. beforeEach('login', async function () {
  100. await user.loginNoUpdate()
  101. await letPasswordCheckRunInBackground()
  102. })
  103. it('should track the strong password', async function () {
  104. const after = await getMetricUnique()
  105. expect(after).to.equal(previous + 1)
  106. })
  107. })
  108. describe('when the api is producing garbage', function () {
  109. beforeEach(function () {
  110. user = new User()
  111. user.password = 'trigger-garbage-output'
  112. })
  113. beforeEach('create the user', async function () {
  114. await user.ensureUserExists()
  115. await letPasswordCheckRunInBackground()
  116. })
  117. beforeEach('fetch previous count', async function () {
  118. previous = await getMetricFailure()
  119. })
  120. beforeEach('login', async function () {
  121. await user.loginNoUpdate()
  122. await letPasswordCheckRunInBackground()
  123. })
  124. it('should track the failure to collect a score', async function () {
  125. const after = await getMetricFailure()
  126. expect(after).to.equal(previous + 1)
  127. })
  128. })
  129. describe('login attempt with weak password', function () {
  130. beforeEach(function () {
  131. user = new User()
  132. // echo -n aLeakedPassword42 | sha1sum
  133. MockHaveIBeenPwnedApi.addPasswordByHash(
  134. 'D1ABBDEEE70CBE8BBCE5D9D039C53C0CE91C0C16'
  135. )
  136. })
  137. beforeEach('create the user', async function () {
  138. await user.ensureUserExists()
  139. await letPasswordCheckRunInBackground()
  140. })
  141. beforeEach('fetch previous counts', async function () {
  142. previous = {
  143. reUsed: await getMetricReUsed(),
  144. unique: await getMetricUnique(),
  145. failure: await getMetricFailure(),
  146. }
  147. })
  148. beforeEach('login', async function () {
  149. await user.loginWithEmailPassword(user.email, 'aLeakedPassword42')
  150. await letPasswordCheckRunInBackground()
  151. })
  152. it('should not increment any counter', async function () {
  153. expect(previous).to.deep.equal({
  154. reUsed: await getMetricReUsed(),
  155. unique: await getMetricUnique(),
  156. failure: await getMetricFailure(),
  157. })
  158. })
  159. })
  160. describe('password reset with a weak password', function () {
  161. beforeEach(function () {
  162. user = new User()
  163. // echo -n aLeakedPassword42 | sha1sum
  164. MockHaveIBeenPwnedApi.addPasswordByHash(
  165. 'D1ABBDEEE70CBE8BBCE5D9D039C53C0CE91C0C16'
  166. )
  167. })
  168. beforeEach('create the user', async function () {
  169. await user.ensureUserExists()
  170. await letPasswordCheckRunInBackground()
  171. })
  172. beforeEach('fetch previous count', async function () {
  173. previous = await getMetricReUsed()
  174. })
  175. beforeEach('set password', async function () {
  176. await resetPassword('aLeakedPassword42')
  177. await letPasswordCheckRunInBackground()
  178. })
  179. it('should track the weak password', async function () {
  180. const after = await getMetricReUsed()
  181. expect(after).to.equal(previous + 1)
  182. })
  183. })
  184. describe('password reset with a strong password', function () {
  185. beforeEach(function () {
  186. user = new User()
  187. })
  188. beforeEach('create the user', async function () {
  189. await user.ensureUserExists()
  190. await letPasswordCheckRunInBackground()
  191. })
  192. beforeEach('fetch previous count', async function () {
  193. previous = await getMetricUnique()
  194. })
  195. beforeEach('set password', async function () {
  196. await resetPassword('a-strong-new-password')
  197. await letPasswordCheckRunInBackground()
  198. })
  199. it('should track the strong password', async function () {
  200. const after = await getMetricUnique()
  201. expect(after).to.equal(previous + 1)
  202. })
  203. })
  204. })