Files
overleaf-cep/services/web/test/unit/src/Security/OneTimeTokenHandlerTests.js
T
June Kelly 3288f87dbe [web] Password set/reset: reject current password (redux) (#8956)
* [web] set-password: reject same as current password

* [web] Add 'peek' operation on tokens

This allows us to improve the UX of the reset-password form,
by not invalidating the token in the case where the new
password will be rejected by validation logic.

We give up to three attempts before invalidating the token.

* [web] Add hide-on-error feature to async forms

This allows us to hide the form elements when certain
named error conditions occur.

* [web] reset-password: handle same-password rejection

We also change the implementation to use the new
peekValueFromToken API, and to expire the token explicitely
after it has been used to set the new password.

* [web] Validate OneTimeToken when loading password reset form

* [web] Rate limit GET: /user/password/set

Now that we are peeking at OneTimeToken when accessing this page,
we add rate to the GET request, matching that of the POST request.

* [web] Tidy up pug layout and mongo query for token peeking

Co-authored-by: Mathias Jakobsen <mathias.jakobsen@overleaf.com>
GitOrigin-RevId: 835205cc7c7ebe1209ee8e5b693efeb939a3056a
2022-09-28 08:06:54 +00:00

249 lines
6.9 KiB
JavaScript

/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const path = require('path')
const sinon = require('sinon')
const modulePath = path.join(
__dirname,
'../../../../app/src/Features/Security/OneTimeTokenHandler'
)
const { expect } = require('chai')
const Errors = require('../../../../app/src/Features/Errors/Errors')
const tk = require('timekeeper')
describe('OneTimeTokenHandler', function () {
beforeEach(function () {
tk.freeze(Date.now()) // freeze the time for these tests
this.stubbedToken = 'mock-token'
this.callback = sinon.stub()
return (this.OneTimeTokenHandler = SandboxedModule.require(modulePath, {
requires: {
'@overleaf/settings': this.settings,
crypto: {
randomBytes: () => this.stubbedToken,
},
'../../infrastructure/mongodb': {
db: (this.db = { tokens: {} }),
},
},
}))
})
afterEach(function () {
return tk.reset()
})
describe('getNewToken', function () {
beforeEach(function () {
return (this.db.tokens.insertOne = sinon.stub().yields())
})
describe('normally', function () {
beforeEach(function () {
return this.OneTimeTokenHandler.getNewToken(
'password',
'mock-data-to-store',
this.callback
)
})
it('should insert a generated token with a 1 hour expiry', function () {
return this.db.tokens.insertOne
.calledWith({
use: 'password',
token: this.stubbedToken,
createdAt: new Date(),
expiresAt: new Date(Date.now() + 60 * 60 * 1000),
data: 'mock-data-to-store',
})
.should.equal(true)
})
it('should call the callback with the token', function () {
return this.callback
.calledWith(null, this.stubbedToken)
.should.equal(true)
})
})
describe('with an optional expiresIn parameter', function () {
beforeEach(function () {
return this.OneTimeTokenHandler.getNewToken(
'password',
'mock-data-to-store',
{ expiresIn: 42 },
this.callback
)
})
it('should insert a generated token with a custom expiry', function () {
return this.db.tokens.insertOne
.calledWith({
use: 'password',
token: this.stubbedToken,
createdAt: new Date(),
expiresAt: new Date(Date.now() + 42 * 1000),
data: 'mock-data-to-store',
})
.should.equal(true)
})
it('should call the callback with the token', function () {
return this.callback
.calledWith(null, this.stubbedToken)
.should.equal(true)
})
})
})
describe('peekValueFromToken', function () {
describe('successfully', function () {
const data = 'some-mock-data'
beforeEach(function () {
this.db.tokens.findOneAndUpdate = sinon
.stub()
.yields(null, { value: { data } })
return this.OneTimeTokenHandler.peekValueFromToken(
'password',
'mock-token',
this.callback
)
})
it('should increment the peekCount', function () {
return this.db.tokens.findOneAndUpdate
.calledWith(
{
use: 'password',
token: 'mock-token',
expiresAt: { $gt: new Date() },
usedAt: { $exists: false },
peekCount: { $not: { $gte: this.OneTimeTokenHandler.MAX_PEEKS } },
},
{
$inc: { peekCount: 1 },
}
)
.should.equal(true)
})
it('should return the data', function () {
return this.callback.calledWith(null, data).should.equal(true)
})
})
describe('when a valid token is not found', function () {
beforeEach(function () {
this.db.tokens.findOneAndUpdate = sinon
.stub()
.yields(null, { value: null })
return this.OneTimeTokenHandler.peekValueFromToken(
'password',
'mock-token',
this.callback
)
})
it('should return a NotFoundError', function () {
return this.callback
.calledWith(sinon.match.instanceOf(Errors.NotFoundError))
.should.equal(true)
})
})
})
describe('expireToken', function () {
beforeEach(function () {
this.db.tokens.updateOne = sinon.stub().yields(null)
this.OneTimeTokenHandler.expireToken(
'password',
'mock-token',
this.callback
)
})
it('should expire the token', function () {
this.db.tokens.updateOne
.calledWith(
{
use: 'password',
token: 'mock-token',
},
{
$set: {
usedAt: new Date(),
},
}
)
.should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
describe('getValueFromTokenAndExpire', function () {
describe('successfully', function () {
beforeEach(function () {
this.db.tokens.findOneAndUpdate = sinon
.stub()
.yields(null, { value: { data: 'mock-data' } })
return this.OneTimeTokenHandler.getValueFromTokenAndExpire(
'password',
'mock-token',
this.callback
)
})
it('should expire the token', function () {
return this.db.tokens.findOneAndUpdate
.calledWith(
{
use: 'password',
token: 'mock-token',
expiresAt: { $gt: new Date() },
usedAt: { $exists: false },
peekCount: { $not: { $gte: this.OneTimeTokenHandler.MAX_PEEKS } },
},
{
$set: { usedAt: new Date() },
}
)
.should.equal(true)
})
it('should return the data', function () {
return this.callback.calledWith(null, 'mock-data').should.equal(true)
})
})
describe('when a valid token is not found', function () {
beforeEach(function () {
this.db.tokens.findOneAndUpdate = sinon
.stub()
.yields(null, { value: null })
return this.OneTimeTokenHandler.getValueFromTokenAndExpire(
'password',
'mock-token',
this.callback
)
})
it('should return a NotFoundError', function () {
return this.callback
.calledWith(sinon.match.instanceOf(Errors.NotFoundError))
.should.equal(true)
})
})
})
})