Skip to content
Merged
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
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions config/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
}
}
53 changes: 50 additions & 3 deletions config/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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.
Expand All @@ -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"],
Expand Down
30 changes: 29 additions & 1 deletion design/resolved-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -138,6 +138,34 @@ 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.
- 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. 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: ... }`.
- Zero risk of "Kind says image but Image is nil" inconsistency.
Expand Down
Loading