react-native-device-defense 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +106 -19
  2. package/android/build.gradle +2 -2
  3. package/android/src/main/cpp/device-security.cpp +281 -7
  4. package/android/src/main/java/{vn/osp/security → com/devicedefense}/DebugDetection.kt +1 -1
  5. package/android/src/main/java/{vn/osp/security → com/devicedefense}/DeviceSecurityModule.kt +127 -3
  6. package/android/src/main/java/{vn/osp/security → com/devicedefense}/DeviceSecurityPackage.kt +1 -1
  7. package/android/src/main/java/{vn/osp/security → com/devicedefense}/EmulatorDetection.kt +30 -19
  8. package/android/src/main/java/{vn/osp/security → com/devicedefense}/HookDetection.kt +1 -1
  9. package/android/src/main/java/{vn/osp/security → com/devicedefense}/NativeSecurityCheck.kt +44 -2
  10. package/android/src/main/java/{vn/osp/security → com/devicedefense}/RootDetection.kt +1 -1
  11. package/android/test-gradle.gradle +3 -0
  12. package/lib/commonjs/NativeDeviceSecurity.js.map +1 -1
  13. package/lib/commonjs/api.js +137 -2
  14. package/lib/commonjs/api.js.map +1 -1
  15. package/lib/commonjs/components/SecurityBlockedScreen.js +12 -2
  16. package/lib/commonjs/components/SecurityBlockedScreen.js.map +1 -1
  17. package/lib/commonjs/hooks/useDeviceSecurity.js +13 -7
  18. package/lib/commonjs/hooks/useDeviceSecurity.js.map +1 -1
  19. package/lib/module/NativeDeviceSecurity.js.map +1 -1
  20. package/lib/module/api.js +137 -2
  21. package/lib/module/api.js.map +1 -1
  22. package/lib/module/components/SecurityBlockedScreen.js +12 -2
  23. package/lib/module/components/SecurityBlockedScreen.js.map +1 -1
  24. package/lib/module/hooks/useDeviceSecurity.js +14 -8
  25. package/lib/module/hooks/useDeviceSecurity.js.map +1 -1
  26. package/lib/typescript/NativeDeviceSecurity.d.ts +7 -0
  27. package/lib/typescript/NativeDeviceSecurity.d.ts.map +1 -1
  28. package/lib/typescript/api.d.ts +29 -1
  29. package/lib/typescript/api.d.ts.map +1 -1
  30. package/lib/typescript/components/SecurityBlockedScreen.d.ts.map +1 -1
  31. package/lib/typescript/hooks/useDeviceSecurity.d.ts.map +1 -1
  32. package/lib/typescript/types.d.ts +30 -1
  33. package/lib/typescript/types.d.ts.map +1 -1
  34. package/package.json +5 -3
  35. package/react-native-device-security.podspec +4 -4
  36. package/src/NativeDeviceSecurity.ts +9 -0
  37. package/src/api.ts +143 -0
  38. package/src/components/SecurityBlockedScreen.tsx +10 -0
  39. package/src/hooks/useDeviceSecurity.ts +14 -11
  40. package/src/types.ts +36 -1
package/src/api.ts CHANGED
@@ -9,6 +9,7 @@ import type {
9
9
  BlockOnSecurityThreatOptions,
10
10
  SecurityStatus,
11
11
  SecurityThreat,
12
+ SSLSecurityStatus,
12
13
  } from './types';
13
14
 
