-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath-interpolation.test.mjs
More file actions
407 lines (333 loc) · 14.5 KB
/
path-interpolation.test.mjs
File metadata and controls
407 lines (333 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import { $ } from '../src/$.mjs';
import { test, expect } from 'bun:test';
import './test-helper.mjs'; // Automatically sets up beforeEach/afterEach cleanup
test('path interpolation - basic unquoted path', () => {
const path = '/bin/echo';
const cmd = $({ mirror: false })`${path} hello`;
// With smart quoting, safe paths don't need quotes
expect(cmd.spec.command).toBe("/bin/echo hello");
});
test('path interpolation - path with spaces gets quoted', () => {
const path = '/path with spaces/command';
const cmd = $({ mirror: false })`${path} hello`;
expect(cmd.spec.command).toBe("'/path with spaces/command' hello");
});
test('path interpolation - path already wrapped in double quotes', () => {
const path = '"/path/to/command"';
const cmd = $({ mirror: false })`${path} hello`;
// Should preserve the double quotes by wrapping in single quotes
expect(cmd.spec.command).toBe('\'"/path/to/command"\' hello');
});
test('path interpolation - path already wrapped in single quotes', () => {
const path = "'/path/to/command'";
const cmd = $({ mirror: false })`${path} hello`;
// With the fix, already-quoted paths should be used as-is when they don't contain internal quotes
expect(cmd.spec.command).toBe("'/path/to/command' hello");
});
test('path interpolation - environment variable inheritance works', () => {
const originalEnv = process.env.TEST_PATH;
try {
process.env.TEST_PATH = '/usr/bin/echo';
const path = process.env.TEST_PATH;
const cmd = $({ mirror: false })`${path} hello`;
// Safe path doesn't need quotes
expect(cmd.spec.command).toBe("/usr/bin/echo hello");
} finally {
if (originalEnv !== undefined) {
process.env.TEST_PATH = originalEnv;
} else {
delete process.env.TEST_PATH;
}
}
});
test('path interpolation - complex path scenarios', () => {
const testCases = [
{
name: 'Simple path',
path: '/Users/konard/.claude/local/claude',
expectedCommand: "/Users/konard/.claude/local/claude --version" // No quotes needed
},
{
name: 'Path with spaces',
path: '/Users/user name/.claude/local/claude',
expectedCommand: "'/Users/user name/.claude/local/claude' --version" // Quotes needed
},
{
name: 'Path with special characters',
path: '/Users/user-name_123/.claude/local/claude',
expectedCommand: "/Users/user-name_123/.claude/local/claude --version" // No quotes needed (dash and underscore are safe)
}
];
testCases.forEach(({ name, path, expectedCommand }) => {
const cmd = $({ mirror: false })`${path} --version`;
expect(cmd.spec.command).toBe(expectedCommand);
});
});
test('path interpolation - stdin option with path works', () => {
const path = '/bin/cat';
const cmd = $({ stdin: 'test input\n', mirror: false })`${path}`;
expect(cmd.spec.command).toBe("/bin/cat"); // Safe path, no quotes needed
expect(cmd.options.stdin).toBe('test input\n');
});
test('path interpolation - command building works correctly', () => {
const nonExistentPath = '/nonexistent/command';
const cmd = $({ mirror: false })`${nonExistentPath} --version`;
// Verify the command is built correctly (safe path, no quotes)
expect(cmd.spec.command).toBe("/nonexistent/command --version");
expect(cmd.spec.mode).toBe('shell');
});
test('path interpolation - fixed escaping for simple pre-quoted paths', () => {
// This test verifies the fix for excessive escaping of pre-quoted paths
// The issue was that paths like "'/path/to/command'" would get double-escaped
const preQuotedPath = "'/path/to/command'"; // Already has single quotes
const cmd = $({ mirror: false })`${preQuotedPath} --version`;
// Fixed behavior: no excessive escaping for simple pre-quoted paths
const generated = cmd.spec.command;
expect(generated).not.toContain("\\'"); // Should NOT contain escaped quotes
expect(generated).toBe("'/path/to/command' --version"); // Should use path as-is
// The generated command should be valid shell syntax
expect(generated).toMatch(/^'.*' --version$/);
});
test('path interpolation - environment variable scenario from GitHub issue', () => {
const originalEnv = process.env.CLAUDE_PATH;
try {
// Simulate the exact scenario from the GitHub issue
process.env.CLAUDE_PATH = '/Users/konard/.claude/local/claude';
const claude = process.env.CLAUDE_PATH || '/Users/konard/.claude/local/claude';
const cmd = $({ stdin: 'hi\n', mirror: false })`${claude} --output-format stream-json --verbose --model sonnet`;
// Safe path, no quotes needed
expect(cmd.spec.command).toBe("/Users/konard/.claude/local/claude --output-format stream-json --verbose --model sonnet");
expect(cmd.options.stdin).toBe('hi\n');
expect(cmd.options.mirror).toBe(false);
// The command should be properly formatted for shell execution
expect(cmd.spec.mode).toBe('shell');
} finally {
if (originalEnv !== undefined) {
process.env.CLAUDE_PATH = originalEnv;
} else {
delete process.env.CLAUDE_PATH;
}
}
});
test('path interpolation - improved handling of pre-quoted paths', () => {
// Test that the improved quoting logic handles pre-quoted paths better
const preQuotedPath = "'/path/to/claude'"; // Already has single quotes
const cmd = $({ mirror: false })`${preQuotedPath} --version`;
// With the fix, already-quoted paths should be used as-is when they don't contain internal quotes
expect(cmd.spec.command).toBe("'/path/to/claude' --version");
// Should not contain excessive escaping
expect(cmd.spec.command).not.toContain("\\'");
expect(cmd.spec.mode).toBe('shell');
});
test('path interpolation - handles complex quoting edge cases', () => {
// Test various edge cases for the improved quoting logic
// Case 1: Path with single quotes inside (should still escape properly)
const pathWithInternalQuotes = "'/path/with'quotes/command'";
const cmd1 = $({ mirror: false })`${pathWithInternalQuotes} --version`;
// This should still use escaping because it has internal quotes
expect(cmd1.spec.command).toContain("\\'");
// Case 2: Empty quotes should be handled
const emptyQuoted = "''";
const cmd2 = $({ mirror: false })`echo ${emptyQuoted}`;
expect(cmd2.spec.command).toBe("echo ''");
// Case 3: Just quotes with no content
const justQuotes = "'";
const cmd3 = $({ mirror: false })`echo ${justQuotes}`;
expect(cmd3.spec.command).toBe("echo ''\\'\'\'");
// Case 4: Double-quoted paths should be wrapped in single quotes
const doubleQuotedPath = '"/path/with spaces/command"';
const cmd4 = $({ mirror: false })`${doubleQuotedPath} --version`;
expect(cmd4.spec.command).toBe('\'"/path/with spaces/command"\' --version');
});
// Shell injection prevention tests
test('shell injection - command substitution attempt', () => {
const malicious = '$(rm -rf /)';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted to prevent execution
expect(cmd.spec.command).toBe("echo '$(rm -rf /)'");
});
test('shell injection - backtick command substitution', () => {
const malicious = '`cat /etc/passwd`';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted
expect(cmd.spec.command).toBe("echo '`cat /etc/passwd`'");
});
test('shell injection - semicolon command chaining', () => {
const malicious = 'test; rm -rf /';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted to prevent command chaining
expect(cmd.spec.command).toBe("echo 'test; rm -rf /'");
});
test('shell injection - pipe attempt', () => {
const malicious = 'test | cat /etc/passwd';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted to prevent piping
expect(cmd.spec.command).toBe("echo 'test | cat /etc/passwd'");
});
test('shell injection - AND operator attempt', () => {
const malicious = 'test && malicious_command';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted
expect(cmd.spec.command).toBe("echo 'test && malicious_command'");
});
test('shell injection - OR operator attempt', () => {
const malicious = 'test || malicious_command';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted
expect(cmd.spec.command).toBe("echo 'test || malicious_command'");
});
test('shell injection - background process attempt', () => {
const malicious = 'test & malicious_command';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted
expect(cmd.spec.command).toBe("echo 'test & malicious_command'");
});
test('shell injection - variable expansion attempt', () => {
const malicious = '$PATH';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted to prevent variable expansion
expect(cmd.spec.command).toBe("echo '$PATH'");
});
test('shell injection - glob expansion attempt', () => {
const malicious = '*.txt';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted to prevent glob expansion
expect(cmd.spec.command).toBe("echo '*.txt'");
});
test('shell injection - redirect attempt', () => {
const malicious = '> /etc/passwd';
const cmd = $({ mirror: false })`echo test ${malicious}`;
// Should be safely quoted to prevent redirection
expect(cmd.spec.command).toBe("echo test '> /etc/passwd'");
});
test('shell injection - newline injection', () => {
const malicious = 'test\nmalicious_command';
const cmd = $({ mirror: false })`echo ${malicious}`;
// Should be safely quoted with newline preserved
expect(cmd.spec.command).toBe("echo 'test\nmalicious_command'");
});
test('shell injection - complex injection attempt', () => {
const malicious = '$(echo "pwned" > /tmp/pwned.txt)';
const cmd = $({ mirror: false })`ls ${malicious}`;
// Should be safely quoted
expect(cmd.spec.command).toBe("ls '$(echo \"pwned\" > /tmp/pwned.txt)'");
});
test('safe strings - no unnecessary quoting', () => {
const testCases = [
{ input: 'hello', expected: 'echo hello' },
{ input: 'test123', expected: 'echo test123' },
{ input: '/usr/bin/node', expected: 'echo /usr/bin/node' },
{ input: 'file.txt', expected: 'echo file.txt' },
{ input: 'user@host.com', expected: 'echo user@host.com' },
{ input: 'key=value', expected: 'echo key=value' },
{ input: 'path/to/file', expected: 'echo path/to/file' },
{ input: 'v1.2.3', expected: 'echo v1.2.3' },
];
testCases.forEach(({ input, expected }) => {
const cmd = $({ mirror: false })`echo ${input}`;
expect(cmd.spec.command).toBe(expected);
});
});
test('double-quoting prevention - user quotes with spaces', () => {
// User quotes a path that actually needs quotes (has spaces)
const pathWithSpaces = '/path with spaces/cmd';
// User provides single quotes
const singleQuoted = `'${pathWithSpaces}'`;
const cmd1 = $({ mirror: false })`${singleQuoted} --test`;
expect(cmd1.spec.command).toBe("'/path with spaces/cmd' --test");
// User provides double quotes
const doubleQuoted = `"${pathWithSpaces}"`;
const cmd2 = $({ mirror: false })`${doubleQuoted} --test`;
expect(cmd2.spec.command).toBe('\'"/path with spaces/cmd"\' --test');
});
test('double-quoting prevention - user quotes with special chars', () => {
// User quotes a string with special characters
const dangerous = 'test; echo INJECTED';
// User provides single quotes
const singleQuoted = `'${dangerous}'`;
const cmd1 = $({ mirror: false })`echo ${singleQuoted}`;
expect(cmd1.spec.command).toBe("echo 'test; echo INJECTED'");
// User provides double quotes
const doubleQuoted = `"${dangerous}"`;
const cmd2 = $({ mirror: false })`echo ${doubleQuoted}`;
expect(cmd2.spec.command).toBe('echo \'"test; echo INJECTED"\'');
});
test('double-quoting prevention - user unnecessarily quotes safe strings', () => {
// User quotes a safe string that doesn't need quotes
const safe = 'hello';
// User provides single quotes (unnecessary)
const singleQuoted = `'${safe}'`;
const cmd1 = $({ mirror: false })`echo ${singleQuoted}`;
expect(cmd1.spec.command).toBe("echo 'hello'");
// User provides double quotes (unnecessary)
const doubleQuoted = `"${safe}"`;
const cmd2 = $({ mirror: false })`echo ${doubleQuoted}`;
expect(cmd2.spec.command).toBe('echo \'"hello"\'');
});
test('double-quoting prevention - mixed scenarios', () => {
const testCases = [
{
desc: 'Already single-quoted safe string',
input: "'safe'",
expected: "echo 'safe'"
},
{
desc: 'Already double-quoted safe string',
input: '"safe"',
expected: 'echo \'"safe"\''
},
{
desc: 'Already single-quoted dangerous string',
input: "'rm -rf /'",
expected: "echo 'rm -rf /'"
},
{
desc: 'Already double-quoted dangerous string',
input: '"rm -rf /"',
expected: 'echo \'"rm -rf /"\''
},
{
desc: 'Single-quoted path with spaces',
input: "'/usr/local bin/app'",
expected: "echo '/usr/local bin/app'"
},
{
desc: 'Double-quoted path with spaces',
input: '"/usr/local bin/app"',
expected: 'echo \'"/usr/local bin/app"\''
}
];
testCases.forEach(({ desc, input, expected }) => {
const cmd = $({ mirror: false })`echo ${input}`;
expect(cmd.spec.command).toBe(expected);
});
});
test('strings requiring quotes - proper quoting applied', () => {
const testCases = [
{ input: 'hello world', expected: "echo 'hello world'" },
{ input: 'test$var', expected: "echo 'test$var'" },
{ input: 'cmd;ls', expected: "echo 'cmd;ls'" },
{ input: 'a|b', expected: "echo 'a|b'" },
{ input: 'a&b', expected: "echo 'a&b'" },
{ input: 'a>b', expected: "echo 'a>b'" },
{ input: 'a<b', expected: "echo 'a<b'" },
{ input: 'a*b', expected: "echo 'a*b'" },
{ input: 'a?b', expected: "echo 'a?b'" },
{ input: 'a[b]c', expected: "echo 'a[b]c'" },
{ input: 'a{b}c', expected: "echo 'a{b}c'" },
{ input: 'a(b)c', expected: "echo 'a(b)c'" },
{ input: 'a!b', expected: "echo 'a!b'" },
{ input: 'a#b', expected: "echo 'a#b'" },
{ input: 'a%b', expected: "echo 'a%b'" },
{ input: 'a^b', expected: "echo 'a^b'" },
{ input: 'a~b', expected: "echo 'a~b'" },
{ input: 'a`b', expected: "echo 'a`b'" },
{ input: "a'b", expected: "echo 'a'\\''b'" },
{ input: 'a"b', expected: "echo 'a\"b'" },
{ input: 'a\\b', expected: "echo 'a\\b'" },
];
testCases.forEach(({ input, expected }) => {
const cmd = $({ mirror: false })`echo ${input}`;
expect(cmd.spec.command).toBe(expected);
});
});