Skip to content

fix(path): only treat a ".." path element as traversal - #4803

Merged
aheritier merged 2 commits into
docker:mainfrom
kevin9327:fix/path-dot-dot-names
Nov 12, 2026
Merged

aheritier merged 2 commits into
docker:mainfrom
kevin9327:fix/path-dot-dot-names

Conversation

@kevin9327

Copy link
Copy Markdown
Contributor

What

path.ValidatePathInDirectory rejected valid paths whose names only start with or contain two dots:

  • ..env or ..config/agent.yaml relative to the allowed directory -> path outside allowed directory, because the check was strings.HasPrefix(relPath, "..").
  • the same names with no allowed directory -> path contains directory traversal sequences (strings.HasPrefix(cleanPath, "..")).
  • absolute paths such as /opt/app..v2/agent.yaml with no allowed directory -> rejected by strings.Contains(path, "..").

It is used for env_file entries (pkg/environment/env_files.go) and toolset paths (pkg/tools/toolsetpath), so such files could not be referenced.

Fix

A small hasParentSegment helper splits the path on separators and reports a .. element; the three checks use it. Real traversal is still rejected exactly as before (.., ../x, /opt/../etc/passwd, a sibling directory that shares the allowed directory's prefix). IsWithin in the same package already applies the same element-based rule.

Tests

  • TestValidatePathInDirectory: new cases for ..env, ..config/agent.yaml (allowed), .. and a <tmp>-other sibling (rejected). The success check now uses IsWithin instead of the same prefix test.
  • New TestValidatePathInDirectoryWithoutAllowedDir for the allowedDir == "" branches (relative and absolute, allowed and traversal).
  • Before the fix 5 of the new cases fail; after it go test ./pkg/path/ ./pkg/environment/ passes. gofmt and go vet ./pkg/path/ are clean (golangci-lint not run locally).

pkg/environment/env_files.go has its own strings.Contains(relOrAbsPath, "..") check for absolute and ~ paths; I left it unchanged to keep this PR to the shared helper, happy to align it here or in a follow-up.

Assisted-By: Grok Bot (AI coding assistant). I reviewed the change and ran the tests above.

ValidatePathInDirectory rejected names that merely start with or contain
two dots, such as "..env" or "/opt/app..v2/agent.yaml", because it
checked string prefixes and substrings instead of path elements. Check for
a ".." element instead, as IsWithin already does.

Signed-off-by: kevin9327 <5299031+kevin9327@users.noreply.github.com>
@kevin9327
kevin9327 requested a review from a team as a code owner November 4, 2026 11:31
@aheritier aheritier added area/core Core agent runtime, session management kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Nov 4, 2026

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed both changed files and the environment/toolset callers against the stated shared-helper scope. See inline comment.

CI operational status: gate and test-windows failed on this head; test-linux, lint, and cross-compilation passed. CI is not green.

Issue context: No explicit linked GitHub/Jira issues or closing issues were found. The PR description supplied the checklist: accept dot-containing names, preserve parent-traversal rejection, and preserve directory containment. External acceptance criteria were not supplied (needs-decision).

Prior threads: None; paginated issue comments, reviews, review comments, and thread-state reads were complete.

Comment thread pkg/path/path_test.go Outdated
{name: "relative parent directory", path: "..", expectError: true},
{name: "absolute path", path: filepath.Join(string(filepath.Separator), "opt", "agent.yaml")},
{name: "absolute name containing two dots", path: filepath.Join(string(filepath.Separator), "opt", "app..v2", "agent.yaml")},
{name: "absolute parent traversal", path: string(filepath.Separator) + strings.Join([]string{"opt", "..", "etc", "passwd"}, string(filepath.Separator)), expectError: true},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] Construct a genuinely absolute path on Windows (confidence: high). This expression produces \opt\..\etc\passwd on Windows, which has no volume and therefore fails filepath.IsAbs. ValidatePathInDirectory consequently takes the relative-path branch after filepath.Clean has removed .., returning success instead of the expected error. The current Windows job confirms this exact subtest fails at line 132: https://github.com/docker/docker-agent/actions/runs/41821640709/job/126835878940. Build the absolute test cases with a volume-qualified root, for example filepath.VolumeName(t.TempDir()) + string(filepath.Separator), and append the traversal components without cleaning them. This restores the Windows suite and ensures the test actually exercises the absolute-path traversal guard on both platforms.

@aheritier
aheritier marked this pull request as draft November 4, 2026 21:35
@kevin9327

Copy link
Copy Markdown
Contributor Author

Thanks, you're right: \opt\..\etc\passwd has no volume on Windows, so it never reached the absolute branch. Fixed in be3abb2. The absolute cases in TestValidatePathInDirectoryWithoutAllowedDir now start from filepath.VolumeName(t.TempDir()) + string(filepath.Separator) and the traversal components are joined with the separator without cleaning. go test ./pkg/path/ passes on Linux and GOOS=windows go vet ./pkg/path/ is clean; I don't have a Windows machine here, so the Windows job is the real check.

@aheritier aheritier added the status/needs-signed-commits Some commits in the PR are signed with a valid SSH/GPG key label Nov 5, 2026
@aheritier

Copy link
Copy Markdown
Collaborator

👋 Some commits in this PR are not signed and verified by GitHub. Please sign your commits with a GPG or SSH key registered in your GitHub account, then force-push.\n\nCommits that are not verified: be3abb2\n\nSee GitHub's guide on signing commits for setup instructions. I've added status/needs-signed-commits; it will be removed automatically once every commit in this PR carries a valid GitHub-verified signature.

Build absolute traversal fixtures with filepath.VolumeName so
filepath.IsAbs is true on Windows and the absolute-path guard is
actually exercised.
@kevin9327
kevin9327 force-pushed the fix/path-dot-dot-names branch from be3abb2 to 65cfcdf Compare November 9, 2026 22:01
@kevin9327

Copy link
Copy Markdown
Contributor Author

Re-pushed the Windows absolute-path test follow-up as a GitHub-signed commit (volume-qualified root via filepath.VolumeName(t.TempDir()), with traversal components joined without cleaning). That should clear status/needs-signed-commits and exercise the absolute-path guard on Windows and Linux.

Happy to re-run anything else you want checked.

@aheritier aheritier removed the status/needs-signed-commits Some commits in the PR are signed with a valid SSH/GPG key label Nov 9, 2026
@aheritier
aheritier self-requested a review November 11, 2026 22:18
@aheritier
aheritier marked this pull request as ready for review November 11, 2026 22:23
Copilot AI balanced review requested due to automatic review settings November 11, 2026 22:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the path-validation update that permits legitimate dotted names.

No open issues 🎉

What changed in this PR

Updates shared path validation to accept names such as ..env and app..v2 without treating them as directory traversal, while preserving rejection of parent-directory traversal.

Changes:

  • Replaces broad two-dot checks with a separator-aware .. path-element check.
  • Adds regression tests for valid dotted names, traversal, and sibling-directory escapes, including paths without an allowed directory.
File Description
pkg/​path/​path.go Adds a parent-segment helper and applies it to the three traversal checks.
pkg/​path/​path_test.go Expands path-validation coverage and uses IsWithin for containment assertions.

Review effort: Balanced

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed both changed files and the environment/toolset callers against the shared-helper scope. No blocking findings.

Prior threads: 4473612526 addressed — the absolute-path regression cases use a volume-qualified root and preserve traversal components without cleaning them.

@aheritier
aheritier added this pull request to the merge queue Nov 12, 2026
Merged via the queue into docker:main with commit 140ff58 Nov 12, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Core agent runtime, session management kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants