Skip to content

policy: rootDirectory confinement uses unanchored prefix matching on uncleaned paths #117

Open

Description

@codeyevsky

Problem

Two related path-boundary gaps in packages/policy:

  1. rego.go:391 — the rootDirectory check uses a plain startswith string comparison between the candidate path and the configured root, with no boundary check after the prefix. A root of /home/user/project would incorrectly match a sibling path like /home/user/project-evil since the latter starts with the former as a raw string.
  2. extract_paths.go:231 — absolute paths containing dot-segments (e.g. /a/b/../../etc/passwd) are not canonicalized (no filepath.Clean or equivalent) before being used in comparisons, so a path can appear to be within a root while its actual resolved target is outside it.

Impact

Both gaps let a path that should be rejected as outside the configured root directory pass the confinement check: via a sibling-directory name that merely shares the same string prefix, or via unresolved dot-segments that walk the real path outside the root after the string comparison already passed.

Repro

  1. Configure rootDirectory to /home/user/project and evaluate a path /home/user/project-other/secret and observe the prefix check allows it.
  2. Configure the same root and evaluate /home/user/project/../../etc/passwd and observe it is treated as inside the root by the raw string/prefix logic despite resolving outside it.

Suggested fix

Clean every path with filepath.Clean (and resolve .. segments) before any root-containment comparison, in both extract_paths.go and rego.go. Change the root check from a bare string-prefix test to a proper path-boundary comparison, e.g. verify the cleaned candidate equals the root or starts with root + string(os.PathSeparator), never a same-string-prefix-without-separator match.

Activity

  1. sharmis-tp commented on Nov 18, 2026

    @sharmis-tp

    Unanchored prefix matching on uncleaned paths is a classic directory traversal bypass — /home/user/../etc/passwd matches a /home/user prefix but escapes the directory. I'll add path cleaning and anchored matching with a trailing separator check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions