Skip to content

Run iOS apps from a signed Parcel bundle (experimental) - #707

Draft
simonhamp wants to merge 4 commits into
mainfrom
parcel-bundles
Draft

simonhamp wants to merge 4 commits into
mainfrom
parcel-bundles

Conversation

@simonhamp

@simonhamp simonhamp commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Lets an iOS app ship its PHP as one signed, compressed phar built by Parcel instead of as a tree of files. The phar sits inside the .app and PHP reads the code straight out of it, so it's never unpacked. OTA updates bring their own signed phar. Bifrost signs with the P-256 key it already uses for OTA releases, and the key never reaches the build machine. It's off by default and iOS only, and apps that don't turn it on behave exactly as before. Needs Parcel, which is now on Packagist, installed globally. Use 1.0.1 or later.

Why

Smaller apps, far less on the device, a faster first launch, the code a little harder to read, and a way to tell if it's been changed. This is the mechanism Bifrost already uses for desktop bundles.

devnull-mobile, debug build, iOS simulator, same code both ways:

Normal build Parcel, signed
PHP payload in the .app 64.4 MB app.zip 12.5 MB phar + 251 KB app.zip
Documents/app on the device 88.7 MB 312 KB
First launch (unpack + migrate) 4.0 to 6.1 s about 1 s
Warm persistent boot ~240 ms median about the same
Signature check per launch none 5 to 10 ms

A release build of the same app, without dev packages, makes a 7.5 MB phar.

How it works

Build. With NATIVEPHP_PARCEL=true, native:build takes an APFS clone of the staged app in nativephp/ios/laravel, runs parcel prepare --mobile, regenerates the autoloader so Parcel's path helpers load, and runs parcel compile. The phar (and its .sig) go into nativephp/ios/NativePHP/, which is a synchronized folder in the Xcode project, so they ship loose in the .app. app.zip only carries .env and public/. A build without Parcel deletes any phar a previous Parcel build left there. NATIVEPHP_PARCEL_BINARY says where parcel is.

Parcel itself. Parcel is a separate tool you install with composer global require nativephp/parcel. It isn't a Composer dependency of this package, and composer.json only suggests it. Box, which Parcel builds on, only supports Symfony up to 7 (still true of Box 4.7.1). Requiring it would stop any app on Symfony 8 from installing nativephp/mobile at all, devnull on Laravel 13 included. It would also put 58 packages, about 34 MB, into every app's vendor folder, Parcel or not. Before a Parcel build bundles anything, it now runs parcel list to check Parcel is there and is 1.0 or later (1.0 is the first with sign and mobile bundles), and it says how to install or update Parcel if not.

Runtime. AppUpdateManager.getCodePath() is where every lane now finds its PHP: persistent, webview, async, queue worker, classic requests and artisan. It returns, in order:

  1. a phar an OTA update installed in Documents/app, if there is one
  2. the phar shipped in the .app
  3. the app directory, for builds without Parcel

A phar is read-only, so prepareCodeEnvironment() points APP_PACKAGES_CACHE, APP_SERVICES_CACHE and friends at Documents/app/bootstrap/cache, and the three iOS bootstrap scripts call nativephp_use_bundle_paths() to read .env and public/ from Documents/app (NATIVEPHP_BUNDLE_ROOT). Storage, compiled views and the database were already outside the app directory.

Signing. With NATIVEPHP_PARCEL_SIGN=true the build runs parcel sign --project=<id>. On Bifrost, Parcel sends just the project, the phar's SHA-256 and its size to Bifrost (PARCEL_SIGNING_URL, authenticated with PARCEL_SIGNING_TOKEN), and Bifrost signs with its OTA key in KMS. So no private key is ever on the Mac. For local testing Parcel signs with a P-256 key in PARCEL_SIGNING_KEY. The project is BIFROST_APP_ID, or the app id for local builds.

The signature is ECDSA P-256 over the same kind of message Bifrost signs for OTA releases: nativephp-parcel-bundle-v1, the project, the SHA-256 and the size, one per line. Binding the project means one key can sign for every app while a bundle signed for one can't run in another. The .sig is JSON naming the key id. The build writes parcel_signing into bundle_meta.json (the project, and the trusted keys by id), which sits inside the code-signed .app. PARCEL_SIGNING_KEY and PARCEL_SIGNING_TOKEN are always stripped from the bundled .env, whatever cleanup_env_keys says.

The app checks whichever phar it's about to run with CryptoKit, once per file, and only against keys listed in bundle_meta.json. A phar that doesn't match gets an empty code path, so PHP can't boot from it. A build that wasn't signed (a local one) runs its bundle unchecked.

