Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Refuse folders and disguised symlinks in Open in default app (PRD-9020) (#6052)

Andrew Mikofalvy4d1d6b896b62
diff --git a/.changeset/open-asset-refuses-folders.md b/.changeset/open-asset-refuses-folders.md
new file mode 100644
index 000000000..6ddaad318
--- /dev/null
+++ b/.changeset/open-asset-refuses-folders.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Open in default app now reveals folders, app bundles, extensionless files and links whose real file has a different extension in the file manager instead of opening them. More script and installer types, such as `.py`, `.tool` and `.cpl`, are blocked too.
diff --git a/knip.config.ts b/knip.config.ts
index 69344114d..0004a763a 100644
--- a/knip.config.ts
+++ b/knip.config.ts
@@ -149,11 +149,15 @@ export default {
'packages/server': {
entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],
project: 'src/**',
- ignoreDependencies: ['@types/shell-quote'],
+ ignoreDependencies: [
+ '@types/shell-quote',
+ ],
},
'packages/cli': {
entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],
- ignoreDependencies: ['yjs'],
+ ignoreDependencies: [
+ 'yjs',
+ ],
},
'packages/desktop': {
entry: [
diff --git a/packages/app/src/components/TerminalPanel.dom.test.tsx b/packages/app/src/components/TerminalPanel.dom.test.tsx
index 99d3e97e9..acaf89621 100644
--- a/packages/app/src/components/TerminalPanel.dom.test.tsx
+++ b/packages/app/src/components/TerminalPanel.dom.test.tsx
@@ -2920,6 +2920,10 @@ describe('TerminalPanel', () => {
openAsset.mockResolvedValueOnce({ ok: false, reason: 'extension-blocked' });
link?.activate({} as MouseEvent, link.text);
await waitFor(() => expect(revealAsset).toHaveBeenCalledWith('data/x.csv'));
+
+ openAsset.mockResolvedValueOnce({ ok: false, reason: 'not-a-file' });
+ link?.activate({} as MouseEvent, link.text);
+ await waitFor(() => expect(revealAsset).toHaveBeenCalledTimes(2));
});
test('clicking an out-of-project absolute path routes the reveal-external dialog', async () => {
diff --git a/packages/app/src/components/TerminalPanel.tsx b/packages/app/src/components/TerminalPanel.tsx
index a5bd157c5..a5f7e7e8e 100644
--- a/packages/app/src/components/TerminalPanel.tsx
+++ b/packages/app/src/components/TerminalPanel.tsx
@@ -3,6 +3,7 @@ import '@xterm/xterm/css/xterm.css';
import {
assertNeverPtyAdoptReason,
assertNeverPtyCreateReason,
+ assetOpenRefusalRevealsInstead,
} from '@inkeep/open-knowledge-core/desktop-bridge';
import {
buildCliLaunchArgString,
@@ -283,7 +284,7 @@ function TerminalSession({
.openAsset(target.relPath)
.then((result) => {
if (result.ok) return;
- if (result.reason === 'extension-blocked') {
+ if (assetOpenRefusalRevealsInstead(result.reason)) {
void bridge.shell
.revealAsset(target.relPath)
.catch((err) => console.warn('[terminal] revealAsset failed:', err));
diff --git a/packages/app/src/editor/asset-dispatch/dispatcher.test.ts b/packages/app/src/editor/asset-dispatch/dispatcher.test.ts
index 5f27b2c75..f45b1886c 100644
--- a/packages/app/src/editor/asset-dispatch/dispatcher.test.ts
+++ b/packages/app/src/editor/asset-dispatch/dispatcher.test.ts
@@ -115,6 +115,43 @@ describe('dispatchAssetClick', () => {
expect(openUrl).not.toHaveBeenCalled();
});
+ test('not-a-file refusal (a directory or app bundle) reveals it instead of opening', async () => {
+ const openAsset = vi.fn(async (_: string) => ({ ok: false, reason: 'not-a-file' }) as const);
+ const revealAsset = vi.fn(async (_: string) => ({ ok: true }) as const);
+ const openUrl = vi.fn((_: string) => {});
+ const desktopBridge = {
+ shell: { openAsset, revealAsset },
+ } as unknown as NonNullable<typeof window.okDesktop>;
+
+ await dispatchAssetClick(ctx({ ext: 'app', projectRelPath: 'tools/Calculator.app' }), {
+ desktopBridge,
+ openUrl,
+ });
+
+ expect(revealAsset).toHaveBeenCalledWith('tools/Calculator.app');
+ expect(openUrl).not.toHaveBeenCalled();
+ });
+
+ test('a refusal reason unknown to this renderer is logged, not thrown', async () => {
+ const openAsset = vi.fn(async (_: string) => ({ ok: false, reason: 'quarantined' }) as const);
+ const revealAsset = vi.fn(async (_: string) => ({ ok: true }) as const);
+ const desktopBridge = {
+ shell: { openAsset, revealAsset },
+ } as unknown as NonNullable<typeof window.okDesktop>;
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ try {
+ await dispatchAssetClick(ctx(), { desktopBridge });
+ expect(revealAsset).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledWith(
+ '[asset-dispatch] openAsset refused:',
+ 'quarantined',
+ expect.objectContaining({ projectRelPath: 'notes/meeting.pdf' }),
+ );
+ } finally {
+ warn.mockRestore();
+ }
+ });
+
test('non-blocked refusal (resolve-error) is logged, does not reveal or fall through to web', async () => {
const openAsset = vi.fn(async (_: string) => ({ ok: false, reason: 'resolve-error' }) as const);
const revealAsset = vi.fn(async (_: string) => ({ ok: true }) as const);
diff --git a/packages/app/src/editor/asset-dispatch/dispatcher.ts b/packages/app/src/editor/asset-dispatch/dispatcher.ts
index 3d4036a8a..74831fa3e 100644
--- a/packages/app/src/editor/asset-dispatch/dispatcher.ts
+++ b/packages/app/src/editor/asset-dispatch/dispatcher.ts
@@ -1,3 +1,4 @@
+import { assetOpenRefusalRevealsInstead } from '@inkeep/open-knowledge-core/desktop-bridge';
import { type AssetViewerRegistry, assetViewerRegistry } from './registry.ts';
import type { AssetClickContext } from './types.ts';
@@ -30,7 +31,7 @@ export async function dispatchAssetClick(
if (desktopBridge) {
const result = await desktopBridge.shell.openAsset(ctx.projectRelPath);
if (!result.ok) {
- if (result.reason === 'extension-blocked') {
+ if (assetOpenRefusalRevealsInstead(result.reason)) {
const revealed = await desktopBridge.shell.revealAsset(ctx.projectRelPath);
if (!revealed.ok) {
console.warn('[asset-dispatch] revealAsset failed:', revealed.reason, {
diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
index a50b00123..4549c74b3 100644
--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
@@ -1,3 +1,4 @@
+
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
index 6fcdc23f2..4fc2c11f9 100644
--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
@@ -1,3 +1,4 @@
+
import { setTimeout as wait } from 'node:timers/promises';
import { normalizeBridge } from '@inkeep/open-knowledge-core';
import { updateYFragment } from '@tiptap/y-tiptap';
diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.ts
index 40b5cdfd6..9f2f6feb6 100644
--- a/packages/app/tests/integration/interior-edit-freshness.test.ts
+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts
@@ -1,3 +1,4 @@
+
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
index 93666af70..2a085742b 100644
--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
@@ -1,3 +1,4 @@
+
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.ts
index 867598d34..878d5196b 100644
--- a/packages/app/tests/integration/producer-guard-wiring.test.ts
+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts
@@ -1,3 +1,4 @@
+
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
index 0970ac12f..4ada3cf26 100644
--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
@@ -1,3 +1,4 @@
+
import { randomUUID } from 'node:crypto';
import { afterEach, describe, expect, test } from 'vitest';
import { agentWriteMd, createTestServer, type TestServer } from './test-harness';
diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
index b632185e1..cbe2b705f 100644
--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
@@ -1,3 +1,4 @@
+
import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { join } from 'node:path';
diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
index c530ce4ba..89bc9b4d9 100644
--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
@@ -1,3 +1,4 @@
+
import { randomUUID } from 'node:crypto';
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';
import type * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.ts
index d3767adc9..a78aeb0e4 100644
--- a/packages/app/tests/integration/undo-after-rollback.test.ts
+++ b/packages/app/tests/integration/undo-after-rollback.test.ts
@@ -1,3 +1,4 @@
+
import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.ts
index b076f955c..e7cc69c3f 100644
--- a/packages/app/tests/integration/undo-recycle-reset.test.ts
+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts
@@ -1,3 +1,4 @@
+
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.ts
index 7935e35f8..9ddcd0730 100644
--- a/packages/app/tests/integration/undo-rederive-survival.test.ts
+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts
@@ -1,3 +1,4 @@
+
import { prependFrontmatter } from '@inkeep/open-knowledge-core';
import { Plugin, PluginKey } from '@tiptap/pm/state';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.ts
index f4cc8a286..2a597ec3d 100644
--- a/packages/core/src/bridge/compose.ts
+++ b/packages/core/src/bridge/compose.ts
@@ -1,3 +1,4 @@
+
import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';
export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';
diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.ts
index e7e63f43d..ffd8fce06 100644
--- a/packages/core/src/bridge/doc-boundary-space.ts
+++ b/packages/core/src/bridge/doc-boundary-space.ts
@@ -1,3 +1,4 @@
+
import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';
import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';
diff --git a/packages/core/src/constants/upload.ts b/packages/core/src/constants/upload.ts
index 56fb38adc..a39c11f94 100644
--- a/packages/core/src/constants/upload.ts
+++ b/packages/core/src/constants/upload.ts
@@ -75,6 +75,12 @@ export const EXECUTABLE_BLOCKLIST_EXTENSIONS: ReadonlySet<string> = new Set([
'com',
'msi',
'vbs',
+ 'vbe',
+ 'py',
+ 'pyw',
+ 'pyc',
+ 'pyz',
+ 'pyzw',
'js',
'jse',
'wsf',
@@ -82,6 +88,7 @@ export const EXECUTABLE_BLOCKLIST_EXTENSIONS: ReadonlySet<string> = new Set([
'hta',
'sh',
'command',
+ 'tool',
'csh',
'ksh',
'bash',
@@ -118,6 +125,21 @@ export const EXECUTABLE_BLOCKLIST_EXTENSIONS: ReadonlySet<string> = new Set([
'scr',
'lnk',
'url',
+ 'cpl',
+ 'msc',
+ 'chm',
+ 'application',
+ 'appref-ms',
+ 'settingcontent-ms',
+ 'reg',
+ 'inf',
+ 'scf',
+ 'sct',
+ 'wsc',
+ 'msp',
+ 'mst',
+ 'shb',
+ 'shs',
]);
export const ASSET_EXTENSIONS: ReadonlySet<string> = new Set([
diff --git a/packages/core/src/desktop-bridge.test.ts b/packages/core/src/desktop-bridge.test.ts
index e0219bf2b..ae3c6344e 100644
--- a/packages/core/src/desktop-bridge.test.ts
+++ b/packages/core/src/desktop-bridge.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import {
+ assetOpenRefusalRevealsInstead,
isTerminalShellNoticeReason,
isTerminalSupportFileNoticeReason,
TERMINAL_SHELL_NOTICE_REASONS,
@@ -57,3 +58,21 @@ describe('terminal support-file notice reasons', () => {
expect(isTerminalSupportFileNoticeReason(null)).toBe(false);
});
});
+
+describe('assetOpenRefusalRevealsInstead', () => {
+ it('reveals refusals of items that exist but must not be opened', () => {
+ expect(assetOpenRefusalRevealsInstead('extension-blocked')).toBe(true);
+ expect(assetOpenRefusalRevealsInstead('not-a-file')).toBe(true);
+ });
+
+ it('does not reveal escapes, missing items or OS handler failures', () => {
+ expect(assetOpenRefusalRevealsInstead('path-escape')).toBe(false);
+ expect(assetOpenRefusalRevealsInstead('not-found')).toBe(false);
+ expect(assetOpenRefusalRevealsInstead('resolve-error')).toBe(false);
+ });
+
+ it('does not reveal or throw on a reason a newer desktop sends', () => {
+ const fromNewerDesktop = 'quarantined' as Parameters<typeof assetOpenRefusalRevealsInstead>[0];
+ expect(assetOpenRefusalRevealsInstead(fromNewerDesktop)).toBe(false);
+ });
+});
diff --git a/packages/core/src/desktop-bridge.ts b/packages/core/src/desktop-bridge.ts
index 2463c610c..8f9176d47 100644
--- a/packages/core/src/desktop-bridge.ts
+++ b/packages/core/src/desktop-bridge.ts
@@ -955,6 +955,33 @@ export type OkServerRestartOutcome = { readonly ok: true } | OkServerRestartFail
export type OkPtyCreateReason = 'no-project' | 'not-consented';
+export type OkAssetOpenRefusalReason =
+ | 'extension-blocked'
+ | 'not-a-file'
+ | 'path-escape'
+ | 'not-found'
+ | 'resolve-error';
+
+export type OkAssetOpenResult =
+ | { readonly ok: true }
+ | { readonly ok: false; readonly reason: OkAssetOpenRefusalReason };
+
+export function assetOpenRefusalRevealsInstead(reason: OkAssetOpenRefusalReason): boolean {
+ switch (reason) {
+ case 'extension-blocked':
+ case 'not-a-file':
+ return true;
+ case 'path-escape':
+ case 'not-found':
+ case 'resolve-error':
+ return false;
+ default: {
+ const _unknownFromNewerDesktop: never = reason;
+ return false;
+ }
+ }
+}
+
export function assertNeverPtyCreateReason(value: never): never {
throw new Error(`unhandled pty create reason: ${String(value)}`);
}
@@ -1150,12 +1177,7 @@ export interface OkDesktopBridge {
readonly scope?: HandoffScope;
}): Promise<void>;
- openAsset(
- relPath: string,
- ): Promise<
- | { ok: true }
- | { ok: false; reason: 'extension-blocked' | 'path-escape' | 'not-found' | 'resolve-error' }
- >;
+ openAsset(relPath: string): Promise<OkAssetOpenResult>;
revealAsset(
relPath: string,
diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.ts
index fc267cbea..fb0ba5a7c 100644
--- a/packages/core/src/markdown/comment-promoter.ts
+++ b/packages/core/src/markdown/comment-promoter.ts
@@ -1,3 +1,4 @@
+
import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.ts
index bfd7e3109..fa3cfcd86 100644
--- a/packages/core/src/markdown/merged-walker.ts
+++ b/packages/core/src/markdown/merged-walker.ts
@@ -1,3 +1,4 @@
+
import type { Nodes, Parent, Root } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.ts
index 4d3250d3a..8be632ba4 100644
--- a/packages/core/src/markdown/safe-url.ts
+++ b/packages/core/src/markdown/safe-url.ts
@@ -1,3 +1,4 @@
+
export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;
const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');
diff --git a/packages/desktop/src/main/asset-allowlist.ts b/packages/desktop/src/main/asset-allowlist.ts
index f62d23a4e..64c8ef880 100644
--- a/packages/desktop/src/main/asset-allowlist.ts
+++ b/packages/desktop/src/main/asset-allowlist.ts
@@ -2,16 +2,20 @@ import { realpathSync, statSync } from 'node:fs';
import * as pathPosix from 'node:path/posix';
import * as pathWin32 from 'node:path/win32';
import { EXECUTABLE_BLOCKLIST_EXTENSIONS } from '@inkeep/open-knowledge-core';
+import {
+ assetOpenRefusalRevealsInstead,
+ type OkAssetOpenResult,
+} from '@inkeep/open-knowledge-core/desktop-bridge';
import { isPathWithinProject } from './ipc-handlers.ts';
-export type AssetOpenResult =
- | { ok: true }
- | { ok: false; reason: 'extension-blocked' | 'path-escape' | 'not-found' | 'resolve-error' };
+export type AssetOpenResult = OkAssetOpenResult;
type AssetRevealResult =
| { ok: true }
| { ok: false; reason: 'path-escape' | 'not-found' | 'resolve-error' };
+type StatKind = 'file' | 'other' | 'missing';
+
export function extractPathExtension(path: string): string {
const lastSep = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
const basename = lastSep >= 0 ? path.slice(lastSep + 1) : path;
@@ -25,7 +29,7 @@ interface OpenAssetDeps {
readonly platform: NodeJS.Platform;
readonly openPath: (canonical: string) => Promise<string>;
readonly resolveCanonical?: (path: string) => string;
- readonly statExists?: (path: string) => boolean;
+ readonly statKind?: (path: string) => StatKind;
}
interface RevealAssetDeps {
@@ -33,7 +37,7 @@ interface RevealAssetDeps {
readonly platform: NodeJS.Platform;
readonly showItemInFolder: (canonical: string) => void;
readonly resolveCanonical?: (path: string) => string;
- readonly statExists?: (path: string) => boolean;
+ readonly statKind?: (path: string) => StatKind;
}
function resolveAndContain(
@@ -41,9 +45,9 @@ function resolveAndContain(
projectPath: string,
platform: NodeJS.Platform,
resolveCanonical: (path: string) => string,
- statExists: (path: string) => boolean,
+ statKind: (path: string) => StatKind,
):
- | { ok: true; canonical: string }
+ | { ok: true; canonical: string; kind: 'file' | 'other' }
| { ok: false; reason: 'path-escape' | 'not-found' | 'resolve-error' } {
const p = platform === 'win32' ? pathWin32 : pathPosix;
if (p.isAbsolute(relPath)) {
@@ -65,23 +69,23 @@ function resolveAndContain(
return { ok: false, reason: 'path-escape' };
}
- if (!statExists(canonical)) {
+ const kind = statKind(canonical);
+ if (kind === 'missing') {
return { ok: false, reason: 'not-found' };
}
- return { ok: true, canonical };
+ return { ok: true, canonical, kind };
}
function defaultResolveCanonical(path: string): string {
return realpathSync(path);
}
-function defaultStatExists(path: string): boolean {
+function defaultStatKind(path: string): StatKind {
try {
- statSync(path);
- return true;
+ return statSync(path).isFile() ? 'file' : 'other';
} catch {
- return false;
+ return 'missing';
}
}
@@ -90,19 +94,27 @@ export async function openAssetSafely(
relPath: string,
): Promise<AssetOpenResult> {
const resolveCanonical = deps.resolveCanonical ?? defaultResolveCanonical;
- const statExists = deps.statExists ?? defaultStatExists;
+ const statKind = deps.statKind ?? defaultStatKind;
const contained = resolveAndContain(
relPath,
deps.projectPath,
deps.platform,
resolveCanonical,
- statExists,
+ statKind,
);
if (!contained.ok) return contained;
+ if (contained.kind !== 'file') {
+ return { ok: false, reason: 'not-a-file' };
+ }
+
const ext = extractPathExtension(contained.canonical);
- if (EXECUTABLE_BLOCKLIST_EXTENSIONS.has(ext)) {
+ if (
+ ext === '' ||
+ ext !== extractPathExtension(relPath) ||
+ EXECUTABLE_BLOCKLIST_EXTENSIONS.has(ext)
+ ) {
return { ok: false, reason: 'extension-blocked' };
}
@@ -118,17 +130,27 @@ export async function revealAssetSafely(
relPath: string,
): Promise<AssetRevealResult> {
const resolveCanonical = deps.resolveCanonical ?? defaultResolveCanonical;
- const statExists = deps.statExists ?? defaultStatExists;
+ const statKind = deps.statKind ?? defaultStatKind;
const contained = resolveAndContain(
relPath,
deps.projectPath,
deps.platform,
resolveCanonical,
- statExists,
+ statKind,
);
if (!contained.ok) return contained;
deps.showItemInFolder(contained.canonical);
return { ok: true };
}
+
+export async function openAssetOrReveal(
+ deps: OpenAssetDeps & Pick<RevealAssetDeps, 'showItemInFolder'>,
+ relPath: string,
+): Promise<AssetOpenResult> {
+ const opened = await openAssetSafely(deps, relPath);
+ if (opened.ok || !assetOpenRefusalRevealsInstead(opened.reason)) return opened;
+ await revealAssetSafely(deps, relPath);
+ return opened;
+}
diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.ts
index 86e5a451a..b647442fd 100644
--- a/packages/desktop/src/main/index.ts
+++ b/packages/desktop/src/main/index.ts
@@ -176,7 +176,7 @@ import {
import { collectDesktopHostSnapshot, withoutProjectScope } from './agent-registry-probes.ts';
import { appendOkIgnoreSync } from './append-okignore.ts';
import { registerAppImageDeepLinks } from './appimage-integration.ts';
-import { openAssetSafely, revealAssetSafely } from './asset-allowlist.ts';
+import { openAssetOrReveal, openAssetSafely, revealAssetSafely } from './asset-allowlist.ts';
import { popAssetMenu, revealMenuLabel } from './asset-menu.ts';
import { attachAssetSafetyNet } from './asset-safety-net.ts';
import { resolveEffectiveInstanceName } from './auto-instance.ts';
@@ -4327,14 +4327,23 @@ function registerIpcHandlers() {
);
},
openInDefault: async () => {
- await openAssetSafely(
+ const outcome = await openAssetOrReveal(
{
projectPath,
platform: process.platform,
openPath: (canonical) => shell.openPath(canonical),
+ showItemInFolder: (canonical) => shell.showItemInFolder(canonical),
},
params.relPath,
);
+ if (!outcome.ok) {
+ logIpcError({
+ event: 'ipc.error',
+ channel: 'ok:shell:show-asset-menu',
+ reason: outcome.reason,
+ handler: 'openInDefault',
+ });
+ }
},
copyLink: () => clipboard.writeText(params.relPath),
},
diff --git a/packages/desktop/src/shared/ipc-channels.ts b/packages/desktop/src/shared/ipc-channels.ts
index 2f28ace14..0c6bcc9e3 100644
--- a/packages/desktop/src/shared/ipc-channels.ts
+++ b/packages/desktop/src/shared/ipc-channels.ts
@@ -39,6 +39,7 @@ import type {
CopyImageRequest,
CopyImageResult,
OkHeadBranchInfo as HeadBranchInfo,
+ OkAssetOpenResult,
OkAssetUploadResult,
OkChromeColors,
OkDesktopConfig,
@@ -393,9 +394,7 @@ export interface RequestChannels {
'ok:shell:record-handoff': { args: [line: HandoffStatsLine]; result: undefined };
'ok:shell:open-asset': {
args: [relPath: string];
- result:
- | { ok: true }
- | { ok: false; reason: 'extension-blocked' | 'path-escape' | 'not-found' | 'resolve-error' };
+ result: OkAssetOpenResult;
};
'ok:shell:reveal-asset': {
args: [relPath: string];
diff --git a/packages/desktop/tests/integration/asset-open-ipc.test.ts b/packages/desktop/tests/integration/asset-open-ipc.test.ts
index d74e7a83e..412a1367f 100644
--- a/packages/desktop/tests/integration/asset-open-ipc.test.ts
+++ b/packages/desktop/tests/integration/asset-open-ipc.test.ts
@@ -61,7 +61,7 @@ describe("'ok:shell:open-asset' round-trip", () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([canonical]),
- statExists: (p) => p === canonical,
+ statKind: (p) => (p === canonical ? 'file' : 'missing'),
},
relPath,
),
@@ -83,7 +83,7 @@ describe("'ok:shell:open-asset' round-trip", () => {
platform: POSIX,
openPath,
resolveCanonical: (p) => p,
- statExists: () => true,
+ statKind: () => 'file',
},
relPath,
),
@@ -106,7 +106,7 @@ describe("'ok:shell:open-asset' round-trip", () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([canonical]),
- statExists: (p) => p === canonical,
+ statKind: (p) => (p === canonical ? 'file' : 'missing'),
},
relPath,
),
@@ -128,7 +128,7 @@ describe("'ok:shell:open-asset' round-trip", () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([]),
- statExists: () => false,
+ statKind: () => 'missing',
},
relPath,
),
@@ -152,7 +152,7 @@ describe("'ok:shell:reveal-asset' round-trip", () => {
platform: POSIX,
showItemInFolder,
resolveCanonical: makeResolver([canonical]),
- statExists: (p) => p === canonical,
+ statKind: (p) => (p === canonical ? 'file' : 'missing'),
},
relPath,
),
@@ -175,7 +175,7 @@ describe("'ok:shell:reveal-asset' round-trip", () => {
platform: POSIX,
showItemInFolder,
resolveCanonical: makeResolver([canonical]),
- statExists: (p) => p === canonical,
+ statKind: (p) => (p === canonical ? 'file' : 'missing'),
},
relPath,
),
diff --git a/packages/desktop/tests/main/asset-open-handlers.test.ts b/packages/desktop/tests/main/asset-open-handlers.test.ts
index b831a4131..9f0022f58 100644
--- a/packages/desktop/tests/main/asset-open-handlers.test.ts
+++ b/packages/desktop/tests/main/asset-open-handlers.test.ts
@@ -1,6 +1,10 @@
-import { describe, expect, test, vi } from 'vitest';
+import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import {
extractPathExtension,
+ openAssetOrReveal,
openAssetSafely,
revealAssetSafely,
} from '../../src/main/asset-allowlist.ts';
@@ -19,9 +23,17 @@ function makeResolver(existingPaths: string[]): (path: string) => string {
};
}
-function makeStatExists(existingPaths: string[]): (path: string) => boolean {
- const set = new Set(existingPaths);
- return (path) => set.has(path);
+function makeStatKind(
+ files: string[],
+ others: string[] = [],
+): (path: string) => 'file' | 'other' | 'missing' {
+ const fileSet = new Set(files);
+ const otherSet = new Set(others);
+ return (path) => {
+ if (fileSet.has(path)) return 'file';
+ if (otherSet.has(path)) return 'other';
+ return 'missing';
+ };
}
describe('extractPathExtension', () => {
@@ -56,7 +68,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([canonical]),
- statExists: makeStatExists([canonical]),
+ statKind: makeStatKind([canonical]),
},
'notes/meeting.pdf',
);
@@ -72,7 +84,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: (p) => p,
- statExists: () => true,
+ statKind: () => 'file',
},
'../../etc/passwd',
);
@@ -88,7 +100,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: (p) => p,
- statExists: () => true,
+ statKind: () => 'file',
},
'/etc/passwd',
);
@@ -104,7 +116,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: () => '/etc/passwd',
- statExists: () => true,
+ statKind: () => 'file',
},
'notes/link.pdf',
);
@@ -120,7 +132,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([]),
- statExists: () => false,
+ statKind: () => 'missing',
},
'notes/missing.pdf',
);
@@ -140,7 +152,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
err.code = 'EACCES';
throw err;
},
- statExists: () => true,
+ statKind: () => 'file',
},
'notes/restricted.pdf',
);
@@ -157,7 +169,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([canonical]),
- statExists: makeStatExists([canonical]),
+ statKind: makeStatKind([canonical]),
},
'notes/setup.sh',
);
@@ -174,7 +186,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([canonical]),
- statExists: makeStatExists([canonical]),
+ statKind: makeStatKind([canonical]),
},
'notes/installer.EXE',
);
@@ -192,7 +204,151 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([canonical]),
- statExists: makeStatExists([canonical]),
+ statKind: makeStatKind([canonical]),
+ },
+ `notes/${name}`,
+ );
+ expect(result).toEqual({ ok: false, reason: 'extension-blocked' });
+ }
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test('directory → not-a-file (shell.openPath would launch it)', async () => {
+ const openPath = vi.fn(async (_: string) => '');
+ const canonical = `${PROJECT}/notes/attachments`;
+ const result = await openAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: POSIX,
+ openPath,
+ resolveCanonical: makeResolver([canonical]),
+ statKind: makeStatKind([], [canonical]),
+ },
+ 'notes/attachments',
+ );
+ expect(result).toEqual({ ok: false, reason: 'not-a-file' });
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test('application bundle directory (.app) → not-a-file', async () => {
+ const openPath = vi.fn(async (_: string) => '');
+ const canonical = `${PROJECT}/notes/Calculator.app`;
+ const result = await openAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: 'darwin',
+ openPath,
+ resolveCanonical: makeResolver([canonical]),
+ statKind: makeStatKind([], [canonical]),
+ },
+ 'notes/Calculator.app',
+ );
+ expect(result).toEqual({ ok: false, reason: 'not-a-file' });
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test('symlink disguise (photo.png → extensionless run-me) → extension-blocked', async () => {
+ const openPath = vi.fn(async (_: string) => '');
+ const target = `${PROJECT}/notes/run-me`;
+ const result = await openAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: POSIX,
+ openPath,
+ resolveCanonical: () => target,
+ statKind: makeStatKind([target]),
+ },
+ 'notes/photo.png',
+ );
+ expect(result).toEqual({ ok: false, reason: 'extension-blocked' });
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test('symlink whose target carries a different, unblocked extension → extension-blocked', async () => {
+ const openPath = vi.fn(async (_: string) => '');
+ const target = `${PROJECT}/notes/report.pdf`;
+ const result = await openAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: POSIX,
+ openPath,
+ resolveCanonical: () => target,
+ statKind: makeStatKind([target]),
+ },
+ 'notes/photo.png',
+ );
+ expect(result).toEqual({ ok: false, reason: 'extension-blocked' });
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test('symlink to a same-extension target opens the canonical target, case-insensitively', async () => {
+ const openPath = vi.fn(async (_: string) => '');
+ const target = `${PROJECT}/archive/meeting.pdf`;
+ const result = await openAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: POSIX,
+ openPath,
+ resolveCanonical: () => target,
+ statKind: makeStatKind([target]),
+ },
+ 'notes/Latest.PDF',
+ );
+ expect(result).toEqual({ ok: true });
+ expect(openPath).toHaveBeenCalledWith(target);
+ });
+
+ test('extensionless regular file (tools/run-me) → extension-blocked', async () => {
+ const openPath = vi.fn(async (_: string) => '');
+ const canonical = `${PROJECT}/tools/run-me`;
+ const result = await openAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: POSIX,
+ openPath,
+ resolveCanonical: makeResolver([canonical]),
+ statKind: makeStatKind([canonical]),
+ },
+ 'tools/run-me',
+ );
+ expect(result).toEqual({ ok: false, reason: 'extension-blocked' });
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test('code-running types that launch on open are blocked', async () => {
+ const openPath = vi.fn(async (_: string) => '');
+ for (const name of [
+ 'build.tool',
+ 'run.vbe',
+ 'panel.cpl',
+ 'console.msc',
+ 'help.chm',
+ 'deploy.application',
+ 'shortcut.appref-ms',
+ 'settings.settingcontent-ms',
+ 'script.py',
+ 'gui.pyw',
+ 'module.pyc',
+ 'app.pyz',
+ 'gui-app.pyzw',
+ 'keys.reg',
+ 'driver.inf',
+ 'explorer.scf',
+ 'scriptlet.sct',
+ 'component.wsc',
+ 'patch.msp',
+ 'transform.mst',
+ 'link.shb',
+ 'scrap.shs',
+ ]) {
+ const canonical = `${PROJECT}/notes/${name}`;
+ const result = await openAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: POSIX,
+ openPath,
+ resolveCanonical: makeResolver([canonical]),
+ statKind: makeStatKind([canonical]),
},
`notes/${name}`,
);
@@ -210,7 +366,7 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
platform: POSIX,
openPath,
resolveCanonical: makeResolver([canonical]),
- statExists: makeStatExists([canonical]),
+ statKind: makeStatKind([canonical]),
},
'notes/file.foo',
);
@@ -219,6 +375,64 @@ describe('openAssetSafely (FR-A6 + D-A5 + D-A9)', () => {
});
});
+describe('openAssetOrReveal (asset context menu)', () => {
+ function deps(statKind: (path: string) => 'file' | 'other' | 'missing', osError = '') {
+ return {
+ projectPath: PROJECT,
+ platform: 'darwin' as NodeJS.Platform,
+ openPath: vi.fn(async (_: string) => osError),
+ showItemInFolder: vi.fn((_: string) => {}),
+ resolveCanonical: (p: string) => p,
+ statKind,
+ };
+ }
+
+ test('opens a regular document without revealing it', async () => {
+ const d = deps(() => 'file');
+ expect(await openAssetOrReveal(d, 'notes/meeting.pdf')).toEqual({ ok: true });
+ expect(d.openPath).toHaveBeenCalledWith(`${PROJECT}/notes/meeting.pdf`);
+ expect(d.showItemInFolder).not.toHaveBeenCalled();
+ });
+
+ test('reveals an app bundle instead of opening it', async () => {
+ const d = deps(() => 'other');
+ expect(await openAssetOrReveal(d, 'tools/Calculator.app')).toEqual({
+ ok: false,
+ reason: 'not-a-file',
+ });
+ expect(d.openPath).not.toHaveBeenCalled();
+ expect(d.showItemInFolder).toHaveBeenCalledWith(`${PROJECT}/tools/Calculator.app`);
+ });
+
+ test('reveals a blocked executable instead of opening it', async () => {
+ const d = deps(() => 'file');
+ expect(await openAssetOrReveal(d, 'tools/setup.sh')).toEqual({
+ ok: false,
+ reason: 'extension-blocked',
+ });
+ expect(d.openPath).not.toHaveBeenCalled();
+ expect(d.showItemInFolder).toHaveBeenCalledWith(`${PROJECT}/tools/setup.sh`);
+ });
+
+ test('does not reveal when the OS handler itself fails', async () => {
+ const d = deps(() => 'file', 'No application knows how to open this');
+ expect(await openAssetOrReveal(d, 'notes/file.foo')).toEqual({
+ ok: false,
+ reason: 'resolve-error',
+ });
+ expect(d.showItemInFolder).not.toHaveBeenCalled();
+ });
+
+ test('does not reveal a path outside the project', async () => {
+ const d = deps(() => 'file');
+ expect(await openAssetOrReveal(d, '../../etc/passwd')).toEqual({
+ ok: false,
+ reason: 'path-escape',
+ });
+ expect(d.showItemInFolder).not.toHaveBeenCalled();
+ });
+});
+
describe('revealAssetSafely (FR-A6; extension blocklist does NOT apply)', () => {
test('happy path: contained + exists → showItemInFolder fires on canonical', async () => {
const showItemInFolder = vi.fn((_: string) => {});
@@ -229,7 +443,7 @@ describe('revealAssetSafely (FR-A6; extension blocklist does NOT apply)', () =>
platform: POSIX,
showItemInFolder,
resolveCanonical: makeResolver([canonical]),
- statExists: makeStatExists([canonical]),
+ statKind: makeStatKind([canonical]),
},
'notes/meeting.pdf',
);
@@ -246,7 +460,7 @@ describe('revealAssetSafely (FR-A6; extension blocklist does NOT apply)', () =>
platform: POSIX,
showItemInFolder,
resolveCanonical: makeResolver([canonical]),
- statExists: makeStatExists([canonical]),
+ statKind: makeStatKind([canonical]),
},
'notes/setup.sh',
);
@@ -254,6 +468,23 @@ describe('revealAssetSafely (FR-A6; extension blocklist does NOT apply)', () =>
expect(showItemInFolder).toHaveBeenCalledWith(canonical);
});
+ test('reveal on a directory is ALLOWED (only Open in default app refuses non-files)', async () => {
+ const showItemInFolder = vi.fn((_: string) => {});
+ const canonical = `${PROJECT}/notes/Calculator.app`;
+ const result = await revealAssetSafely(
+ {
+ projectPath: PROJECT,
+ platform: 'darwin',
+ showItemInFolder,
+ resolveCanonical: makeResolver([canonical]),
+ statKind: makeStatKind([], [canonical]),
+ },
+ 'notes/Calculator.app',
+ );
+ expect(result).toEqual({ ok: true });
+ expect(showItemInFolder).toHaveBeenCalledWith(canonical);
+ });
+
test('path escape still refused', async () => {
const showItemInFolder = vi.fn((_: string) => {});
const result = await revealAssetSafely(
@@ -262,7 +493,7 @@ describe('revealAssetSafely (FR-A6; extension blocklist does NOT apply)', () =>
platform: POSIX,
showItemInFolder,
resolveCanonical: (p) => p,
- statExists: () => true,
+ statKind: () => 'file',
},
'../../etc/passwd',
);
@@ -278,7 +509,7 @@ describe('revealAssetSafely (FR-A6; extension blocklist does NOT apply)', () =>
platform: POSIX,
showItemInFolder,
resolveCanonical: makeResolver([]),
- statExists: () => false,
+ statKind: () => 'missing',
},
'notes/missing.pdf',
);
@@ -286,3 +517,75 @@ describe('revealAssetSafely (FR-A6; extension blocklist does NOT apply)', () =>
expect(showItemInFolder).not.toHaveBeenCalled();
});
});
+
+describe('openAssetSafely against the real filesystem', () => {
+ let project: string;
+
+ beforeEach(() => {
+ project = realpathSync(mkdtempSync(join(tmpdir(), 'ok-asset-open-')));
+ mkdirSync(join(project, 'Calculator.app', 'Contents'), { recursive: true });
+ writeFileSync(join(project, 'meeting.pdf'), '%PDF-1.4');
+ writeFileSync(join(project, 'run-me'), '#!/bin/sh\n', { mode: 0o755 });
+ });
+
+ afterEach(() => {
+ rmSync(project, { recursive: true, force: true });
+ });
+
+ function open(relPath: string) {
+ const openPath = vi.fn(async (_: string) => '');
+ const result = openAssetSafely(
+ { projectPath: project, platform: process.platform, openPath },
+ relPath,
+ );
+ return { result, openPath };
+ }
+
+ test('a regular document opens', async () => {
+ const { result, openPath } = open('meeting.pdf');
+ expect(await result).toEqual({ ok: true });
+ expect(openPath).toHaveBeenCalledWith(join(project, 'meeting.pdf'));
+ });
+
+ test('an extensionless executable named as itself is refused', async () => {
+ const { result, openPath } = open('run-me');
+ expect(await result).toEqual({ ok: false, reason: 'extension-blocked' });
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test('an application bundle directory is refused', async () => {
+ const { result, openPath } = open('Calculator.app');
+ expect(await result).toEqual({ ok: false, reason: 'not-a-file' });
+ expect(openPath).not.toHaveBeenCalled();
+ });
+
+ test.skipIf(process.platform === 'win32')(
+ 'a .png symlink to an extensionless executable is refused',
+ async () => {
+ symlinkSync('run-me', join(project, 'photo.png'));
+ const { result, openPath } = open('photo.png');
+ expect(await result).toEqual({ ok: false, reason: 'extension-blocked' });
+ expect(openPath).not.toHaveBeenCalled();
+ },
+ );
+
+ test.skipIf(process.platform === 'win32')(
+ 'a .png symlink to a real .pdf is refused',
+ async () => {
+ symlinkSync('meeting.pdf', join(project, 'cover.png'));
+ const { result, openPath } = open('cover.png');
+ expect(await result).toEqual({ ok: false, reason: 'extension-blocked' });
+ expect(openPath).not.toHaveBeenCalled();
+ },
+ );
+
+ test.skipIf(process.platform === 'win32')(
+ 'a .pdf symlink to a directory is refused',
+ async () => {
+ symlinkSync('Calculator.app', join(project, 'brochure.pdf'));
+ const { result, openPath } = open('brochure.pdf');
+ expect(await result).toEqual({ ok: false, reason: 'not-a-file' });
+ expect(openPath).not.toHaveBeenCalled();
+ },
+ );
+});