From 68c5355dff47366317df13f211392a8aeef75c43 Mon Sep 17 00:00:00 2001 From: nvima Date: Sat, 22 Apr 2023 19:24:25 +0200 Subject: [PATCH] Fix stdin string escaping issue in ReplaceStdIn function This commit resolves the issue with incorrect escaping of special characters in the ReplaceStdIn function. Instead of manually escaping double quotes and new lines, we now use the json.Marshal function to properly escape the input string. This ensures that the resulting JSON is valid, even when the input contains special characters or escape sequences. --- util/replaceStdIn.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/util/replaceStdIn.go b/util/replaceStdIn.go index d486470..c09b55a 100644 --- a/util/replaceStdIn.go +++ b/util/replaceStdIn.go @@ -32,8 +32,13 @@ func ReplaceStdIn(input []byte) ([]byte, error) { stdInStr := string(data) stdInStr = removeControlChars(stdInStr) - stdInStr = strings.Replace(stdInStr, "\"", "\\\"", -1) - stdInStr = strings.Replace(stdInStr, "\n", "\\n", -1) + + escapedStdIn, err := json.Marshal(stdInStr) + if err != nil { + return nil, fmt.Errorf("Error escaping stdin string: %v", err) + } + stdInStr = string(escapedStdIn[1 : len(escapedStdIn)-1]) + result := strings.Replace(inputStr, "${STDIN}", stdInStr, -1) return []byte(result), nil }