Repository navigation
fix(path): only treat a ".." path element as traversal - #4803
Conversation
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>
aheritier
left a comment
There was a problem hiding this comment.
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.
| {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}, |
There was a problem hiding this comment.
[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.
|
Thanks, you're right: |
|
👋 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: |
Build absolute traversal fixtures with filepath.VolumeName so filepath.IsAbs is true on Windows and the absolute-path guard is actually exercised.
be3abb2 to
65cfcdf
Compare
|
Re-pushed the Windows absolute-path test follow-up as a GitHub-signed commit (volume-qualified root via Happy to re-run anything else you want checked. |
There was a problem hiding this comment.
🟢 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
left a comment
There was a problem hiding this comment.
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.
What
path.ValidatePathInDirectoryrejected valid paths whose names only start with or contain two dots:..envor..config/agent.yamlrelative to the allowed directory ->path outside allowed directory, because the check wasstrings.HasPrefix(relPath, "..").path contains directory traversal sequences(strings.HasPrefix(cleanPath, ".."))./opt/app..v2/agent.yamlwith no allowed directory -> rejected bystrings.Contains(path, "..").It is used for
env_fileentries (pkg/environment/env_files.go) and toolset paths (pkg/tools/toolsetpath), so such files could not be referenced.Fix
A small
hasParentSegmenthelper 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).IsWithinin the same package already applies the same element-based rule.Tests
TestValidatePathInDirectory: new cases for..env,..config/agent.yaml(allowed),..and a<tmp>-othersibling (rejected). The success check now usesIsWithininstead of the same prefix test.TestValidatePathInDirectoryWithoutAllowedDirfor theallowedDir == ""branches (relative and absolute, allowed and traversal).go test ./pkg/path/ ./pkg/environment/passes.gofmtandgo vet ./pkg/path/are clean (golangci-lint not run locally).pkg/environment/env_files.gohas its ownstrings.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.