From afc9aef0588b1f55a11ea6e2ab7fea792b35e987 Mon Sep 17 00:00:00 2001 From: Benson Wong Date: Thu, 15 May 2025 15:28:50 -0700 Subject: [PATCH] Fix #133 SanitizeCommand removes comments (#134) --- proxy/config.go | 20 +++++++++++++++++--- proxy/config_posix_test.go | 6 ++++++ proxy/config_windows_test.go | 9 ++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/proxy/config.go b/proxy/config.go index 7e05c4e..cd289fe 100644 --- a/proxy/config.go +++ b/proxy/config.go @@ -229,9 +229,23 @@ func AddDefaultGroupToConfig(config Config) Config { } func SanitizeCommand(cmdStr string) ([]string, error) { - // Remove trailing backslashes - cmdStr = strings.ReplaceAll(cmdStr, "\\ \n", " ") - cmdStr = strings.ReplaceAll(cmdStr, "\\\n", " ") + var cleanedLines []string + for _, line := range strings.Split(cmdStr, "\n") { + trimmed := strings.TrimSpace(line) + // Skip comment lines + if strings.HasPrefix(trimmed, "#") { + continue + } + // Handle trailing backslashes by replacing with space + if strings.HasSuffix(trimmed, "\\") { + cleanedLines = append(cleanedLines, strings.TrimSuffix(trimmed, "\\")+" ") + } else { + cleanedLines = append(cleanedLines, line) + } + } + + // put it back together + cmdStr = strings.Join(cleanedLines, "\n") // Split the command into arguments var args []string diff --git a/proxy/config_posix_test.go b/proxy/config_posix_test.go index 7f0317d..6f9c452 100644 --- a/proxy/config_posix_test.go +++ b/proxy/config_posix_test.go @@ -14,8 +14,14 @@ func TestConfig_SanitizeCommand(t *testing.T) { -a "double quotes" \ --arg2 'single quotes' -s + # comment 1 --arg3 123 \ + + # comment 2 --arg4 '"string in string"' + + + # this will get stripped out as well as the white space above -c "'single quoted'" `) assert.NoError(t, err) diff --git a/proxy/config_windows_test.go b/proxy/config_windows_test.go index 72ad932..ec3c3d7 100644 --- a/proxy/config_windows_test.go +++ b/proxy/config_windows_test.go @@ -11,10 +11,17 @@ import ( func TestConfig_SanitizeCommand(t *testing.T) { // does not support single quoted strings like in config_posix_test.go args, err := SanitizeCommand(`python model1.py \ - -a "double quotes" \ + + -a "double quotes" \ -s --arg3 123 \ + + # comment 2 --arg4 '"string in string"' + + + + # this will get stripped out as well as the white space above -c "'single quoted'" `) assert.NoError(t, err)