From 59c8cbed55e580bb19187382edfc984f067c504b Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Mon, 2 Dec 2019 21:33:09 -0300 Subject: [PATCH] MM-20780 MM-20782 Clear cookies on logout (#3645) --- app/init/global_event_handler.js | 24 +++++++++++++++++++++++- app/init/global_event_handler.test.js | 7 ++++++- app/screens/sso/sso.js | 16 +++------------- app/utils/file.js | 1 + test/setup.js | 5 +++-- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/app/init/global_event_handler.js b/app/init/global_event_handler.js index fec276468..d79faf717 100644 --- a/app/init/global_event_handler.js +++ b/app/init/global_event_handler.js @@ -2,7 +2,9 @@ // See LICENSE.txt for license information. import {Alert, AppState, Dimensions, Linking, NativeModules, Platform} from 'react-native'; +import CookieManager from 'react-native-cookies'; import DeviceInfo from 'react-native-device-info'; +import RNFetchBlob from 'rn-fetch-blob'; import semver from 'semver'; import {setAppState, setServerVersion} from 'mattermost-redux/actions/general'; @@ -22,6 +24,7 @@ import {showOverlay} from 'app/actions/navigation'; import {loadConfigAndLicense, setDeepLinkURL, startDataCleanup} from 'app/actions/views/root'; import {NavigationTypes, ViewTypes} from 'app/constants'; import {getTranslations, resetMomentLocale} from 'app/i18n'; +import mattermostBucket from 'app/mattermost_bucket'; import mattermostManaged from 'app/mattermost_managed'; import PushNotifications from 'app/push_notifications'; import {getCurrentLocale} from 'app/selectors/i18n'; @@ -148,11 +151,30 @@ class GlobalEventHandler { onLogout = async () => { this.store.dispatch(closeWebSocket(false)); this.store.dispatch(setServerVersion('')); - deleteFileCache(); removeAppCredentials(); + deleteFileCache(); resetMomentLocale(); + // TODO: Handle when multi-server support is added + CookieManager.clearAll(Platform.OS === 'ios'); PushNotifications.clearNotifications(); + const cacheDir = RNFetchBlob.fs.dirs.CacheDir; + const mainPath = cacheDir.split('/').slice(0, -1).join('/'); + await RNFetchBlob.fs.unlink(cacheDir); + mattermostBucket.removePreference('cert'); + if (Platform.OS === 'ios') { + mattermostBucket.removeFile('entities'); + } else { + const cookies = await RNFetchBlob.fs.exists(`${mainPath}/app_webview/Cookies`); + const cookiesJ = await RNFetchBlob.fs.exists(`${mainPath}/app_webview/Cookies-journal`); + if (cookies) { + RNFetchBlob.fs.unlink(`${mainPath}/app_webview/Cookies`); + } + + if (cookiesJ) { + RNFetchBlob.fs.unlink(`${mainPath}/app_webview/Cookies-journal`); + } + } if (this.launchApp) { this.launchApp(); diff --git a/app/init/global_event_handler.test.js b/app/init/global_event_handler.test.js index 4d9649643..55faad433 100644 --- a/app/init/global_event_handler.test.js +++ b/app/init/global_event_handler.test.js @@ -6,6 +6,7 @@ import thunk from 'redux-thunk'; import intitialState from 'app/initial_state'; import PushNotification from 'app/push_notifications'; +import mattermostBucket from 'app/mattermost_bucket'; import * as I18n from 'app/i18n'; import GlobalEventHandler from './global_event_handler'; @@ -43,10 +44,14 @@ describe('GlobalEventHandler', () => { it('should clear notifications and reset moment locale on logout', async () => { const clearNotifications = jest.spyOn(PushNotification, 'clearNotifications'); const resetMomentLocale = jest.spyOn(I18n, 'resetMomentLocale'); + const removePreference = jest.spyOn(mattermostBucket, 'removePreference'); + const removeFile = jest.spyOn(mattermostBucket, 'removeFile'); await GlobalEventHandler.onLogout(); expect(clearNotifications).toHaveBeenCalled(); - expect(resetMomentLocale).toHaveBeenCalledWith(); + expect(resetMomentLocale).toHaveBeenCalled(); + expect(removePreference).toHaveBeenCalledWith('cert'); + expect(removeFile).toHaveBeenCalledWith('entities'); }); it('should call onAppStateChange after configuration', () => { diff --git a/app/screens/sso/sso.js b/app/screens/sso/sso.js index 51fb02f38..d3d08a286 100644 --- a/app/screens/sso/sso.js +++ b/app/screens/sso/sso.js @@ -5,7 +5,6 @@ import React, {PureComponent} from 'react'; import PropTypes from 'prop-types'; import {injectIntl, intlShape} from 'react-intl'; import { - InteractionManager, Text, View, Platform, @@ -81,7 +80,7 @@ class SSO extends PureComponent { this.state = { error: null, - renderWebView: false, + renderWebView: true, jsCode: '', messagingEnabled: false, }; @@ -106,16 +105,6 @@ class SSO extends PureComponent { } } - componentDidMount() { - InteractionManager.runAfterInteractions(this.clearPreviousCookies); - } - - clearPreviousCookies = () => { - CookieManager.clearAll(this.useWebkit).then(() => { - this.setState({renderWebView: true}); - }); - }; - goToChannel = () => { tracker.initialLoad = Date.now(); @@ -232,7 +221,8 @@ class SSO extends PureComponent { onLoadEnd={this.onLoadEnd} onMessage={messagingEnabled ? this.onMessage : null} useSharedProcessPool={true} - cacheEnabled={true} + sharedCookiesEnabled={true} + cacheEnabled={false} /> ); } diff --git a/app/utils/file.js b/app/utils/file.js index 6d7f022ae..4a77576bd 100644 --- a/app/utils/file.js +++ b/app/utils/file.js @@ -103,6 +103,7 @@ export async function deleteFileCache() { if (isVideosDir) { await RNFetchBlob.fs.unlink(VIDEOS_PATH); } + return true; } diff --git a/test/setup.js b/test/setup.js index 1833f6182..40cc4d34e 100644 --- a/test/setup.js +++ b/test/setup.js @@ -117,6 +117,7 @@ jest.mock('react-native-cookies', () => ({ openURL: jest.fn(), canOpenURL: jest.fn(), getInitialURL: jest.fn(), + clearAll: jest.fn(), })); jest.mock('react-native-navigation', () => { @@ -201,7 +202,7 @@ jest.mock('rn-fetch-blob', () => ({ fs: { dirs: { DocumentDir: () => jest.fn(), - CacheDir: () => jest.fn(), + CacheDir: '/data/com.mattermost.beta/cache', }, exists: jest.fn(), existsWithDiffExt: jest.fn(), @@ -215,7 +216,7 @@ jest.mock('rn-fetch-blob', () => ({ jest.mock('rn-fetch-blob/fs', () => ({ dirs: { DocumentDir: () => jest.fn(), - CacheDir: () => jest.fn(), + CacheDir: '/data/com.mattermost.beta/cache', }, exists: jest.fn(), existsWithDiffExt: jest.fn(),