📝 Originally published (in Japanese) at forge.workstyle.tech.
TL;DR A screen check script used the same settings location as the Electron app and handled ciphertext with a differently signed executable. Decryption then failed, and the error handler deleted all four API keys. Keep unreadable ciphertext under another name, retry it on read, and keep the check script away from getSettings() and safeStorage.
TL;DR
In an Electron app, a test script signed differently from the production app accessed the same storage. After that, safeStorage could no longer decrypt the ciphertext, and the error handler deleted all four API keys. We changed the app to preserve undecryptable values under a different name and retry reading them. The test script now receives the values it needs through environment variables.
Why Did a UI Check Script Touch a Product Secret?
The problem started because the UI check script used the same settings storage location as the product. The script ran in plain Electron, and I matched the app name and settings storage location to the product’s. I thought using the same settings would let me verify the UI.
As a result, the script handled encrypted data stored in the same area as the product’s. After an executable with a different signature accessed the key, the encrypted data could no longer be decrypted. I only wanted to check the UI features; there was no need to read the API key. Even so, opening the product’s storage area created a path to the key.
Why Did the API Keys Disappear After a Decryption Error?
When the app tried to read secrets encrypted with safeStorage, it deleted the ciphertext if decryption failed. Keeping an unreadable value would leave the UI showing “Configured,” so the implementation cleared it and returned the setting to “Not configured.” From a display-only perspective, this seemed reasonable.
But there can be temporary reasons why ciphertext cannot be read. This can happen if the app is accessed from a binary signed differently, or if the keychain is locked or the OS has been updated.
On the next launch, decryption failed. The old readSecret handled the error by deleting the ciphertext, and all four API keys were lost. The keychain’s parent key was intact, so re-entering the keys restored the app to a usable state. But the deleted values themselves could not be recovered.
How to Preserve Ciphertext That Cannot Be Decrypted
The fix now moves ciphertext that cannot be decrypted instead of deleting it, appending .unreadable- and a timestamp to the original filename. Since the file is no longer in its original location, the screen returns to “Not configured,” making it clear that the setting needs to be configured again. At the same time, the ciphertext itself is preserved, so it can be retried later if the inability to decrypt it was temporary.
I also added a step to try decrypting moved ciphertext before reading the secret. If decryption succeeds, the file is restored to its original location. This allows recovery without re-entering the secret if the vault key becomes available again.
The moved files have names that do not end in .bin. The logic that looks for configured keys picks up .bin files, so this prevents moved ciphertext from being mistaken for a configured key.
How to Keep Verification Scripts from Reading Product Settings
Another fix is to keep verification scripts from reading product settings. The scripts get the endpoints and keys they need from environment variables, and they don’t go through getSettings() or safeStorage. The code used to inspect the screen runs with only the inputs needed for that inspection.
To avoid repeating the same problem, I also added a check that scans the scripts in scripts/. It verifies that they don’t specify the product’s storage locations or refer to its name. Because the check targets code rather than comments, explanations of past failures can stay in place.
Key takeaways
- Check whether a screen check script uses the product's settings location.
- Check whether the keychain is locked or the OS was updated.
- Keep ciphertext that fails decryption under another name instead of deleting it.
- Retry the moved ciphertext during reads and restore it if decryption succeeds.
- Pass required values to the check script through environment variables, without
getSettings()orsafeStorage.
Top comments (0)