RegistrationTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* eslint-disable
  2. handle-callback-err
  3. */
  4. const { expect } = require('chai')
  5. const async = require('async')
  6. const metrics = require('./helpers/metrics')
  7. const User = require('./helpers/User')
  8. const UserPromises = require('./helpers/User').promises
  9. const redis = require('./helpers/redis')
  10. const Features = require('../../../app/src/infrastructure/Features')
  11. // Expectations
  12. const expectProjectAccess = function (user, projectId, callback) {
  13. // should have access to project
  14. if (callback == null) {
  15. callback = function (err, result) {}
  16. }
  17. user.openProject(projectId, err => {
  18. expect(err).to.be.oneOf([null, undefined])
  19. return callback()
  20. })
  21. }
  22. const expectNoProjectAccess = function (user, projectId, callback) {
  23. // should not have access to project page
  24. if (callback == null) {
  25. callback = function (err, result) {}
  26. }
  27. user.openProject(projectId, err => {
  28. expect(err).to.be.instanceof(Error)
  29. return callback()
  30. })
  31. }
  32. // Actions
  33. const tryLoginThroughRegistrationForm = function (
  34. user,
  35. email,
  36. password,
  37. callback
  38. ) {
  39. if (callback == null) {
  40. callback = function (err, response, body) {}
  41. }
  42. user.getCsrfToken(err => {
  43. if (err != null) {
  44. return callback(err)
  45. }
  46. user.request.post(
  47. {
  48. url: '/register',
  49. json: {
  50. email,
  51. password,
  52. },
  53. },
  54. callback
  55. )
  56. })
  57. }
  58. describe('Registration', function () {
  59. describe('LoginRateLimit', function () {
  60. let userA
  61. beforeEach(function () {
  62. userA = new UserPromises()
  63. })
  64. function loginRateLimited(line) {
  65. return line.includes('rate_limit_hit') && line.includes('login')
  66. }
  67. async function getLoginRateLimitHitMetricValue() {
  68. return await metrics.promises.getMetric(loginRateLimited)
  69. }
  70. let beforeCount
  71. beforeEach('get baseline metric value', async function () {
  72. beforeCount = await getLoginRateLimitHitMetricValue()
  73. })
  74. beforeEach('setup csrf token', async function () {
  75. await userA.getCsrfToken()
  76. })
  77. describe('pushing an account just below the rate limit', function () {
  78. async function doLoginAttempts(user, n, pushInto) {
  79. while (n--) {
  80. const { body } = await user.doRequest('POST', {
  81. url: '/login',
  82. json: {
  83. email: user.email,
  84. password: 'invalid-password',
  85. },
  86. })
  87. const message = body && body.message && body.message.text
  88. pushInto.push(message)
  89. }
  90. }
  91. let results = []
  92. beforeEach('do 9 login attempts', async function () {
  93. results = []
  94. await doLoginAttempts(userA, 9, results)
  95. })
  96. it('should not record any rate limited requests', async function () {
  97. const afterCount = await getLoginRateLimitHitMetricValue()
  98. expect(afterCount).to.equal(beforeCount)
  99. })
  100. it('should produce the correct responses so far', function () {
  101. expect(results.length).to.equal(9)
  102. expect(results).to.deep.equal(
  103. Array(9).fill('Your email or password is incorrect. Please try again')
  104. )
  105. })
  106. describe('pushing the account past the limit', function () {
  107. beforeEach('do 6 login attempts', async function () {
  108. await doLoginAttempts(userA, 6, results)
  109. })
  110. it('should record 5 rate limited requests', async function () {
  111. const afterCount = await getLoginRateLimitHitMetricValue()
  112. expect(afterCount).to.equal(beforeCount + 5)
  113. })
  114. it('should produce the correct responses', function () {
  115. expect(results.length).to.equal(15)
  116. expect(results).to.deep.equal(
  117. Array(10)
  118. .fill('Your email or password is incorrect. Please try again')
  119. .concat(
  120. Array(5).fill(
  121. 'This account has had too many login requests. Please wait 2 minutes before trying to log in again'
  122. )
  123. )
  124. )
  125. })
  126. describe('logging in with another user', function () {
  127. let userB
  128. beforeEach(function () {
  129. userB = new UserPromises()
  130. })
  131. beforeEach('update baseline metric value', async function () {
  132. beforeCount = await getLoginRateLimitHitMetricValue()
  133. })
  134. beforeEach('setup csrf token', async function () {
  135. await userB.getCsrfToken()
  136. })
  137. let messages = []
  138. beforeEach('do bad login', async function () {
  139. messages = []
  140. await doLoginAttempts(userB, 1, messages)
  141. })
  142. it('should not rate limit their request', function () {
  143. expect(messages).to.deep.equal([
  144. 'Your email or password is incorrect. Please try again',
  145. ])
  146. })
  147. it('should not record any further rate limited requests', async function () {
  148. const afterCount = await getLoginRateLimitHitMetricValue()
  149. expect(afterCount).to.equal(beforeCount)
  150. })
  151. })
  152. })
  153. describe('performing a valid login for clearing the limit', function () {
  154. beforeEach('do login', async function () {
  155. await userA.login()
  156. })
  157. it('should log the user in', async function () {
  158. const { response } = await userA.doRequest('GET', '/project')
  159. expect(response.statusCode).to.equal(200)
  160. })
  161. it('should not record any rate limited requests', async function () {
  162. const afterCount = await getLoginRateLimitHitMetricValue()
  163. expect(afterCount).to.equal(beforeCount)
  164. })
  165. describe('logging out and performing more invalid login requests', function () {
  166. beforeEach('logout', async function () {
  167. await userA.logout()
  168. })
  169. beforeEach('fetch new csrf token', async function () {
  170. await userA.getCsrfToken()
  171. })
  172. let results = []
  173. beforeEach('do 9 login attempts', async function () {
  174. results = []
  175. await doLoginAttempts(userA, 9, results)
  176. })
  177. it('should not record any rate limited requests yet', async function () {
  178. const afterCount = await getLoginRateLimitHitMetricValue()
  179. expect(afterCount).to.equal(beforeCount)
  180. })
  181. it('should not emit any rate limited responses yet', function () {
  182. expect(results.length).to.equal(9)
  183. expect(results).to.deep.equal(
  184. Array(9).fill(
  185. 'Your email or password is incorrect. Please try again'
  186. )
  187. )
  188. })
  189. })
  190. })
  191. })
  192. })
  193. describe('CSRF protection', function () {
  194. before(function () {
  195. if (!Features.hasFeature('registration')) {
  196. this.skip()
  197. }
  198. })
  199. beforeEach(function () {
  200. this.user = new User()
  201. this.email = `test+${Math.random()}@example.com`
  202. this.password = 'password11'
  203. })
  204. afterEach(function (done) {
  205. this.user.fullDeleteUser(this.email, done)
  206. })
  207. it('should register with the csrf token', function (done) {
  208. this.user.request.get('/login', (err, res, body) => {
  209. this.user.getCsrfToken(error => {
  210. this.user.request.post(
  211. {
  212. url: '/register',
  213. json: {
  214. email: this.email,
  215. password: this.password,
  216. },
  217. headers: {
  218. 'x-csrf-token': this.user.csrfToken,
  219. },
  220. },
  221. (error, response, body) => {
  222. expect(err != null).to.equal(false)
  223. expect(response.statusCode).to.equal(200)
  224. return done()
  225. }
  226. )
  227. })
  228. })
  229. })
  230. it('should fail with no csrf token', function (done) {
  231. this.user.request.get('/login', (err, res, body) => {
  232. this.user.getCsrfToken(error => {
  233. this.user.request.post(
  234. {
  235. url: '/register',
  236. json: {
  237. email: this.email,
  238. password: this.password,
  239. },
  240. headers: {
  241. 'x-csrf-token': '',
  242. },
  243. },
  244. (error, response, body) => {
  245. expect(response.statusCode).to.equal(403)
  246. return done()
  247. }
  248. )
  249. })
  250. })
  251. })
  252. it('should fail with a stale csrf token', function (done) {
  253. this.user.request.get('/login', (err, res, body) => {
  254. this.user.getCsrfToken(error => {
  255. const oldCsrfToken = this.user.csrfToken
  256. this.user.logout(err => {
  257. this.user.request.post(
  258. {
  259. url: '/register',
  260. json: {
  261. email: this.email,
  262. password: this.password,
  263. },
  264. headers: {
  265. 'x-csrf-token': oldCsrfToken,
  266. },
  267. },
  268. (error, response, body) => {
  269. expect(response.statusCode).to.equal(403)
  270. return done()
  271. }
  272. )
  273. })
  274. })
  275. })
  276. })
  277. })
  278. describe('Register', function () {
  279. before(function () {
  280. if (!Features.hasFeature('registration')) {
  281. this.skip()
  282. }
  283. })
  284. beforeEach(function () {
  285. this.user = new User()
  286. })
  287. it('Set emails attribute', function (done) {
  288. this.user.register((error, user) => {
  289. expect(error).to.not.exist
  290. user.email.should.equal(this.user.email)
  291. user.emails.should.exist
  292. user.emails.should.be.a('array')
  293. user.emails.length.should.equal(1)
  294. user.emails[0].email.should.equal(this.user.email)
  295. return done()
  296. })
  297. })
  298. })
  299. describe('LoginViaRegistration', function () {
  300. beforeEach(function (done) {
  301. this.timeout(60000)
  302. this.user1 = new User()
  303. this.user2 = new User()
  304. async.series(
  305. [
  306. cb => this.user1.login(cb),
  307. cb => this.user1.logout(cb),
  308. cb => redis.clearUserSessions(this.user1, cb),
  309. cb => this.user2.login(cb),
  310. cb => this.user2.logout(cb),
  311. cb => redis.clearUserSessions(this.user2, cb),
  312. ],
  313. done
  314. )
  315. this.project_id = null
  316. })
  317. describe('[Security] Trying to register/login as another user', function () {
  318. before(function () {
  319. if (!Features.hasFeature('registration')) {
  320. this.skip()
  321. }
  322. })
  323. it('should not allow sign in with secondary email', function (done) {
  324. const secondaryEmail = 'acceptance-test-secondary@example.com'
  325. this.user1.addEmail(secondaryEmail, err => {
  326. this.user1.loginWith(secondaryEmail, err => {
  327. expect(err != null).to.equal(false)
  328. this.user1.isLoggedIn((err, isLoggedIn) => {
  329. expect(isLoggedIn).to.equal(false)
  330. return done()
  331. })
  332. })
  333. })
  334. })
  335. it('should have user1 login and create a project, which user2 cannot access', function (done) {
  336. let projectId
  337. async.series(
  338. [
  339. // user1 logs in and creates a project which only they can access
  340. cb => {
  341. this.user1.login(err => {
  342. expect(err).not.to.exist
  343. cb()
  344. })
  345. },
  346. cb => {
  347. this.user1.createProject('Private Project', (err, id) => {
  348. expect(err).not.to.exist
  349. projectId = id
  350. cb()
  351. })
  352. },
  353. cb => expectProjectAccess(this.user1, projectId, cb),
  354. cb => expectNoProjectAccess(this.user2, projectId, cb),
  355. // should prevent user2 from login/register with user1 email address
  356. cb => {
  357. tryLoginThroughRegistrationForm(
  358. this.user2,
  359. this.user1.email,
  360. 'totally_not_the_right_password',
  361. (err, response, body) => {
  362. expect(body.redir != null).to.equal(false)
  363. expect(body.message != null).to.equal(true)
  364. expect(body.message).to.have.all.keys('type', 'text')
  365. expect(body.message.type).to.equal('error')
  366. cb()
  367. }
  368. )
  369. },
  370. // check user still can't access the project
  371. cb => expectNoProjectAccess(this.user2, projectId, done),
  372. ],
  373. done
  374. )
  375. })
  376. })
  377. })
  378. })