fix(secure-storage): make the ios simulator NSUserDefaults fallback opt-in - #670
Merged
Merged
Conversation
…pt-in * `disableFallbackToUserDefaults` now defaults to `true`, so the keychain is used everywhere including the simulator. Xcode embeds entitlements into simulator builds since Xcode 9, so the `-34018` (errSecMissingEntitlement) failure the fallback worked around no longer occurs. * The fallback wrote raw, unprefixed keys into the same `NSUserDefaults` domain that `ApplicationSettings` uses, so "secure" values collided with plain application settings and could clobber them while migrating plaintext values into secure storage. * Simulator detection no longer consults `UIDevice.currentDevice.name`. That name is user-editable, so naming a real device "my simulator" silently downgraded every read and write to plaintext `NSUserDefaults`. Detection is now solely the `SIMULATOR_DEVICE_NAME` environment variable, which only the simulator runtime sets. The accompanying iOS 9 version gate was dead code given the plugin's deployment target. BREAKING CHANGES: * Values stored on the iOS simulator through the old `NSUserDefaults` fallback are not visible to the keychain path and will read back as `null`. Pass `disableFallbackToUserDefaults: false` as the second constructor argument to restore the previous behaviour.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
NathanWalker
approved these changes
Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Spoofable simulator detection downgrades secure storage to plaintext
SecureStorageon iOS decides whether to use the keychain or fall back toNSUserDefaultsin its constructor. The pre-iOS-9 branch of that decision was:UIDevice.nameis the user-editable device name (Settings → General → About → Name). A real device named e.g. "my simulator" therefore takes the fallback path, and everyget/set/remove(sync and async) silently reads and writes plaintextNSUserDefaultsinstead of the keychain — with no error, no log, and no API difference. That is user-controlled input deciding whether secrets are encrypted.The iOS 9 gate guarding that branch is dead code — the plugin's deployment target is far above iOS 9 — so the branch is removed entirely. Simulator detection is now solely the
SIMULATOR_DEVICE_NAMEenvironment variable, which is set by the simulator runtime and is not settable on a device.The fallback shares a namespace with plain application settings
When active, the fallback writes the raw, unprefixed key straight into
NSUserDefaults.standardUserDefaults— the same domain and key namespace thatApplicationSettingsand any other plainNSUserDefaultsusage writes to. "Secure" and plain values collide on one store, so an app migrating a plaintext value into secure storage under the same key can destroy the value it is migrating, on the simulator only, in a way that never reproduces on device.Why flipping the default is safe
The fallback exists for the Xcode 8 / iOS 10 simulator era (2016), when
SecItemAddreturned-34018(errSecMissingEntitlement) in simulator builds that had no embedded application-identifier entitlement (see EddyVerbruggen/nativescript-secure-storage#10). Since Xcode ~9 (2017) Xcode embeds entitlements into every simulator build via the__TEXT,__entitlementssection, so the simulator keychain works the same as on device. The workaround has outlived its cause, and defaulting to it means the least secure path is the one nobody opts into.disableFallbackToUserDefaultsnow defaults totrue. The fallback remains available, unchanged, for anyone on a toolchain old enough to need it.Changes
packages/secure-storage/index.ios.ts:disableFallbackToUserDefaultsdefaults totrue; the iOS 9 version gate and theUIDevice.currentDevice.namedetection branch are deleted, leaving theSIMULATOR_DEVICE_NAMEcheck.packages/secure-storage/README.md: documents the new default, how to opt back in, and why the fallback is not equivalent to the keychain.Android is untouched — the fallback is iOS-only.
Breaking change
Values written on the iOS simulator through the old
NSUserDefaultsfallback are not visible to the keychain path and will read back asnull. This affects simulator development data only; nothing on device changes, since device builds already used the keychain (unless the device name happened to contain "simulator", which is precisely the bug).To restore the previous behaviour:
Validation
npx tsc -p packages/secure-storage/tsconfig.json --noEmitpasses; the touched TS file is prettier-clean. (The README was already not prettier-clean onmain, so it was left unformatted rather than reformatted wholesale.)