Skip to content

Replace hardcoded encryption key with dynamic key management - #39

Open
0suu wants to merge 1 commit into
mainfrom
fix-insecure-hardcoded-key-8229730346190127787
Open

Replace hardcoded encryption key with dynamic key management#39
0suu wants to merge 1 commit into
mainfrom
fix-insecure-hardcoded-key-8229730346190127787

Conversation

@0suu

@0suu 0suu commented Feb 13, 2026

Copy link
Copy Markdown
Owner

The encryption key for electron-store was previously hardcoded or missing, making sensitive data like apiToken and apiSecret vulnerable.

This change implements a secure dynamic key management system:

  1. A unique 256-bit key is generated per installation.
  2. This key is encrypted using the OS-level credential vault via Electron's safeStorage API.
  3. The encrypted key is stored in a dedicated store file.
  4. The main application store now uses this unique key for AES-256 encryption.
  5. Backward compatibility is maintained by detecting if an existing config file is unencrypted, in which case encryption is gracefully disabled for that file to prevent data loss.
  6. Main process initialization was refactored to ensure all store-dependent logic (including IPC handlers) runs only after the store is securely initialized.

PR created automatically by Jules for task 8229730346190127787 started by @0suu

- Implemented dynamic encryption key generation using crypto.randomBytes.
- Used Electron safeStorage to protect the generated key on disk.
- Persisted the encryption key in a separate 'switchbot-client-auth' store.
- Moved electron-store initialization and IPC handlers to app.whenReady() to support safeStorage.
- Added backward compatibility check to avoid breaking existing unencrypted config files.
- Added guard clause to buildApiHeaders to ensure store is initialized.
- Moved imports to the top of the file for better style.

Co-authored-by: 0suu <46421931+0suu@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @0suu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the security posture of the application by replacing a potentially hardcoded or missing encryption key with a robust, dynamic key management system. It ensures that sensitive user data stored in electron-store is encrypted using a unique, OS-secured key, while also maintaining backward compatibility for existing installations.

Highlights

  • Dynamic Key Generation: A unique 256-bit encryption key is now generated for each application installation.
  • OS-level Credential Vault Integration: The generated encryption key is securely encrypted using Electron's safeStorage API, leveraging the operating system's credential vault.
  • Secure Key Storage: The encrypted key is stored in a dedicated, separate store file (switchbot-client-auth).
  • Main Store Encryption: The primary application store (switchbot-client-config) now utilizes this dynamically managed, unique key for AES-256 encryption of sensitive data.
  • Backward Compatibility: Existing unencrypted configuration files are detected, and encryption is gracefully disabled for them to prevent data loss, ensuring compatibility with previous installations.
  • Main Process Initialization Refactoring: The main process initialization logic was refactored to ensure that all store-dependent operations, including IPC handlers, are only executed after the secure store initialization is complete.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/main/main.ts
    • Added safeStorage and fs modules for secure storage and file system operations.
    • Implemented getEncryptionKey function to generate a unique 256-bit key, encrypt it with safeStorage (if available), and store it in a dedicated keyStore.
    • Created initializeStore function to set up the main electron-store instance, incorporating the dynamic encryption key and handling backward compatibility for unencrypted config files.
    • Refactored the app.whenReady() block to ensure initializeStore() is called before any other store-dependent logic or IPC handlers are registered.
    • Moved all ipcMain.handle registrations for electron-store and SwitchBot API calls inside the app.whenReady() callback to guarantee store is initialized.
    • Added a runtime check if (!store) in buildApiHeaders to prevent errors if the store is accessed before initialization.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying switchbotcontroller with  Cloudflare Pages  Cloudflare Pages

Latest commit: f668416
Status: ✅  Deploy successful!
Preview URL: https://eba8a022.switchbotcontroller.pages.dev
Branch Preview URL: https://fix-insecure-hardcoded-key-8.switchbotcontroller.pages.dev

View logs

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a significant security and stability improvement. It correctly replaces a hardcoded encryption key with a dynamic key management system using Electron's safeStorage. The implementation is robust, handling key generation, secure storage, and backward compatibility for existing unencrypted configurations. The refactoring of the main process initialization to occur within app.whenReady() is an excellent fix that resolves potential race conditions by ensuring the store is initialized before IPC handlers are registered. I have one minor suggestion to improve logging in a specific failure case to make debugging easier, but overall this is a high-quality change.

Comment thread src/main/main.ts
Comment on lines +70 to +74
} catch (error) {
// If decryption fails, it might have been stored unencrypted
// console.log("[Main] Failed to decrypt key, assuming unencrypted.");
return key;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When key decryption fails, the error is caught but nothing is logged, making it difficult to debug a potentially critical failure (like a lost key). While the fallback to return the raw key is a reasonable failure mode, logging this event as a warning would make it explicit and greatly aid in diagnosing issues where the application's configuration becomes inaccessible.

Suggested change
} catch (error) {
// If decryption fails, it might have been stored unencrypted
// console.log("[Main] Failed to decrypt key, assuming unencrypted.");
return key;
}
} catch (error) {
// If decryption fails, it might have been stored unencrypted.
console.warn("[Main] Failed to decrypt key, assuming unencrypted. Error:", error);
return key;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant