Nâng CaoReact Native iconReact Native

Security trong RN — Keychain/Keystore, certificate pinning, encrypted storage?

1. Lưu sensitive data (token, password, biometric):
- iOS: Keychain — mã hóa hardware-backed, persistent qua app reinstall (option), bio-protected.
- Android: Keystore + EncryptedSharedPreferences.
- Lib: react-native-keychain hoặc expo-secure-store (cross-platform wrapper).

ts
import * as SecureStore from 'expo-secure-store'
await SecureStore.setItemAsync('authToken', token, {
  keychainAccessible: SecureStore.WHEN_UNLOCKED, // chỉ đọc khi device unlocked
})

Đừng lưu token trong AsyncStorage hay MMKV không encryption — plain text trong file system, jailbreak/root device đọc được.

2. Certificate pinning: kiểm tra public key của server cert match expected → chống MitM (Man-in-the-Middle), proxy như Charles không inspect được traffic.
- Lib: react-native-ssl-pinning hoặc react-native-cert-pinner.
- Cấu hình:

ts
import { fetch } from 'react-native-ssl-pinning'

fetch('https://api.example.com/me', {
  method: 'GET',
  sslPinning: { certs: ['cert1'] }, // file cert.cer trong assets
})
  • Pitfall: cert rotate → app cần update để pin cert mới, không OTA được. Pin public key thay cert (longer-lived) hoặc multi-pin với fallback.

3. Code obfuscation:
- Hermes bytecode đã hard hơn JS plain để reverse, nhưng vẫn decompile được.
- Tool: react-native-obfuscating-transformer (rename variable, dead code).
- Native code: ProGuard/R8 (Android), Strip Linked Product (iOS).

4. Jailbreak/Root detection:
- Lib: react-native-jail-monkey hoặc react-native-device-info.
- Check isJailBroken/isRooted → block app launch hoặc downgrade feature (vd disable banking transaction).
- Lưu ý: chỉ là deterrent, không tuyệt đối — sophisticated attacker có thể bypass.

5. App Transport Security (ATS) iOS:
- Default require HTTPS với TLS 1.2+. Không tắt cho production.
- Info.plist NSAllowsArbitraryLoads chỉ cho dev nếu cần.

6. Biometric auth:
- Lib: react-native-keychainsetGenericPassword(..., { accessible: BIOMETRY_CURRENT_SET }).
- iOS Face ID/Touch ID, Android Fingerprint/Face/Iris đều support.
- Workflow: user login lần đầu → save token + biometric flag → lần sau prompt biometric → release token từ Keychain.

Xem toàn bộ React Native cùng filter theo level & chủ đề con.

Mở danh sách React Native