# @bam\.tech/react-native-app-security 中文文档

规范地址：https://npmcn.com/package/%40bam.tech/react-native-app-security/

版本：1\.0\.0
npm 元数据更新时间：2026-10-13T12:37:55.718Z
中文译文更新时间：2026-11-07T15:56:02.066Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40bam.tech/react-native-app-security
原文对照：https://npmcn.com/package/%40bam.tech/react-native-app-security/?lang=original
项目主页：https://github.com/bamlab/react-native-app-security#readme
源码仓库：https://github.com/bamlab/react-native-app-security

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：39.5 kB，46 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：expo \*，react \*，react-native \*
- 首次发布：2023-12-04T11:03:50.151Z
- 最新版本发布：2026-04-16T08:13:52.064Z
- 近 30 天下载量：68046（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：19（近 12 个月 2 个）

## 中文简介

在 React Industrial Expo apps 中轻松执行通常的安全措施

## 安装

```sh
npm install @bam.tech/react-native-app-security
```

也可以使用：`pnpm add @bam.tech/react-native-app-security`、`yarn add @bam.tech/react-native-app-security`、`bun add @bam.tech/react-native-app-security`

## README

# 反应-本土-应用-安全 qq

在 React Industrial Expo apps 中轻松执行通常的安全措施

-   [安装和设置](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-installation-and-setup)
-   [特征](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-features)
    -   [SSL 平接键](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-ssl-pinning)
        -   [配置](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-configuration)
            -   [固定子域](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-pinning-subdomains)
        -   [生成公钥散列](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-generating-the-public-key-hashes)
        -   [测试](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-testing)
    -   [防止"最新截图"](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-prevent-recent-screenshots)
        -   [配置](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-configuration-1)
    -   [安全键盘检测器](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-safe-keyboard-detector)
    -   [\[警告 - iOS\] 禁用默认缓存 `Cache.db`](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-experimental---ios-only-disable-default-caching-in-cachedb)
