electron: [Bug]: macOS password prompt when using safeStorage after electron upgrade

Preflight Checklist

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:

  1. Start below example (npm start) --> Encryption works and buffer printed in console
  2. Upgrade electron (npm install electron@31.3.1 --save-dev)
  3. 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)

Most upvoted comments

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()

EncryptString

Calling safeStorage.encryptString in JS ends up calling this C++ code, which calls OSCrypt::EncryptString.

OSCrypt::EncryptString

OSCrypt::EncryptString in Chromium is just a wrapper for OSCryptImpl::GetInstance()->EncryptString.

OSCryptImpl::EncryptString

OSCryptImpl::EncryptString calls GetEncryptionKey.

OSCryptImpl::GetEncryptionKey

OSCryptImpl::GetEncryptionKey calls KeychainPassword::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 via AddRandomPasswordToKeychain. 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.

AddRandomPasswordToKeychain

AddRandomPasswordToKeychain creates a random password, and then calls keychain.AddGenericPassword.

keychain.AddGenericPassword

keychain.AddGenericPassword calls SecKeychainAddGenericPassword.

SecKeychainAddGenericPassword documentation

SecKeychainAddGenericPassword is function that is part of the Security framework on macOS. In the documentation, it states:

This function sets the initial access rights for the new keychain item so that the application creating the item is given trusted access.

SecKeychainAddGenericPassword source

After looking at the source code for the SecKeychainAddGenericPassword function, 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:

  1. Electron’s safeStorage API makes use of
  2. Chromium’s OSCrypt API, which makes use of
  3. macOS’s SecKeychain API, to setup and use a single keychain item to be used across subsequent OSCrypt calls, and
  4. macOS’s SecKeychain API 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

[!WARNING]
This will allow any application to access your app’s encryption password during development. Only do this if you are aware of the risks and are ok with them.

  1. Open Keychain Access on the macOS device you use for development
  2. Search for the item 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)
  3. Double-click the item Electron Safe Storage
  4. Select the Access Control tab of the resulting modal
  5. Select “Allow all applications to access this item”
  6. Click “Save Changes”

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 😅

+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.