@bam.tech/react-native-app-security

中文文档
1.0.0发布于 2026/04/16MITTypeScriptCommonJS零依赖近 30 天下载 6.8万

Easily implement usual security measures in React Native Expo apps

原始使用文档
文档目录9 节
  1. SSL Pinning
  2. Configuration
  3. Generating the public key hashes
  4. Testing
  5. Prevent "recent screenshots"
  6. Configuration
  7. Safe Keyboard Detector
  8. [EXPERIMENTAL - iOS only] Disable Default Caching in Cache.…
  9. Configuration

react-native-app-security 🔐

Easily implement usual security measures in React Native Expo apps

⚠️ Disclaimer
This package is intended to help implement a few basic security features but does not in itself guarantee that an app is secure.
Refer to OWASP's resources for more information on mobile app security.
You can also contact us if you need help with securing your app.

Installation and setup

This package is designed for use in expo apps with development builds.

yarn add @bam.tech/react-native-app-security

Add the config plugin to app.config.ts / app.config.js / app.json:

{
  "plugins": [
    [
      "@bam.tech/react-native-app-security",
      {
        "sslPinning": {
          "yahoo.com": [
            "TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
            "rFjc3wG7lTZe43zeYTvPq8k4xdDEutCmIhI5dn4oCeE="
          ]
        },
        "preventRecentScreenshots": {
          "ios": { "enabled": true },
          "android": { "enabled": true }
        }
      }
    ]
  ]
}

Anytime you change the config, don't forget to run:

yarn expo prebuild

Features

SSL Pinning

🥷 What's the threat? Attackers intercepting your app's network requests and accessing private data or sending malicious responses. More details

This package implements public key pinning using TrustKit on iOS and the certificate pinner included in OkHttp on Android.

Configuration

[
  "@bam.tech/react-native-app-security",
  {
    "sslPinning": {
      // The hostname you want to pin, without `https://`
      "yahoo.com": [
        // The public key hashes for the pinned certificates, without a `sha256/` prefix
        "TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
        "rFjc3wG7lTZe43zeYTvPq8k4xdDEutCmIhI5dn4oCeE="
      ]
    }
  }
]

Please note that you'll need to provide 2 public key hashes. This is to encourage having proper procedures in place to avoid locking users out, as described here in the TrustKit docs.

Pinning subdomains

To pin a specific subdomain, simply include it in the string you provide, eg:

    "sslPinning": {
      "subdomain.domain.com": [/* ... */]
    }

To pin a domain and all its subdomains, use a wildcard:

    "sslPinning": {
      // domain.com and all its subdomains will be pinned
      "*.domain.com": [/* ... */]
    }

The wildcard can only be used for the full lefmost part of the hostname.

These are invalid: *domain.com, domain.*.com, sub.*.domain.com

Generating the public key hashes

You'll need the certificates (.cer or .crt)

openssl x509 -in certificate.cer -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64

Testing

To test that SSL pinning is working as expected, you can:

  • break (change) a certificate and check that the connection fails (don't forget to yarn expo prebuild then yarn ios or yarn android to rebuild the app)
  • set up a proxy (we love Proxyman) and check that the connection fails

Prevent "recent screenshots"

🥷 What's the threat? When the OS terminates the app, it may take a screenshot and store it on the device to display in the app switcher. This screenshot could leak sensitive data

Mitigating this threat is achieved by:

Configuration

[
  "@bam.tech/react-native-app-security",
  {
    "preventRecentScreenshots": {
      "ios": { "enabled": true },
      "android": { "enabled": true }
    }
  }
]

Safe Keyboard Detector

🥷 What's the threat? A third-party keyboard might embed a malicious keylogger to record passwords and sensitive data. More details

Mitigating this threat is achieved by:

  • On Android, comparing the current keyboard id with a list of keyboard packages that we deem safe.
  • On iOS, doing nothing specific since iOS already prevent the use of third-party keyboard on sensitive fields such as passwords.
import { SafeKeyboardDetector } from "@bam.tech/react-native-app-security";

const { isInDefaultSafeList, inputMethodId } = getCurrentInputMethodInfo(); // Will always return {isInDefaultSafeList: true, inputMethodId: "iosKeyboard"} on iOS
if (!isInDefaultSafeList) {
  console.warn(`Your current keyboard (${inputMethodId}) is not safe`);
}

// Prompt the user to change the current keyboard
SafeKeyboardDetector.showInputMethodPicker(); // can only be called on Android

[EXPERIMENTAL - iOS only] Disable Default Caching in Cache.db

⚠️ DISCLAIMER: This experimental feature may impact app behavior. Use it at your own risk. Disabling caching can cause unexpected issues.

Possible side effects:

  • Slower performance due to lack of cached responses
  • Higher network usage from repeated requests
  • Crashes in components expecting cached data
  • Features failing in offline mode

🥷 Threat: On iOS, every NSURL request may be cached by default in Cache.db, potentially storing sensitive data unless explicitly disabled. This can lead to unintentional data leaks.

Mitigating this threat is achieved by:

  • Fully clearing the existing cache
  • Remove the cache by setting it to an empty cache:
URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)

Configuration

If you want to enable this functionality, it need to be enabled in the app configuration file (by default it's disabled)

[
  "@bam.tech/react-native-app-security",
  {
    "disableCache": {
      "ios": { "enabled": true }
    }
  }
]

Contributing

Contributions are welcome. See the Expo modules docs for information on how to build/run/develop on the project.

When making a change to the plugin folder, you'll need to run yarn prepare before prebuilding and building the example app.

👉 About BAM

We are a 100 people company developing and designing multi-platform applications with React Native using the Lean & Agile methodology. To get more information on the solutions that would suit your needs, feel free to get in touch by email or through our contact form!

We will always answer you with pleasure 😁

版本与发布记录

@bam.tech/react-native-app-security 共发布 19 个版本,首次发布于 2023/12/04;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.0.0latest2026/04/166 个月前
0.6.12026/03/058 个月前
0.6.02025/07/171.3 年前
0.5.52025/03/101.7 年前
0.5.42025/02/261.7 年前
0.5.4-next.0next2025/02/261.7 年前

关于 @bam.tech/react-native-app-security 的常见问题

@bam.tech/react-native-app-security 是什么?

Easily implement usual security measures in React Native Expo apps

如何安装 @bam.tech/react-native-app-security?

在项目目录运行 npm install @bam.tech/react-native-app-security,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:expo(*)、react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bam.tech/react-native-app-security 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bam.tech/react-native-app-security 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bam.tech/react-native-app-security 可以商用吗?

@bam.tech/react-native-app-security 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bam.tech/react-native-app-security 的下载量有多少?

根据 npm 官方下载统计,@bam.tech/react-native-app-security 在 2026/10/08 – 2026/11/06 这 30 天内被下载 68,046 次,日均约 2,268 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bam.tech/react-native-app-security 还在维护吗?

最新版本 1.0.0 发布于 2026/04/16(6 个月前);至今共发布 19 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/12/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。