|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { afterEach, beforeEach, describe, it } from 'mocha'; |
| 3 | +import proxyquire from 'proxyquire'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +const settingsMock = sinon.stub(); |
| 7 | +const getLoginTokenStub = sinon.stub(); |
| 8 | + |
| 9 | +class TOTPCheckMock { |
| 10 | + name = 'totp'; |
| 11 | + |
| 12 | + isEnabled() { |
| 13 | + return true; |
| 14 | + } |
| 15 | + |
| 16 | + async processInvalidCode() { |
| 17 | + return {}; |
| 18 | + } |
| 19 | + |
| 20 | + async verify() { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + async maxFaildedAttemtpsReached() { |
| 25 | + return false; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class DisabledCheckMock { |
| 30 | + name = 'email'; |
| 31 | + |
| 32 | + isEnabled() { |
| 33 | + return false; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class MeteorErrorMock extends Error { |
| 38 | + error: string; |
| 39 | + |
| 40 | + details: unknown; |
| 41 | + |
| 42 | + constructor(error: string, reason?: string, details?: unknown) { |
| 43 | + super(reason); |
| 44 | + this.error = error; |
| 45 | + this.details = details; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const { checkCodeForUser } = proxyquire.noCallThru().load('./index', { |
| 50 | + './TOTPCheck': { TOTPCheck: TOTPCheckMock }, |
| 51 | + './EmailCheck': { EmailCheck: DisabledCheckMock }, |
| 52 | + './PasswordCheckFallback': { PasswordCheckFallback: class extends DisabledCheckMock {} }, |
| 53 | + '../../../lib/server/functions/getModifiedHttpHeaders': { normalizeHeaders: (headers: unknown) => headers }, |
| 54 | + '../../../settings/server': { settings: { get: settingsMock } }, |
| 55 | + '@rocket.chat/models': { |
| 56 | + Users: { |
| 57 | + findOneById: async () => null, |
| 58 | + setTwoFactorAuthorizationHashAndUntilForUserIdAndToken: async () => undefined, |
| 59 | + }, |
| 60 | + }, |
| 61 | + 'meteor/accounts-base': { Accounts: { _getLoginToken: getLoginTokenStub } }, |
| 62 | + 'meteor/meteor': { Meteor: { Error: MeteorErrorMock } }, |
| 63 | +}); |
| 64 | + |
| 65 | +const HASHED_TOKEN = 'hashed-login-token'; |
| 66 | + |
| 67 | +const userWithBypassToken = { |
| 68 | + _id: 'user-id', |
| 69 | + services: { resume: { loginTokens: [{ hashedToken: HASHED_TOKEN, bypassTwoFactor: true }] } }, |
| 70 | +} as any; |
| 71 | + |
| 72 | +const connection = { |
| 73 | + id: 'connection-id', |
| 74 | + clientAddress: '127.0.0.1', |
| 75 | + httpHeaders: {}, |
| 76 | +} as any; |
| 77 | + |
| 78 | +describe('checkCodeForUser - bypassTwoFactor token resolution (SUP-1064)', () => { |
| 79 | + let testMode: string | undefined; |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + // TEST_MODE short-circuits the whole check; remove it so the token resolution path runs. |
| 83 | + testMode = process.env.TEST_MODE; |
| 84 | + delete process.env.TEST_MODE; |
| 85 | + |
| 86 | + settingsMock.reset(); |
| 87 | + settingsMock.withArgs('Accounts_TwoFactorAuthentication_Enabled').returns(true); |
| 88 | + getLoginTokenStub.reset(); |
| 89 | + }); |
| 90 | + |
| 91 | + afterEach(() => { |
| 92 | + if (testMode !== undefined) { |
| 93 | + process.env.TEST_MODE = testMode; |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('should honor a bypassTwoFactor token resolved from the REST connection (connection.token)', async () => { |
| 98 | + // REST: the token is not registered in account data, only carried on the connection. |
| 99 | + getLoginTokenStub.returns(undefined); |
| 100 | + |
| 101 | + const authorized = await checkCodeForUser({ |
| 102 | + user: userWithBypassToken, |
| 103 | + connection: { ...connection, token: HASHED_TOKEN }, |
| 104 | + options: {}, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(authorized).to.be.equal(true); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should honor a bypassTwoFactor token resolved from account data (DDP, _getLoginToken)', async () => { |
| 111 | + // DDP: the token is registered in Accounts._accountData and read via _getLoginToken. |
| 112 | + getLoginTokenStub.returns(HASHED_TOKEN); |
| 113 | + |
| 114 | + const authorized = await checkCodeForUser({ |
| 115 | + user: userWithBypassToken, |
| 116 | + connection: { ...connection, token: undefined }, |
| 117 | + options: {}, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(authorized).to.be.equal(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should still require a second factor when the token cannot be resolved from either source', async () => { |
| 124 | + // Regression guard: this is the buggy state (#38017) the fix addresses. |
| 125 | + getLoginTokenStub.returns(undefined); |
| 126 | + |
| 127 | + await expect( |
| 128 | + checkCodeForUser({ |
| 129 | + user: userWithBypassToken, |
| 130 | + connection: { ...connection, token: undefined }, |
| 131 | + options: {}, |
| 132 | + }), |
| 133 | + ).to.be.rejectedWith('TOTP Required'); |
| 134 | + }); |
| 135 | +}); |
0 commit comments