-
-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(suite-desktop): open system settings using desktopApi #17017
base: develop
Are you sure you want to change the base?
Conversation
WalkthroughThis pull request introduces new methods to the desktop API and integrates a system settings module for handling Bluetooth configuration across different operating systems. In the API layer, two methods are added: one to invoke the system settings via an IPC channel and another to serve as the API endpoint. The factory function’s return object is updated to include a corresponding method for invoking the system settings action. Additionally, a new module is introduced specifically for system settings, which determines the appropriate command to open Bluetooth settings based on the operating system—handling distinctions for Linux (GNOME/KDE), macOS, and Windows—with error handling for unsupported environments. This module is integrated into the core modules list with a defined service name and is initialized via an IPC handler for the relevant system settings event. ✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
packages/suite-desktop-core/src/modules/system-settings.ts (1)
47-55
: Enhance type safety and extensibility.The IPC handler could benefit from better type safety and extensibility for future settings.
Consider this enhancement:
+type SupportedSettings = 'bluetooth' | 'camera' | 'privacy'; + export const init: ModuleInit = () => { - ipcMain.handle('system/open-settings', (_, settings) => { + ipcMain.handle('system/open-settings', (_, settings: SupportedSettings) => { if (settings === 'bluetooth') { return openBluetoothSettings(); + } else if (settings === 'camera') { + // TODO: Implement camera settings + return { success: false, error: 'Camera settings not implemented yet' }; + } else if (settings === 'privacy') { + // TODO: Implement privacy settings + return { success: false, error: 'Privacy settings not implemented yet' }; } return { success: false, error: `Unknown settings: ${settings}` }; }); };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
packages/suite-desktop-api/src/api.ts
(2 hunks)packages/suite-desktop-api/src/factory.ts
(1 hunks)packages/suite-desktop-core/src/modules/index.ts
(2 hunks)packages/suite-desktop-core/src/modules/system-settings.ts
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: run-desktop-tests (@group=wallet, trezor-user-env-unix bitcoin-regtest)
- GitHub Check: run-desktop-tests (@group=other, trezor-user-env-unix)
- GitHub Check: run-desktop-tests (@group=passphrase, trezor-user-env-unix)
- GitHub Check: run-desktop-tests (@group=settings, trezor-user-env-unix bitcoin-regtest)
- GitHub Check: run-desktop-tests (@group=device-management, trezor-user-env-unix)
- GitHub Check: run-desktop-tests (@group=suite, trezor-user-env-unix)
- GitHub Check: Setup and Cache Dependencies
- GitHub Check: Analyze with CodeQL (javascript)
- GitHub Check: build-web
🔇 Additional comments (2)
packages/suite-desktop-api/src/api.ts (1)
112-112
: LGTM!The API interface changes are well-defined and consistent with the implementation.
Also applies to: 179-179
packages/suite-desktop-core/src/modules/index.ts (1)
33-33
: LGTM!The module integration follows the established pattern and is correctly placed in the MODULES array.
Also applies to: 67-67
const openSettings = (cmd: string, env?: Record<string, string>) => | ||
new Promise<any>(resolve => { | ||
exec(cmd, { env: { ...process.env, ...env } }, error => { | ||
if (error) { | ||
resolve({ success: false, error } as const); | ||
} else { | ||
resolve({ success: true } as const); | ||
} | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate command input to prevent command injection.
The openSettings
function executes shell commands without proper input validation, which could lead to command injection vulnerabilities if the cmd
parameter contains malicious input.
Consider:
- Validating the command string against a whitelist of allowed commands.
- Using
execFile
instead ofexec
for better security.
-const openSettings = (cmd: string, env?: Record<string, string>) =>
+const ALLOWED_COMMANDS = ['gnome-control-center', 'systemsettings5', 'open', 'start'] as const;
+type AllowedCommand = typeof ALLOWED_COMMANDS[number];
+
+const openSettings = (cmd: AllowedCommand, args: string[], env?: Record<string, string>) =>
new Promise<any>(resolve => {
- exec(cmd, { env: { ...process.env, ...env } }, error => {
+ execFile(cmd, args, { env: { ...process.env, ...env } }, error => {
if (error) {
resolve({ success: false, error } as const);
} else {
resolve({ success: true } as const);
}
});
});
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const openSettings = (cmd: string, env?: Record<string, string>) => | |
new Promise<any>(resolve => { | |
exec(cmd, { env: { ...process.env, ...env } }, error => { | |
if (error) { | |
resolve({ success: false, error } as const); | |
} else { | |
resolve({ success: true } as const); | |
} | |
}); | |
}); | |
const ALLOWED_COMMANDS = ['gnome-control-center', 'systemsettings5', 'open', 'start'] as const; | |
type AllowedCommand = typeof ALLOWED_COMMANDS[number]; | |
const openSettings = (cmd: AllowedCommand, args: string[], env?: Record<string, string>) => | |
new Promise<any>(resolve => { | |
execFile(cmd, args, { env: { ...process.env, ...env } }, error => { | |
if (error) { | |
resolve({ success: false, error } as const); | |
} else { | |
resolve({ success: true } as const); | |
} | |
}); | |
}); |
const openBluetoothSettings = () => { | ||
if (isLinux()) { | ||
// https://github.com/electron/electron/blob/ab2a4fd836d539194bc5cde5f0d665eddeb6a134/docs/api/environment-variables.md?plain=1#L190 | ||
// Electron modifies the value of XDG_CURRENT_DESKTOP | ||
const xdg = process.env.ORIGINAL_XDG_CURRENT_DESKTOP || process.env.XDG_CURRENT_DESKTOP; | ||
if (xdg?.includes('GNOME')) { | ||
return openSettings('gnome-control-center bluetooth', { | ||
XDG_CURRENT_DESKTOP: xdg, | ||
}); | ||
} else if (xdg?.includes('KDE')) { | ||
return openSettings('systemsettings5', { | ||
XDG_CURRENT_DESKTOP: xdg, | ||
}); | ||
} | ||
} | ||
if (isMacOs()) { | ||
return openSettings('open "x-apple.systempreferences:com.apple.Bluetooth"'); | ||
} | ||
if (isWindows()) { | ||
return openSettings('start ms-settings:bluetooth'); | ||
} | ||
|
||
return { success: false, error: 'Unsupported os' }; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
❓ Verification inconclusive
Improve error handling and environment detection.
The function has several areas that could be improved:
- No error handling for unsupported desktop environments on Linux
- No validation of environment variables
- Hardcoded commands without version checks
Consider this refactoring:
const openBluetoothSettings = () => {
if (isLinux()) {
const xdg = process.env.ORIGINAL_XDG_CURRENT_DESKTOP || process.env.XDG_CURRENT_DESKTOP;
+ if (!xdg) {
+ return { success: false, error: 'Desktop environment not detected' };
+ }
if (xdg?.includes('GNOME')) {
- return openSettings('gnome-control-center bluetooth', {
+ return openSettings('gnome-control-center', ['bluetooth'], {
XDG_CURRENT_DESKTOP: xdg,
});
} else if (xdg?.includes('KDE')) {
- return openSettings('systemsettings5', {
+ return openSettings('systemsettings5', [], {
XDG_CURRENT_DESKTOP: xdg,
});
+ } else {
+ return { success: false, error: `Unsupported desktop environment: ${xdg}` };
}
}
if (isMacOs()) {
- return openSettings('open "x-apple.systempreferences:com.apple.Bluetooth"');
+ return openSettings('open', ['x-apple.systempreferences:com.apple.Bluetooth']);
}
if (isWindows()) {
- return openSettings('start ms-settings:bluetooth');
+ return openSettings('start', ['ms-settings:bluetooth']);
}
return { success: false, error: 'Unsupported os' };
};
Additionally, consider adding version checks for the commands:
🏁 Script executed:
#!/bin/bash
# Check if the commands exist and get their versions
which gnome-control-center && gnome-control-center --version
which systemsettings5 && systemsettings5 --version
Length of output: 244
Action Required: Enhance Error Handling and Environment Detection in openBluetoothSettings
The updated review comment proposes several improvements:
- Linux Error Handling: Return a clear error if the desktop environment variable (
xdg
) is missing or unrecognized. - Environment Validation: Validate the
xdg
variable before attempting to use it. - Command Invocation: Refactor the command calls to use argument arrays and add a fallback for unsupported desktop environments.
- Version Checks: Replace the use of
which
(which isn’t available in your environment) withcommand -v
for verifying the existence and version of external commands.
Manual verification is needed—please run the above script to check forgnome-control-center
andsystemsettings5
availability and versions in your environment.
const openBluetoothSettings = () => {
if (isLinux()) {
// Electron sets XDG_CURRENT_DESKTOP; prefer ORIGINAL_XDG_CURRENT_DESKTOP if available
const xdg = process.env.ORIGINAL_XDG_CURRENT_DESKTOP || process.env.XDG_CURRENT_DESKTOP;
+ if (!xdg) {
+ return { success: false, error: 'Desktop environment not detected' };
+ }
if (xdg?.includes('GNOME')) {
- return openSettings('gnome-control-center bluetooth', {
- XDG_CURRENT_DESKTOP: xdg,
- });
+ return openSettings('gnome-control-center', ['bluetooth'], {
+ XDG_CURRENT_DESKTOP: xdg,
+ });
} else if (xdg?.includes('KDE')) {
- return openSettings('systemsettings5', {
- XDG_CURRENT_DESKTOP: xdg,
- });
+ return openSettings('systemsettings5', [], {
+ XDG_CURRENT_DESKTOP: xdg,
+ });
} else {
+ return { success: false, error: `Unsupported desktop environment: ${xdg}` };
}
}
if (isMacOs()) {
- return openSettings('open "x-apple.systempreferences:com.apple.Bluetooth"');
+ return openSettings('open', ['x-apple.systempreferences:com.apple.Bluetooth']);
}
if (isWindows()) {
- return openSettings('start ms-settings:bluetooth');
+ return openSettings('start', ['ms-settings:bluetooth']);
}
return { success: false, error: 'Unsupported os' };
};
🏁 Script executed:
#!/bin/bash
# Re-verify existence and version info for desktop-setting commands using a more reliable check (command -v)
if command -v gnome-control-center > /dev/null; then
echo "gnome-control-center found, version:"
gnome-control-center --version || echo "Version check failed for gnome-control-center"
else
echo "gnome-control-center not found"
fi
if command -v systemsettings5 > /dev/null; then
echo "systemsettings5 found, version:"
systemsettings5 --version || echo "Version check failed for systemsettings5"
else
echo "systemsettings5 not found"
fi
Length of output: 194
Enhance Error Handling and Desktop Command Validation in openBluetoothSettings
The suggested improvements focus on:
- Validating the desktop environment variable (xdg) and returning a clear error if it’s missing.
- Providing a fallback for unsupported desktop environments.
- Refactoring the command invocations to use argument arrays.
- NOTE: Manual verification is recommended because our recent checks show that both
gnome-control-center
andsystemsettings5
were not found in the environment. Please verify that these commands (or appropriate alternatives) are installed and available on the target system.
const openBluetoothSettings = () => {
if (isLinux()) {
// Electron sets XDG_CURRENT_DESKTOP; prefer ORIGINAL_XDG_CURRENT_DESKTOP if available
const xdg = process.env.ORIGINAL_XDG_CURRENT_DESKTOP || process.env.XDG_CURRENT_DESKTOP;
+ if (!xdg) {
+ return { success: false, error: 'Desktop environment not detected' };
+ }
if (xdg?.includes('GNOME')) {
- return openSettings('gnome-control-center bluetooth', {
- XDG_CURRENT_DESKTOP: xdg,
- });
+ return openSettings('gnome-control-center', ['bluetooth'], {
+ XDG_CURRENT_DESKTOP: xdg,
+ });
} else if (xdg?.includes('KDE')) {
- return openSettings('systemsettings5', {
- XDG_CURRENT_DESKTOP: xdg,
- });
+ return openSettings('systemsettings5', [], {
+ XDG_CURRENT_DESKTOP: xdg,
+ });
} else {
+ return { success: false, error: `Unsupported desktop environment: ${xdg}` };
}
}
if (isMacOs()) {
- return openSettings('open "x-apple.systempreferences:com.apple.Bluetooth"');
+ return openSettings('open', ['x-apple.systempreferences:com.apple.Bluetooth']);
}
if (isWindows()) {
- return openSettings('start ms-settings:bluetooth');
+ return openSettings('start', ['ms-settings:bluetooth']);
}
return { success: false, error: 'Unsupported os' };
};
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const openBluetoothSettings = () => { | |
if (isLinux()) { | |
// https://github.com/electron/electron/blob/ab2a4fd836d539194bc5cde5f0d665eddeb6a134/docs/api/environment-variables.md?plain=1#L190 | |
// Electron modifies the value of XDG_CURRENT_DESKTOP | |
const xdg = process.env.ORIGINAL_XDG_CURRENT_DESKTOP || process.env.XDG_CURRENT_DESKTOP; | |
if (xdg?.includes('GNOME')) { | |
return openSettings('gnome-control-center bluetooth', { | |
XDG_CURRENT_DESKTOP: xdg, | |
}); | |
} else if (xdg?.includes('KDE')) { | |
return openSettings('systemsettings5', { | |
XDG_CURRENT_DESKTOP: xdg, | |
}); | |
} | |
} | |
if (isMacOs()) { | |
return openSettings('open "x-apple.systempreferences:com.apple.Bluetooth"'); | |
} | |
if (isWindows()) { | |
return openSettings('start ms-settings:bluetooth'); | |
} | |
return { success: false, error: 'Unsupported os' }; | |
}; | |
const openBluetoothSettings = () => { | |
if (isLinux()) { | |
// Electron sets XDG_CURRENT_DESKTOP; prefer ORIGINAL_XDG_CURRENT_DESKTOP if available | |
const xdg = process.env.ORIGINAL_XDG_CURRENT_DESKTOP || process.env.XDG_CURRENT_DESKTOP; | |
if (!xdg) { | |
return { success: false, error: 'Desktop environment not detected' }; | |
} | |
if (xdg?.includes('GNOME')) { | |
return openSettings('gnome-control-center', ['bluetooth'], { | |
XDG_CURRENT_DESKTOP: xdg, | |
}); | |
} else if (xdg?.includes('KDE')) { | |
return openSettings('systemsettings5', [], { | |
XDG_CURRENT_DESKTOP: xdg, | |
}); | |
} else { | |
return { success: false, error: `Unsupported desktop environment: ${xdg}` }; | |
} | |
} | |
if (isMacOs()) { | |
return openSettings('open', ['x-apple.systempreferences:com.apple.Bluetooth']); | |
} | |
if (isWindows()) { | |
return openSettings('start', ['ms-settings:bluetooth']); | |
} | |
return { success: false, error: 'Unsupported os' }; | |
}; |
@@ -183,5 +183,6 @@ export const factory = <R extends StrictIpcRenderer<any, IpcRendererEvent>>( | |||
connectPopupResponse: response => ipcRenderer.invoke('connect-popup/response', response), | |||
|
|||
getSystemInformation: () => ipcRenderer.invoke('system/get-system-information'), | |||
openSystemSettings: settings => ipcRenderer.invoke('system/open-settings', settings), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add input validation for settings parameter.
Other methods in the factory include input validation, but it's missing for openSystemSettings
.
Add validation:
- openSystemSettings: settings => ipcRenderer.invoke('system/open-settings', settings),
+ openSystemSettings: settings => {
+ if (validation.isPrimitive('string', settings) && ['bluetooth', 'camera', 'privacy'].includes(settings)) {
+ return ipcRenderer.invoke('system/open-settings', settings);
+ }
+ return Promise.resolve({ success: false, error: 'invalid params' });
+ },
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
openSystemSettings: settings => ipcRenderer.invoke('system/open-settings', settings), | |
openSystemSettings: settings => { | |
if (validation.isPrimitive('string', settings) && ['bluetooth', 'camera', 'privacy'].includes(settings)) { | |
return ipcRenderer.invoke('system/open-settings', settings); | |
} | |
return Promise.resolve({ success: false, error: 'invalid params' }); | |
}, |
Description
Another cherry pick from the
bluetooth
branch.Open system settings using desktopApi.
Originally i did it only for the bluetooth module but then i realized it could be also used to open other settings, like for example camera settings if it was once declined (useful especially on macos)
tested on linux ubuntu gnome, debian gnome, macos and windows, i didnt tried KDE or any other