Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions apps/frontend/src/main/ipc-handlers/terminal-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { terminalNameGenerator } from '../terminal-name-generator';
import { readSettingsFileAsync } from '../settings-utils';
import { debugLog, } from '../../shared/utils/debug-logger';
import { migrateSession } from '../claude-profile/session-utils';
import { createProfileDirectory } from '../claude-profile/profile-utils';
import { createProfileDirectory, expandHomePath } from '../claude-profile/profile-utils';
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The expandHomePath utility is correctly imported here. This is a good practice to centralize path manipulation logic.

import { isValidConfigDir } from '../utils/config-path-validator';


Expand Down Expand Up @@ -161,10 +161,13 @@ export function registerTerminalHandlers(
};
}

// Ensure config directory exists
// Ensure config directory exists (expand ~ before passing to Node.js fs functions,
// which do not perform shell tilde expansion)
const resolvedConfigDir = expandHomePath(profile.configDir);
profile.configDir = resolvedConfigDir;
const { mkdirSync, existsSync } = await import('fs');
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

Replace dynamic import('fs') with a static top-level import

fs is a Node.js built-in always present in the Electron main process; a dynamic await import('fs') adds unnecessary overhead on every call and obscures the module's dependencies.

♻️ Proposed refactor

At the top of the file, add:

+import { existsSync, mkdirSync } from 'fs';

Then remove the inline dynamic import:

-        const { mkdirSync, existsSync } = await import('fs');
📝 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.

Suggested change
const { mkdirSync, existsSync } = await import('fs');
import { existsSync, mkdirSync } from 'fs';
// ... rest of file ...
// Dynamic import removed - fs is now imported at the top
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/frontend/src/main/ipc-handlers/terminal-handlers.ts` at line 168,
Replace the dynamic in-function import of fs with a top-level static import: add
a top-level import that brings in mkdirSync and existsSync from the built-in fs
module, then remove the inline "const { mkdirSync, existsSync } = await
import('fs');" statement in terminal-handlers.ts (where that const appears) so
the code uses the top-level symbols directly (update any references in the
surrounding function if needed).

if (!existsSync(profile.configDir)) {
mkdirSync(profile.configDir, { recursive: true });
if (!existsSync(resolvedConfigDir)) {
mkdirSync(resolvedConfigDir, { recursive: true });
}
Comment on lines +164 to 171
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Synchronous existsSync/mkdirSync blocks the Electron main process; existsSync check is also a redundant TOCTOU

This is an async IPC handler; mkdirSync and existsSync are synchronous and will block the main process event loop during any I/O wait. The existsSync check before mkdirSync({ recursive: true }) is also a TOCTOU race — mkdirSync with recursive: true is already idempotent (silently succeeds when the directory exists), making the guard redundant.

Prefer fs/promises.mkdir:

⚡ Proposed fix: use async mkdir
-        const { mkdirSync, existsSync } = await import('fs');
-        if (!existsSync(profile.configDir)) {
-          mkdirSync(profile.configDir, { recursive: true });
-        }
+        const { mkdir } = await import('fs/promises');
+        await mkdir(profile.configDir, { recursive: true });
📝 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.

Suggested change
// Ensure config directory exists (expand ~ before passing to Node.js fs functions,
// which do not perform shell tilde expansion)
const resolvedConfigDir = expandHomePath(profile.configDir);
profile.configDir = resolvedConfigDir;
const { mkdirSync, existsSync } = await import('fs');
if (!existsSync(profile.configDir)) {
mkdirSync(profile.configDir, { recursive: true });
if (!existsSync(resolvedConfigDir)) {
mkdirSync(resolvedConfigDir, { recursive: true });
}
// Ensure config directory exists (expand ~ before passing to Node.js fs functions,
// which do not perform shell tilde expansion)
const resolvedConfigDir = expandHomePath(profile.configDir);
profile.configDir = resolvedConfigDir;
const { mkdir } = await import('fs/promises');
await mkdir(resolvedConfigDir, { recursive: true });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/frontend/src/main/ipc-handlers/terminal-handlers.ts` around lines 164 -
171, The handler currently uses synchronous fs functions (existsSync and
mkdirSync) which block the Electron main process and also performs a redundant
TOCTOU check; replace the sync calls by importing and calling the promise-based
mkdir from 'fs/promises' and await it (use await mkdir(resolvedConfigDir, {
recursive: true })); remove the existsSync guard entirely, keep the
expandHomePath call and assignment to profile.configDir, and ensure any import
reference to mkdirSync/existsSync is removed or replaced so only the async mkdir
is used.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ export function AccountSettings({ settings, onSettingsChange, isOpen }: AccountS
description: authResult.error || t('accounts.toast.tryAgain'),
});
}
} else if (!result.success) {
toast({
variant: 'destructive',
title: t('accounts.toast.addProfileFailed'),
description: result.error || t('accounts.toast.tryAgain'),
});
}
} catch (_err) {
toast({
Expand Down
Loading