Skip to content

Commit f81a44e

Browse files
committed
fix(WebServer): only quote --path argument when path contains spaces
1 parent 07bef76 commit f81a44e

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

Tools/WebServer/core/file_transfer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def _sanitize_path(path: str) -> str:
2828

2929

3030
def _format_path_arg(path: str) -> str:
31-
"""Format path argument for command line, quoting only if needed."""
32-
if len(path) == 1:
33-
return path
34-
return f'"{path}"'
31+
"""Format path argument for command line, quoting only if path contains spaces."""
32+
if " " in path:
33+
return f'"{path}"'
34+
return path
3535

3636

3737
class FileTransfer:

Tools/WebServer/tests/test_file_transfer.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def test_frename_success(self):
394394
success, msg = self.ft.frename("/old.txt", "/new.txt")
395395
self.assertTrue(success)
396396
self.mock_fpb.send_fl_cmd.assert_called_with(
397-
'fl -c frename --path "/old.txt" --newpath "/new.txt"',
397+
"fl -c frename --path /old.txt --newpath /new.txt",
398398
timeout=2.0,
399399
max_retries=3,
400400
)
@@ -1496,21 +1496,27 @@ def test_path_with_carriage_return_rejected(self):
14961496
self.assertIn("control characters", str(ctx.exception))
14971497

14981498
def test_path_with_quotes_escaped(self):
1499-
"""Test path with quotes is properly escaped."""
1499+
"""Test path with quotes is escaped by sanitize, no wrapping without spaces."""
15001500
ft = FileTransfer(self.mock_fpb)
15011501
ft.flist('/path/with"quotes"')
15021502
call_args = self.mock_fpb.send_fl_cmd.call_args[0][0]
1503-
# Verify quotes are escaped
1504-
self.assertIn('\\"', call_args)
1505-
# Verify the path is properly quoted
1506-
self.assertIn('--path "/path/with', call_args)
1503+
# _sanitize_path escapes " to \", no spaces → no wrapping quotes
1504+
self.assertIn('--path /path/with\\"quotes\\"', call_args)
1505+
1506+
def test_path_with_quotes_and_spaces_escaped(self):
1507+
"""Test path with both quotes and spaces is properly escaped and quoted."""
1508+
ft = FileTransfer(self.mock_fpb)
1509+
ft.flist('/path/with "quotes"')
1510+
call_args = self.mock_fpb.send_fl_cmd.call_args[0][0]
1511+
# _sanitize_path escapes " to \", then _format_path_arg wraps in quotes due to space
1512+
self.assertIn('--path "/path/with \\"quotes\\""', call_args)
15071513

15081514
def test_normal_path_unchanged(self):
1509-
"""Test normal path works correctly."""
1515+
"""Test normal path works correctly without quotes."""
15101516
ft = FileTransfer(self.mock_fpb)
15111517
ft.fopen("/normal/path/file.txt", "r")
15121518
call_args = self.mock_fpb.send_fl_cmd.call_args[0][0]
1513-
self.assertIn('"/normal/path/file.txt"', call_args)
1519+
self.assertIn("--path /normal/path/file.txt", call_args)
15141520

15151521
def test_fmkdir_sanitizes_path(self):
15161522
"""Test fmkdir sanitizes path."""

Tools/WebServer/tests/test_file_transfer_spaces.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ def test_fopen_single_char_path_no_quotes(self):
178178
max_retries=3,
179179
)
180180

181-
def test_multi_char_path_has_quotes(self):
182-
"""Test that multi-char paths still have quotes."""
181+
def test_multi_char_path_no_quotes_without_spaces(self):
182+
"""Test that multi-char paths without spaces have no quotes."""
183183
success, msg = self.ft.fopen("/a", "r")
184184
self.assertTrue(success)
185185
self.mock_fpb.send_fl_cmd.assert_called_with(
186-
'fl -c fopen --path "/a" --mode r',
186+
"fl -c fopen --path /a --mode r",
187187
timeout=2.0,
188188
max_retries=3,
189189
)

0 commit comments

Comments
 (0)