11package com.freeraspreactnative
22
3+ import com.aheaditec.talsec_security.security.api.SuspiciousAppInfo
34import com.aheaditec.talsec_security.security.api.Talsec
45import com.aheaditec.talsec_security.security.api.TalsecConfig
56import com.aheaditec.talsec_security.security.api.ThreatListener
@@ -12,8 +13,14 @@ import com.facebook.react.bridge.ReadableMap
1213import com.facebook.react.bridge.UiThreadUtil.runOnUiThread
1314import com.facebook.react.bridge.WritableArray
1415import com.facebook.react.modules.core.DeviceEventManagerModule
15-
16- class FreeraspReactNativeModule (val reactContext : ReactApplicationContext ) :
16+ import com.freeraspreactnative.utils.getArraySafe
17+ import com.freeraspreactnative.utils.getBooleanSafe
18+ import com.freeraspreactnative.utils.getMapThrowing
19+ import com.freeraspreactnative.utils.getNestedArraySafe
20+ import com.freeraspreactnative.utils.getStringThrowing
21+ import com.freeraspreactnative.utils.toEncodedWritableArray
22+
23+ class FreeraspReactNativeModule (private val reactContext : ReactApplicationContext ) :
1724 ReactContextBaseJavaModule (reactContext) {
1825
1926 private val listener = ThreatListener (FreeraspThreatHandler , FreeraspThreatHandler )
@@ -42,8 +49,7 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
4249
4350 promise.resolve(" freeRASP started" )
4451
45- }
46- catch (e: Exception ) {
52+ } catch (e: Exception ) {
4753 promise.reject(" TalsecInitializationError" , e.message, e)
4854 }
4955 }
@@ -65,6 +71,7 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
6571 val channelData: WritableArray = Arguments .createArray()
6672 channelData.pushString(THREAT_CHANNEL_NAME )
6773 channelData.pushString(THREAT_CHANNEL_KEY )
74+ channelData.pushString(MALWARE_CHANNEL_KEY )
6875 promise.resolve(channelData)
6976 }
7077
@@ -87,6 +94,15 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
8794 // Remove upstream listeners, stop unnecessary background tasks
8895 }
8996
97+ /* *
98+ * Method to add apps to Malware whitelist, so they don't get flagged as malware
99+ */
100+ @ReactMethod
101+ fun addToWhitelist (packageName : String , promise : Promise ) {
102+ Talsec .addToWhitelist(reactContext, packageName)
103+ promise.resolve(true )
104+ }
105+
90106 private fun buildTalsecConfig (config : ReadableMap ): TalsecConfig {
91107 val androidConfig = config.getMapThrowing(" androidConfig" )
92108 val packageName = androidConfig.getStringThrowing(" packageName" )
@@ -97,6 +113,14 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
97113 .supportedAlternativeStores(androidConfig.getArraySafe(" supportedAlternativeStores" ))
98114 .prod(config.getBooleanSafe(" isProd" ))
99115
116+ if (androidConfig.hasKey(" malwareConfig" )) {
117+ val malwareConfig = androidConfig.getMapThrowing(" malwareConfig" )
118+ talsecBuilder.whitelistedInstallationSources(malwareConfig.getArraySafe(" whitelistedInstallationSources" ))
119+ talsecBuilder.blacklistedHashes(malwareConfig.getArraySafe(" blacklistedHashes" ))
120+ talsecBuilder.blacklistedPackageNames(malwareConfig.getArraySafe(" blacklistedPackageNames" ))
121+ talsecBuilder.suspiciousPermissions(malwareConfig.getNestedArraySafe(" suspiciousPermissions" ))
122+ }
123+
100124 return talsecBuilder.build()
101125 }
102126
@@ -106,6 +130,8 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
106130 .toString() // name of the channel over which threat callbacks are sent
107131 val THREAT_CHANNEL_KEY = (10000 .. 999999999 ).random()
108132 .toString() // key of the argument map under which threats are expected
133+ val MALWARE_CHANNEL_KEY = (10000 .. 999999999 ).random()
134+ .toString() // key of the argument map under which malware data is expected
109135 private lateinit var appReactContext: ReactApplicationContext
110136 private fun notifyListeners (threat : Threat ) {
111137 val params = Arguments .createMap()
@@ -114,11 +140,30 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
114140 .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
115141 .emit(THREAT_CHANNEL_NAME , params)
116142 }
143+
144+ /* *
145+ * Sends malware detected event to React Native
146+ */
147+ private fun notifyMalware (suspiciousApps : MutableList <SuspiciousAppInfo >) {
148+ val params = Arguments .createMap()
149+ params.putInt(THREAT_CHANNEL_KEY , Threat .Malware .value)
150+ params.putArray(
151+ MALWARE_CHANNEL_KEY , suspiciousApps.toEncodedWritableArray(appReactContext)
152+ )
153+
154+ appReactContext
155+ .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
156+ .emit(THREAT_CHANNEL_NAME , params)
157+ }
117158 }
118159
119160 internal object ThreatListener : FreeraspThreatHandler.TalsecReactNative {
120161 override fun threatDetected (threatType : Threat ) {
121162 notifyListeners(threatType)
122163 }
164+
165+ override fun malwareDetected (suspiciousApps : MutableList <SuspiciousAppInfo >) {
166+ notifyMalware(suspiciousApps)
167+ }
123168 }
124169}
0 commit comments