| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- const Settings = require('@overleaf/settings')
- const { expect } = require('chai')
- const User = require('./helpers/User').promises
- const MockHaveIBeenPwnedApiClass = require('./mocks/MockHaveIBeenPwnedApi')
- const { db } = require('../../../app/src/infrastructure/mongodb')
- const { getMetric } = require('./helpers/metrics').promises
- const sleep = require('util').promisify(setTimeout)
- let MockHaveIBeenPwnedApi
- before(function () {
- MockHaveIBeenPwnedApi = MockHaveIBeenPwnedApiClass.instance()
- })
- async function letPasswordCheckRunInBackground() {
- await sleep(200)
- }
- async function getMetricReUsed() {
- return getMetric(
- line => line.includes('password_re_use') && line.includes('re-used')
- )
- }
- async function getMetricUnique() {
- return getMetric(
- line => line.includes('password_re_use') && line.includes('unique')
- )
- }
- async function getMetricFailure() {
- return getMetric(
- line => line.includes('password_re_use') && line.includes('failure')
- )
- }
- let user, previous
- async function resetPassword(password) {
- await user.getCsrfToken()
- await user.doRequest('POST', {
- url: '/user/password/reset',
- form: {
- email: user.email,
- },
- })
- const token = (
- await db.tokens.findOne({
- 'data.user_id': user._id.toString(),
- })
- ).token
- await user.doRequest('GET', {
- url: `/user/password/set?passwordResetToken=${token}&email=${user.email}`,
- })
- await user.doRequest('POST', {
- url: '/user/password/set',
- form: {
- passwordResetToken: token,
- password,
- },
- })
- }
- describe('HaveIBeenPwnedApi', function () {
- before(function () {
- Settings.apis.haveIBeenPwned.enabled = true
- })
- after(function () {
- Settings.apis.haveIBeenPwned.enabled = false
- })
- describe('login with weak password', function () {
- beforeEach(function () {
- user = new User()
- user.password = 'aLeakedPassword42'
- // echo -n aLeakedPassword42 | sha1sum
- MockHaveIBeenPwnedApi.addPasswordByHash(
- 'D1ABBDEEE70CBE8BBCE5D9D039C53C0CE91C0C16'
- )
- })
- beforeEach('create the user', async function () {
- await user.ensureUserExists()
- await letPasswordCheckRunInBackground()
- })
- beforeEach('fetch previous count', async function () {
- previous = await getMetricReUsed()
- })
- beforeEach('login', async function () {
- await user.loginNoUpdate()
- await letPasswordCheckRunInBackground()
- })
- it('should track the weak password', async function () {
- const after = await getMetricReUsed()
- expect(after).to.equal(previous + 1)
- })
- })
- describe('login with strong password', function () {
- beforeEach(function () {
- user = new User()
- user.password = 'this-is-a-strong-password'
- })
- beforeEach('create the user', async function () {
- await user.ensureUserExists()
- await letPasswordCheckRunInBackground()
- })
- beforeEach('fetch previous count', async function () {
- previous = await getMetricUnique()
- })
- beforeEach('login', async function () {
- await user.loginNoUpdate()
- await letPasswordCheckRunInBackground()
- })
- it('should track the strong password', async function () {
- const after = await getMetricUnique()
- expect(after).to.equal(previous + 1)
- })
- })
- describe('when the api is producing garbage', function () {
- beforeEach(function () {
- user = new User()
- user.password = 'trigger-garbage-output'
- })
- beforeEach('create the user', async function () {
- await user.ensureUserExists()
- await letPasswordCheckRunInBackground()
- })
- beforeEach('fetch previous count', async function () {
- previous = await getMetricFailure()
- })
- beforeEach('login', async function () {
- await user.loginNoUpdate()
- await letPasswordCheckRunInBackground()
- })
- it('should track the failure to collect a score', async function () {
- const after = await getMetricFailure()
- expect(after).to.equal(previous + 1)
- })
- })
- describe('login attempt with weak password', function () {
- beforeEach(function () {
- user = new User()
- // echo -n aLeakedPassword42 | sha1sum
- MockHaveIBeenPwnedApi.addPasswordByHash(
- 'D1ABBDEEE70CBE8BBCE5D9D039C53C0CE91C0C16'
- )
- })
- beforeEach('create the user', async function () {
- await user.ensureUserExists()
- await letPasswordCheckRunInBackground()
- })
- beforeEach('fetch previous counts', async function () {
- previous = {
- reUsed: await getMetricReUsed(),
- unique: await getMetricUnique(),
- failure: await getMetricFailure(),
- }
- })
- beforeEach('login', async function () {
- await user.loginWithEmailPassword(user.email, 'aLeakedPassword42')
- await letPasswordCheckRunInBackground()
- })
- it('should not increment any counter', async function () {
- expect(previous).to.deep.equal({
- reUsed: await getMetricReUsed(),
- unique: await getMetricUnique(),
- failure: await getMetricFailure(),
- })
- })
- })
- describe('password reset with a weak password', function () {
- beforeEach(function () {
- user = new User()
- // echo -n aLeakedPassword42 | sha1sum
- MockHaveIBeenPwnedApi.addPasswordByHash(
- 'D1ABBDEEE70CBE8BBCE5D9D039C53C0CE91C0C16'
- )
- })
- beforeEach('create the user', async function () {
- await user.ensureUserExists()
- await letPasswordCheckRunInBackground()
- })
- beforeEach('fetch previous count', async function () {
- previous = await getMetricReUsed()
- })
- beforeEach('set password', async function () {
- await resetPassword('aLeakedPassword42')
- await letPasswordCheckRunInBackground()
- })
- it('should track the weak password', async function () {
- const after = await getMetricReUsed()
- expect(after).to.equal(previous + 1)
- })
- })
- describe('password reset with a strong password', function () {
- beforeEach(function () {
- user = new User()
- })
- beforeEach('create the user', async function () {
- await user.ensureUserExists()
- await letPasswordCheckRunInBackground()
- })
- beforeEach('fetch previous count', async function () {
- previous = await getMetricUnique()
- })
- beforeEach('set password', async function () {
- await resetPassword('a-strong-new-password')
- await letPasswordCheckRunInBackground()
- })
- it('should track the strong password', async function () {
- const after = await getMetricUnique()
- expect(after).to.equal(previous + 1)
- })
- })
- })
|