Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Refuse dropped file paths containing DEL or C1 control characters (#5069)
Nick Gomeza3c43ab91afd
diff --git a/.changeset/terminal-drop-control-character-names.md b/.changeset/terminal-drop-control-character-names.mdnew file mode 100644index 000000000..07d74851d--- /dev/null+++ b/.changeset/terminal-drop-control-character-names.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Dropping a file whose name contains a control character, such as DEL, onto a terminal now leaves that file out with a notice instead of typing a name that can change the command.diff --git a/packages/app/src/components/TerminalPanel.dom.test.tsx b/packages/app/src/components/TerminalPanel.dom.test.tsxindex d95f06a6e..c8764e383 100644--- a/packages/app/src/components/TerminalPanel.dom.test.tsx+++ b/packages/app/src/components/TerminalPanel.dom.test.tsx@@ -1045,6 +1045,183 @@ describe('TerminalPanel', () => {expect(terminal.input).toHaveBeenCalledWith('pty-1', "'/dropped/shot.png' ");});+ describe('dropped file names with control characters', () => {+ const DROPPED_NAME_CONTROL_CHARACTERS: ReadonlyArray<+ readonly [label: string, character: string]+ > = [+ ['U+0000 (C0)', '\u0000'],+ ['U+0001 (C0)', '\u0001'],+ ['U+000A (C0)', '\u000a'],+ ['U+001B (C0)', '\u001b'],+ ['U+001F (C0)', '\u001f'],+ ['U+007F (DEL)', '\u007f'],+ ['U+0080 (C1)', '\u0080'],+ ['U+0085 (C1)', '\u0085'],+ ['U+009B (C1)', '\u009b'],+ ['U+009F (C1)', '\u009f'],+ ];+ const DROPPED_NAME_TYPED_CHARACTERS: ReadonlyArray<+ readonly [label: string, character: string]+ > = [+ ['U+0020', '\u0020'],+ ['U+007E', '\u007e'],+ ['U+00A0', '\u00a0'],+ ['U+200B', '\u200b'],+ ['U+200E', '\u200e'],+ ['U+2028', '\u2028'],+ ['U+202E', '\u202e'],+ ['U+202F', '\u202f'],+ ['U+2066', '\u2066'],+ ['U+FEFF', '\ufeff'],+ ];+ const DROP_TERMINALS: ReadonlyArray<{+ readonly label: string;+ readonly platform: OkDesktopBridge['platform'];+ readonly shellFamily: 'powershell' | 'cmd' | 'bash' | null;+ readonly root: string;+ readonly quote: string;+ readonly otherRefusedCharacters: string;+ }> = [+ {+ label: 'macOS',+ platform: 'darwin',+ shellFamily: null,+ root: '/dropped/',+ quote: "'",+ otherRefusedCharacters: '',+ },+ {+ label: 'Linux',+ platform: 'linux',+ shellFamily: null,+ root: '/dropped/',+ quote: "'",+ otherRefusedCharacters: '',+ },+ {+ label: 'Windows PowerShell',+ platform: 'win32',+ shellFamily: 'powershell',+ root: 'C:\\Users\\me\\',+ quote: "'",+ otherRefusedCharacters: '',+ },+ {+ label: 'Windows cmd',+ platform: 'win32',+ shellFamily: 'cmd',+ root: 'C:\\Users\\me\\',+ quote: '"',+ otherRefusedCharacters: '!"%',+ },+ {+ label: 'Windows Git Bash',+ platform: 'win32',+ shellFamily: 'bash',+ root: 'C:\\Users\\me\\',+ quote: "'",+ otherRefusedCharacters: '',+ },+ ];+ type DropTerminal = (typeof DROP_TERMINALS)[number];+ const LATIN_1_CODE_POINTS = Array.from({ length: 0x100 }, (_, codePoint) => codePoint);+ const isControlCodePoint = (codePoint: number): boolean =>+ codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);+ const formatCodePoint = (codePoint: number): string =>+ `U+${codePoint.toString(16).toUpperCase().padStart(4, '0')}`;++ async function renderDropTarget(+ target: DropTerminal,+ ): Promise<(names: readonly string[]) => string[]> {+ const { bridge, terminal, pushNotice } = makeBridge(+ { ok: true, ptyId: 'pty-1' },+ WIRED,+ undefined,+ target.platform,+ );+ (bridge as unknown as { getPathForFile: (file: File) => string }).getPathForFile = (file) =>+ `${target.root}${file.name}`;+ render(<TerminalPanel bridge={bridge} />);+ await waitFor(() =>+ expect(document.querySelector('[data-terminal-status="running"]')).not.toBeNull(),+ );+ const { shellFamily } = target;+ if (shellFamily !== null) {+ act(() => pushNotice({ ptyId: 'pty-1', notice: 'shell-resolved', shellFamily }));+ }+ const container = document.querySelector('[data-terminal-status]');+ if (container === null) throw new Error('terminal container not found');+ return (names) => {+ const writesBefore = terminal.input.mock.calls.length;+ const files = names.map((name) => new File(['x'], name, { type: 'text/markdown' }));+ fireEvent.drop(container, { dataTransfer: { types: ['Files'], files } });+ return terminal.input.mock.calls.slice(writesBefore).map(([, data]) => data);+ };+ }++ function typedPath(target: DropTerminal, name: string): string {+ return `${target.quote}${target.root}${name}${target.quote}`;+ }++ describe.each(DROP_TERMINALS)('on a $label terminal', (target) => {+ const exceptions = Array.from(target.otherRefusedCharacters, (character) =>+ formatCodePoint(character.charCodeAt(0)),+ );++ test.each(DROPPED_NAME_CONTROL_CHARACTERS)(+ 'leaves out a file whose name contains %s and types the rest of the drop',+ async (_label, character) => {+ const drop = await renderDropTarget(target);+ expect(drop(['before.md', `a${character}b.md`, 'after.md'])).toEqual([+ `${typedPath(target, 'before.md')} ${typedPath(target, 'after.md')} `,+ ]);+ expect(screen.getByTestId('terminal-path-drop-notice-banner')).toBeTruthy();+ },+ );++ test.each(DROPPED_NAME_TYPED_CHARACTERS)(+ 'types a file whose name contains %s as-is',+ async (_label, character) => {+ const name = `a${character}b.md`;+ const drop = await renderDropTarget(target);+ expect(drop(['before.md', name, 'after.md'])).toEqual([+ `${typedPath(target, 'before.md')} ${typedPath(target, name)} ${typedPath(target, 'after.md')} `,+ ]);+ expect(screen.queryByTestId('terminal-path-drop-notice-banner')).toBeNull();+ },+ );++ test('types nothing when every dropped file name contains DEL or a C1 control', async () => {+ const drop = await renderDropTarget(target);+ expect(drop(['a\u007fb.md', 'c\u009bd.md'])).toEqual([]);+ expect(screen.getByTestId('terminal-path-drop-notice-banner')).toBeTruthy();+ });++ test('leaves out every file whose name contains a control character from U+0000 to U+00FF', async () => {+ const drop = await renderDropTarget(target);+ const controls = LATIN_1_CODE_POINTS.filter(isControlCodePoint);+ const leftOut = controls.filter(+ (codePoint) => drop([`a${String.fromCharCode(codePoint)}b.md`]).length === 0,+ );+ expect(leftOut.map(formatCodePoint)).toEqual(controls.map(formatCodePoint));+ expect(drop(['after.md'])).toEqual([`${typedPath(target, 'after.md')} `]);+ });++ test(`types a file whose name contains any other character from U+0000 to U+00FF${exceptions.length === 0 ? '' : ` except ${exceptions.join(', ')}`}`, async () => {+ const drop = await renderDropTarget(target);+ const others = LATIN_1_CODE_POINTS.filter(+ (codePoint) =>+ !isControlCodePoint(codePoint) &&+ !target.otherRefusedCharacters.includes(String.fromCharCode(codePoint)),+ );+ const typed = others.filter(+ (codePoint) => drop([`a${String.fromCharCode(codePoint)}b.md`]).length === 1,+ );+ expect(typed.map(formatCodePoint)).toEqual(others.map(formatCodePoint));+ });+ });+ });+test('a drag that carries no external files is ignored (no PTY write)', async () => {const { bridge, terminal } = makeBridge({ ok: true, ptyId: 'pty-1' });render(<TerminalPanel bridge={bridge} />);diff --git a/packages/app/src/components/TerminalPanel.tsx b/packages/app/src/components/TerminalPanel.tsxindex c92d8b4ae..908bfde9c 100644--- a/packages/app/src/components/TerminalPanel.tsx+++ b/packages/app/src/components/TerminalPanel.tsx@@ -889,10 +889,7 @@ function TerminalSession({const droppedFiles = filesFromExternalDrop(event);const paths = droppedFiles.map((file) => bridge.getPathForFile(file))- .filter(- (path): path is string =>- path !== null && path !== '' && !Array.from(path).some((ch) => ch.charCodeAt(0) < 0x20),- );+ .filter((path): path is string => path !== null && path !== '' && !/\p{Cc}/u.test(path));let rejectedCount = droppedFiles.length - paths.length;const escapedPaths = paths.flatMap((path) => {if (bridge.platform !== 'win32') return [shellSingleQuote(path)];diff --git a/packages/core/src/handoff/terminal-launch.test.ts b/packages/core/src/handoff/terminal-launch.test.tsindex 27fc6c2d9..33ec1f125 100644--- a/packages/core/src/handoff/terminal-launch.test.ts+++ b/packages/core/src/handoff/terminal-launch.test.ts@@ -299,6 +299,90 @@ describe('Windows launch composition', () => {});});+const DROPPED_NAME_CONTROL_CHARACTERS: ReadonlyArray<readonly [label: string, character: string]> =+ [+ ['U+0000 (C0)', '\u0000'],+ ['U+0001 (C0)', '\u0001'],+ ['U+000A (C0)', '\u000a'],+ ['U+001B (C0)', '\u001b'],+ ['U+001F (C0)', '\u001f'],+ ['U+007F (DEL)', '\u007f'],+ ['U+0080 (C1)', '\u0080'],+ ['U+0085 (C1)', '\u0085'],+ ['U+009B (C1)', '\u009b'],+ ['U+009F (C1)', '\u009f'],+ ];++const DROPPED_NAME_TYPED_CHARACTERS: ReadonlyArray<readonly [label: string, character: string]> = [+ ['U+0020', '\u0020'],+ ['U+007E', '\u007e'],+ ['U+00A0', '\u00a0'],+ ['U+200B', '\u200b'],+ ['U+200E', '\u200e'],+ ['U+2028', '\u2028'],+ ['U+202E', '\u202e'],+ ['U+202F', '\u202f'],+ ['U+2066', '\u2066'],+ ['U+FEFF', '\ufeff'],+];++const LATIN_1_CODE_POINTS = Array.from({ length: 0x100 }, (_, codePoint) => codePoint);++function isControlCodePoint(codePoint: number): boolean {+ return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);+}++function formatCodePoint(codePoint: number): string {+ return `U+${codePoint.toString(16).toUpperCase().padStart(4, '0')}`;+}++function droppedWindowsPath(character: string): string {+ return `C:\\Users\\me\\a${character}b.md`;+}++describe.each([+ ['powershell', "'", ''],+ ['cmd', '"', '!"%'],+ ['bash', "'", ''],+] as const)('quoteWindowsShellPath for a %s terminal', (family, quote, otherRefusedCharacters) => {+ const quotesCodePoint = (codePoint: number): boolean =>+ quoteWindowsShellPath(family, droppedWindowsPath(String.fromCharCode(codePoint))) !== null;+ const exceptions = Array.from(otherRefusedCharacters, (character) =>+ formatCodePoint(character.charCodeAt(0)),+ );++ it.each(DROPPED_NAME_CONTROL_CHARACTERS)(+ 'refuses a dropped path containing %s',+ (_label, character) => {+ expect(quoteWindowsShellPath(family, droppedWindowsPath(character))).toBeNull();+ },+ );++ it.each(DROPPED_NAME_TYPED_CHARACTERS)(+ 'quotes a dropped path containing %s as-is',+ (_label, character) => {+ const path = droppedWindowsPath(character);+ expect(quoteWindowsShellPath(family, path)).toBe(`${quote}${path}${quote}`);+ },+ );++ it('refuses every control character from U+0000 to U+00FF', () => {+ const controls = LATIN_1_CODE_POINTS.filter(isControlCodePoint);+ const refused = controls.filter((codePoint) => !quotesCodePoint(codePoint));+ expect(refused.map(formatCodePoint)).toEqual(controls.map(formatCodePoint));+ });++ it(`quotes every other character from U+0000 to U+00FF${exceptions.length === 0 ? '' : ` except ${exceptions.join(', ')}`}`, () => {+ const others = LATIN_1_CODE_POINTS.filter(+ (codePoint) =>+ !isControlCodePoint(codePoint) &&+ !otherRefusedCharacters.includes(String.fromCharCode(codePoint)),+ );+ const quoted = others.filter(quotesCodePoint);+ expect(quoted.map(formatCodePoint)).toEqual(others.map(formatCodePoint));+ });+});+function findNulMapfileBash(): string {const candidates = [process.env.OK_TEST_BASH,diff --git a/packages/core/src/handoff/terminal-launch.ts b/packages/core/src/handoff/terminal-launch.tsindex 2a993fab7..923d39c9e 100644--- a/packages/core/src/handoff/terminal-launch.ts+++ b/packages/core/src/handoff/terminal-launch.ts@@ -233,7 +233,7 @@ export function composeWindowsShellLaunchArgs(}export function quoteWindowsShellPath(family: WindowsShellFamily, path: string): string | null {- if (path.length === 0 || Array.from(path).some((ch) => ch.charCodeAt(0) < 0x20)) return null;+ if (path.length === 0 || /\p{Cc}/u.test(path)) return null;if (family === 'powershell') return psQuoteArg(path);if (family === 'bash') return shellSingleQuote(path);if (/["%!]/u.test(path)) return null;