From 22daa4fea2254e62787349ba423c5b8733d5e30b Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 15 Dec 2025 18:35:39 +0100 Subject: [PATCH] Fix #1085 Signed-off-by: David Gageot --- pkg/js/expand.go | 8 ++++---- pkg/js/expand_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/pkg/js/expand.go b/pkg/js/expand.go index 8fc08be38..13753da16 100644 --- a/pkg/js/expand.go +++ b/pkg/js/expand.go @@ -14,10 +14,10 @@ import ( // escapeForTemplateLiteral escapes characters that have special meaning in // JavaScript template literals func escapeForTemplateLiteral(s string) string { - // Escape backticks so they don't terminate the template literal. - // Also escape backslashes that precede backticks to avoid double-escaping issues. - s = strings.ReplaceAll(s, "\\`", "\\\\`") // First escape already-escaped backticks - s = strings.ReplaceAll(s, "`", "\\`") // Then escape remaining backticks + // Escape backslashes first (must be done before backticks to avoid double-escaping) + s = strings.ReplaceAll(s, "\\", "\\\\") + // Then escape backticks so they don't terminate the template literal + s = strings.ReplaceAll(s, "`", "\\`") return s } diff --git a/pkg/js/expand_test.go b/pkg/js/expand_test.go index 43f2f8520..b24147475 100644 --- a/pkg/js/expand_test.go +++ b/pkg/js/expand_test.go @@ -71,6 +71,48 @@ func TestExpand(t *testing.T) { envVars: map[string]string{}, expected: "Use `inline` and ```block``` code", }, + { + name: "single backslash", + commands: "test\\value", + envVars: map[string]string{}, + expected: "test\\value", + }, + { + name: "backslash n (not newline)", + commands: "test\\nvalue", + envVars: map[string]string{}, + expected: "test\\nvalue", + }, + { + name: "backslash t (not tab)", + commands: "test\\tvalue", + envVars: map[string]string{}, + expected: "test\\tvalue", + }, + { + name: "windows path", + commands: "C:\\Users\\Alice\\Documents", + envVars: map[string]string{}, + expected: "C:\\Users\\Alice\\Documents", + }, + { + name: "network path", + commands: "\\\\server\\share\\file", + envVars: map[string]string{}, + expected: "\\\\server\\share\\file", + }, + { + name: "multiple backslashes", + commands: "test\\\\value", + envVars: map[string]string{}, + expected: "test\\\\value", + }, + { + name: "regex pattern with backslashes", + commands: "\\d+\\.\\d+", + envVars: map[string]string{}, + expected: "\\d+\\.\\d+", + }, } for _, tt := range tests {