mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-04 09:58:57 +00:00
* [MM-70605][MM-70607] Scope pre-auth secret storage per server The pre-auth secret was written with react-native-keychain's generic-password API using the `server` option, which those calls do not read. Every server therefore resolved to one shared entry, and a falsy secret argument deleted it, so ordinary operations such as building a client, refreshing the token header or removing another server discarded the stored value. Key the entry per server instead. On Android the alias namespace is shared with internet credentials, so the service is prefixed to keep it away from the session token; iOS uses the bare server URL, which is what the native reader in Gekidou already queries. The account value is renamed to `preauth_secret` on both sides. Writing the token no longer touches the secret: removal is explicit, via removePreauthSecret. A one-time migration adopts the previously shared entry when a single server makes ownership unambiguous, and discards it otherwise. It only deletes the old entry once the new one is confirmed stored, and defers when no server is active. Also forward the secret through the login screen to SSO, and fix loginOptions to count only enabled providers so the single-provider redirect works. Relands the `.enabled` filter from #9585 by @pltnm239. Edit Server now updates the live client's headers rather than invalidating the session, and validates with the candidate secret so a value the server rejects cannot be saved. * [MM-70605] Harden preauth secret flows and fix Edit Server Detox on iOS Address review feedback around migration markers, credential errors, and keeping re-login secrets out of nav params. Focus the empty Authentication secret field via Display Name return so iOS Detox can type into it. Co-authored-by: Cursor <cursoragent@cursor.com> * [MM-70605] Fix lines-around-comment in Edit Server Detox helper Co-authored-by: Cursor <cursoragent@cursor.com> * [MM-70605] Roll back preauth keychain when Edit Server client sync fails After the secret is stored, header apply or createClient failures left the new value in secure storage while UI state still held the old baseline. Restore the previous secret (and live header) before surfacing the error. Co-authored-by: Cursor <cursoragent@cursor.com> * [MM-70605] Preserve exact preauth secret on Edit Server rollback Avoid trimming during keychain restore so boundary whitespace from the previously stored value is not altered. Co-authored-by: Cursor <cursoragent@cursor.com> * [MM-70605] Harden preauth migration ownership and secret handoff Adopt the legacy secret only when a single server exists in the app DB, keep add-server candidates in EphemeralStore instead of nav params, and resync the live client to stored credentials when keychain rollback fails. Co-authored-by: Cursor <cursoragent@cursor.com> * [MM-70605] Discard the legacy preauth secret when no server can own it * [MM-70605] Shorten the preauth migration docblock * [MM-70605] Skip the Authorization header when there is no bearer token * [MM-70605] Surface preauth secret rejections with a specific error message * [MM-70605] Mock clearPendingPreauthSecret in session action tests * [MM-70605] Remove the unreachable catch from restorePreviousPreauthSecret setPreauthSecret and removePreauthSecret never reject, and the returned promise was not awaited, so the catch could not run. * [MM-70605] Use network-client fix for the iOS addHeaders crash Temporary pin to mattermost/react-native-network-client#175 to check CI. Replace with the released version before merge. * [MM-70605] Bump react-native-network-client to 1.11.5 1.11.5 gives each recreated iOS session its own delegate in addHeaders, which fixes the SIGTRAP on pre-auth secret changes. --------- Co-authored-by: Cursor <cursoragent@cursor.com>
151 lines
6.3 KiB
TypeScript
151 lines
6.3 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {storePreauthSecretMigrationDone} from '@actions/app/global';
|
|
import {getLegacyPreauthSecret, getPreauthSecret, removeLegacyPreauthSecret, setPreauthSecret} from '@init/credentials';
|
|
import {getPreauthSecretMigrationDone} from '@queries/app/global';
|
|
|
|
import {migrateLegacyPreauthSecret} from './credentials_migration';
|
|
|
|
jest.mock('@actions/app/global');
|
|
jest.mock('@queries/app/global');
|
|
jest.mock('@init/credentials');
|
|
|
|
describe('migrateLegacyPreauthSecret', () => {
|
|
const activeUrl = 'https://active.com';
|
|
const otherUrl = 'https://other.com';
|
|
const legacySecret = 'secret-a';
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
jest.mocked(getPreauthSecretMigrationDone).mockResolvedValue(false);
|
|
jest.mocked(getLegacyPreauthSecret).mockResolvedValue(legacySecret);
|
|
jest.mocked(getPreauthSecret).mockResolvedValue(undefined);
|
|
jest.mocked(setPreauthSecret).mockResolvedValue(true);
|
|
});
|
|
|
|
it('should adopt the legacy secret when a single server owns it', async () => {
|
|
await migrateLegacyPreauthSecret([activeUrl]);
|
|
|
|
expect(setPreauthSecret).toHaveBeenCalledTimes(1);
|
|
expect(setPreauthSecret).toHaveBeenCalledWith(activeUrl, legacySecret);
|
|
expect(removeLegacyPreauthSecret).toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should discard the secret rather than guess an owner with several servers', async () => {
|
|
await migrateLegacyPreauthSecret([activeUrl, otherUrl]);
|
|
|
|
expect(setPreauthSecret).not.toHaveBeenCalled();
|
|
expect(removeLegacyPreauthSecret).toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should do nothing when the migration already ran', async () => {
|
|
jest.mocked(getPreauthSecretMigrationDone).mockResolvedValue(true);
|
|
|
|
await migrateLegacyPreauthSecret([activeUrl]);
|
|
|
|
expect(getLegacyPreauthSecret).not.toHaveBeenCalled();
|
|
expect(setPreauthSecret).not.toHaveBeenCalled();
|
|
expect(removeLegacyPreauthSecret).not.toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should mark the migration done when there is no legacy entry', async () => {
|
|
jest.mocked(getLegacyPreauthSecret).mockResolvedValue(undefined);
|
|
|
|
await migrateLegacyPreauthSecret([activeUrl]);
|
|
|
|
expect(setPreauthSecret).not.toHaveBeenCalled();
|
|
expect(removeLegacyPreauthSecret).not.toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should not mark the migration done when the legacy read fails', async () => {
|
|
jest.mocked(getLegacyPreauthSecret).mockRejectedValue(new Error('Keystore error'));
|
|
|
|
await expect(migrateLegacyPreauthSecret([activeUrl])).resolves.not.toThrow();
|
|
|
|
expect(setPreauthSecret).not.toHaveBeenCalled();
|
|
expect(removeLegacyPreauthSecret).not.toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should discard the secret when there are no servers to own it', async () => {
|
|
await migrateLegacyPreauthSecret([]);
|
|
|
|
expect(setPreauthSecret).not.toHaveBeenCalled();
|
|
expect(removeLegacyPreauthSecret).toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should discard when inactive servers make ownership ambiguous even if only one is active', async () => {
|
|
// Callers pass every DB server URL; soft-logged-out hosts still count.
|
|
await migrateLegacyPreauthSecret([activeUrl, otherUrl]);
|
|
|
|
expect(setPreauthSecret).not.toHaveBeenCalled();
|
|
expect(removeLegacyPreauthSecret).toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should not overwrite a secret the target server already has', async () => {
|
|
jest.mocked(getPreauthSecret).mockResolvedValue('secret-b');
|
|
|
|
await migrateLegacyPreauthSecret([activeUrl]);
|
|
|
|
expect(setPreauthSecret).not.toHaveBeenCalled();
|
|
expect(removeLegacyPreauthSecret).toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should keep the legacy entry and retry later when the write is not stored', async () => {
|
|
jest.mocked(setPreauthSecret).mockResolvedValue(false);
|
|
|
|
await migrateLegacyPreauthSecret([activeUrl]);
|
|
|
|
expect(removeLegacyPreauthSecret).not.toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should keep the legacy entry and retry later when the write throws', async () => {
|
|
jest.mocked(setPreauthSecret).mockRejectedValue(new Error('Keystore error'));
|
|
|
|
await expect(migrateLegacyPreauthSecret([activeUrl])).resolves.not.toThrow();
|
|
|
|
expect(removeLegacyPreauthSecret).not.toHaveBeenCalled();
|
|
expect(storePreauthSecretMigrationDone).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should not mark the migration done when the legacy delete fails', async () => {
|
|
jest.mocked(removeLegacyPreauthSecret).mockRejectedValue(new Error('Keystore error'));
|
|
|
|
await expect(migrateLegacyPreauthSecret([activeUrl])).resolves.not.toThrow();
|
|
|
|
expect(storePreauthSecretMigrationDone).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should not mark the migration done when the flag write returns an error', async () => {
|
|
jest.mocked(storePreauthSecretMigrationDone).mockResolvedValue({error: new Error('DB error')});
|
|
|
|
await expect(migrateLegacyPreauthSecret([activeUrl])).resolves.not.toThrow();
|
|
|
|
expect(removeLegacyPreauthSecret).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should not throw when the flag write rejects', async () => {
|
|
jest.mocked(storePreauthSecretMigrationDone).mockRejectedValue(new Error('DB error'));
|
|
|
|
await expect(migrateLegacyPreauthSecret([activeUrl])).resolves.not.toThrow();
|
|
});
|
|
|
|
it('should be idempotent once the legacy entry is gone', async () => {
|
|
await migrateLegacyPreauthSecret([activeUrl]);
|
|
|
|
jest.mocked(getLegacyPreauthSecret).mockResolvedValue(undefined);
|
|
await migrateLegacyPreauthSecret([activeUrl]);
|
|
|
|
expect(setPreauthSecret).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|