Fix backslash escaping in placeholder value escaping

This commit is contained in:
Ingo Oppermann
2022-07-05 10:45:57 +02:00
parent 5900b725e6
commit ae8f5f00a6
3 changed files with 13 additions and 10 deletions

View File

@@ -650,7 +650,8 @@ Currently supported placeholders are:
Before replacing the placeholders in the process config, all references (see below) will be resolved.
If the value that gets filled in on the place of the placeholder needs escaping, you can define the character to be escaped in the placeholder by adding it to the placeholder name and prefix it with a `^`.
E.g. escape all `:` in the value (`http://example.com:8080`) for `{memfs}` placeholder, write `{memfs^:}`. It will then be replaced by `http\://example.com\:8080`. The escape character is always `\`.
E.g. escape all `:` in the value (`http://example.com:8080`) for `{memfs}` placeholder, write `{memfs^:}`. It will then be replaced by `http\://example.com\:8080`. The escape character is always `\`. In
case there are `\` in the value, they will also get escaped. If the placeholder doesn't imply escaping, the value will be uses as-is.
### References

View File

@@ -88,12 +88,13 @@ func replace(what, placeholder, value string) string {
what = re.ReplaceAllStringFunc(what, func(match string) string {
matches := re.FindStringSubmatch(match)
var v string
v := value
if matches[2] != "" {
v = strings.ReplaceAll(value, matches[2], `\`+matches[2])
} else {
v = value
if matches[2] != `\` {
v = strings.ReplaceAll(v, `\`, `\\`)
}
v = strings.ReplaceAll(v, matches[2], `\`+matches[2])
}
return strings.Replace(match, match, v, 1)

View File

@@ -7,21 +7,22 @@ import (
)
func TestReplace(t *testing.T) {
foobar := `;:.,-_$£!^`
foobar := `;:.,-_$\£!^`
samples := [][2]string{
{"{foobar}", foobar},
{"{foobar^:}", `;\:.,-_$£!^`},
{"{foobar^:}barfoo{foobar^:}", `;\:.,-_$£!^barfoo;\:.,-_$£!^`},
{"{foobar^:}", `;\:.,-_$\\£!^`},
{"{foobar^:}barfoo{foobar^:}", `;\:.,-_$\\£!^barfoo;\:.,-_$\\£!^`},
{"{foobar^:.}", "{foobar^:.}"},
{"{foobar^}", "{foobar^}"},
{"{barfoo^:}", "{barfoo^:}"},
{"{foobar^^}", `;:.,-_$£!\^`},
{"{foobar^^}", `;:.,-_$\\£!\^`},
{`{foobar^\}`, `;:.,-_$\\£!^`},
}
for _, e := range samples {
replaced := replace(e[0], "foobar", foobar)
require.Equal(t, e[1], replaced)
require.Equal(t, e[1], replaced, e[0])
}
}