electron: [Bug]: macOS password prompt when using safeStorage after electron upgrade
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
31.3.1
What operating system(s) are you using?
macOS
Operating System Version
Sonoma 14.5
What arch are you using?
arm64 (including Apple Silicon)
Last Known Working Electron version
No response
Expected Behavior
After upgrading electron, safeStorage can still be used without showing the macOS password prompt.
Actual Behavior
I ran into the exact same issues as described in https://github.com/electron/electron/issues/40236 (as this issue was closed, I opened this one).
Testcase Gist URL
No response
Additional Information
Steps to reproduce:
- Start below example (npm start) --> Encryption works and buffer printed in console
- Upgrade electron (npm install electron@31.3.1 --save-dev)
- Run example again after upgrade (npm start) --> macOS password prompt shown and encryption/decryption doesn’t work until password entered.
package.json
{
"name": "electron-safestorage-upgrade-bug",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "30.3.1"
}
}
main.js
const { app, safeStorage } = require("electron/main");
app.whenReady().then(() => {
useSafeStorage();
});
const useSafeStorage = async () => {
try {
const encryptedString = await safeStorage.encryptString("hello world");
console.log(encryptedString);
} catch (e) {
console.log(e);
}
};
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (3 by maintainers)
This won’t happen if your app is codesigned correctly, the ACL is based on the app bundle ID + the team ID of the certificate used to sign your app (by default). Under the hood it’s actually the DR used to signed your app but by default that translates to bundle ID + team ID.
If you aren’t signing your app this is going to happen constantly, if people are re-signing your app it’s going to happen constantly. If you sign your app with multiple teams certs / have migrated between apple teams then this will happen constantly. But just don’t do that and sign your app properly and you’ll be fine 😅
OK. I’ve figured it out mostly. Here is the breakdown.
Note: Version numbers mentioned are the most recent at the time of writing. Discussion of behavior on current versions does not preclude the existence of the same behavior on previous versions.
Electron Version: v32.0.1 Chromium Version: v128.0.6613.36 Source: https://releases.electronjs.org/release/v32.0.1
macOS Version: 14.6 Security framework verson: 61123.140.15 (commit: b4bd4fda62af1ebf469ee97f0112fb49363fb482) Source: https://github.com/apple-oss-distributions/distribution-macOS/tree/macos-146
Electron/Chromium implementation of
safeStorage.encryptString()EncryptStringCalling
safeStorage.encryptStringin JS ends up calling this C++ code, which callsOSCrypt::EncryptString.OSCrypt::EncryptStringOSCrypt::EncryptStringin Chromium is just a wrapper forOSCryptImpl::GetInstance()->EncryptString.OSCryptImpl::EncryptStringOSCryptImpl::EncryptStringcallsGetEncryptionKey.OSCryptImpl::GetEncryptionKeyOSCryptImpl::GetEncryptionKeycallsKeychainPassword::GetPassword()KeychainPassword::GetPassword()KeychainPassword::GetPassword()attempts to get the password stored in the keychain via the service name and account name. If the account does not exist, it creates one viaAddRandomPasswordToKeychain. Note that for macOS builds of Electron, the service name and account name are assigned/overwritten to be named after the given Electron app at build time.AddRandomPasswordToKeychainAddRandomPasswordToKeychaincreates a random password, and then callskeychain.AddGenericPassword.keychain.AddGenericPasswordkeychain.AddGenericPasswordcallsSecKeychainAddGenericPassword.SecKeychainAddGenericPassworddocumentationSecKeychainAddGenericPasswordis function that is part of the Security framework on macOS. In the documentation, it states:SecKeychainAddGenericPasswordsourceAfter looking at the source code for the
SecKeychainAddGenericPasswordfunction, it does not appear to explicitly do what Apple’s documentation claims it does. However, it’s possible this is done elsewhere in the code, or in sections that are not open-sourced by Apple.In short
On macOS:
safeStorageAPI makes use ofOSCryptAPI, which makes use ofSecKeychainAPI, to setup and use a single keychain item to be used across subsequentOSCryptcalls, andSecKeychainAPI automatically limits access to that item so that only the creating application is given trusted access (password-less access).So it seems that whatever indicators that macOS uses to confirm if the application that is accessing the keychain item is, indeed, the same one that created it initially do not survive rebuilds (or potentially even subsequent runs). As @nikwen mentioned, it’s very likely related to the code signatures.
Workaround
Electron Safe Storage(it’s named after Electron since you are using the main Electron binary to launch your app and not your app’s own custom-built Electron binary)Electron Safe StorageAccess Controltab of the resulting modal+1 to this. I can definitely confirm on my end, having looked into macOS keychain ACL’s for unrelated reasons (even before my deep dive above), this is all correct. By the way, for anyone confused, DR stands for “designated requirement”.
https://developer.apple.com/documentation/technotes/tn3127-inside-code-signing-requirements#Designated-requirement
Question: Does this only happen in development or also when you package, codesign and notarize the app?
I’ve seen similar issues with other macOS APIs in development (e.g.
app.setLoginItemSettings()), but everything worked once the app was packaged, codesigned and notarized.I assume macOS might not be able to deduce that an unsigned Electron executable and another unsigned Electron executable are the same app. In production, however, when everything is codesigned and notarized, it is able to.