One kind of app or the other. An app runs Parcel bundles if it shipped with a phar, and a plain tree otherwise, never both. A plain-tree app ignores any phar it finds and refuses Parcel payloads, so existing apps behave exactly as before. A Parcel app refuses plain-tree payloads.

OTA updates. Every phar carries its own signature, made once when it's built, and every one is checked against the same keys. So a new release never replaces anyone's signature, and the shipped phar and .sig in the .app are never touched: going back to the code the app was installed with always works. In installUpdate:

  • A Parcel payload (__nativephp_app_bundle.phar, its .sig, .env, public/) has its signature checked before it replaces the working app, then again on every launch.
  • If an installed phar stops matching its signature, launch puts the shipped app back.
  • A payload of the other kind is refused by looking inside the zip before unpacking it, so this takes a fraction of a second instead of unpacking thousands of files.
  • A refused payload is now deleted. Before, it stayed as pending.zip and was unpacked and refused again on every launch. That part applies to any app, not just Parcel ones.

Bifrost needs some work before this ships (not in this PR): a per-project switch that sets NATIVEPHP_PARCEL and NATIVEPHP_PARCEL_SIGN, Parcel installed on the Mac builders (composer global require nativephp/parcel, no token needed now), an endpoint that signs a bundle hash with ReleaseSigner once per build, OTA builds that produce Parcel payloads (phar, .sig, .env, public/, ota.json), and Parcel mode in the shell fingerprint so each app only gets updates of its own kind. Bifrost's own payload signature, which the mobile-ota plugin checks at download, stays as it is. The plugin needs no change: core does the unpacking.

Tested

