mattermost-mobile/app/utils/security.test.ts
Elias Nahum 44eb76bed7
feat(iOS): Add Microsoft Intune MAM integration with multi-server support (#9312)
* refactor: implement custom ExpoImage wrapper for cache control

Add ExpoImage component with automatic cacheKey/cachePath management and replace all expo-image imports across the app

* refactor(ios): convert Gekidou to CocoaPods

Migrate from Swift Package Manager to CocoaPods, add Keychain write operations, refactor notification handler to remove react-native-notifications headers, and upgrade Swift to 5.0

* npm audit

* update fastlane

* feat(ci): integrate Intune MAM for enterprise builds with strict OSS protection

Add Intune submodule, CI actions, Fastlane configuration, developer scripts, pre-commit hooks, and validation workflows to enable internal MAM builds while protecting OSS repository

* fix tests by mocking @mattermost/intune

* feat: implement Intune MAM integration with comprehensive security enforcement

Add IntuneManager, refactor SecurityManager/SessionManager for MAM policies, implement native OIDC auth flow, add biometric enforcement, conditional launch blocking, and file protection controls

* fix alerts when no server database is present

* Unify cache strategy

* fix emit config changed after it was stored in the db

* Handle Mid-Session Enrollment Detection

* fix ADALLogOverrideDisabled missing in Fastfile

* fix flow for initial enrollment

* fix and add unit tests

* enable Intune configuration for PR and beta builds, CLIENT_ID should be changed before actual release

* Update intune submodule with addressed feedback

* fix validate-intune-clean workflow

* feat(intune): add comprehensive error handling and SAML+Entra support

Add production-ready error handling for native Entra authentication with
user-friendly i18n messages, comprehensive test coverage, and support for
Entra login when server requires SAML.

* update i18n

* update intune submodule

* update build-pr token

* fix race condition between server auth and intune enrollment

* fix CI workflow to build with intune

* use deploy key for intune submodule

* set the config directly in the submodule .git

* debug injection

* try setting GIT_SSH_COMMAND

* remove action debug

* fix server url input

* match pod cache with intune hash

* Fastfile and envs

* have workflows check for intune/.git

* have ci cache intune frameworks as well

* update Fastlane to set no-cache to artifacts uploaded

* fix s3 upload

* fix pblist template

* Attempt to remove the cache control for PR uploads to s3

* use hash from commit for S3 path

* Implement crash-resilient selective wipe with automatic retry and add removeInternetPassword to Gekidou Keychain

* Fix surface errors from intune login

* fix postinstall scripts

* use cacheKey for draft md images

* remove unnecessary double await during test

* Have isMinimumLicenseTier accept valid license sku tier as target

* Add missing Auth error messages

* remove the last period for intune errors in i18n

* do not call unenroll with wipe on manual logout

* Fix tests and Intune error messages

* do not filter any SSO type regardless of which is used for Intune

* fix 412 to not retry

* fix tests, app logs sharing and share_extension avatar cache

* apply setScreenCapturePolicy on license change

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>

* re-apply screen capture on enrollment

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>

* use userData from intunr login and prevent getMe

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>

* Check for Biometrics and Jailbreak as we used to

---------

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>
2025-12-10 13:07:28 +02:00

183 lines
6.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import CookieManager from '@react-native-cookies/cookies';
import {Platform} from 'react-native';
import {clearCookies, clearCookiesForServer, getCSRFFromCookie, urlSafeBase64Encode} from './security';
// Mock CookieManager
jest.mock('@react-native-cookies/cookies', () => ({
get: jest.fn(),
clearByName: jest.fn(),
flush: jest.fn(),
}));
describe('getCSRFFromCookie function', () => {
afterEach(() => {
jest.clearAllMocks(); // Clear all mock calls after each test
});
it('should return MMCSRF value from cookies', async () => {
const url = 'https://example.com';
const mockCookies = {
MMCSRF: {value: 'mock_CSRF_token'},
};
(CookieManager.get as jest.Mock).mockResolvedValue(mockCookies);
const result = await getCSRFFromCookie(url);
expect(CookieManager.get).toHaveBeenCalledWith(url, false);
expect(result).toEqual('mock_CSRF_token');
});
it('should return undefined if MMCSRF value is not found', async () => {
const url = 'https://example.com';
const mockCookies = {};
(CookieManager.get as jest.Mock).mockResolvedValue(mockCookies);
const result = await getCSRFFromCookie(url);
expect(CookieManager.get).toHaveBeenCalledWith(url, false);
expect(result).toBeUndefined();
});
});
describe('urlSafeBase64Encode function', () => {
it('should encode a string to URL-safe Base64', () => {
const input = 'Hello, World!';
const expectedOutput = 'SGVsbG8sIFdvcmxkIQ==';
const result = urlSafeBase64Encode(input);
expect(result).toEqual(expectedOutput);
});
it('should handle special characters in the input string', () => {
const input = 'a+b/c=d';
const expectedOutput = 'YStiL2M9ZA==';
const result = urlSafeBase64Encode(input);
expect(result).toEqual(expectedOutput);
});
it('should handle empty input', () => {
const input = '';
const expectedOutput = '';
const result = urlSafeBase64Encode(input);
expect(result).toEqual(expectedOutput);
});
});
describe('clearCookies function', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should clear all cookies for the server URL', async () => {
const serverUrl = 'https://example.com';
const mockCookies = {
MMCSRF: {name: 'MMCSRF'},
MMAUTHTOKEN: {name: 'MMAUTHTOKEN'},
MMUSERID: {name: 'MMUSERID'},
};
(CookieManager.get as jest.Mock).mockResolvedValue(mockCookies);
(CookieManager.clearByName as jest.Mock).mockResolvedValue(true);
await clearCookies(serverUrl, false);
expect(CookieManager.get).toHaveBeenCalledWith(serverUrl, false);
expect(CookieManager.clearByName).toHaveBeenCalledWith(serverUrl, 'MMCSRF', false);
expect(CookieManager.clearByName).toHaveBeenCalledWith(serverUrl, 'MMAUTHTOKEN', false);
expect(CookieManager.clearByName).toHaveBeenCalledWith(serverUrl, 'MMUSERID', false);
expect(CookieManager.clearByName).toHaveBeenCalledTimes(3);
});
it('should use webKit parameter when clearing cookies', async () => {
const serverUrl = 'https://example.com';
const mockCookies = {
MMCSRF: {name: 'MMCSRF'},
};
(CookieManager.get as jest.Mock).mockResolvedValue(mockCookies);
(CookieManager.clearByName as jest.Mock).mockResolvedValue(true);
await clearCookies(serverUrl, true);
expect(CookieManager.get).toHaveBeenCalledWith(serverUrl, true);
expect(CookieManager.clearByName).toHaveBeenCalledWith(serverUrl, 'MMCSRF', true);
});
it('should handle when no cookies exist', async () => {
const serverUrl = 'https://example.com';
const mockCookies = {};
(CookieManager.get as jest.Mock).mockResolvedValue(mockCookies);
await clearCookies(serverUrl, false);
expect(CookieManager.get).toHaveBeenCalledWith(serverUrl, false);
expect(CookieManager.clearByName).not.toHaveBeenCalled();
});
it('should handle errors gracefully', async () => {
const serverUrl = 'https://example.com';
const mockCookies = {
MMCSRF: {name: 'MMCSRF'},
};
(CookieManager.get as jest.Mock).mockResolvedValue(mockCookies);
(CookieManager.clearByName as jest.Mock).mockRejectedValue(new Error('Clear failed'));
// Should not throw
await expect(clearCookies(serverUrl, false)).resolves.not.toThrow();
expect(CookieManager.clearByName).toHaveBeenCalledWith(serverUrl, 'MMCSRF', false);
});
});
describe('clearCookiesForServer function', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should clear cookies for iOS platform', async () => {
Platform.OS = 'ios';
const serverUrl = 'https://example.com';
const mockCookies = {
MMCSRF: {name: 'MMCSRF'},
};
(CookieManager.get as jest.Mock).mockResolvedValue(mockCookies);
(CookieManager.clearByName as jest.Mock).mockResolvedValue(true);
await clearCookiesForServer(serverUrl);
// Should be called twice for iOS - once with webKit false, once with true
expect(CookieManager.get).toHaveBeenCalledWith(serverUrl, false);
expect(CookieManager.get).toHaveBeenCalledWith(serverUrl, true);
expect(CookieManager.clearByName).toHaveBeenCalledWith(serverUrl, 'MMCSRF', false);
expect(CookieManager.clearByName).toHaveBeenCalledWith(serverUrl, 'MMCSRF', true);
expect(CookieManager.clearByName).toHaveBeenCalledTimes(2);
});
it('should flush cookies for Android platform', async () => {
Platform.OS = 'android';
const serverUrl = 'https://example.com';
(CookieManager.flush as jest.Mock).mockResolvedValue(true);
await clearCookiesForServer(serverUrl);
// On Android, it should only flush (not clear individual cookies)
expect(CookieManager.flush).toHaveBeenCalled();
expect(CookieManager.get).not.toHaveBeenCalled();
expect(CookieManager.clearByName).not.toHaveBeenCalled();
});
});