Repository navigation
feat: file-type associations with auto-detected local apps - #5
Conversation
Scan the development store for projects declaring electron-builder
fileAssociations and offer them as Open with targets: unpacked exe,
portable artifact, or dev Electron fallback. Settings gains a File apps
section with the detected list, a per-extension default picker, and a
custom-app form ({file} argument template). Double-click, Enter, Open and
the right-click menu honor the chosen default through the existing
injection-safe spawner; launches are validated in main.
Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Review Summary
This PR introduces the file-type association feature with auto-detection of local Electron apps, settings UI, and IPC plumbing. The core logic is well-structured and input validation is thorough in most places. However, there are two blocking platform correctness bugs and two security concerns that need to be addressed before merging.
🛑 Must-Fix Issues
-
apps:openbroken on non-Windows (src/main/main.tsline 244): The\.exe|com|cmd|batexecutable extension guard unconditionally blocks all launches on macOS and Linux, including thedev-mode Electron binary (no extension) and any custom app. This renders the entireopenWithAppflow non-functional on those platforms. -
win-unpackeddetection broken on non-Windows (src/main/app-detect.tslines 37–39): The unpacked binary lookup hardcodes${name}.exe, so theunpackedsource path is never found on macOS (.appbundle) or Linux (no extension), silently degrading toportable/devfallback even when an unpacked build is present. -
Path traversal via unconstrained
apps:detectroot (src/main/app-detect.tslines 55–59): The IPC handler accepts any absolute path, allowing a compromised renderer to scan arbitrary filesystem locations and leak directory/file contents viapackage.jsonreads. -
spawnDetachedshell-injection risk (src/main/main.tslines 256–258):resolveAppArgsinjects file paths into args before passing tospawnDetached. If the spawner ever uses a shell invocation internally, a crafted file path could execute arbitrary OS commands. This should be verified and explicitly enforced.
ℹ️ Minor Note
The extensionOptions function seeds from raw prefs.defaults keys without normalizing them, which could expose un-normalized extension strings in the UI if prefs are loaded from an unexpected source. A defensive normalization pass is suggested.
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
| if (!existsSync(filePath)) throw new Error('That file no longer exists.'); | ||
| const command = typeof request.command === 'string' ? request.command.trim() : ''; | ||
| if (!command || !isAbsolute(command) || !existsSync(command)) throw new Error('App executable not found.'); | ||
| if (!/\.(exe|com|cmd|bat)$/i.test(command)) throw new Error('The app must be an executable file.'); |
There was a problem hiding this comment.
🛑 Logic Error: The executable extension check /\.(exe|com|cmd|bat)$/i only matches Windows executable formats. On macOS and Linux — where the detected source: 'dev' path resolves to electron or Electron.app/.../Electron (no extension), and custom apps may be any binary — this check will unconditionally reject every valid launch and throw 'The app must be an executable file.'. The apps:open handler is completely broken on non-Windows platforms.
| if (!/\.(exe|com|cmd|bat)$/i.test(command)) throw new Error('The app must be an executable file.'); | |
| if (process.platform === 'win32' && !/\.(exe|com|cmd|bat)$/i.test(command)) throw new Error('The app must be an executable file.'); |
| for (const out of outputs) { | ||
| const unpacked = join(dir, out, 'win-unpacked', `${name}.exe`); | ||
| if (existsSync(unpacked)) return { launch: { command: unpacked, args: ['{file}'] }, source: 'unpacked' }; |
There was a problem hiding this comment.
🛑 Logic Error: The win-unpacked lookup constructs a ${name}.exe path unconditionally. On macOS, the equivalent would be a .app bundle, and on Linux there is no .exe extension. This means the unpacked source is never found on non-Windows platforms, silently falling through to the portable or dev fallback even when a proper unpacked build exists. The PR description states multi-platform support, so this will silently produce wrong results on macOS/Linux.
| export function extensionOptions(detected: readonly FileApp[], prefs: FileAppPrefs): string[] { | ||
| const seen = new Set<string>(Object.keys(prefs.defaults)); | ||
| for (const app of [...detected, ...prefs.customApps.map(customFileApp)]) { | ||
| for (const ext of app.extensions) seen.add(ext); | ||
| } | ||
| return [...seen].sort(); | ||
| } |
There was a problem hiding this comment.
🛑 Logic Error: extensionOptions seeds the seen set with Object.keys(prefs.defaults), which are raw stored keys (e.g. .md as strings from localStorage). These keys were validated and normalized to lowercase dot-prefixed form by parseFileAppPrefs, so they will be in the set in their normalized form. However, keys from detected and customApps are also already normalized (.md). The issue is that if a stale/un-normalized key somehow reaches prefs.defaults (bypassing parseFileAppPrefs), it would appear in the dropdown without matching any extension lookup, causing a broken default that silently does nothing. The function should normalize the keys as it iterates to be defensively consistent with resolveDefaultApp.
| export function extensionOptions(detected: readonly FileApp[], prefs: FileAppPrefs): string[] { | |
| const seen = new Set<string>(Object.keys(prefs.defaults)); | |
| for (const app of [...detected, ...prefs.customApps.map(customFileApp)]) { | |
| for (const ext of app.extensions) seen.add(ext); | |
| } | |
| return [...seen].sort(); | |
| } | |
| export function extensionOptions(detected: readonly FileApp[], prefs: FileAppPrefs): string[] { | |
| const seen = new Set<string>(); | |
| for (const key of Object.keys(prefs.defaults)) { | |
| const ext = normalizeExtension(key); | |
| if (ext) seen.add(ext); | |
| } | |
| for (const app of [...detected, ...prefs.customApps.map(customFileApp)]) { | |
| for (const ext of app.extensions) seen.add(ext); | |
| } | |
| return [...seen].sort(); | |
| } |
| const resolved = resolveAppArgs(args as string[], filePath); | ||
| await new Promise<void>((resolvePromise, reject) => { | ||
| spawnDetached(command, resolved, { cwd, onError: reject }); |
There was a problem hiding this comment.
🛑 Security Vulnerability: The apps:open handler resolves {file} into the args array via resolveAppArgs and then passes those resolved args directly to spawnDetached. If spawnDetached internally uses child_process.exec or joins args into a shell command string (rather than using child_process.spawn with an array), a file path containing shell metacharacters (e.g. ; rm -rf ~) would be executed as a shell command. Confirm that spawnDetached always uses spawn/execFile with a separate args array — never a shell-interpolated command string — to prevent OS command injection.1
Footnotes
-
CWE-78: OS Command Injection - https://cwe.mitre.org/data/definitions/78.html ↩
| export async function detectLocalApps(rootRaw: unknown): Promise<DetectedApp[]> { | ||
| const root = | ||
| typeof rootRaw === 'string' && rootRaw.trim() && isAbsolute(rootRaw.trim()) | ||
| ? rootRaw.trim() | ||
| : defaultAppsRoot(); |
There was a problem hiding this comment.
🛑 Logic Error: detectLocalApps accepts any user-supplied absolute path as rootRaw, which is exposed via the apps:detect IPC channel. A compromised renderer could pass a crafted absolute path to scan directories outside the intended development store (e.g. /etc or C:\Windows), reading arbitrary package.json files and leaking directory structure information. The IPC handler at handle('apps:detect', ...) performs no additional path restriction. Consider validating that the supplied root is confined to expected parent directories (e.g. the user's home directory), or restricting the scan to the configured prefs value only.
Review follow-up: the apps:open executable check now only requires .exe/.com/.cmd/.bat on Windows; POSIX launches validate the execute bit instead, so detected dev-mode and custom binaries can launch there. Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
|
Review follow-up merged (886f2b3): apps:open now requires .exe/.com/.cmd/.bat only on Windows; on macOS/Linux it validates the execute bit instead, so extension-less binaries launch (+2 tests). The injection flag is covered by design: spawnDetached uses spawn with an argument array (cmd.exe shims quote each arg via its own env var), never a shell-interpolated string. apps:detect reads only package.json one level under the user-configured apps folder from our own renderer; the root is a Settings field by design. Detection artifacts remain Windows-first (win-unpacked/portable); macOS/Linux use the dev-Electron fallback or custom apps. Merged as 813a73d; released as v0.3.0. |
Stacked on #4 — merge that first; until then this PR also shows its two commits, and the diff shrinks to this feature alone once #4 lands.
What
~/Documents/development, changeable in Settings) for projects whosepackage.jsondeclares electron-builderbuild.fileAssociations. Launch target preference:win-unpackedexe → newest portable artifact → the project's own Electron binary (dev mode). Markdown Viewer 0.2.3 is detected with its six extensions (md, markdown, mdown, mkd, mkdn, mdx).{file}argument template and extension list.New files
src/shared/file-apps.ts(pure logic),src/main/app-detect.ts(scan),src/renderer/file-apps-settings.ts(storage),tests/file-apps.test.ts+tests/app-detect.test.ts(25 new tests).Verification
npm run cigreen: typecheck + 213 tests (was 188) + build;git diff --checkclean..mddefault persists; right-click menu contains "Open with Markdown Viewer"; double-clicking a.mdactually launched Markdown Viewer with that file; a custom app added through the form opened a.txtvia Notepad; launched processes cleaned up, test custom app removed.Docs: PROGRESS/ROADMAP (P1.9)/README updated.