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
1 change: 1 addition & 0 deletions PROGRESS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Open check: builtin Whisper model download under the narrowed CSP `connect-src`
- In-app name dialog replaces `window.prompt` (Electron never implements it): New Folder, New File type/name, Rename (F2) and favorite naming now work in both panes; inline validation, Esc/Cancel aborts, new folders land selected
- Explorer-style drag-drop inside and between panes: drag = move, Ctrl = copy, Shift = force move, cross-drive defaults to copy; targets are folder rows/tiles, pane background, ".." and breadcrumbs; whole-selection drag; both panes refresh and the transcript logs each op; same-folder moves and drops into a folder's own subtree are no-ops
- Name dialog + drag-drop rules covered by 24 new tests (188 total); `npm run ci` passes; verified in a real hidden Electron window against a temp filesystem: 14/14 UI checks (dialog in the right pane, invalid-name error, Esc cancel, F2 rename, cross-pane move, Ctrl copy, folder-row drop, same-folder no-op)
- Native-app detection: projects without Electron associations can declare a `zerogapp.json` manifest (`name`, `version`, `extensions`, `launch.command`/`args`/`cwd`); relative commands resolve against the project dir, missing executables are skipped, `package.json` associations still win; the Rust sister apps ZeroG Edit / ZeroG Play use this (8 new tests; 225 total)

## Next

Expand Down
45 changes: 35 additions & 10 deletions src/main/app-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
packageAppName,
packageAssociations,
pickPortableExe,
zerogAppManifest,
type AppLaunch,
type DetectedApp
} from '../shared/file-apps.js';
Expand Down Expand Up @@ -67,24 +68,48 @@ export async function detectLocalApps(rootRaw: unknown): Promise<DetectedApp[]>
for (const entry of entries) {
if (!entry.isDirectory() || entry.name.startsWith('.') || entry.name === 'node_modules') continue;
const projectDir = join(root, entry.name);
let detected = false;
try {
const pkg = JSON.parse(await readFile(join(projectDir, 'package.json'), 'utf8')) as Record<string, unknown>;
const extensions = packageAssociations(pkg);
if (!extensions.length) continue;
const name = packageAppName(pkg) || entry.name;
const found = await launchForProject(projectDir, name, pkg);
if (!found) continue;
if (extensions.length) {
const name = packageAppName(pkg) || entry.name;
const found = await launchForProject(projectDir, name, pkg);
if (found) {
apps.push({
id: `detected:${entry.name}`,
name,
version: typeof pkg.version === 'string' ? pkg.version : '',
extensions,
launch: found.launch,
source: found.source,
projectDir
});
detected = true;
}
}
} catch {
/* no package.json — try a native manifest below */
}
Comment on lines +91 to +93

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛑 Logic Error: The catch block at line 91 swallows all exceptions from the entire try block, including errors thrown by launchForProject (which performs async file I/O). If launchForProject throws — for example, due to a filesystem permission error — the exception is treated as "no package.json", detected stays false, and the code then tries to read zerogapp.json for a project that actually has a valid package.json. This silently drops legitimate Electron projects from detection results. The catch block should only guard the readFile/JSON.parse for package.json, not the launch resolution logic.

if (detected) continue;
try {
const manifest = zerogAppManifest(JSON.parse(await readFile(join(projectDir, 'zerogapp.json'), 'utf8')));
if (!manifest) continue;
const command = isAbsolute(manifest.launch.command)
? manifest.launch.command
: join(projectDir, manifest.launch.command);
if (!existsSync(command)) continue;
apps.push({
id: `detected:${entry.name}`,
name,
version: typeof pkg.version === 'string' ? pkg.version : '',
extensions,
launch: found.launch,
source: found.source,
name: manifest.name,
version: manifest.version,
extensions: manifest.extensions,
launch: { ...manifest.launch, command },
source: 'manifest',
projectDir
});
} catch {
/* not an app project */
/* not a manifest project */
}
}
return apps.sort((a, b) => a.name.localeCompare(b.name));
Expand Down
39 changes: 38 additions & 1 deletion src/shared/file-apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type DetectedApp = {
version: string;
extensions: string[];
launch: AppLaunch;
source: 'unpacked' | 'portable' | 'dev';
source: 'unpacked' | 'portable' | 'dev' | 'manifest';
projectDir: string;
};