14
15
  /**
@@ -52,6 +53,12 @@ class DeviceSecurity {
52
53
  hasMagisk: false,
53
54
  isDebuggable: false,
54
55
  isEmulator: false,
56
+ hasSSLValidationBypass: false,
57
+ hasSSLPinningBypass: false,
58
+ hasProxyConfiguration: false,
59
+ hasModifiedSSLLibraries: false,
60
+ hasCertificateTampering: false,
61
+ hasSSLSecurityIssue: false,
55
62
  };
56
63
  }
57
64
 
@@ -64,6 +71,38 @@ class DeviceSecurity {
64
71
  }
65
72
  }
66
73
 
74
+ /**
75
+ * Get detailed SSL security status
76
+ */
77
+ async getSSLSecurityStatus(): Promise<SSLSecurityStatus> {
78
+ if (Platform.OS !== 'android') {
79
+ // iOS: no-op for now, return secure status
80
+ return {
81
+ hasSSLValidationBypass: false,
82
+ hasSSLPinningBypass: false,
83
+ hasProxyConfiguration: false,
84
+ hasModifiedSSLLibraries: false,
85
+ hasCertificateTampering: false,
86
+ hasSSLSecurityIssue: false,
87
+ };
88
+ }
89
+
90
+ try {
91
+ const statusJson = await NativeDeviceSecurity.getSSLSecurityStatus();
92
+ return JSON.parse(statusJson) as SSLSecurityStatus;
93
+ } catch (error) {
94
+ console.error('DeviceSecurity: Error getting SSL security status', error);
95
+ return {
96
+ hasSSLValidationBypass: false,
97
+ hasSSLPinningBypass: false,
98
+ hasProxyConfiguration: false,
99
+ hasModifiedSSLLibraries: false,
100
+ hasCertificateTampering: false,
101
+ hasSSLSecurityIssue: false,
102
+ };
103
+ }
104
+ }
105
+
67
106
  /**
68
107
  * Check if device is rooted (synchronous, Android only)
69
108
  */
@@ -160,6 +199,104 @@ class DeviceSecurity {
160
199
  }
161
200
  }
162
201
 
