@@ -5,10 +5,9 @@ import TalsecRuntime
55class FreeraspReactNative : RCTEventEmitter {
66
77 public static var shared : FreeraspReactNative ?
8-
8+
99 let threatChannelKey = String ( Int . random ( in: 100_000 ..< 999_999_999 ) ) // key of the argument map under which threats are expected
1010 let threatChannelName = String ( Int . random ( in: 100_000 ..< 999_999_999 ) ) // name of the channel over which threat callbacks are sent
11- let threatIdentifierList = ( 1 ... 12 ) . map { _ in Int . random ( in: 100_000 ..< 999_999_999 ) }
1211
1312 override init ( ) {
1413 super. init ( )
@@ -45,27 +44,27 @@ class FreeraspReactNative: RCTEventEmitter {
4544 let config = TalsecConfig ( appBundleIds: [ appBundleIds] , appTeamId: appTeamId, watcherMailAddress: watcherMailAddress, isProd: isProd)
4645 Talsec . start ( config: config)
4746 }
48-
47+
4948 /**
5049 * Method to setup the message passing between native and React Native
5150 */
5251 @objc ( getThreatChannelData: withRejecter: )
5352 private func getThreatChannelData( resolve: RCTPromiseResolveBlock , rejecter reject: RCTPromiseRejectBlock ) -> Void {
5453 resolve ( [ threatChannelName, threatChannelKey] )
5554 }
56-
55+
5756 func dispatchEvent( securityThreat: SecurityThreat ) -> Void {
5857 FreeraspReactNative . shared!. sendEvent ( withName: threatChannelName, body: [ threatChannelKey: securityThreat. callbackIdentifier] )
5958 }
60-
59+
6160 /**
6261 * Method to get the random identifiers of callbacks
6362 */
6463 @objc ( getThreatIdentifiers: withRejecter: )
6564 private func getThreatIdentifiers( resolve: RCTPromiseResolveBlock , rejecter reject: RCTPromiseRejectBlock ) -> Void {
6665 resolve ( getThreatIdentifiers ( ) )
6766 }
68-
67+
6968 /**
7069 * We never send an invalid callback over our channel.
7170 * Therefore, if this happens, we want to kill the app.
@@ -74,7 +73,7 @@ class FreeraspReactNative: RCTEventEmitter {
7473 private func onInvalidCallback( ) -> Void {
7574 abort ( )
7675 }
77-
76+
7877 private func getThreatIdentifiers( ) -> [ Int ] {
7978 return SecurityThreat . allCases
8079 . filter {
@@ -90,33 +89,39 @@ class FreeraspReactNative: RCTEventEmitter {
9089 }
9190}
9291
92+ struct ThreatIdentifiers {
93+ static let threatIdentifierList : [ Int ] = ( 1 ... 12 ) . map { _ in Int . random ( in: 100_000 ..< 999_999_999 ) }
94+ }
95+
9396/// An extension to unify callback names with RN ones.
9497extension SecurityThreat {
9598
9699 var callbackIdentifier : Int {
97100 switch self {
98101 case . signature:
99- return FreeraspReactNative . shared! . threatIdentifierList [ 0 ]
102+ return ThreatIdentifiers . threatIdentifierList [ 0 ]
100103 case . jailbreak:
101- return FreeraspReactNative . shared! . threatIdentifierList [ 1 ]
104+ return ThreatIdentifiers . threatIdentifierList [ 1 ]
102105 case . debugger:
103- return FreeraspReactNative . shared! . threatIdentifierList [ 2 ]
106+ return ThreatIdentifiers . threatIdentifierList [ 2 ]
104107 case . runtimeManipulation:
105- return FreeraspReactNative . shared! . threatIdentifierList [ 3 ]
108+ return ThreatIdentifiers . threatIdentifierList [ 3 ]
106109 case . passcode:
107- return FreeraspReactNative . shared! . threatIdentifierList [ 4 ]
110+ return ThreatIdentifiers . threatIdentifierList [ 4 ]
108111 case . passcodeChange:
109- return FreeraspReactNative . shared! . threatIdentifierList [ 5 ]
112+ return ThreatIdentifiers . threatIdentifierList [ 5 ]
110113 case . simulator:
111- return FreeraspReactNative . shared! . threatIdentifierList [ 6 ]
114+ return ThreatIdentifiers . threatIdentifierList [ 6 ]
112115 case . missingSecureEnclave:
113- return FreeraspReactNative . shared!. threatIdentifierList [ 7 ]
116+ return ThreatIdentifiers . threatIdentifierList [ 7 ]
117+ case . systemVPN:
118+ return ThreatIdentifiers . threatIdentifierList [ 8 ]
114119 case . deviceChange:
115- return FreeraspReactNative . shared! . threatIdentifierList [ 8 ]
120+ return ThreatIdentifiers . threatIdentifierList [ 9 ]
116121 case . deviceID:
117- return FreeraspReactNative . shared! . threatIdentifierList [ 9 ]
122+ return ThreatIdentifiers . threatIdentifierList [ 10 ]
118123 case . unofficialStore:
119- return FreeraspReactNative . shared! . threatIdentifierList [ 10 ]
124+ return ThreatIdentifiers . threatIdentifierList [ 11 ]
120125 @unknown default :
121126 abort ( )
122127 }
0 commit comments