Expand Down Expand Up @@ -249,6 +249,43 @@ export function packageAppName(pkg: unknown): string {
return typeof source.name === 'string' ? source.name.trim() : '';
}

/** A native (non-Electron) app's zerogapp.json manifest, validated. */
export type ZerogAppManifest = {
name: string;
version: string;
extensions: string[];
launch: AppLaunch;
};

export function zerogAppManifest(raw: unknown): ZerogAppManifest | null {
if (!raw || typeof raw !== 'object') return null;
const source = raw as Record<string, unknown>;
const name = typeof source.name === 'string' ? source.name.trim() : '';
if (!name || name.length > 80) return null;
const version = typeof source.version === 'string' ? source.version.trim() : '';
const extensions = parseExtensionList(
Array.isArray(source.extensions) ? source.extensions.join(',') : source.extensions
);
if (!extensions.length) return null;
const launchRaw = source.launch;
if (!launchRaw || typeof launchRaw !== 'object') return null;
const launchSource = launchRaw as Record<string, unknown>;
const command = typeof launchSource.command === 'string' ? launchSource.command.trim() : '';
if (!command || command.length > 2048) return null;
const args = Array.isArray(launchSource.args)
? launchSource.args
.filter((arg): arg is string => typeof arg === 'string' && arg.length <= 2048 && !/[\0\r\n]/.test(arg))
.slice(0, 32)
: [];
const cwd = typeof launchSource.cwd === 'string' && launchSource.cwd.trim() ? launchSource.cwd.trim() : undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛑 Security Vulnerability: The cwd value from the manifest is accepted and stored without validation. Unlike command, which is resolved against projectDir and checked for existence with existsSync, cwd is passed through verbatim (after a trim()). A malicious zerogapp.json could supply a cwd like ../../../sensitive/dir, causing the spawned process to run in an arbitrary directory outside the project. At minimum, cwd should be validated as an absolute path; if it must support relative paths, it should be resolved against projectDir and kept within it.

return {
name,
version,
extensions,
launch: { command, args: args.length ? args : ['{file}'], ...(cwd ? { cwd } : {}) }
};
}

/** Best launchable exe among build artifacts: portable over installer-free, newest version first. */
export function pickPortableExe(names: readonly string[]): string | null {
const candidates = names.filter(
Expand Down
89 changes: 89 additions & 0 deletions tests/app-detect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,92 @@ describe('detectLocalApps', () => {
expect(await detectLocalApps(join(root, 'does-not-exist'))).toEqual([]);
});
});