-   [捐款](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme-contributing)
-   [关于BAM](https://npmcn.com/package/%40bam.tech/react-native-app-security/#readme--about-bam)

> **• 免责声明**  
> 这个软件包旨在帮助实施一些基本的安全特性,但本身并不能保证应用程序的安全.  
> 参见 [OWASP的资源](https://mas.owasp.org/) 关于移动应用程序安全的更多信息.  
> 你也可以这样 [联系我们](https://apps.theodo.com/en/contact) 如果你需要帮助 保护您的应用程序.

# 安装和设置

此软件包设计用于 expo apps 中 [发展](https://docs.expo.dev/develop/development-builds/introduction/)。 。 。 .

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

将配置插件添加到 `app.config.ts` 编号 : `app.config.js` 编号 : `app.json`数字 :

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

每当你改变配置,不要忘记运行:

```sh
yarn expo prebuild
```

# 特征

## SSL 平接键

> **什么威胁?** 攻击者拦截了您的应用程序的网络请求并访问私人数据或发送恶意回复. [更多细节](https://github.com/OWASP/owasp-mastg/blob/master/Document/0x04f-Testing-Network-Communication.md#restricting-trust-identity-pinning)

此软件包执行 [公钥接线](https://cheatsheetseries.owasp.org/cheatsheets/Pinning_Cheat_Sheet.html#public-key) 使用 [信任键](https://github.com/datatheorem/TrustKit) 在iOS上,并包含在Android上的OkHttp中的证书pinner.

### 配置

```jsonc
[
  "@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="
      ]
    }
  }
]
```

请注意,你需要提供 _2个_ 公用钥匙散列。 这是为了鼓励制定适当的程序,避免将用户锁出, [如本文所描述的 TrustKit 文档](https://github.com/datatheorem/TrustKit/blob/master/docs/getting-started.md#always-provide-at-least-one-backup-pin)。 。 。 .

#### 固定子域

要给特定子域打针, 只需将其包含在您提供的字符串中, 例如 :

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

要标出一个域及其所有子域,请使用通配符:

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

> 通配符只能用于主机名中最通配的部分.
> 
> 无效 : `*domain.com`, (中文(简体) ), `domain.*.com`, (中文(简体) ), `sub.*.domain.com`

### 生成公钥散列

您需要证书( E)`.cer` 或者说 `.crt`(中文(简体) )

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

### 测试

要测试 SSL 针线是否正常, 您可以:

-   换出( 更改) 证书并检查连接失败 _(别忘了) `yarn expo prebuild` 接下来 `yarn ios` 或者说 `yarn android` 以重建应用程序)_
-   设置代理( 我们爱 [代理服务器](https://proxyman.io/))并检查连接失败

## 防止"最新截图"

> **什么威胁?** OS终止应用时,可能会取出截图并存储在设备上,以显示在应用切换器上. 这个截图可能泄露敏感数据

通过以下方式来缓解这一威胁:

-   使用 [`FLAG_SECURE`](https://developer.android.com/reference/android/view/WindowManager.LayoutParams#FLAG_SECURE) android < 13号
-   使用 [`Activity.setRecentScreenshotsEnabled`](https://developer.android.com/reference/android/app/Activity#setRecentsScreenshotEnabled\(boolean\)) 安卓13号
-   在 iOS (要求) 上用溅出屏幕覆盖应用程序 [expo- splash- 屏幕](https://docs.expo.dev/versions/latest/sdk/splash-screen/) (待设置)

### 配置

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

## 安全键盘检测器

> **什么威胁?** 第三方键盘可能嵌入恶意键盘来记录密码和敏感数据. [更多细节](https://www.synopsys.com/blogs/software-security/mitigate-third-party-mobile-keyboard-risk.html)

通过以下方式来缓解这一威胁:

-   在Android上,将当前键盘ID与列表比较 [我们认为安全的键盘包](https://npmcn.com/package/%40bam.tech/react-native-app-security/android/src/main/java/tech/bam/rnas/RNASModule.kt#31)。 。 。 .
-   在iOS上,由于iOS已经阻止了在密码等敏感领域使用第三方键盘,所以没有做具体的事情.

```tsx
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
```

## \[警告 - iOS\] 禁用默认缓存 `Cache.db`

> ⚠️ **分局:** 这个实验特征可能会影响应用行为. 冒险用吧 瘫痪的缓存会造成出乎意料的问题.
> 
> **可能的副作用:**
> 
> -   由于缺乏缓存答复,性能更慢
> -   重复请求的网络使用率较高
> -   需要缓存数据的组件发生崩溃
> -   离线模式中的功能失败

> **威胁:** 在iOS上,每个 `NSURL` 请求可能默认在 `Cache.db`除非明确禁用,否则有可能储存敏感数据。 这可能导致无意的数据泄露.

通过以下方式来缓解这一威胁:

-   完全清除已存在的缓存
-   通过设置为空缓存来删除缓存 :

```swift
URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
```

### 配置

如果您想要启用此功能, 需要在应用程序配置文件中启用( 默认情况下已禁用)

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

# 捐款

欢迎捐款。 见 [展览模块文件](https://docs.expo.dev/modules/get-started/) 关于如何建设/运行/开发项目的信息.

当改变到 `plugin` 文件夹,您需要运行 `yarn prepare` 在创建和创建示例应用程序之前.

# 关于BAM

我们是100个人公司 开发和设计多平台的应用程序 [原生反应](https://apps.theodo.com/expertise/react-native) 使用 Lean & Agile 方法。 要了解更多适合你需要的解决方案,请随意联系 [电子邮件](mailto:contact-apps@theodo.com) 或者通过我们 [联系表单](https://apps.theodo.com/en/contact)来啊!

我们总是会很高兴地回答你

## 常见问题

### @bam\.tech/react-native-app-security 是什么？

在 React Industrial 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 和提交记录判断。

