Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

136 lines
3.5 KiB
TypeScript

import { expect } from 'chai';
import { afterEach, beforeEach, describe, it } from 'mocha';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
const settingsMock = sinon.stub();
const getLoginTokenStub = sinon.stub();
class TOTPCheckMock {
name = 'totp';
isEnabled() {
return true;
}
async processInvalidCode() {
return {};
}
async verify() {
return false;
}
async maxFaildedAttemtpsReached() {
return false;
}
}
class DisabledCheckMock {
name = 'email';
isEnabled() {
return false;
}
}
class MeteorErrorMock extends Error {
error: string;
details: unknown;
constructor(error: string, reason?: string, details?: unknown) {
super(reason);
this.error = error;
this.details = details;
}
}
const { checkCodeForUser } = proxyquire.noCallThru().load('./index', {
'./TOTPCheck': { TOTPCheck: TOTPCheckMock },
'./EmailCheck': { EmailCheck: DisabledCheckMock },
'./PasswordCheckFallback': { PasswordCheckFallback: class extends DisabledCheckMock {} },
'../../shared/getModifiedHttpHeaders': { normalizeHeaders: (headers: unknown) => headers },
'../../../settings': { settings: { get: settingsMock } },
'@rocket.chat/models': {
Users: {
findOneById: async () => null,
setTwoFactorAuthorizationHashAndUntilForUserIdAndToken: async () => undefined,
},
},
'meteor/accounts-base': { Accounts: { _getLoginToken: getLoginTokenStub } },
'meteor/meteor': { Meteor: { Error: MeteorErrorMock } },
});
const HASHED_TOKEN = 'hashed-login-token';
const userWithBypassToken = {
_id: 'user-id',
services: { resume: { loginTokens: [{ hashedToken: HASHED_TOKEN, bypassTwoFactor: true }] } },
} as any;
const connection = {
id: 'connection-id',
clientAddress: '127.0.0.1',
httpHeaders: {},
} as any;
describe('checkCodeForUser - bypassTwoFactor token resolution (SUP-1064)', () => {
let testMode: string | undefined;
beforeEach(() => {
// TEST_MODE short-circuits the whole check; remove it so the token resolution path runs.
testMode = process.env.TEST_MODE;
delete process.env.TEST_MODE;
settingsMock.reset();
settingsMock.withArgs('Accounts_TwoFactorAuthentication_Enabled').returns(true);
getLoginTokenStub.reset();
});
afterEach(() => {
if (testMode !== undefined) {
process.env.TEST_MODE = testMode;
}
});
it('should honor a bypassTwoFactor token resolved from the REST connection (connection.token)', async () => {
// REST: the token is not registered in account data, only carried on the connection.
getLoginTokenStub.returns(undefined);
const authorized = await checkCodeForUser({
user: userWithBypassToken,
connection: { ...connection, token: HASHED_TOKEN },
options: {},
});
expect(authorized).to.be.equal(true);
});
it('should honor a bypassTwoFactor token resolved from account data (DDP, _getLoginToken)', async () => {
// DDP: the token is registered in Accounts._accountData and read via _getLoginToken.
getLoginTokenStub.returns(HASHED_TOKEN);
const authorized = await checkCodeForUser({
user: userWithBypassToken,
connection: { ...connection, token: undefined },
options: {},
});
expect(authorized).to.be.equal(true);
});
it('should still require a second factor when the token cannot be resolved from either source', async () => {
// Regression guard: this is the buggy state (#38017) the fix addresses.
getLoginTokenStub.returns(undefined);
await expect(
checkCodeForUser({
user: userWithBypassToken,
connection: { ...connection, token: undefined },
options: {},
}),
).to.be.rejectedWith('TOTP Required');
});
});