describe('detectLocalApps — zerogapp.json manifests', () => {
let manifestRoot: string;

async function manifestProject(name: string, manifest: unknown, files: string[] = []): Promise<void> {
const dir = join(manifestRoot, name);
await mkdir(dir, { recursive: true });
await writeFile(join(dir, 'zerogapp.json'), JSON.stringify(manifest));
for (const file of files) {
const target = join(dir, file);
await mkdir(join(target, '..'), { recursive: true });
await writeFile(target, 'x');
}
}

beforeAll(async () => {
manifestRoot = await mkdtemp(join(tmpdir(), 'zerog-manifest-'));
await manifestProject(
'zerogedit',
{
name: 'ZeroG Edit',
version: '0.1.0',
extensions: ['txt', 'md'],
launch: { command: 'target/release/zerogedit.exe', args: ['{file}'] }
},
['target/release/zerogedit.exe']
);
await manifestProject('no-exe', {
name: 'No Exe',
version: '1.0.0',
extensions: ['xyz'],
launch: { command: 'target/release/noexe.exe' }
});
await manifestProject('bad-manifest', { name: 'Bad', extensions: [], launch: { command: 'x.exe' } }, ['x.exe']);
await manifestProject('absolute', {
name: 'Absolute App',
version: '2.0.0',
extensions: ['.wav'],
launch: { command: join(manifestRoot, 'zerogedit', 'target', 'release', 'zerogedit.exe') }
});
});

afterAll(async () => {
await rm(manifestRoot, { recursive: true, force: true });
});

it('detects native apps via zerogapp.json and resolves relative commands', async () => {
const apps = await detectLocalApps(manifestRoot);
expect(apps.map((app) => app.id)).toEqual(['detected:absolute', 'detected:zerogedit']);
const edit = apps[1];
expect(edit.name).toBe('ZeroG Edit');
expect(edit.version).toBe('0.1.0');
expect(edit.extensions).toEqual(['.txt', '.md']);
expect(edit.source).toBe('manifest');
expect(edit.launch.command).toBe(join(manifestRoot, 'zerogedit', 'target', 'release', 'zerogedit.exe'));
expect(edit.launch.args).toEqual(['{file}']);
});

it('defaults args to {file} and keeps absolute commands', async () => {
const apps = await detectLocalApps(manifestRoot);
const absolute = apps[0];
expect(absolute.launch.args).toEqual(['{file}']);
expect(absolute.launch.command).toBe(join(manifestRoot, 'zerogedit', 'target', 'release', 'zerogedit.exe'));
expect(absolute.extensions).toEqual(['.wav']);
});

it('package.json associations win when both exist', async () => {
const bothRoot = await mkdtemp(join(tmpdir(), 'zerog-both-'));
try {
const dir = join(bothRoot, 'both');
await mkdir(join(dir, 'dist', 'win-unpacked'), { recursive: true });
await writeFile(join(dir, 'dist', 'win-unpacked', 'Both Electron.exe'), 'x');
await writeFile(
join(dir, 'package.json'),
JSON.stringify({ name: 'both', version: '9.9.9', build: { productName: 'Both Electron', fileAssociations: [{ ext: 'md' }] } })
);
await writeFile(
join(dir, 'zerogapp.json'),
JSON.stringify({ name: 'Both Native', version: '0.1.0', extensions: ['txt'], launch: { command: 'dist/win-unpacked/Both Electron.exe' } })
);
const apps = await detectLocalApps(bothRoot);
expect(apps).toHaveLength(1);
expect(apps[0].name).toBe('Both Electron');
expect(apps[0].source).toBe('unpacked');
} finally {
await rm(bothRoot, { recursive: true, force: true });
}
});
});
45 changes: 45 additions & 0 deletions tests/file-apps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
pickPortableExe,
resolveAppArgs,
resolveDefaultApp,
zerogAppManifest,
type CustomApp,
type DetectedApp,
type FileAppPrefs
Expand Down Expand Up @@ -251,3 +252,47 @@ describe('appCommandProblem', () => {
expect(appCommandProblem('', 'linux')).toBe('App executable not found.');
});
});

describe('zerogAppManifest', () => {
const valid = {
name: 'ZeroG Play',
version: '0.1.0',
extensions: ['mp3', '.FLAC'],
launch: { command: 'target/release/zerogplay.exe', args: ['{file}'] }
};

it('parses a valid manifest and normalizes extensions', () => {
const manifest = zerogAppManifest(valid);
expect(manifest).not.toBeNull();
expect(manifest!.name).toBe('ZeroG Play');
expect(manifest!.extensions).toEqual(['.mp3', '.flac']);
expect(manifest!.launch).toEqual({ command: 'target/release/zerogplay.exe', args: ['{file}'] });
});

it('defaults missing args to {file}', () => {
const manifest = zerogAppManifest({ ...valid, launch: { command: 'app.exe' } });
expect(manifest!.launch.args).toEqual(['{file}']);
});

it('keeps an optional cwd', () => {
const manifest = zerogAppManifest({ ...valid, launch: { command: 'app.exe', cwd: 'C:/apps' } });
expect(manifest!.launch.cwd).toBe('C:/apps');
});

it('rejects missing name, extensions, launch, or command', () => {
expect(zerogAppManifest(null)).toBeNull();
expect(zerogAppManifest({ ...valid, name: '' })).toBeNull();
expect(zerogAppManifest({ ...valid, extensions: [] })).toBeNull();
expect(zerogAppManifest({ ...valid, launch: undefined })).toBeNull();
expect(zerogAppManifest({ ...valid, launch: { command: '' } })).toBeNull();
expect(zerogAppManifest({ ...valid, name: 'x'.repeat(81) })).toBeNull();
});

it('filters unsafe or oversized args', () => {
const manifest = zerogAppManifest({
...valid,
launch: { command: 'app.exe', args: ['{file}', 42, 'bad\0arg', 'ok'] }
});
expect(manifest!.launch.args).toEqual(['{file}', 'ok']);
});
});
Loading