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

builder: fix escaping for ENV variables.

Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
This commit is contained in:
Erik Hollensbe 2014-10-17 19:15:07 +00:00
parent 6ac9768eaf
commit df0e0c7683
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)