Skip to content

Commit 537b04a

Browse files
Merge pull request #1 from ExodusForks/exo-doguhan/verify-on-native-side
feat: implement native bundle hash verification on iOS and Android
2 parents eef7428 + acbde65 commit 537b04a

13 files changed

Lines changed: 780 additions & 445 deletions

File tree

README.md

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Loads a remote React Native JS bundle, with optional **hash-pinned integrity ver
99

1010
Loading a remote JS bundle is, by construction, remote code execution inside the host app. **This library is intended for internal/development builds only — do not ship it in store builds without an out-of-band, statically-stripped feature flag.** See `SECURITY.md`.
1111

12-
The `loadVerified()` API closes the dominant runtime risk: it fetches the bundle bytes itself, hashes them with `@exodus/crypto`, compares the hash to a caller-supplied digest in constant time, and only then asks the bridge to reload from the verified bytes. Anything that mutates the response between fetch and reload is rejected.
12+
The `loadVerified()` API closes the dominant runtime risk: it downloads the bundle natively, hashes the bytes with platform crypto (iOS: `CommonCrypto CC_SHA256`, Android: `MessageDigest SHA-256`), compares the hash to a caller-supplied digest in constant time, and only then loads the verified bytes from app-private storage. Anything that mutates the response between fetch and reload is rejected.
1313

1414
## Installation
1515

@@ -23,7 +23,7 @@ iOS:
2323
cd ios && pod install
2424
```
2525

26-
Android: `BundleLoaderPackage` is autolinked.
26+
Android: requires both Gradle wiring and host app changes — see [Android integration](#android-integration) below.
2727

2828
## Usage
2929

@@ -43,11 +43,12 @@ Behavior:
4343

4444
- The URL must use the `https:` scheme.
4545
- The expected sha256 must be a 64-character hex string.
46-
- The bytes are fetched, hashed in JS using `@exodus/crypto/hash`, and compared to the expected hash with a constant-time comparison.
47-
- On match, the bytes are written to the platform's app-private cache (iOS: `NSTemporaryDirectory()` with `NSDataWritingFileProtectionComplete`; Android: `Context.getCacheDir()`) and the bridge is reloaded from the local file path.
48-
- On mismatch, an error is thrown and the bridge is left untouched.
46+
- Download, SHA-256 hashing, and constant-time comparison all happen in native code. This avoids the Hermes `RangeError` that JS-side `response.arrayBuffer()` causes on large bundles (≥ ~70 MB).
47+
- On match, the bytes are written to app-private storage and the bundle is loaded (see platform notes below).
48+
- On mismatch, an error is thrown and the current bundle is left untouched.
49+
- The remote bundle is active for **one session only**. The next cold start returns to the local bundle — matching the behaviour consumers expect from a developer preview tool.
4950

50-
Works on iOS and Android. The hash check happens in JS before any native call, so the integrity contract is identical on both platforms.
51+
Works on iOS and Android.
5152

5253
### Unverified loading
5354

@@ -87,12 +88,105 @@ Example: `https://example.ngrok.io/index.bundle?dev=false&platform=ios&excludeSo
8788
| `loadVerified(url, sha256)` |||
8889
| `runningMode()` |||
8990

90-
### How the in-process swap works
91+
### How bundle loading works
9192

92-
- **iOS** writes the bundle to `NSTemporaryDirectory()` and sets the bridge's `bundleURL` via KVC (`[bridge setValue:url forKey:@"bundleURL"]`), then calls `[bridge reload]`.
93-
- **Android** writes the bundle to `Context.getCacheDir()`, builds a `JSBundleLoader.createFileLoader(path)`, swaps it into the private `mBundleLoader` field on `ReactInstanceManager` via reflection, and calls `recreateReactContextInBackground()`.
93+
**iOS** downloads and verifies the bundle natively via `NSURLSession` + `CommonCrypto CC_SHA256`, writes it to `NSTemporaryDirectory()` with `NSDataWritingFileProtectionComplete`, then sets the bridge's `bundleURL` via KVC (`[bridge setValue:url forKey:@"bundleURL"]`) and calls `[bridge reload]`. This is an in-process reload: the old bridge is torn down and a new one is created with the cached file. Because iOS uses ARC, the old bridge's memory (including the Hermes runtime) is freed immediately when the bridge reference is released, before the new runtime allocates — no double-memory peak.
9494

