Skip to content

Commit 10288cb

Browse files
authored
fix(secure-storage): make the ios simulator NSUserDefaults fallback opt-in (#670)
1 parent 748af10 commit 10288cb

2 files changed

Lines changed: 13 additions & 15 deletions

File tree

packages/secure-storage/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,15 @@ const secureStorage = new SecureStorage(kSecAttrAccessibleWhenUnlockedThisDevice
113113

114114
## iOS Simulator
115115

116-
Currently this plugin defaults to using `NSUserDefaults` on **iOS Simulators**. You can change this behaviour by providing `disableFallbackToUserDefaults` to the constructor of `SecureStorage`. This then uses the keychain instead of `NSUserDefaults` on simulators.
116+
The plugin uses the keychain on **iOS Simulators** just like it does on device, so no configuration is needed.
117117

118-
If you're running into issues similar to [issue_10](https://github.com/EddyVerbruggen/nativescript-secure-storage/issues/10), consider using the default behaviour again.
118+
An opt-in fallback to `NSUserDefaults` on the simulator is still available for toolchains old enough that `SecItemAdd` fails there with `-34018` (`errSecMissingEntitlement`), see [issue_10](https://github.com/EddyVerbruggen/nativescript-secure-storage/issues/10). Enable it by passing `disableFallbackToUserDefaults: false` as the second constructor argument:
119+
120+
```ts
121+
const secureStorage = new SecureStorage(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, false);
122+
```
123+
124+
Note that the fallback stores values **unencrypted**, under the raw key, in the same `NSUserDefaults` domain that `ApplicationSettings` writes to, so those keys can collide with plain application settings. Only turn it on if the simulator keychain is genuinely unavailable to you.
119125

120126
## iOS Keychain Access/App Groups
121127

@@ -152,7 +158,7 @@ To setup:
152158
```
153159

154160
## Credits
155-
* On __iOS__ we're leveraging the KeyChain using the [SAMKeychain](https://github.com/soffes/SAMKeychain) library (on the Simulator `NSUserDefaults`),
161+
* On __iOS__ we're leveraging the KeyChain using the [SAMKeychain](https://github.com/soffes/SAMKeychain) library,
156162
* On __Android__ we're using [Hawk](https://github.com/orhanobut/hawk) library which internally uses [Facebook conceal](https://github.com/facebook/conceal).
157163
* Thanks, [Prabu Devarrajan](https://github.com/prabudevarrajan) for [adding the `deleteAll` function](https://github.com/EddyVerbruggen/nativescript-secure-storage/pull/11)!
158164
* Thank you [Eddy Verbruggen](https://github.com/EddyVerbruggen) for all the years of service and great work!

packages/secure-storage/index.ios.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,15 @@ export class SecureStorage extends SecureStorageCommon {
1111
// This is a copy of 'kSSKeychainAccountKey_copy' which is not exposed from SSKeychain.h by {N}
1212
private static kSSKeychainAccountKey_copy: string = 'acct';
1313

14-
constructor(accessibilityType: string = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, disableFallbackToUserDefaults = false) {
14+
constructor(accessibilityType: string = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, disableFallbackToUserDefaults = true) {
1515
super();
1616

1717
if (disableFallbackToUserDefaults) {
1818
this.isSimulator = false;
1919
} else {
20-
const isMinIOS9 = NSProcessInfo.processInfo.isOperatingSystemAtLeastVersion({
21-
majorVersion: 9,
22-
minorVersion: 0,
23-
patchVersion: 0,
24-
});
25-
if (isMinIOS9) {
26-
const simDeviceName = NSProcessInfo.processInfo.environment.objectForKey('SIMULATOR_DEVICE_NAME');
27-
this.isSimulator = simDeviceName !== null;
28-
} else {
29-
this.isSimulator = UIDevice.currentDevice.name.toLowerCase().indexOf('simulator') > -1;
30-
}
20+
// Only the simulator runtime sets this; anything derived from the device name can be spoofed by renaming a real device.
21+
const simDeviceName = NSProcessInfo.processInfo.environment.objectForKey('SIMULATOR_DEVICE_NAME');
22+
this.isSimulator = simDeviceName !== null;
3123
}
3224

3325
this.accessibilityType = accessibilityType;

0 commit comments

Comments
 (0)