RegistrationTests.js 13 KB

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