95-
Both mechanisms touch private/internal React Native surface and could break on a major RN upgrade. See `SECURITY.md`. The Android implementation requires the host app to implement `ReactApplication` (the standard React Native template does).
95+
**Android** uses a process restart instead of an in-process bridge swap. The reason: Android's ART garbage collector is non-deterministic. When a new React context is created alongside an existing one, ART does not guarantee the old Hermes runtime's native heap is freed before the new runtime allocates. On real-world bundle sizes (~50 MB of Hermes bytecode) this causes OOM. The process restart avoids the problem entirely by ensuring only one runtime is ever live.
96+
97+
After download and hash verification, the module:
98+
99+
1. Writes the bundle to `Context.getCacheDir()/verified-bundle.jsbundle`.
100+
2. Sets a one-shot flag in `SharedPreferences` (`"BundleLoader"` / `"pending_remote_bundle"`), using a synchronous `commit()` so the flag survives the imminent process kill.
101+
3. Restarts the process via `startActivity` + `Process.killProcess`.
102+
103+
On the next launch, the host app reads the flag, disables Metro (so `ReactInstanceManager` does not query the packager and ignore the file — confirmed necessary by bytecode analysis of RN 0.78), and serves `verified-bundle.jsbundle` as the JS bundle for this session. The flag is consumed on first use so subsequent restarts return to Metro.
104+
105+
## Android integration
106+
107+
Because Android requires host app changes that cannot be encapsulated in the module itself, the following manual steps are required.
108+
109+
### 1. Gradle wiring
110+
111+
`settings.gradle` — include the subproject conditionally (the module is a `devDependency`; prod CI runs `yarn install --production` and the directory won't exist):
112+
113+
```groovy
114+
def bundleLoaderDir = new File(rootProject.projectDir, '../node_modules/@exodus/react-native-bundle-loader/android')
115+
if (bundleLoaderDir.exists()) {
116+
include ':@exodus_react-native-bundle-loader'
117+
project(':@exodus_react-native-bundle-loader').projectDir = bundleLoaderDir
118+
}
119+
```
120+
121+
`app/build.gradle` — depend only in debug builds:
122+
123+
```groovy
124+
if (new File("$rootDir/../node_modules/@exodus/react-native-bundle-loader/android").exists()) {
125+
debugImplementation project(':@exodus_react-native-bundle-loader')
126+
}
127+
```
128+
129+
### 2. Register the package
130+
131+
In `MainApplication.java`, inside `getPackages()`, add the package via reflection so a missing module (absent in prod CI) doesn't cause a compile-time error:
132+
133+
```java
134+
if (BuildConfig.DEBUG) {
135+
// devDependency absent in prod CI (yarn install --production); reflection avoids a compile-time import
136+
try {
137+
packages.add((ReactPackage) Class.forName("com.reactnativebundleloader.BundleLoaderPackage")
138+
.getDeclaredConstructor().newInstance());
139+
} catch (ReflectiveOperationException e) {
140+
throw new RuntimeException(e);
141+
}
142+
}
143+
```
144+
145+
### 3. Hook bundle loading into ReactNativeHost
146+
147+
Add these three methods to your `ReactNativeHost` anonymous subclass in `MainApplication.java`:
148+
149+
```java
150+
import java.io.File;
151+
152+
// ...
153+
154+
@Override
155+
public boolean getUseDeveloperSupport() {
156+
if (BuildConfig.DEBUG && hasPendingRemoteBundle()) {
157+
// Must disable dev support: when enabled and Metro is reachable,
158+
// ReactInstanceManager queries the packager and ignores getJSBundleFile().
159+
return false;
160+
}
161+
// No pending bundle — clear the active flag so runningMode() returns LOCAL.
162+
getSharedPreferences("BundleLoader", MODE_PRIVATE)
163+
.edit().remove("active_remote_bundle").apply();
164+
return BuildConfig.DEBUG;
165+
}
166+
167+
@Override
168+
protected String getJSBundleFile() {
169+
if (BuildConfig.DEBUG && hasPendingRemoteBundle()) {
170+
File cachedBundle = new File(getCacheDir(), "verified-bundle.jsbundle");
171+
if (cachedBundle.exists()) {
172+
// Consume the one-shot latch: next restart goes back to Metro.
173+
getSharedPreferences("BundleLoader", MODE_PRIVATE).edit()
174+
.remove("pending_remote_bundle")
175+
.putBoolean("active_remote_bundle", true)
176+
.apply();
177+
return cachedBundle.getAbsolutePath();
178+
}
179+
}
180+
return null;
181+
}
182+
183+
private boolean hasPendingRemoteBundle() {
184+
return getSharedPreferences("BundleLoader", MODE_PRIVATE)
185+
.getBoolean("pending_remote_bundle", false);
186+
}
187+
```
188+
189+
The SharedPreferences keys (`"BundleLoader"`, `"pending_remote_bundle"`, `"active_remote_bundle"`) must match the constants defined in `BundleLoaderModule` (`PREFS_NAME`, `PREFS_PENDING_KEY`, `PREFS_ACTIVE_KEY`).
96190

97191
## Provenance
98192

SECURITY.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ This library exists to load and execute a remote JavaScript bundle inside the ho
1515

1616
| Surface | Upstream `0.1.0` | This fork |
1717
| ------------------------------------------- | ------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
18-
| Bundle integrity | None — bridge fetches whatever the URL serves | `loadVerified(url, sha256)` fetches bytes in JS, hashes with `@exodus/crypto`, compares constant-time, only then reloads from disk |
18+
| Bundle integrity | None — bridge fetches whatever the URL serves | `loadVerified(url, sha256)` downloads bytes natively (iOS: `NSURLSession`, Android: `HttpURLConnection`), hashes with platform crypto (iOS: `CommonCrypto CC_SHA256`, Android: `MessageDigest SHA-256`), compares in constant-time, writes to app-private storage, and reloads the bridge from the local file — closing the TOCTOU window between fetch and load |
1919
| `BundlePrompt` default URL | Hardcoded `cdn.jsdelivr.net/gh/jusbrasil/...` (deleted) | Empty — operator must type a URL |
20-
| Scheme enforcement | None — accepts `http://`, `file://`, etc. | `https://` required at the JS boundary; native iOS `load:` re-checks |
20+
| Scheme enforcement | None — accepts `http://`, `file://`, etc. | `https://` required at the JS boundary; both native `load` implementations re-check before touching the network |
2121
| Verified bundle on-disk protection (iOS) | n/a | Written with `NSDataWritingFileProtectionComplete` |
2222
| Lockfile | Not shipped | `yarn.lock` committed; `.yarnrc` enforces `--frozen-lockfile` |
2323
| Dependency version pinning | Carets (`^`) | All direct deps pinned to exact versions; `.npmrc` `save-exact=true` |
@@ -35,15 +35,15 @@ This library exists to load and execute a remote JavaScript bundle inside the ho
3535

3636
- **The bridge `bundleURL` setter is a KVC write** on iOS (`[bridge setValue:url forKey:@"bundleURL"]`) to a non-public RN property. Behavior could change on an RN upgrade and silently no-op the loader.
3737
- **The Android bundle swap reflects on a private field.** `ReactInstanceManager.mBundleLoader` has no public setter, so we use `Field.setAccessible(true)` to install a fresh `JSBundleLoader.createFileLoader(...)` before calling `recreateReactContextInBackground()`. The field name has been stable across RN 0.62–0.74 but is not part of the public API; an RN upgrade could rename or remove it, in which case `loadVerified`/`load` will throw `NoSuchFieldException` rather than silently no-op.
38-
- **`@exodus/crypto/hash` runs in JS on the JS thread.** Bundles are typically a few MB; hashing time is acceptable. We deliberately keep hashing in JS so the threat-model contract — "Exodus crypto verifies, and the verified bytes are what we hand to native" — is auditable in TypeScript and identical on iOS and Android.
39-
- **`timingSafeEqual()` is currently inlined** as a small constant-time XOR loop. The threat model anticipates this moving to a future `@exodus/crypto` export. The inlined version is functionally equivalent and lives in `src/index.tsx`.
38+
- **Hash verification runs in native code, not JS.** `loadVerifiedFromUrl` uses `CommonCrypto CC_SHA256` (iOS) and `MessageDigest SHA-256` (Android) with a constant-time XOR comparison loop in native code. This avoids a Hermes `RangeError: Maximum regex stack depth reached` that the previous JS-side `response.arrayBuffer()` path hit on bundles ≥ ~70 MB. The trade-off is that the integrity contract is no longer auditable as TypeScript.
39+
- **`timingSafeEqual` is an inlined XOR loop in native code.** Both `ios/BundleLoader.m` and `android/src/main/java/com/reactnativebundleloader/BundleLoaderModule.java` XOR all byte pairs into an accumulator and reject the bundle if the accumulator is non-zero.
4040

4141
## Release process
4242

4343
This package has no CI/CD. Maintainers cut releases manually from a developer machine:
4444

4545
```sh
46-
yarn preflight # lint + typecheck + test + verify-pack
46+
yarn preflight # lint + typecheck + JS tests + Android JVM tests + iOS XCTests + verify-pack
4747
npm publish --access public
4848
```
4949

0 commit comments

Comments
 (0)