202
+ // ===== SSL Security Methods =====
203
+
204
+ /**
205
+ * Check if SSL validation has been bypassed
206
+ */
207
+ hasSSLValidationBypass(): boolean {
208
+ if (Platform.OS !== 'android') {
209
+ return false;
210
+ }
211
+
212
+ try {
213
+ return NativeDeviceSecurity.hasSSLValidationBypass();
214
+ } catch (error) {
215
+ console.error('DeviceSecurity: Error checking SSL validation bypass', error);
216
+ return false;
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Check if SSL pinning bypass tools are present
222
+ */
223
+ hasSSLPinningBypass(): boolean {
224
+ if (Platform.OS !== 'android') {
225
+ return false;
226
+ }
227
+
228
+ try {
229
+ return NativeDeviceSecurity.hasSSLPinningBypass();
230
+ } catch (error) {
231
+ console.error('DeviceSecurity: Error checking SSL pinning bypass', error);
232
+ return false;
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Check if proxy is configured (potential MITM)
238
+ */
239
+ hasProxyConfiguration(): boolean {
240
+ if (Platform.OS !== 'android') {
241
+ return false;
242
+ }
243
+
244
+ try {
245
+ return NativeDeviceSecurity.hasProxyConfiguration();
246
+ } catch (error) {
247
+ console.error('DeviceSecurity: Error checking proxy configuration', error);
248
+ return false;
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Check if SSL libraries have been modified
254
+ */
255
+ hasModifiedSSLLibraries(): boolean {
256
+ if (Platform.OS !== 'android') {
257
+ return false;
258
+ }
259
+
260
+ try {
261
+ return NativeDeviceSecurity.hasModifiedSSLLibraries();
262
+ } catch (error) {
263
+ console.error('DeviceSecurity: Error checking modified SSL libraries', error);
264
+ return false;
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Check if certificates have been tampered with
270
+ */
271
+ hasCertificateTampering(): boolean {
272
+ if (Platform.OS !== 'android') {
273
+ return false;
274
+ }
275
+
276
+ try {
277
+ return NativeDeviceSecurity.hasCertificateTampering();
278
+ } catch (error) {
279
+ console.error('DeviceSecurity: Error checking certificate tampering', error);
280
+ return false;
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Comprehensive SSL security check
286
+ */
287
+ hasSSLSecurityIssue(): boolean {
288
+ if (Platform.OS !== 'android') {
289
+ return false;
290
+ }
291
+
292
+ try {
293
+ return NativeDeviceSecurity.hasSSLSecurityIssue();
294
+ } catch (error) {
295
+ console.error('DeviceSecurity: Error checking SSL security', error);
296
+ return false;
297
+ }
298
+ }
299
+
163
300
  /**
164
301
  * Block app when security threat detected
165
302
  * This will show an alert and potentially exit the app
@@ -215,6 +352,12 @@ class DeviceSecurity {
215
352
  hasMagisk: false,
216
353
  isDebuggable: false,
217
354
  isEmulator: false,
355
+ hasSSLValidationBypass: false,
356
+ hasSSLPinningBypass: false,
357
+ hasProxyConfiguration: false,
358
+ hasModifiedSSLLibraries: false,
359
+ hasCertificateTampering: false,
360
+ hasSSLSecurityIssue: false,
218
361
  };
219
362
  }
220
363
  }
@@ -38,6 +38,11 @@ const THREAT_TITLES: Record<SecurityThreat, string> = {
38
38
  magisk_detected: 'Phát hiện Magisk',
39
39
  debugger_detected: 'Phát hiện Debugger',
40
40
  emulator_detected: 'Phát hiện Bộ giả lập',
41
+ ssl_validation_bypass: 'SSL Validation bị bypass',
42
+ ssl_pinning_bypass: 'SSL Pinning bị bypass',
43
+ proxy_configuration: 'Phát hiện Proxy (MITM)',
44
+ modified_ssl_libraries: 'SSL Library đã sửa đổi',
45
+ certificate_tampering: 'Certificate bị can thiệp',
41
46
  };
42
47
 
43
48
  const THREAT_DESCRIPTIONS: Record<SecurityThreat, string> = {
@@ -52,6 +57,11 @@ const THREAT_DESCRIPTIONS: Record<SecurityThreat, string> = {
52
57
  magisk_detected: 'Phát hiện Magisk - công cụ root ẩn danh.',
53
58
  debugger_detected: 'Phát hiện debugger đang gắn vào ứng dụng.',
54
59
  emulator_detected: 'Ứng dụng đang chạy trên bộ giả lập, không an toàn cho môi trường production.',
60
+ ssl_validation_bypass: 'SSL certificate validation đã bị bypass, kết nối có thể không an toàn.',
61
+ ssl_pinning_bypass: 'Phát hiện công cụ SSL pinning bypass (Frida, Xposed...) - tấn công MITM có thể xảy ra.',
62
+ proxy_configuration: 'Phát hiện cấu hình proxy - có thể là dấu hiệu của tấn công MITM.',
63
+ modified_ssl_libraries: 'SSL libraries đã bị sửa đổi - có thể dẫn đến kết nối không an toàn.',
64
+ certificate_tampering: 'Phát hiện certificate đã bị can thiệp - kết nối có thể bị theo dõi.',
55
65
  };
56
66
 
57
67
  /**
@@ -2,7 +2,7 @@
2
2
  * React hook for device security
3
3
  */
4
4
 
5
- import { useCallback, useEffect, useState } from 'react';
5
+ import { useCallback, useEffect, useRef, useState } from 'react';
6
6
  import { Platform } from 'react-native';
7
7
  import deviceSecurity from '../api';
8
8
  import type {
@@ -14,12 +14,7 @@ import type {
14
14
  export function useDeviceSecurity(
15
15
  options: UseDeviceSecurityOptions = {},
16
16
  ): UseDeviceSecurityReturn {
17
- const {
18
- onSecurityThreat,
19
- blockOnThreat = false,
20
- blockOptions,
21
- checkInterval = 0,
22
- } = options;
17
+ const { checkInterval = 0 } = options;
23
18
 
24
19
  const [isSecure, setIsSecure] = useState<boolean | null>(null);
25
20
  const [isLoading, setIsLoading] = useState(true);
@@ -28,6 +23,12 @@ export function useDeviceSecurity(
28
23
  );
29
24
  const [error, setError] = useState<Error | null>(null);
30
25
 
26
+ // Use refs for options to avoid infinite loops when inline objects/functions are passed
27
+ const optionsRef = useRef(options);
28
+ useEffect(() => {
29
+ optionsRef.current = options;
30
+ }, [options]);
31
+
31
32
  const checkSecurity = useCallback(async () => {
32
33
  if (Platform.OS !== 'android') {
33
34
  setIsSecure(true);
@@ -45,14 +46,16 @@ export function useDeviceSecurity(
45
46
 
46
47
  // Handle security threats
47
48
  if (!status.isSecure) {
49
+ const currentOptions = optionsRef.current;
50
+
48
51
  // Call callback for each threat
49
52
  for (const threat of status.threats) {
50
- onSecurityThreat?.(threat, status);
53
+ currentOptions.onSecurityThreat?.(threat, status);
51
54
  }
52
55
 
53
56
  // Block if requested
54
- if (blockOnThreat) {
55
- deviceSecurity.blockOnSecurityThreat(blockOptions);
57
+ if (currentOptions.blockOnThreat) {
58
+ deviceSecurity.blockOnSecurityThreat(currentOptions.blockOptions);
56
59
  }
57
60
  }
58
61
  } catch (err) {
@@ -63,7 +66,7 @@ export function useDeviceSecurity(
63
66
  } finally {
64
67
  setIsLoading(false);
65
68
  }
66
- }, [onSecurityThreat, blockOnThreat, blockOptions]);
69
+ }, []);
67
70
 
68
71
  const recheck = useCallback(async () => {
69
72
  await checkSecurity();
package/src/types.ts CHANGED
@@ -12,7 +12,30 @@ export type SecurityThreat =
12
12
  | 'xposed_detected'
13
13
  | 'magisk_detected'
14
14
  | 'debugger_detected'
15
- | 'emulator_detected';
15
+ | 'emulator_detected'
16
+ | 'ssl_validation_bypass'
17
+ | 'ssl_pinning_bypass'
18
+ | 'proxy_configuration'
19
+ | 'modified_ssl_libraries'
20
+ | 'certificate_tampering';
21
+
22
+ /**
23
+ * SSL Security Status
24
+ */
25
+ export interface SSLSecurityStatus {
26
+ /** SSL validation has been bypassed */
27
+ hasSSLValidationBypass: boolean;
28
+ /** SSL pinning bypass tools detected */
29
+ hasSSLPinningBypass: boolean;
30
+ /** Proxy configuration detected (potential MITM) */
31
+ hasProxyConfiguration: boolean;
32
+ /** SSL libraries have been modified */
33
+ hasModifiedSSLLibraries: boolean;
34
+ /** Certificate tampering detected */
35
+ hasCertificateTampering: boolean;
36
+ /** Any SSL security issue detected */
37
+ hasSSLSecurityIssue: boolean;
38
+ }
16
39
 
17
40
  /**
18
41
  * Detailed security status
@@ -44,6 +67,18 @@ export interface SecurityStatus {
44
67
  isDebuggable: boolean;
45
68
  /** Running on emulator */
46
69
  isEmulator: boolean;
70
+ /** SSL validation has been bypassed */
71
+ hasSSLValidationBypass: boolean;
72
+ /** SSL pinning bypass tools detected */
73
+ hasSSLPinningBypass: boolean;
74
+ /** Proxy configuration detected */
75
+ hasProxyConfiguration: boolean;
76
+ /** SSL libraries have been modified */
77
+ hasModifiedSSLLibraries: boolean;
78
+ /** Certificate tampering detected */
79
+ hasCertificateTampering: boolean;
80
+ /** Any SSL security issue detected */
81
+ hasSSLSecurityIssue: boolean;
47
82
  /** Additional details about detection */
48
83
  details?: Record<string, boolean | string | number>;
49
84
  }