1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #8639 from erikh/fix_escapes

builder: fix escaping for ENV variables.
This commit is contained in:
Alexandr Morozov 2014-10-17 15:50:37 -07:00
commit 75fd1b614d
2 changed files with 68 additions and 1 deletions

View file

@ -10,13 +10,25 @@ var (
// `\$` - match literal $
// `[[:alnum:]_]+` - match things like `$SOME_VAR`
// `{[[:alnum:]_]+}` - match things like `${SOME_VAR}`
tokenEnvInterpolation = regexp.MustCompile(`(\\\\+|[^\\]|\b|\A)\$([[:alnum:]_]+|{[[:alnum:]_]+})`)
tokenEnvInterpolation = regexp.MustCompile(`(\\|\\\\+|[^\\]|\b|\A)\$([[:alnum:]_]+|{[[:alnum:]_]+})`)
// this intentionally punts on more exotic interpolations like ${SOME_VAR%suffix} and lets the shell handle those directly
)
// handle environment replacement. Used in dispatcher.
func (b *Builder) replaceEnv(str string) string {
for _, match := range tokenEnvInterpolation.FindAllString(str, -1) {
idx := strings.Index(match, "\\$")
if idx != -1 {
if idx+2 >= len(match) {
str = strings.Replace(str, match, "\\$", -1)
continue
}
stripped := match[idx+2:]
str = strings.Replace(str, match, "$"+stripped, -1)
continue
}
match = match[strings.Index(match, "$"):]
matchKey := strings.Trim(match, "${}")

View file

@ -15,6 +15,61 @@ import (
"github.com/docker/docker/pkg/archive"
)
func TestBuildEnvEscapes(t *testing.T) {
name := "testbuildenvescapes"
defer deleteAllContainers()
defer deleteImages(name)
_, err := buildImage(name,
`
FROM busybox
ENV TEST foo
CMD echo \$
`,
true)
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-t", name))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(out) != "$" {
t.Fatalf("Env TEST was not overwritten with bar when foo was supplied to dockerfile: was %q", strings.TrimSpace(out))
}
logDone("build - env should handle \\$ properly")
}
func TestBuildEnvOverwrite(t *testing.T) {
name := "testbuildenvoverwrite"
defer deleteAllContainers()
defer deleteImages(name)
_, err := buildImage(name,
`
FROM busybox
ENV TEST foo
CMD echo \${TEST}
`,
true)
if err != nil {
t.Fatal(err)
}
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-e", "TEST=bar", "-t", name))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(out) != "bar" {
t.Fatalf("Env TEST was not overwritten with bar when foo was supplied to dockerfile: was %q", strings.TrimSpace(out))
}
logDone("build - env should overwrite builder ENV during run")
}
func TestBuildOnBuildForbiddenMaintainerInSourceImage(t *testing.T) {
name := "testbuildonbuildforbiddenmaintainerinsourceimage"
defer deleteImages(name)