On the simulator with devnull-mobile, signed through a stand-in for Bifrost's endpoint (Parcel's test fixture under php -S):

  • Onboarding renders from the phar inside the .app, taps round-trip through PHP, all migrations ran from inside the phar (the app's and this package's), and storage:link lands in Documents/app/public.
  • A signed update with a changed view installs and runs.
  • Refused and deleted, with the app left on what it had: an update signed for another project, one signed with a key the app doesn't list, one signed with another key but claiming the trusted key's id, and a plain tree.
  • One byte flipped in an installed update's phar: caught at launch, the shipped app put back, boots normally.
  • The same app built without Parcel: no phar in the .app, boots from the unpacked tree, takes a plain-tree update, refuses a Parcel payload.
  • Parcel 1.0.1 installed from Packagist into a clean Composer home, with an unrelated box first on the PATH: builds, signs and boots. (1.0.0 picked up that other box and failed. Fixed in Run the Box that was installed alongside Parcel parcel#10.)
  • NATIVEPHP_PARCEL_BINARY pointing at nothing: the build stops before bundling and gives the install command.
  • With an earlier version of this branch, deleting the phar and unpacking an edited, unsigned tree in its place ran the edited code. That can't happen now.

Unit tests cover the bundle-paths helper, the signing secrets never reaching .env, parcel_signing only appearing for a signed build, a non-Parcel build removing a leftover phar, and the Parcel check (missing, older than 1.0, 1.0). The unit suite passes (1476) and the Feature files pass one by one. The full vendor/bin/pest run stops early on main too, at TestingSuiteV2Test.

Watch for

  • Key rotation: bundle_meta.json already holds keys by id, so a new key can be added to new builds. Apps already installed only know the keys they were built with, so Bifrost has to keep signing their updates with an old key until none are left, as it does for OTA.
  • Rolling back to an older signed release is allowed on purpose. Nothing stops an older, validly signed phar from being put back.
  • public/ isn't covered by the signature. For apps that run a lot of JavaScript in a web view that's code too.
  • Hot reload writes into the extracted tree, so it does nothing for a Parcel build. This is for release-style builds.
  • If even the shipped phar fails its check the user sees a blank fallback WebView. It should say "reinstall the app". On a normal phone that can't happen, because iOS won't launch an app whose bundle was changed.
  • The Parcel check can't tell 1.0.0 from 1.0.1 (parcel --version has no number). 1.0.0 only works if its own vendor/bin comes first on the PATH, so it's worth giving Parcel a real version number.
  • Android isn't done. It can't read files inside the APK in place, so it would copy the phar out once.
  • PHP for iOS has no fnmatch() (HAVE_FNMATCH is undefined when cross-compiling). Parcel doesn't need it any more, but app code calling it would crash. Probably worth ac_cv_func_fnmatch_works=yes in php-bin-mobile.

Trying it

composer global require nativephp/parcel
openssl ecparam -name prime256v1 -genkey -noout | openssl pkcs8 -topk8 -nocrypt > parcel-test.pem
NATIVEPHP_PARCEL=true \
NATIVEPHP_PARCEL_SIGN=true \
PARCEL_SIGNING_KEY="$(cat parcel-test.pem)" \
php artisan native:run ios

🤖 Generated with Claude Code

Experimental and off by default. With NATIVEPHP_PARCEL=true, native:build
hands the staged app to Parcel and ships its PHP as a single compressed
phar instead of a tree of files. app.zip then holds the phar, .env and
public/. On the device PHP reads the code straight out of the phar
through phar://, so it is never unpacked.

Build: Parcel works on an APFS clone of nativephp/ios/laravel, so the
staged app the next build reuses is left as it was.
NATIVEPHP_PARCEL_BINARY says where parcel is.

Runtime: AppUpdateManager.getCodePath() is where every lane now finds
its PHP (persistent, webview, async, queue, classic, artisan). A phar is
read-only, so prepareCodeEnvironment() points Laravel's bootstrap caches
at the extracted app directory, and the iOS bootstrap scripts read .env
and public/ from there through NATIVEPHP_BUNDLE_ROOT.

Signing: with NATIVEPHP_PARCEL_SIGNING_KEY set, the build runs
`parcel sign`, ships the .sig beside the phar and writes the public key
into bundle_meta.json, inside the code-signed .app. The key is always
stripped from the bundled .env. A shell with a key runs only a phar that
matches its signature. Anything else, including no phar at all, gets an
empty code path, and launch puts back the copy shipped in the .app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
simonhamp and others added 3 commits November 18, 2026 03:44
The phar and its signature now ship loose inside the .app instead of in
app.zip, and PHP reads the code from there. Nothing unpacks it, so the
device keeps one copy instead of two, the code isn't in the app's backed
up Documents folder, and on a normal phone the shipped copy can't be
changed at all. app.zip only carries .env and public/, which the device
has to be able to rewrite. A build without Parcel deletes any phar a
previous Parcel build left in the Xcode project, because the runtime
would run it in preference to app.zip.

An OTA update brings its own phar and signature into Documents/app, and
that runs instead of the shipped one while it is there. Each phar carries
its own signature, checked against the one public key the shell was built
with, so a release never replaces another's signature and the shipped
pair is always there to fall back to. A payload's phar is checked before
it replaces the working app as well as on every launch, and an installed
phar that stops matching puts the shipped app back.

A shell that ships a phar refuses a plain-tree payload by looking inside
the zip before unpacking it. Any refused payload is now deleted: it would
fail the same way on every launch, after being unpacked again each time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signatures are now ECDSA P-256 in the same shape as Bifrost's OTA release
signatures, so Bifrost can sign bundles with its existing KMS key. The
build no longer holds a key: with NATIVEPHP_PARCEL_SIGN=true it runs
`parcel sign`, which asks Bifrost to sign (PARCEL_SIGNING_URL and
PARCEL_SIGNING_TOKEN) or uses a local PARCEL_SIGNING_KEY for testing.
The signature is bound to the project (BIFROST_APP_ID, or the app id
for local builds), so with one key for every app a bundle signed for
one still can't run in another.

bundle_meta.json now carries parcel_signing: the project and the keys
the app trusts, by key id. The .sig names the key it was made with,
and only listed keys count, which leaves room to rotate keys later.
PARCEL_SIGNING_KEY and PARCEL_SIGNING_TOKEN are always stripped from
the bundled .env.

An app is now built to run either Parcel bundles or a plain tree, never
both. A plain-tree app ignores any phar and refuses Parcel payloads,
just as a Parcel app refuses plain-tree ones, so existing apps behave
exactly as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nativephp/parcel can't be a Composer dependency of this package: Box,
which it builds on, only supports Symfony up to 7, so requiring it would
stop any app on Symfony 8 (Laravel 13 apps, for one) installing
nativephp/mobile at all. It would also add 58 packages, about 34 MB, to
every app's vendor folder. So Parcel stays a separate tool, installed
with `composer global require nativephp/parcel`, and the package
suggests it.

Before a Parcel build runs, check Parcel is there and is 1.0 or later
(1.0 is the first with `sign` and mobile bundles), and say how to install
or update it if not, instead of failing later with "command not found".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonhamp

simonhamp commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

public/ isn't covered by the signature. For apps that run a lot of JavaScript in a web view that's code too.

It should be

Android isn't done. It can't read files inside the APK in place, so it would copy the phar out once.

Weird. Ok... Let's do it

PHP for iOS has no fnmatch() (HAVE_FNMATCH is undefined when cross-compiling). Parcel doesn't need it any more, but app code calling it would crash. Probably worth ac_cv_func_fnmatch_works=yes in php-bin-mobile.

What's this about?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant