From 838be232ffa310e8f10101111ac56a7c5fd26bdc Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Tue, 7 Dec 2021 10:57:11 -0300 Subject: [PATCH] [FIX] Certificate stops working after app update on iOS (#3537) --- app/utils/sslPinning.js | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/app/utils/sslPinning.js b/app/utils/sslPinning.js index 50f944e6..c3e2128c 100644 --- a/app/utils/sslPinning.js +++ b/app/utils/sslPinning.js @@ -7,6 +7,21 @@ import I18n from '../i18n'; import { extractHostname } from './server'; const { SSLPinning } = NativeModules; +const { documentDirectory } = FileSystem; + +const extractFileScheme = path => path.replace('file://', ''); // file:// isn't allowed by obj-C + +const getPath = name => `${documentDirectory}/${name}`; + +const persistCertificate = async (name, password) => { + const certificatePath = getPath(name); + const certificate = { + path: extractFileScheme(certificatePath), + password + }; + await UserPreferences.setMapAsync(name, certificate); + return certificate; +}; const RCSSLPinning = Platform.select({ ios: { @@ -25,17 +40,9 @@ const RCSSLPinning = Platform.select({ text: 'OK', onPress: async password => { try { - const certificatePath = `${FileSystem.documentDirectory}/${name}`; - + const certificatePath = getPath(name); await FileSystem.copyAsync({ from: uri, to: certificatePath }); - - const certificate = { - path: certificatePath.replace('file://', ''), // file:// isn't allowed by obj-C - password - }; - - await UserPreferences.setMapAsync(name, certificate); - + await persistCertificate(name, password); resolve(name); } catch (e) { reject(e); @@ -49,16 +56,19 @@ const RCSSLPinning = Platform.select({ reject(e); } }), - setCertificate: async (alias, server) => { - if (alias) { - const certificate = await UserPreferences.getMapAsync(alias); + setCertificate: async (name, server) => { + if (name) { + let certificate = await UserPreferences.getMapAsync(name); + if (!certificate.path.match(extractFileScheme(documentDirectory))) { + certificate = await persistCertificate(name, certificate.password); + } await UserPreferences.setMapAsync(extractHostname(server), certificate); } } }, android: { pickCertificate: () => SSLPinning?.pickCertificate(), - setCertificate: alias => SSLPinning?.setCertificate(alias) + setCertificate: name => SSLPinning?.setCertificate(name) } });