Keychain
Keychain provides secure access to the system keychain for storing sensitive and persistent data inside the Scripting environment. It is designed for:
- Authentication tokens
- Login credentials
- License and subscription states
- Encryption keys
- Private user data
All data is protected using the system-level Keychain security mechanism.
1. Per-Script Keychain Scope
In Scripting, Keychain uses a per-script isolation model.
1.1 Scope Rules
-
Each script has its own independent Keychain scope
-
A script can only access the Keychain data it has written
-
Different scripts:
- Cannot read each other’s Keychain data
- Cannot overwrite each other’s keys
- Even if the same key name is used
- Even if
synchronizable: trueis enabled
-
Each script is treated as an independent security sandbox
1.2 Security Implications
This design ensures that:
- No script can steal credentials from another script
- Subscription, login state, and authorization data are fully isolated
- Malicious scripts cannot access private user data stored by other scripts
- The security boundary is stricter than the system-level app Keychain alone
1.3 Script Removal Behavior
-
When a script is deleted:
- All Keychain data under that script’s scope is automatically removed
-
Other scripts’ Keychain data is not affected
2. Namespace
3. Supported Data Types
Keychain supports three data types:
4. KeychainAccessibility
Default value:
5. iCloud Synchronization (synchronizable)
Default:
Even when enabled, synchronization is still restricted to the current script scope.
6. Writing Data
6.1 Store a String
6.2 Store a Boolean
6.3 Store Binary Data
6.4 Overwrite Rules
- Existing values are automatically overwritten
trueis returned on successfalseis returned on failure
7. Reading Data
7.1 Read a String
7.2 Read a Boolean
7.3 Read Binary Data
8. Removing Data
- If the key exists, it is deleted and returns
true - If the key does not exist, it still safely returns
true
9. Checking for Key Existence
10. Listing All Keys
11. Clearing the Keychain
Behavior:
- Only clears data within the current script scope
- Does not affect other scripts
- Does not affect the app’s own Keychain data or other apps
12. synchronizable Read/Write Consistency Rules
If a key is written with:
Then all subsequent operations must use the same flag:
13. Security Recommendations
Suitable Data
- Authentication tokens
- Subscription and license states
- User identifiers
- Encryption keys
Not Recommended
- Large binary files
- High-frequency cache data
- Public configuration values
