From e7477625830f6ae83d5f01fd5daa0457c9521bec Mon Sep 17 00:00:00 2001 From: bilby91 <2201079+bilby91@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:19:41 +0000 Subject: [PATCH 1/3] fix(build): bound outside-context Dockerfiles to the workspace v0.5.5 (#156) reads a build.dockerfile outside the build context from the host and injects it into the context tar. The path comes from an untrusted devcontainer.json and nothing bounded it, so an absolute path, ../ traversal, ${localEnv:...} or a symlink could name any file the process can read; the daemon's parse error ("unknown instruction: ...") then echoes its first line into the build output. - runtime/docker: locateDockerfile is the only host read of the Dockerfile. It resolves symlinks on both sides and reads a file outside the context only if it is a regular file of at most 1 MiB inside BuildSpec.WorkspaceRoot; an in-context file's host copy must resolve inside the context or the workspace. Otherwise it returns *runtime.DockerfileRefusedError, naming the configured path, before the context is tarred or the daemon is called. prePullBaseImages takes the contents it read instead of reading the path again. - runtime: BuildSpec.WorkspaceRoot; empty allows nothing outside the context. The engine sets it to LocalWorkspaceFolder for the devcontainer build and compose service builds. - config: Resolve rejects, lexically, a build.dockerfile outside both the workspace and the context with *ConfigInvalidError. - In-context Dockerfiles and the #156 layout (.devcontainer/Dockerfile with "context": "..") build as in v0.5.5. - Tests: refusal cases in both packages, a no-daemon BuildImage test, and an integration test with a hostile config that asserts through a recording socket proxy that no build or pull reaches the daemon and that the secret appears in no event or error. Refs #156, crunchloop/dap#8967 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 27 +++ config/resolve.go | 32 +++ config/resolve_test.go | 53 ++++- design/resolved-config.md | 25 +- runtime/docker/build.go | 177 ++++++++++++-- runtime/docker/build_test.go | 217 +++++++++++++++--- runtime/errors.go | 17 ++ runtime/runtime.go | 21 +- .../build_dockerfile_bound_test.go | 197 ++++++++++++++++ up.go | 39 ++-- 10 files changed, 719 insertions(+), 86 deletions(-) create mode 100644 test/integration/build_dockerfile_bound_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ba6c73..c7f251b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Security + +- **build** — a `build.dockerfile` outside the build context is read + from the host only if it resolves, after symlinks, to a regular file + of at most 1 MiB inside the workspace (`LocalWorkspaceFolder`). Since + v0.5.5, whose `build.dockerfile` fix below (#156) added + outside-context Dockerfiles, an untrusted `devcontainer.json` could + name any file the process can read (by absolute path, `../` + traversal, `${localEnv:…}` or a symlink) and have it sent to the + daemon as the Dockerfile, whose parse error (`unknown instruction: + …`) echoes the file's first line into the build output. + - Such a path now fails before any daemon call. `Resolve` returns + `*config.ConfigInvalidError` when the path lies outside both the + workspace and the context. `BuildImage` returns + `*runtime.DockerfileRefusedError` when, after symlinks, the path + lies outside the workspace (or, for one inside the context, outside + both), or when an outside-context file is not a regular file or is + too large. Both errors name the configured path, never the file's + contents. + - The base-image pre-pull reads the Dockerfile through the same check. + - A Dockerfile inside the context builds as before, unless it is a + symlink resolving outside both the context and the workspace; that + now fails early instead of being left to the daemon. + - `runtime.BuildSpec` gains `WorkspaceRoot`. When it is empty, no + Dockerfile outside `ContextPath` is read. Compose service builds + are bounded the same way. + ### Added - **features** — `devcontainer-lock.json` support, following the spec's diff --git a/config/resolve.go b/config/resolve.go index 698f4ef..5cc9618 100644 --- a/config/resolve.go +++ b/config/resolve.go @@ -188,6 +188,9 @@ func resolveFromRaw(raw *rawConfig, input ResolveInput) (*ResolvedConfig, error) out.ContainerWorkspaceFolder = resolvedFolder substituteAll(out, subCtx, input.ConfigPath) + if err := checkDockerfileBound(out.Source, raw, input); err != nil { + return nil, err + } if len(raw.Features) > 0 { feats, fwarns := buildPartialFeatures(raw.Features, raw.OverrideFeatureInstallOrder, input.ConfigPath) @@ -600,3 +603,32 @@ func substituteCommand(cmd *LifecycleCommand, path string, sub func(s, p string) cmd.Parallel[name] = c } } + +// checkDockerfileBound rejects a build.dockerfile that resolves, +// lexically, outside both the build context and the workspace +// (LocalWorkspaceFolder, or only the context when that is unset). The +// runtime reads a Dockerfile outside the context from the host to send +// it to the daemon, and the daemon echoes a bad Dockerfile's first +// line, so an untrusted config must not name an arbitrary host file +// by an absolute path, ../ traversal or a substituted variable. This +// is the early, filesystem-free half of the bound; the runtime +// enforces it again after resolving symlinks, before it reads the +// file. The error names the path as configured. +func checkDockerfileBound(src Source, raw *rawConfig, input ResolveInput) error { + bs, ok := src.(*BuildSource) + if !ok || bs.Dockerfile == "" { + return nil + } + for _, root := range []string{bs.Context, input.LocalWorkspaceFolder} { + if root == "" { + continue + } + if rel, err := filepath.Rel(root, bs.Dockerfile); err == nil && filepath.IsLocal(rel) { + return nil + } + } + return &ConfigInvalidError{ + Path: input.ConfigPath, + Message: fmt.Sprintf("build.dockerfile %q resolves outside the workspace folder and the build context", raw.Build.Dockerfile), + } +} diff --git a/config/resolve_test.go b/config/resolve_test.go index b0c47b3..d8ddea5 100644 --- a/config/resolve_test.go +++ b/config/resolve_test.go @@ -2,6 +2,8 @@ package config import ( "encoding/json" + "errors" + "fmt" "reflect" "strings" "testing" @@ -113,11 +115,19 @@ func TestResolveBytes_BuildSourceDockerfilePath(t *testing.T) { wantContext: "/home/u/proj", }, { - name: "absolute dockerfile", - build: `{"dockerfile": "/srv/images/Dockerfile", "context": ".."}`, - wantDockerfile: "/srv/images/Dockerfile", + name: "absolute dockerfile in the workspace", + build: `{"dockerfile": "/home/u/proj/docker/Dockerfile", "context": ".."}`, + wantDockerfile: "/home/u/proj/docker/Dockerfile", wantContext: "/home/u/proj", }, + { + // Outside the workspace but inside the context: the daemon + // reads it from the context tar, as before. + name: "absolute dockerfile in an outside context", + build: `{"dockerfile": "/srv/images/Dockerfile", "context": "/srv/images"}`, + wantDockerfile: "/srv/images/Dockerfile", + wantContext: "/srv/images", + }, { // Substitution runs before resolution, so a variable that // expands to an absolute path is not joined to configDir. @@ -144,6 +154,43 @@ func TestResolveBytes_BuildSourceDockerfilePath(t *testing.T) { } } +// A build.dockerfile outside both the workspace and the context would be +// read from the host and shown to the daemon, which echoes its first +// line, so resolution refuses it before anything touches the disk. +func TestResolveBytes_BuildSourceDockerfileOutsideWorkspace(t *testing.T) { + cases := []struct{ name, dockerfile, context string }{ + {"absolute system file", "/etc/hostname", ".."}, + {"absolute file beside the workspace", "/home/u/other/secret", ".."}, + {"traversal out of the workspace", "../../../../etc/hostname", ".."}, + {"traversal just past the root", "../../proj-secrets/Dockerfile", ".."}, + {"localEnv outside the workspace", "${localEnv:HOME}/x", ".."}, + {"absolute outside, context outside elsewhere", "/etc/hostname", "/srv/images"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + const configPath = "/home/u/proj/.devcontainer/devcontainer.json" + src := fmt.Sprintf(`{"build": {"dockerfile": %q, "context": %q}}`, tc.dockerfile, tc.context) + _, err := ResolveBytes([]byte(src), ResolveInput{ + LocalWorkspaceFolder: "/home/u/proj", + ConfigPath: configPath, + DevcontainerID: fakeID, + LocalEnv: map[string]string{"HOME": "/home/u"}, + }) + var invalid *ConfigInvalidError + if !errors.As(err, &invalid) { + t.Fatalf("err = %v, want *ConfigInvalidError", err) + } + if invalid.Path != configPath { + t.Errorf("Path = %q, want %q", invalid.Path, configPath) + } + // Names the path as configured. + if !strings.Contains(err.Error(), fmt.Sprintf("%q", tc.dockerfile)) { + t.Errorf("error %q does not name %q", err, tc.dockerfile) + } + }) + } +} + func TestResolveBytes_ComposeSource(t *testing.T) { cfg := resolveJSON(t, `{ "dockerComposeFile": ["docker-compose.yml","docker-compose.override.yml"], diff --git a/design/resolved-config.md b/design/resolved-config.md index 48b33ee..37ca5f7 100644 --- a/design/resolved-config.md +++ b/design/resolved-config.md @@ -123,7 +123,7 @@ type ImageSource struct { } type BuildSource struct { - Dockerfile string // absolute, host-side (spec: relative to devcontainer.json's folder) + Dockerfile string // absolute, host-side (spec: relative to devcontainer.json's folder); bounded, see below Context string // absolute, host-side Args map[string]string Target string @@ -138,6 +138,29 @@ type ComposeSource struct { } ``` +**`BuildSource.Dockerfile` is bounded to the workspace.** It is an +absolute host path taken from an untrusted `devcontainer.json`. When it +lies outside `Context`, the runtime reads it from the host and appends +it to the context tar. The daemon echoes a bad Dockerfile's first line +in its parse error, so an unbounded path would print any host file the +process can read into the build output. The bound: + +- Inside `Context`, it is sent as part of the context, as any context + file is. Its host copy, read only to pre-pull base images, must + resolve (after symlinks) inside `Context` or the workspace. +- Outside `Context`, it is read only if it resolves, after + `filepath.EvalSymlinks` on both sides, to a regular file of at most + 1 MiB inside the workspace root: `LocalWorkspaceFolder`, the folder + the config was discovered in. The runtime gets this root as + `runtime.BuildSpec.WorkspaceRoot`; when it is empty, nothing outside + `Context` is read. +- `Resolve` applies the bound lexically and returns + `*ConfigInvalidError` for a path outside both the workspace and + `Context`. The runtime applies it again after resolving symlinks, + before any read and before any daemon call, and returns + `*runtime.DockerfileRefusedError`. Both errors name the configured + path and never the file's contents. + Why interface over `Kind`+pointer-fields struct: - Type switches read more naturally: `switch s := cfg.Source.(type) { case *ImageSource: ... }`. - Zero risk of "Kind says image but Image is nil" inconsistency. diff --git a/runtime/docker/build.go b/runtime/docker/build.go index a8387ff..8b46d5a 100644 --- a/runtime/docker/build.go +++ b/runtime/docker/build.go @@ -7,6 +7,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "io" "io/fs" @@ -14,6 +15,7 @@ import ( "path/filepath" "regexp" "strings" + "syscall" "time" "unicode" @@ -46,7 +48,10 @@ func (r *Runtime) BuildImage(ctx context.Context, spec runtime.BuildSpec, events return runtime.ImageRef{}, fmt.Errorf("BuildImage: spec.Tag required") } - dockerfilePath, dockerfile, injected, err := locateDockerfile(spec.ContextPath, spec.Dockerfile) + // Bound and read the Dockerfile before anything else: a refused + // path fails here, before the context is tarred or the daemon is + // called, and nothing below reads it from the host again. + dockerfile, dockerfileBody, injected, err := locateDockerfile(spec.ContextPath, spec.Dockerfile, spec.WorkspaceRoot) if err != nil { return runtime.ImageRef{}, err } @@ -73,7 +78,7 @@ func (r *Runtime) BuildImage(ctx context.Context, spec runtime.BuildSpec, events // (which is unaffected by the session requirement) seeds the local // store; BuildKit then picks up the cached manifest instead of // reaching for the registry. - prePullBaseImages(ctx, r, dockerfilePath, spec.Args, events) + prePullBaseImages(ctx, r, dockerfileBody, spec.Args, events) res, err := r.api.ImageBuild(ctx, pr, client.ImageBuildOptions{ Dockerfile: dockerfile, @@ -187,39 +192,166 @@ type tarFile struct { Body []byte } -// locateDockerfile maps a BuildSpec.Dockerfile onto the host file to -// read (hostPath) and the name the daemon should open inside the -// context tar (tarName). dockerfile is either relative to contextPath -// (empty means "Dockerfile") or an absolute host path. +// maxInjectedDockerfileSize caps a Dockerfile read from the host, +// which bounds what can be appended to the context tar from outside +// the context. Real Dockerfiles are a few KiB. +const maxInjectedDockerfileSize = 1 << 20 + +// locateDockerfile maps a BuildSpec.Dockerfile onto the name the +// daemon should open inside the context tar (tarName) and the host +// copy's contents (body, for pre-pulling base images; nil when the +// host copy of an in-context Dockerfile can't be read, leaving the +// daemon to report it). dockerfile is relative to contextPath (empty +// means "Dockerfile") or an absolute host path. // -// An absolute path inside the context becomes its context-relative -// name. One outside the context can't be reached through the tar, so -// it is read and returned as injected, to be appended under a reserved +// A path inside the context becomes its context-relative name, as +// before. One outside the context can't be reached through the tar, +// so it is returned as injected, to be appended under a reserved // name, matching `docker build -f ` (which the // reference devcontainer CLI runs). The name carries a content hash // rather than the docker CLI's random suffix so the tar stays // reproducible. -func locateDockerfile(contextPath, dockerfile string) (hostPath, tarName string, injected *tarFile, err error) { +// +// The path comes from an untrusted devcontainer.json, and the daemon +// echoes a bad Dockerfile's first line in its parse error, so every +// host read is bounded first: the file must resolve, after symlinks, +// inside the context or workspaceRoot, and one outside the context +// only inside workspaceRoot. It must also be a regular file of at +// most maxInjectedDockerfileSize. A refused path yields a +// *runtime.DockerfileRefusedError and nothing is read. +func locateDockerfile(contextPath, dockerfile, workspaceRoot string) (tarName string, body []byte, injected *tarFile, err error) { if dockerfile == "" { dockerfile = "Dockerfile" } - if !filepath.IsAbs(dockerfile) { - return filepath.Join(contextPath, dockerfile), filepath.ToSlash(dockerfile), nil, nil - } absCtx, err := filepath.Abs(contextPath) if err != nil { - return "", "", nil, fmt.Errorf("BuildImage: context path %s: %w", contextPath, err) + return "", nil, nil, fmt.Errorf("BuildImage: context path %s: %w", contextPath, err) + } + hostPath := dockerfile + if !filepath.IsAbs(hostPath) { + hostPath = filepath.Join(absCtx, hostPath) + } + + if rel, ok := lexicallyWithin(absCtx, hostPath); ok { + // The daemon opens it from the tar. The host copy is only + // read for pre-pulling, and a failure to read it is left for + // the daemon to report, as before; escaping the bound is not. + real, err := resolveWithin(dockerfile, hostPath, absCtx, workspaceRoot) + var refused *runtime.DockerfileRefusedError + if errors.As(err, &refused) { + return "", nil, nil, err + } + if err == nil { + body, _ = readDockerfile(dockerfile, real) + } + return filepath.ToSlash(rel), body, nil, nil } - if rel, err := filepath.Rel(absCtx, dockerfile); err == nil && filepath.IsLocal(rel) { - return dockerfile, filepath.ToSlash(rel), nil, nil + + real, err := resolveWithin(dockerfile, hostPath, workspaceRoot) + if err != nil { + return "", nil, nil, err } - body, err := os.ReadFile(dockerfile) + body, err = readDockerfile(dockerfile, real) if err != nil { - return "", "", nil, fmt.Errorf("BuildImage: read dockerfile outside context: %w", err) + return "", nil, nil, err } sum := sha256.Sum256(body) name := ".dockerfile." + hex.EncodeToString(sum[:])[:12] - return dockerfile, name, &tarFile{Name: name, Body: body}, nil + return name, body, &tarFile{Name: name, Body: body}, nil +} + +// lexicallyWithin reports whether path is root or below it, without +// touching the filesystem, and returns path relative to root. +func lexicallyWithin(root, path string) (string, bool) { + rel, err := filepath.Rel(root, path) + if err != nil || !filepath.IsLocal(rel) { + return "", false + } + return rel, true +} + +// resolveWithin resolves hostPath's symlinks and returns the real +// path if it lies inside one of roots (each also resolved; empty +// roots are skipped). Otherwise it returns a +// *runtime.DockerfileRefusedError naming configured. A path that +// can't be resolved is refused too unless it lies lexically inside a +// root, so a missing file outside the bound can't be told apart from +// an existing one. +func resolveWithin(configured, hostPath string, roots ...string) (string, error) { + var bounds []string + for _, root := range roots { + if root == "" { + continue + } + bounds = append(bounds, root) + } + if len(bounds) == 0 { + return "", &runtime.DockerfileRefusedError{Path: configured, Reason: "outside the build context, and no workspace root is set"} + } + outside := &runtime.DockerfileRefusedError{Path: configured, Reason: "resolves outside " + strings.Join(bounds, " and ")} + + real, err := filepath.EvalSymlinks(hostPath) + if err != nil { + for _, root := range bounds { + if absRoot, aerr := filepath.Abs(root); aerr == nil { + if _, ok := lexicallyWithin(absRoot, hostPath); ok { + return "", fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + } + } + } + return "", outside + } + for _, root := range bounds { + realRoot, err := filepath.EvalSymlinks(root) + if err != nil { + continue + } + if _, ok := lexicallyWithin(realRoot, real); ok { + return real, nil + } + } + return "", outside +} + +// readDockerfile reads real, a path resolveWithin returned, as a +// regular file of at most maxInjectedDockerfileSize. The file is +// opened non-blocking and re-checked through its descriptor, so a +// FIFO or device can't stall the build and a file swapped after the +// check isn't read. +func readDockerfile(configured, real string) ([]byte, error) { + notRegular := &runtime.DockerfileRefusedError{Path: configured, Reason: "not a regular file"} + tooLarge := &runtime.DockerfileRefusedError{Path: configured, Reason: fmt.Sprintf("larger than %d bytes", maxInjectedDockerfileSize)} + + fi, err := os.Stat(real) + if err != nil { + return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + } + if !fi.Mode().IsRegular() { + return nil, notRegular + } + if fi.Size() > maxInjectedDockerfileSize { + return nil, tooLarge + } + f, err := os.OpenFile(real, os.O_RDONLY|syscall.O_NONBLOCK, 0) + if err != nil { + return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + } + defer func() { _ = f.Close() }() + ofi, err := f.Stat() + if err != nil { + return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + } + if !ofi.Mode().IsRegular() || !os.SameFile(fi, ofi) { + return nil, &runtime.DockerfileRefusedError{Path: configured, Reason: "changed while being checked"} + } + body, err := io.ReadAll(io.LimitReader(f, maxInjectedDockerfileSize+1)) + if err != nil { + return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + } + if len(body) > maxInjectedDockerfileSize { + return nil, tooLarge + } + return body, nil } // tarDirectory writes the contents of dir (recursively) into w as a @@ -318,7 +450,7 @@ func tarDirectory(dir string, extra *tarFile, w io.Writer) error { return err } -// prePullBaseImages reads the Dockerfile at dockerfilePath, +// prePullBaseImages takes the Dockerfile contents locateDockerfile read, // extracts its frontend image (a `# syntax=` directive or the // BUILDKIT_SYNTAX build arg) and FROM image references (resolving // simple $ARG substitutions against ARG defaults in the file and the @@ -335,9 +467,8 @@ func tarDirectory(dir string, extra *tarFile, w io.Writer) error { // anonymous registry access when no session is attached, so this only // changes the outcome on older engines; there it is what makes public // images resolvable at all. -func prePullBaseImages(ctx context.Context, r *Runtime, dockerfilePath string, buildArgs map[string]string, events chan<- runtime.BuildEvent) { - df, err := os.ReadFile(dockerfilePath) - if err != nil { +func prePullBaseImages(ctx context.Context, r *Runtime, df []byte, buildArgs map[string]string, events chan<- runtime.BuildEvent) { + if df == nil { return } refs := extractBaseImages(string(df), buildArgs) diff --git a/runtime/docker/build_test.go b/runtime/docker/build_test.go index 5a48e8c..65bce6b 100644 --- a/runtime/docker/build_test.go +++ b/runtime/docker/build_test.go @@ -3,13 +3,19 @@ package docker import ( "archive/tar" "bytes" + "context" + "errors" "io" + "io/fs" "os" "path/filepath" "reflect" "strings" + "syscall" "testing" "time" + + "github.com/crunchloop/devcontainer/runtime" ) func TestExtractBaseImages(t *testing.T) { @@ -242,65 +248,206 @@ func TestSyntaxFrontend(t *testing.T) { } } -func TestLocateDockerfile(t *testing.T) { +// dockerfileFixture lays out a workspace for locateDockerfile: +// +// /ws/.devcontainer/Dockerfile the config's Dockerfile +// /ws/app/ a context that doesn't hold it +// /outside/secret a file outside the workspace +type dockerfileFixture struct { + root, ws, app, dockerfile, secret string +} + +const fixtureSecret = "TOP-SECRET-must-not-leak" + +func newDockerfileFixture(t *testing.T) dockerfileFixture { + t.Helper() root := t.TempDir() - ctxDir := filepath.Join(root, "repo") - outside := filepath.Join(root, "elsewhere", "Dockerfile") - if err := os.MkdirAll(filepath.Dir(outside), 0o755); err != nil { + f := dockerfileFixture{ + root: root, + ws: filepath.Join(root, "ws"), + app: filepath.Join(root, "ws", "app"), + dockerfile: filepath.Join(root, "ws", ".devcontainer", "Dockerfile"), + secret: filepath.Join(root, "outside", "secret"), + } + writeFile(t, f.dockerfile, "FROM alpine\n") + writeFile(t, filepath.Join(f.app, "app.txt"), "app\n") + writeFile(t, f.secret, fixtureSecret+"\n") + return f +} + +func writeFile(t *testing.T, path, body string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } - if err := os.WriteFile(outside, []byte("FROM alpine\n"), 0o644); err != nil { + if err := os.WriteFile(path, []byte(body), 0o644); err != nil { t.Fatal(err) } +} - cases := []struct { - name, dockerfile, wantHost, wantTar string - }{ - {"empty defaults to Dockerfile", "", filepath.Join(ctxDir, "Dockerfile"), "Dockerfile"}, - {"relative to context", "sub/Dockerfile", filepath.Join(ctxDir, "sub", "Dockerfile"), "sub/Dockerfile"}, - {"absolute inside context", filepath.Join(ctxDir, ".devcontainer", "Dockerfile"), - filepath.Join(ctxDir, ".devcontainer", "Dockerfile"), ".devcontainer/Dockerfile"}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - host, name, injected, err := locateDockerfile(ctxDir, tc.dockerfile) - if err != nil { - t.Fatal(err) - } - if host != tc.wantHost || name != tc.wantTar || injected != nil { - t.Errorf("got (%q, %q, %v), want (%q, %q, nil)", host, name, injected, tc.wantHost, tc.wantTar) - } - }) - } +func TestLocateDockerfile(t *testing.T) { + f := newDockerfileFixture(t) + + t.Run("in context", func(t *testing.T) { + cases := []struct { + name, dockerfile, wantTar, wantBody string + }{ + // .devcontainer/Dockerfile with "context": ".." (#156). + {"absolute, context is the workspace", f.dockerfile, ".devcontainer/Dockerfile", "FROM alpine\n"}, + {"relative to context", ".devcontainer/Dockerfile", ".devcontainer/Dockerfile", "FROM alpine\n"}, + // Unreadable on the host: left for the daemon to report. + {"missing", "sub/Dockerfile", "sub/Dockerfile", ""}, + {"empty defaults to Dockerfile", "", "Dockerfile", ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + name, body, injected, err := locateDockerfile(f.ws, tc.dockerfile, f.ws) + if err != nil { + t.Fatal(err) + } + if name != tc.wantTar || string(body) != tc.wantBody || injected != nil { + t.Errorf("got (%q, %q, %v), want (%q, %q, nil)", name, body, injected, tc.wantTar, tc.wantBody) + } + }) + } + }) - t.Run("absolute outside context is injected", func(t *testing.T) { - host, name, injected, err := locateDockerfile(ctxDir, outside) + t.Run("in context with no workspace root", func(t *testing.T) { + name, body, injected, err := locateDockerfile(f.ws, f.dockerfile, "") + if err != nil || name != ".devcontainer/Dockerfile" || string(body) != "FROM alpine\n" || injected != nil { + t.Fatalf("got (%q, %q, %v, %v)", name, body, injected, err) + } + }) + + t.Run("outside context, inside workspace is injected", func(t *testing.T) { + name, body, injected, err := locateDockerfile(f.app, f.dockerfile, f.ws) if err != nil { t.Fatal(err) } - if host != outside { - t.Errorf("host = %q, want %q", host, outside) - } - if injected == nil || injected.Name != name || string(injected.Body) != "FROM alpine\n" { - t.Fatalf("injected = %+v, name %q", injected, name) + if injected == nil || injected.Name != name || string(injected.Body) != "FROM alpine\n" || string(body) != "FROM alpine\n" { + t.Fatalf("injected = %+v, name %q, body %q", injected, name, body) } if !strings.HasPrefix(name, ".dockerfile.") { t.Errorf("name = %q, want .dockerfile.", name) } // Deterministic: same content, same name. - _, again, _, _ := locateDockerfile(ctxDir, outside) + again, _, _, _ := locateDockerfile(f.app, f.dockerfile, f.ws) if again != name { t.Errorf("name not stable: %q vs %q", name, again) } }) - t.Run("missing outside dockerfile errors", func(t *testing.T) { - if _, _, _, err := locateDockerfile(ctxDir, filepath.Join(root, "nope", "Dockerfile")); err == nil { - t.Fatal("want error") + t.Run("relative traversal inside workspace is injected", func(t *testing.T) { + _, _, injected, err := locateDockerfile(f.app, "../.devcontainer/Dockerfile", f.ws) + if err != nil || injected == nil { + t.Fatalf("injected = %+v, err = %v", injected, err) + } + }) + + t.Run("symlink staying inside workspace is injected", func(t *testing.T) { + link := filepath.Join(f.ws, "Dockerfile.link") + if err := os.Symlink(f.dockerfile, link); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Remove(link) }) + _, _, injected, err := locateDockerfile(f.app, link, f.ws) + if err != nil || injected == nil || string(injected.Body) != "FROM alpine\n" { + t.Fatalf("injected = %+v, err = %v", injected, err) + } + }) + + t.Run("missing inside workspace is a plain error", func(t *testing.T) { + _, _, _, err := locateDockerfile(f.app, filepath.Join(f.ws, "nope", "Dockerfile"), f.ws) + if err == nil || !errors.Is(err, fs.ErrNotExist) { + t.Fatalf("err = %v, want not-exist", err) } }) } +// Every refused path fails with *runtime.DockerfileRefusedError naming +// the configured path, and never with the file's contents. +func TestLocateDockerfileRefuses(t *testing.T) { + f := newDockerfileFixture(t) + + escape := filepath.Join(f.ws, "escape") + if err := os.Symlink(f.secret, escape); err != nil { + t.Fatal(err) + } + inCtxEscape := filepath.Join(f.app, "Dockerfile") + if err := os.Symlink(f.secret, inCtxEscape); err != nil { + t.Fatal(err) + } + home := filepath.Join(f.root, "home") + writeFile(t, filepath.Join(home, "x"), fixtureSecret+"\n") + fifo := filepath.Join(f.ws, "fifo") + if err := syscall.Mkfifo(fifo, 0o644); err != nil { + t.Fatal(err) + } + big := filepath.Join(f.ws, "big") + writeFile(t, big, fixtureSecret+strings.Repeat("#", maxInjectedDockerfileSize)) + + cases := []struct { + name, context, dockerfile, root string + }{ + {"absolute temp file outside the workspace", f.app, f.secret, f.ws}, + {"absolute traversal out of the workspace", f.app, filepath.Join(f.ws, "..", "outside", "secret"), f.ws}, + {"relative traversal out of the workspace", f.app, "../../outside/secret", f.ws}, + {"symlink in the workspace pointing outside", f.app, escape, f.ws}, + {"in-context symlink pointing outside", f.app, "Dockerfile", f.ws}, + {"HOME outside the workspace", f.app, filepath.Join(home, "x"), f.ws}, + {"missing file outside the workspace", f.app, filepath.Join(f.root, "outside", "nope"), f.ws}, + {"directory", f.app, filepath.Join(f.ws, ".devcontainer"), f.ws}, + {"fifo", f.app, fifo, f.ws}, + {"oversized", f.app, big, f.ws}, + {"outside the context with no workspace root", f.app, f.dockerfile, ""}, + } + if _, err := os.Stat("/etc/hostname"); err == nil { + cases = append(cases, struct{ name, context, dockerfile, root string }{"/etc/hostname", f.app, "/etc/hostname", f.ws}) + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + name, body, injected, err := locateDockerfile(tc.context, tc.dockerfile, tc.root) + var refused *runtime.DockerfileRefusedError + if !errors.As(err, &refused) { + t.Fatalf("err = %v, want *runtime.DockerfileRefusedError", err) + } + if name != "" || body != nil || injected != nil { + t.Errorf("refused path returned (%q, %q, %v)", name, body, injected) + } + if refused.Path != tc.dockerfile || !strings.Contains(err.Error(), tc.dockerfile) { + t.Errorf("error %q does not name %q", err, tc.dockerfile) + } + if strings.Contains(err.Error(), fixtureSecret) { + t.Errorf("error leaks file contents: %q", err) + } + }) + } +} + +// A refused Dockerfile fails BuildImage before any daemon call: the +// Runtime here has no client, so reaching the daemon would panic. +func TestBuildImageRefusesBeforeDaemon(t *testing.T) { + f := newDockerfileFixture(t) + events := make(chan runtime.BuildEvent, 16) + _, err := (&Runtime{}).BuildImage(context.Background(), runtime.BuildSpec{ + ContextPath: f.app, + Dockerfile: f.secret, + WorkspaceRoot: f.ws, + Tag: "refused:test", + }, events) + var refused *runtime.DockerfileRefusedError + if !errors.As(err, &refused) { + t.Fatalf("err = %v, want *runtime.DockerfileRefusedError", err) + } + if strings.Contains(err.Error(), fixtureSecret) { + t.Errorf("error leaks file contents: %q", err) + } + close(events) + for ev := range events { + t.Errorf("unexpected event %+v", ev) + } +} + func TestTarDirectoryAppendsExtra(t *testing.T) { dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, "file.txt"), []byte("hello"), 0o644); err != nil { diff --git a/runtime/errors.go b/runtime/errors.go index 4570e34..3b2d590 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -80,3 +80,20 @@ func (e *DaemonUnavailableError) Error() string { } func (e *DaemonUnavailableError) Unwrap() error { return e.Err } + +// DockerfileRefusedError indicates BuildImage refused to read a +// BuildSpec.Dockerfile from the host: it resolves (after symlinks) +// outside both ContextPath and WorkspaceRoot, or it is not a regular +// file, or it is too large to inject. The build fails before any +// daemon call. The message names the configured path and never +// includes the file's contents, which are not read. +type DockerfileRefusedError struct { + // Path is the Dockerfile path as given in the BuildSpec. + Path string + // Reason says why the path was refused. + Reason string +} + +func (e *DockerfileRefusedError) Error() string { + return fmt.Sprintf("dockerfile %s refused: %s", e.Path, e.Reason) +} diff --git a/runtime/runtime.go b/runtime/runtime.go index b9c323e..8f71e44 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -473,13 +473,22 @@ type BuildSpec struct { ContextPath string // Dockerfile is relative to ContextPath (empty means "Dockerfile") // or an absolute host path, which may lie outside ContextPath. + // After symlinks are resolved it must lie inside ContextPath or + // WorkspaceRoot; otherwise BuildImage returns a + // *DockerfileRefusedError without reading it. Dockerfile string - Tag string - Args map[string]string - Target string - CacheFrom []string - NoCache bool - Platform string + // WorkspaceRoot bounds a Dockerfile outside ContextPath: such a + // file is read from the host and sent to the daemon only if it + // resolves to a regular file inside this folder (the project the + // config was discovered in). Empty allows no Dockerfile outside + // ContextPath. + WorkspaceRoot string + Tag string + Args map[string]string + Target string + CacheFrom []string + NoCache bool + Platform string } // TtySize is a pty geometry in character cells. A zero value means diff --git a/test/integration/build_dockerfile_bound_test.go b/test/integration/build_dockerfile_bound_test.go new file mode 100644 index 0000000..b7be838 --- /dev/null +++ b/test/integration/build_dockerfile_bound_test.go @@ -0,0 +1,197 @@ +//go:build integration + +package integration + +import ( + "context" + "errors" + "fmt" + "net" + "net/http" + "net/http/httputil" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + devcontainer "github.com/crunchloop/devcontainer" + "github.com/crunchloop/devcontainer/config" + "github.com/crunchloop/devcontainer/events" + "github.com/crunchloop/devcontainer/runtime" + "github.com/crunchloop/devcontainer/runtime/docker" +) + +// TestBuildSource_DockerfileOutsideWorkspaceRefused: a hostile +// devcontainer.json names a secret-looking host file outside the +// workspace as its Dockerfile. Sent to the daemon, its first line +// would come back in the parse error ("unknown instruction: …") and +// end up in the build output. The build must fail before the daemon +// builds or pulls anything, and the secret must appear in no event and +// no error. +// +// The absolute path is caught while resolving the config; the symlink +// passes that lexical check and is caught by the runtime after +// resolving symlinks. +func TestBuildSource_DockerfileOutsideWorkspaceRefused(t *testing.T) { + if testing.Short() { + t.Skip() + } + + secret := fmt.Sprintf("DCGO-SECRET-%d", time.Now().UnixNano()) + secretFile := filepath.Join(t.TempDir(), "credentials") + mustWrite(t, secretFile, secret+"\n") + + cases := []struct { + name string + setup func(t *testing.T, ws string) string // returns build.dockerfile + wantErr func(error) bool + }{ + { + name: "absolute path", + setup: func(t *testing.T, ws string) string { return secretFile }, + wantErr: func(err error) bool { + var invalid *config.ConfigInvalidError + return errors.As(err, &invalid) + }, + }, + { + name: "symlink in the workspace", + setup: func(t *testing.T, ws string) string { + if err := os.Symlink(secretFile, filepath.Join(ws, ".devcontainer", "Dockerfile")); err != nil { + t.Fatal(err) + } + return "Dockerfile" + }, + wantErr: func(err error) bool { + var refused *runtime.DockerfileRefusedError + return errors.As(err, &refused) + }, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + eng, rt, requests := newRecordingEngine(t) + defer rt.Close() + + ws := t.TempDir() + mustWrite(t, filepath.Join(ws, "app", "app.txt"), "app\n") + if err := os.MkdirAll(filepath.Join(ws, ".devcontainer"), 0o755); err != nil { + t.Fatal(err) + } + dockerfile := tc.setup(t, ws) + mustWrite(t, filepath.Join(ws, ".devcontainer", "devcontainer.json"), + fmt.Sprintf(`{"build": {"dockerfile": %q, "context": "../app"}}`, dockerfile)) + + evCh := make(chan events.Event, 1024) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + const tag = "dc-go-build-test-refused:v1" + _, err := eng.Build(ctx, devcontainer.BuildOptions{ + LocalWorkspaceFolder: ws, + ImageName: tag, + Events: evCh, + }) + t.Cleanup(func() { _ = rt.RemoveImage(context.Background(), tag) }) + + if err == nil { + t.Fatal("Build succeeded, want refusal") + } + if !tc.wantErr(err) { + t.Errorf("err = %v (%T), want the typed refusal", err, err) + } + if !strings.Contains(err.Error(), dockerfile) { + t.Errorf("error %q does not name the configured path %q", err, dockerfile) + } + if strings.Contains(err.Error(), secret) { + t.Errorf("error leaks the secret: %q", err) + } + + close(evCh) + for ev := range evCh { + if s := fmt.Sprintf("%#v", ev); strings.Contains(s, secret) { + t.Errorf("event leaks the secret: %s", s) + } + } + + if len(requests()) == 0 { + t.Fatal("proxy recorded no requests; the recorder is not in the path") + } + for _, req := range requests() { + if strings.HasSuffix(req, "/build") || strings.Contains(req, "/images/create") { + t.Errorf("daemon reached before the refusal: %s", req) + } + } + }) + } +} + +// newRecordingEngine is newEngine with the runtime talking to the +// daemon through a proxy that records each request's method and path, +// so a test can assert what never reached the daemon. +func newRecordingEngine(t *testing.T) (*devcontainer.Engine, *docker.Runtime, func() []string) { + t.Helper() + daemon := "/var/run/docker.sock" + if host := os.Getenv("DOCKER_HOST"); host != "" { + if !strings.HasPrefix(host, "unix://") { + t.Skipf("recording proxy needs a unix DOCKER_HOST, have %q", host) + } + daemon = strings.TrimPrefix(host, "unix://") + } + + // Short path: unix socket paths are limited to ~104 bytes. + dir, err := os.MkdirTemp("", "dcgo") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.RemoveAll(dir) }) + sock := filepath.Join(dir, "docker.sock") + ln, err := net.Listen("unix", sock) + if err != nil { + t.Fatal(err) + } + + var ( + mu sync.Mutex + seen []string + ) + proxy := &httputil.ReverseProxy{ + Rewrite: func(r *httputil.ProxyRequest) { + r.Out.URL.Scheme = "http" + r.Out.URL.Host = "docker" + }, + Transport: &http.Transport{ + DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { + var d net.Dialer + return d.DialContext(ctx, "unix", daemon) + }, + }, + FlushInterval: -1, + } + srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + seen = append(seen, r.Method+" "+r.URL.Path) + mu.Unlock() + proxy.ServeHTTP(w, r) + })} + go func() { _ = srv.Serve(ln) }() + t.Cleanup(func() { _ = srv.Close() }) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + rt, err := docker.New(ctx, docker.Options{Host: "unix://" + sock}) + if err != nil { + t.Skipf("Docker daemon unavailable through proxy: %v", err) + } + eng, err := devcontainer.New(devcontainer.EngineOptions{Runtime: rt}) + if err != nil { + _ = rt.Close() + t.Fatalf("New: %v", err) + } + return eng, rt, func() []string { + mu.Lock() + defer mu.Unlock() + return append([]string(nil), seen...) + } +} diff --git a/up.go b/up.go index 9c1a295..f9ba25f 100644 --- a/up.go +++ b/up.go @@ -1164,11 +1164,12 @@ func (e *Engine) buildComposeSidecarImages( } opts.bus.Emit(events.BuildStartEvent{Source: events.BuildSourceDockerfile, Ref: tag}) if _, err := e.runtime.BuildImage(ctx, runtime.BuildSpec{ - ContextPath: ctxPath, - Dockerfile: svc.Build.Dockerfile, - Tag: tag, - Args: flattenStringMap(svc.Build.Args), - Target: svc.Build.Target, + ContextPath: ctxPath, + Dockerfile: svc.Build.Dockerfile, + WorkspaceRoot: opts.LocalWorkspaceFolder, + Tag: tag, + Args: flattenStringMap(svc.Build.Args), + Target: svc.Build.Target, }, opts.bus.BuildChan(events.BuildSourceDockerfile)); err != nil { return fmt.Errorf("build compose service %q image: %w", name, err) } @@ -1203,11 +1204,12 @@ func (e *Engine) prepareComposeServiceImage(ctx context.Context, cfg *config.Res } opts.bus.Emit(events.BuildStartEvent{Source: events.BuildSourceDockerfile, Ref: tag}) _, err := e.runtime.BuildImage(ctx, runtime.BuildSpec{ - ContextPath: ctxPath, - Dockerfile: svc.Build.Dockerfile, - Tag: tag, - Args: flattenStringMap(svc.Build.Args), - Target: svc.Build.Target, + ContextPath: ctxPath, + Dockerfile: svc.Build.Dockerfile, + WorkspaceRoot: cfg.LocalWorkspaceFolder, + Tag: tag, + Args: flattenStringMap(svc.Build.Args), + Target: svc.Build.Target, }, opts.bus.BuildChan(events.BuildSourceDockerfile)) if err != nil { return "", fmt.Errorf("build compose primary service image: %w", err) @@ -1352,14 +1354,15 @@ func (e *Engine) prepareBaseImage(ctx context.Context, cfg *config.ResolvedConfi } opts.bus.Emit(events.BuildStartEvent{Source: events.BuildSourceDockerfile, Ref: tag}) _, err := e.runtime.BuildImage(ctx, runtime.BuildSpec{ - ContextPath: s.Context, - Dockerfile: s.Dockerfile, - Tag: tag, - Args: s.Args, - Target: s.Target, - CacheFrom: cacheFrom, - NoCache: noCache, - Platform: platform, + ContextPath: s.Context, + Dockerfile: s.Dockerfile, + WorkspaceRoot: cfg.LocalWorkspaceFolder, + Tag: tag, + Args: s.Args, + Target: s.Target, + CacheFrom: cacheFrom, + NoCache: noCache, + Platform: platform, }, opts.bus.BuildChan(events.BuildSourceDockerfile)) if err != nil { return "", fmt.Errorf("build base image from %s: %w", s.Context, err) From a4e935fecd964efe0e0e3bdedc25230455189fe5 Mon Sep 17 00:00:00 2001 From: bilby91 <2201079+bilby91@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:35:30 +0000 Subject: [PATCH 2/3] fix(build): read the bounded Dockerfile through os.Root The bound was checked on a resolved pathname and the file then opened by pathname again, so a directory on the way swapped for a symlink in between could carry the read outside the workspace; SameFile only compared the stat and the open, which both followed the swap. readDockerfile now opens os.Root on the resolved root and stats, opens and reads the file through it, so no symlink or ".." can leave the root whatever the tree does after the check. Any failure to open it there, after the check found it, is a refusal. Co-Authored-By: Claude Opus 5.5 --- design/resolved-config.md | 3 ++ runtime/docker/build.go | 81 +++++++++++++++++++++++------------- runtime/docker/build_test.go | 27 ++++++++++++ 3 files changed, 81 insertions(+), 30 deletions(-) diff --git a/design/resolved-config.md b/design/resolved-config.md index 37ca5f7..af3bfcc 100644 --- a/design/resolved-config.md +++ b/design/resolved-config.md @@ -160,6 +160,9 @@ process can read into the build output. The bound: before any read and before any daemon call, and returns `*runtime.DockerfileRefusedError`. Both errors name the configured path and never the file's contents. +- The runtime reads the file through an `os.Root` on the resolved root, + so a directory swapped for a symlink after the check can't carry the + read outside it. Why interface over `Kind`+pointer-fields struct: - Type switches read more naturally: `switch s := cfg.Source.(type) { case *ImageSource: ... }`. diff --git a/runtime/docker/build.go b/runtime/docker/build.go index 8b46d5a..a50a99b 100644 --- a/runtime/docker/build.go +++ b/runtime/docker/build.go @@ -236,22 +236,22 @@ func locateDockerfile(contextPath, dockerfile, workspaceRoot string) (tarName st // The daemon opens it from the tar. The host copy is only // read for pre-pulling, and a failure to read it is left for // the daemon to report, as before; escaping the bound is not. - real, err := resolveWithin(dockerfile, hostPath, absCtx, workspaceRoot) + at, err := resolveWithin(dockerfile, hostPath, absCtx, workspaceRoot) var refused *runtime.DockerfileRefusedError if errors.As(err, &refused) { return "", nil, nil, err } if err == nil { - body, _ = readDockerfile(dockerfile, real) + body, _ = readDockerfile(dockerfile, at) } return filepath.ToSlash(rel), body, nil, nil } - real, err := resolveWithin(dockerfile, hostPath, workspaceRoot) + at, err := resolveWithin(dockerfile, hostPath, workspaceRoot) if err != nil { return "", nil, nil, err } - body, err = readDockerfile(dockerfile, real) + body, err = readDockerfile(dockerfile, at) if err != nil { return "", nil, nil, err } @@ -270,14 +270,21 @@ func lexicallyWithin(root, path string) (string, bool) { return rel, true } -// resolveWithin resolves hostPath's symlinks and returns the real -// path if it lies inside one of roots (each also resolved; empty -// roots are skipped). Otherwise it returns a -// *runtime.DockerfileRefusedError naming configured. A path that -// can't be resolved is refused too unless it lies lexically inside a -// root, so a missing file outside the bound can't be told apart from -// an existing one. -func resolveWithin(configured, hostPath string, roots ...string) (string, error) { +// confinedPath is a Dockerfile located inside a bound: Rel, relative +// to the real (symlink-free) Root. +type confinedPath struct { + Root, Rel string +} + +// resolveWithin resolves hostPath's symlinks and returns where it lies +// inside one of roots (each also resolved; empty roots are skipped). +// Otherwise it returns a *runtime.DockerfileRefusedError naming +// configured. A path that can't be resolved is refused too unless it +// lies lexically inside a root, so a missing file outside the bound +// can't be told apart from an existing one. The result only picks the +// root: readDockerfile opens through it, which is what confines the +// read if the tree changes after this check. +func resolveWithin(configured, hostPath string, roots ...string) (confinedPath, error) { var bounds []string for _, root := range roots { if root == "" { @@ -286,7 +293,7 @@ func resolveWithin(configured, hostPath string, roots ...string) (string, error) bounds = append(bounds, root) } if len(bounds) == 0 { - return "", &runtime.DockerfileRefusedError{Path: configured, Reason: "outside the build context, and no workspace root is set"} + return confinedPath{}, &runtime.DockerfileRefusedError{Path: configured, Reason: "outside the build context, and no workspace root is set"} } outside := &runtime.DockerfileRefusedError{Path: configured, Reason: "resolves outside " + strings.Join(bounds, " and ")} @@ -295,36 +302,50 @@ func resolveWithin(configured, hostPath string, roots ...string) (string, error) for _, root := range bounds { if absRoot, aerr := filepath.Abs(root); aerr == nil { if _, ok := lexicallyWithin(absRoot, hostPath); ok { - return "", fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + return confinedPath{}, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) } } } - return "", outside + return confinedPath{}, outside } for _, root := range bounds { realRoot, err := filepath.EvalSymlinks(root) if err != nil { continue } - if _, ok := lexicallyWithin(realRoot, real); ok { - return real, nil + if rel, ok := lexicallyWithin(realRoot, real); ok { + return confinedPath{Root: realRoot, Rel: rel}, nil } } - return "", outside + return confinedPath{}, outside } -// readDockerfile reads real, a path resolveWithin returned, as a -// regular file of at most maxInjectedDockerfileSize. The file is -// opened non-blocking and re-checked through its descriptor, so a -// FIFO or device can't stall the build and a file swapped after the -// check isn't read. -func readDockerfile(configured, real string) ([]byte, error) { +// readDockerfile reads at as a regular file of at most +// maxInjectedDockerfileSize. It opens the file through an os.Root on +// at.Root, so no symlink or ".." can carry the open outside the root, +// even if a directory on the way is swapped for a symlink after +// resolveWithin checked the path. The file is opened non-blocking and +// re-checked through its descriptor, so a FIFO or device can't stall +// the build and a file swapped after the stat isn't read. +func readDockerfile(configured string, at confinedPath) ([]byte, error) { notRegular := &runtime.DockerfileRefusedError{Path: configured, Reason: "not a regular file"} tooLarge := &runtime.DockerfileRefusedError{Path: configured, Reason: fmt.Sprintf("larger than %d bytes", maxInjectedDockerfileSize)} + // resolveWithin just found the file inside at.Root, so failing to + // open it there means the tree changed, possibly to point outside + // (os.Root refuses that with a plain path error): refuse it. + wrap := func(err error) error { + return &runtime.DockerfileRefusedError{Path: configured, Reason: "cannot be read inside " + at.Root + ": " + err.Error()} + } + + root, err := os.OpenRoot(at.Root) + if err != nil { + return nil, wrap(err) + } + defer func() { _ = root.Close() }() - fi, err := os.Stat(real) + fi, err := root.Stat(at.Rel) if err != nil { - return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + return nil, wrap(err) } if !fi.Mode().IsRegular() { return nil, notRegular @@ -332,21 +353,21 @@ func readDockerfile(configured, real string) ([]byte, error) { if fi.Size() > maxInjectedDockerfileSize { return nil, tooLarge } - f, err := os.OpenFile(real, os.O_RDONLY|syscall.O_NONBLOCK, 0) + f, err := root.OpenFile(at.Rel, os.O_RDONLY|syscall.O_NONBLOCK, 0) if err != nil { - return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + return nil, wrap(err) } defer func() { _ = f.Close() }() ofi, err := f.Stat() if err != nil { - return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + return nil, wrap(err) } if !ofi.Mode().IsRegular() || !os.SameFile(fi, ofi) { return nil, &runtime.DockerfileRefusedError{Path: configured, Reason: "changed while being checked"} } body, err := io.ReadAll(io.LimitReader(f, maxInjectedDockerfileSize+1)) if err != nil { - return nil, fmt.Errorf("BuildImage: dockerfile %s: %w", configured, err) + return nil, wrap(err) } if len(body) > maxInjectedDockerfileSize { return nil, tooLarge diff --git a/runtime/docker/build_test.go b/runtime/docker/build_test.go index 65bce6b..4226cf0 100644 --- a/runtime/docker/build_test.go +++ b/runtime/docker/build_test.go @@ -424,6 +424,33 @@ func TestLocateDockerfileRefuses(t *testing.T) { } } +// A directory on the checked path swapped for a symlink to outside the +// workspace after resolveWithin ran must not be followed by the read: +// this is the state the swap leaves, read through the checked result. +func TestReadDockerfileRefusesAncestorSwap(t *testing.T) { + f := newDockerfileFixture(t) + writeFile(t, filepath.Join(f.ws, "sub", "secret"), "FROM alpine\n") + at, err := resolveWithin("sub/secret", filepath.Join(f.ws, "sub", "secret"), f.ws) + if err != nil { + t.Fatal(err) + } + if err := os.RemoveAll(filepath.Join(f.ws, "sub")); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Dir(f.secret), filepath.Join(f.ws, "sub")); err != nil { + t.Fatal(err) + } + + body, err := readDockerfile("sub/secret", at) + var refused *runtime.DockerfileRefusedError + if !errors.As(err, &refused) { + t.Fatalf("err = %v, body %q; want *runtime.DockerfileRefusedError", err, body) + } + if body != nil || strings.Contains(err.Error(), fixtureSecret) { + t.Errorf("read through the swapped directory: body %q, err %q", body, err) + } +} + // A refused Dockerfile fails BuildImage before any daemon call: the // Runtime here has no client, so reaching the daemon would panic. func TestBuildImageRefusesBeforeDaemon(t *testing.T) { From 138bda077f1ab97f502f7c82bdcbf4460a3df3f2 Mon Sep 17 00:00:00 2001 From: bilby91 <2201079+bilby91@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:06:27 +0000 Subject: [PATCH 3/3] fix(build): confine in-context Dockerfile reads to the workspace root os.OpenRoot opens the root itself by path. For a Dockerfile inside the context, the context was tried first as the root; a context inside the workspace is a folder the repository controls, so it could be swapped for a symlink between the check and the open. Try the workspace root first: its own path lies outside what the repository controls. The context remains the root only for a context outside the workspace. Co-Authored-By: Claude Opus 5.5 --- design/resolved-config.md | 4 +++- runtime/docker/build.go | 17 ++++++++++++----- runtime/docker/build_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/design/resolved-config.md b/design/resolved-config.md index af3bfcc..e518107 100644 --- a/design/resolved-config.md +++ b/design/resolved-config.md @@ -162,7 +162,9 @@ process can read into the build output. The bound: path and never the file's contents. - The runtime reads the file through an `os.Root` on the resolved root, so a directory swapped for a symlink after the check can't carry the - read outside it. + read outside it. That root is the workspace whenever the file is in + it, not a context inside the workspace, whose own path the repository + could swap. Why interface over `Kind`+pointer-fields struct: - Type switches read more naturally: `switch s := cfg.Source.(type) { case *ImageSource: ... }`. diff --git a/runtime/docker/build.go b/runtime/docker/build.go index a50a99b..c74cefb 100644 --- a/runtime/docker/build.go +++ b/runtime/docker/build.go @@ -236,7 +236,10 @@ func locateDockerfile(contextPath, dockerfile, workspaceRoot string) (tarName st // The daemon opens it from the tar. The host copy is only // read for pre-pulling, and a failure to read it is left for // the daemon to report, as before; escaping the bound is not. - at, err := resolveWithin(dockerfile, hostPath, absCtx, workspaceRoot) + // Workspace first: a context inside the workspace is a folder + // the repository controls, so it must not be the root the read + // is confined to (see resolveWithin). + at, err := resolveWithin(dockerfile, hostPath, workspaceRoot, absCtx) var refused *runtime.DockerfileRefusedError if errors.As(err, &refused) { return "", nil, nil, err @@ -277,13 +280,17 @@ type confinedPath struct { } // resolveWithin resolves hostPath's symlinks and returns where it lies -// inside one of roots (each also resolved; empty roots are skipped). -// Otherwise it returns a *runtime.DockerfileRefusedError naming -// configured. A path that can't be resolved is refused too unless it +// inside the first of roots that holds it (each also resolved; empty +// roots are skipped). Otherwise it returns a +// *runtime.DockerfileRefusedError naming configured. A path that can't be resolved is refused too unless it // lies lexically inside a root, so a missing file outside the bound // can't be told apart from an existing one. The result only picks the // root: readDockerfile opens through it, which is what confines the -// read if the tree changes after this check. +// read if the tree changes after this check. os.OpenRoot opens the +// root itself by path, so list the outermost root first: the +// workspace root's path lies outside what the repository controls, +// while a context inside the workspace could be swapped for a symlink +// between this check and the open. func resolveWithin(configured, hostPath string, roots ...string) (confinedPath, error) { var bounds []string for _, root := range roots { diff --git a/runtime/docker/build_test.go b/runtime/docker/build_test.go index 4226cf0..8d62fa9 100644 --- a/runtime/docker/build_test.go +++ b/runtime/docker/build_test.go @@ -451,6 +451,38 @@ func TestReadDockerfileRefusesAncestorSwap(t *testing.T) { } } +// A context inside the workspace is a folder the repository controls, +// so an in-context Dockerfile is confined to the workspace root, not +// to the context: swapping the context for a symlink to outside after +// the check must not carry the read with it. +func TestReadDockerfileRefusesContextSwap(t *testing.T) { + f := newDockerfileFixture(t) + writeFile(t, filepath.Join(f.app, "Dockerfile"), "FROM alpine\n") + writeFile(t, filepath.Join(filepath.Dir(f.secret), "Dockerfile"), fixtureSecret+"\n") + at, err := resolveWithin("Dockerfile", filepath.Join(f.app, "Dockerfile"), f.ws, f.app) + if err != nil { + t.Fatal(err) + } + if realWS, _ := filepath.EvalSymlinks(f.ws); at.Root != realWS { + t.Fatalf("Root = %q, want the workspace %q", at.Root, realWS) + } + if err := os.RemoveAll(f.app); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Dir(f.secret), f.app); err != nil { + t.Fatal(err) + } + + body, err := readDockerfile("Dockerfile", at) + var refused *runtime.DockerfileRefusedError + if !errors.As(err, &refused) { + t.Fatalf("err = %v, body %q; want *runtime.DockerfileRefusedError", err, body) + } + if body != nil || strings.Contains(err.Error(), fixtureSecret) { + t.Errorf("read through the swapped context: body %q, err %q", body, err) + } +} + // A refused Dockerfile fails BuildImage before any daemon call: the // Runtime here has no client, so reaching the daemon would panic. func TestBuildImageRefusesBeforeDaemon(t *testing.T) {