Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pkg/utils/parse_arf_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,14 @@ func toArrayByComma(format string) []string {
return strings.Split(format, ",")
}

func urlEncode(s string) string {
return url.PathEscape(s)
}

func unescapeNewlines(s string) string {
return strings.ReplaceAll(s, "\\n", "\n")
}

// This function will take original remediation content, and a list of all values found in the configMap
// It will processed and substitue the value in remediation content, and return processed Remediation content
// The return will be Processed-Remdiation Content, Value-Used List, Un-Set List, and err if possible
Expand Down Expand Up @@ -1013,8 +1021,11 @@ func processContent(preProcessedContent string, resultValues map[string]string)
var valuesUsedList []string
var valuesMissingList []string
var valuesParsedList []string
t, err := template.New("").Option("missingkey=zero").Funcs(template.FuncMap{"toArrayByComma": toArrayByComma}).
Parse(preProcessedContent)
t, err := template.New("").Option("missingkey=zero").Funcs(template.FuncMap{
"toArrayByComma": toArrayByComma,
"urlencode": urlEncode,
"unescapeNewlines": unescapeNewlines,
}).Parse(preProcessedContent)
if err != nil {
return preProcessedContent, valuesUsedList, valuesMissingList, errors.Wrap(err, "wrongly formatted remediation context: ") //Error creating template // Wrongly formatted remediation context
}
Expand Down
91 changes: 91 additions & 0 deletions pkg/utils/parse_arf_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,97 @@ Server 3.fedora.pool.ntp.org`
})
})

Describe("Testing unescapeNewlines function", func() {
Context("When given strings with literal newline escape sequences", func() {
It("Should convert single newline", func() {
input := "line1\\nline2"
expected := "line1\nline2"
result := unescapeNewlines(input)
Expect(result).To(Equal(expected))
})

It("Should convert multiple newlines", func() {
input := "line1\\nline2\\n\\nline3"
expected := "line1\nline2\n\nline3"
result := unescapeNewlines(input)
Expect(result).To(Equal(expected))
})

It("Should handle strings without newlines", func() {
input := "single line"
expected := "single line"
result := unescapeNewlines(input)
Expect(result).To(Equal(expected))
})

It("Should handle empty string", func() {
input := ""
expected := ""
result := unescapeNewlines(input)
Expect(result).To(Equal(expected))
})
})
})

Describe("Testing urlEncode function", func() {
Context("When given strings that need URL encoding", func() {
It("Should encode spaces", func() {
input := "hello world"
expected := "hello%20world"
result := urlEncode(input)
Expect(result).To(Equal(expected))
})

It("Should encode special characters", func() {
input := "hello&world=test"
expected := "hello&world=test"
result := urlEncode(input)
Expect(result).To(Equal(expected))
})

It("Should encode forward slashes", func() {
input := "path/to/file"
expected := "path%2Fto%2Ffile"
result := urlEncode(input)
Expect(result).To(Equal(expected))
})

It("Should encode question marks", func() {
input := "what?"
expected := "what%3F"
result := urlEncode(input)
Expect(result).To(Equal(expected))
})

It("Should handle already safe strings", func() {
input := "simple-string_123"
expected := "simple-string_123"
result := urlEncode(input)
Expect(result).To(Equal(expected))
})

It("Should handle empty string", func() {
input := ""
expected := ""
result := urlEncode(input)
Expect(result).To(Equal(expected))
})

It("Should encode percent signs", func() {
input := "100%"
expected := "100%25"
result := urlEncode(input)
Expect(result).To(Equal(expected))
})

It("Should encode complex strings with multiple special characters", func() {
input := "config file.txt"
expected := "config%20file.txt"
result := urlEncode(input)
Expect(result).To(Equal(expected))
})
})
})
})

// printUniquePaths prints all unique paths within an XML document, starting from a given node.
Expand Down
Loading