mattermost-mobile-upstream/app/utils/security/index.ts
Elias Nahum f00701ec9a
On iOS, resuming after the 5-minute window showed no biometric prompt — (#10102)
* On iOS, resuming after the 5-minute window showed no biometric prompt —
just a blocking alert whose only button logged the user out. A failure to
evaluate device security was indistinguishable from a verdict that the
device is not secured. Android had the same class of bug, where
user-cancel codes led to logout.

Outcomes are now classified three ways, so only a genuine credential
rejection or a genuinely unsecured device acts destructively; cancels and
indeterminate results offer a retry. SecurityManager and ManagedApp share
the policy-free parts via app/utils/security/.

Also fixes a resume stacking a second prompt behind an open alert, a
backgroundSince bypass, and a latched flag that disabled the MDM PIN.

* feedback review

* gate app state

* fix tests
2026-09-02 15:03:13 +08:00

40 lines
1.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Buffer} from 'buffer';
import CookieManager, {type Cookie} from '@preeternal/react-native-cookie-manager';
import {Platform} from 'react-native';
export async function getCSRFFromCookie(url: string) {
const cookies = await CookieManager.get(url, false);
return cookies.MMCSRF?.value;
}
export async function clearCookies(serverUrl: string, webKit: boolean) {
try {
const cookies = await CookieManager.get(serverUrl, webKit);
const values = Object.values(cookies);
values.forEach((cookie: Cookie) => {
CookieManager.clearByName(serverUrl, cookie.name, webKit);
});
} catch (error) {
// Nothing to clear
}
}
export async function clearCookiesForServer(serverUrl: string) {
if (Platform.OS === 'ios') {
clearCookies(serverUrl, false);
clearCookies(serverUrl, true);
} else if (Platform.OS === 'android') {
CookieManager.flush();
}
}
/** URL-safe base64 for cache keys and ids. UTF-8 safe (URLs may contain non-Latin1 characters). */
export const urlSafeBase64Encode = (str: string): string => {
const encoded = Buffer.from(str, 'utf8').toString('base64');
return encoded.replace(/\+/g, '-').replace(/\//g, '_');
};