-
Notifications
You must be signed in to change notification settings - Fork 61
fix(windows): improve argument escaping and error detection #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moffermann
wants to merge
2
commits into
tuannvm:main
Choose a base branch
from
moffermann:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { escapeArgForWindowsShell } from '../utils/escape.js'; | ||
|
|
||
| describe('escapeArgForWindowsShell', () => { | ||
| describe('basic arguments', () => { | ||
| test('should not modify simple arguments without special chars', () => { | ||
| expect(escapeArgForWindowsShell('hello')).toBe('hello'); | ||
| expect(escapeArgForWindowsShell('--flag')).toBe('--flag'); | ||
| expect(escapeArgForWindowsShell('-x')).toBe('-x'); | ||
| expect(escapeArgForWindowsShell('filename.txt')).toBe('filename.txt'); | ||
| }); | ||
|
|
||
| test('should wrap arguments with spaces in double quotes', () => { | ||
| expect(escapeArgForWindowsShell('hello world')).toBe('"hello world"'); | ||
| expect(escapeArgForWindowsShell('path with spaces')).toBe( | ||
| '"path with spaces"' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('percent signs', () => { | ||
| test('should escape percent signs to prevent env var expansion', () => { | ||
| expect(escapeArgForWindowsShell('100%')).toBe('100%%'); | ||
| expect(escapeArgForWindowsShell('%PATH%')).toBe('%%PATH%%'); | ||
| expect(escapeArgForWindowsShell('50% off')).toBe('"50%% off"'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('newlines', () => { | ||
| test('should replace newlines with spaces', () => { | ||
| expect(escapeArgForWindowsShell('line1\nline2')).toBe('"line1 line2"'); | ||
| expect(escapeArgForWindowsShell('line1\r\nline2')).toBe('"line1 line2"'); | ||
| expect(escapeArgForWindowsShell('a\n\n\nb')).toBe('"a b"'); | ||
| }); | ||
|
|
||
| test('should handle multiline prompts', () => { | ||
| const multilinePrompt = 'First line\nSecond line\nThird line'; | ||
| const result = escapeArgForWindowsShell(multilinePrompt); | ||
| expect(result).toBe('"First line Second line Third line"'); | ||
| expect(result).not.toContain('\n'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('quotes', () => { | ||
| test('should escape quotes with caret when no spaces present', () => { | ||
| // In practice, -c and model="gpt-5" are separate args in the array | ||
| // So we test model="gpt-5" alone (no spaces) | ||
| expect(escapeArgForWindowsShell('model="gpt-5"')).toBe('model=^"gpt-5^"'); | ||
| expect(escapeArgForWindowsShell('key="value"')).toBe('key=^"value^"'); | ||
| expect(escapeArgForWindowsShell('"quoted"')).toBe('^"quoted^"'); | ||
| }); | ||
|
|
||
| test('should double quotes when wrapped in outer quotes (has spaces)', () => { | ||
| // When arg has spaces AND quotes, wrap in quotes and double internal quotes | ||
| expect(escapeArgForWindowsShell('say "hello"')).toBe('"say ""hello"""'); | ||
| expect(escapeArgForWindowsShell('text with "quotes" inside')).toBe( | ||
| '"text with ""quotes"" inside"' | ||
| ); | ||
| // This is a single arg with space - gets wrapped | ||
| expect(escapeArgForWindowsShell('-c model="gpt-5"')).toBe( | ||
| '"-c model=""gpt-5"""' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('special shell characters', () => { | ||
| test('should wrap args with shell metacharacters in quotes', () => { | ||
| expect(escapeArgForWindowsShell('a&b')).toBe('"a&b"'); | ||
| expect(escapeArgForWindowsShell('a|b')).toBe('"a|b"'); | ||
| expect(escapeArgForWindowsShell('a<b')).toBe('"a<b"'); | ||
| expect(escapeArgForWindowsShell('a>b')).toBe('"a>b"'); | ||
| expect(escapeArgForWindowsShell('a^b')).toBe('"a^b"'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('complex real-world cases', () => { | ||
| test('should handle codex config arguments', () => { | ||
| // This is a common pattern: -c key="value" | ||
| const result = escapeArgForWindowsShell('model="gpt-5.2-codex"'); | ||
| expect(result).toBe('model=^"gpt-5.2-codex^"'); | ||
| }); | ||
|
|
||
| test('should handle prompts with special characters', () => { | ||
| const prompt = 'What is 50% of 100?'; | ||
| const result = escapeArgForWindowsShell(prompt); | ||
| expect(result).toBe('"What is 50%% of 100?"'); | ||
| }); | ||
|
|
||
| test('should handle JSON-like arguments', () => { | ||
| const jsonArg = '{"key":"value"}'; | ||
| const result = escapeArgForWindowsShell(jsonArg); | ||
| expect(result).toBe('{^"key^":^"value^"}'); | ||
| }); | ||
|
|
||
| test('should handle paths with spaces', () => { | ||
| const path = 'C:\\Program Files\\App\\file.exe'; | ||
| const result = escapeArgForWindowsShell(path); | ||
| expect(result).toBe('"C:\\Program Files\\App\\file.exe"'); | ||
| }); | ||
|
|
||
| test('should handle empty string', () => { | ||
| expect(escapeArgForWindowsShell('')).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('combined escaping', () => { | ||
| test('should handle multiple escape requirements', () => { | ||
| // Spaces + percent | ||
| expect(escapeArgForWindowsShell('100% complete')).toBe('"100%% complete"'); | ||
|
|
||
| // Newline + spaces | ||
| expect(escapeArgForWindowsShell('line1\nwith spaces')).toBe( | ||
| '"line1 with spaces"' | ||
| ); | ||
|
|
||
| // Percent + quotes (no spaces) | ||
| expect(escapeArgForWindowsShell('rate=100%')).toBe('rate=100%%'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * Escape an argument for Windows shell (cmd.exe) to prevent injection attacks. | ||
| * Note: This is only used for arguments, not for the command itself. | ||
| * The command (file) should be a simple executable name without special characters. | ||
| * | ||
| * Handles: | ||
| * - Newlines: replaced with spaces (cmd.exe treats \n as command separator) | ||
| * - Percent signs: escaped as %% to prevent env var expansion | ||
| * - Arguments with spaces/special chars: wrapped in double quotes | ||
| * - Internal quotes: doubled ("") per cmd.exe convention | ||
| * | ||
| * @param arg - The argument to escape | ||
| * @returns The escaped argument safe for cmd.exe | ||
| */ | ||
| export function escapeArgForWindowsShell(arg: string): string { | ||
| // Replace newlines with spaces - cmd.exe interprets \n as command separator | ||
| let escaped = arg.replace(/[\r\n]+/g, ' '); | ||
|
|
||
| // Escape percent signs to prevent environment variable expansion | ||
| escaped = escaped.replace(/%/g, '%%'); | ||
|
|
||
| // Check if we need to wrap in quotes (has spaces or special shell chars) | ||
| // but not if it's already a simple flag like --flag or -x | ||
| const needsQuotes = /[\s&|<>^]/.test(escaped); | ||
|
|
||
| if (needsQuotes) { | ||
| // Escape internal double quotes using CMD-style doubling | ||
| escaped = `"${escaped.replace(/"/g, '""')}"`; | ||
| } else if (escaped.includes('"')) { | ||
| // Has quotes but no spaces - escape quotes with caret for cmd.exe | ||
| escaped = escaped.replace(/"/g, '^"'); | ||
| } | ||
|
|
||
| return escaped; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add safeguards against circular references and unbounded traversal.
The cause chain traversal lacks protection against circular references (e.g.,
err1.cause = err2, err2.cause = err1), which would cause an infinite loop. While uncommon in typical usage, error-handling code should be defensive against edge cases.🔒 Proposed fix with cycle detection
📝 Committable suggestion
🤖 Prompt for AI Agents