Skip to content

admin: normalize request path in remote admin access-control check (defense-in-depth) - #7910

Merged
mholt merged 11 commits into
caddyserver:masterfrom
AmariahAK:master
Aug 25, 2026
Merged

mholt merged 11 commits into
caddyserver:masterfrom
AmariahAK:master

Conversation

@AmariahAK

Copy link
Copy Markdown
Contributor

(References #7909)

The remote admin API's access-control function adminPathAllowed compared
raw request paths against allowed paths without normalization. A request
like /pki/ca/prod/../../../../load would pass the prefix check for an
identity scoped to /pki/ca/prod, even though the path resolves to
/load.

While not exploitable today (the router redirects .. paths before
dispatch), the access-control layer depends on the router for safety.
This is defense-in-depth: normalize both the request path and allowed
path with path.Clean (purely lexical, no filesystem access) before
the boundary check. This preserves the existing segment-boundary
semantics (e.g., /pki must not match /pkisecret).

Added regression tests for: dot-dot traversal, encoded traversal,
sibling traversal, collapsed slashes, and trailing-slash requests.
All five fail without the fix and pass with it.

Assistance Disclosure

Atlarix agent (DeepSeek) generated the test cases and the two-line
path.Clean implementation. I authored the fix concept/approach,
reviewed every line, and verified correctness including running the
full test suite and confirming the tests fail without the fix and
pass with it.

@CLAassistant

CLAassistant commented Jul 27, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@iabdullah215

Copy link
Copy Markdown
Contributor

Hi @AmariahAK: Thanks for putting this together. I'm the author of the referenced issue #7909; I'd also prepared a fix for this locally (same path.Clean normalization on both the request path and allowed path, plus regression tests for dot-dot/sibling/collapsed-slash/trailing-slash cases) and was about to open a PR.
Happy to see it addressed either way, I'll review the diff here and leave any notes. A couple of things worth double-checking that I covered in my version: that the segment-boundary semantics are preserved (e.g. /pki must still not match /pkisecret), and behavior on an exact match with a trailing slash.

@AmariahAK

Copy link
Copy Markdown
Contributor Author

@iabdullah215 thanks for the review, ill wait for the full list of things you find so that i can fix them all in one clean sweep
But let me take a look into what youve asked for

@iabdullah215

iabdullah215 commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Took a proper look, the core path.Clean on reqPath is the right fix. Two things worth folding in:

  1. path.Clean("") returns ".", not "". Cleaning allowedPath unconditionally means an empty allowed path (Paths: [""]) becomes ".", which no longer satisfies the allowedPath == "" || allowedPath == "/" guard and falls through to deny. Upstream today treats an empty allowedPath as allow-all (HasPrefix(reqPath, "") is always true), so this silently changes that behavior. Safest to short-circuit the empty case before cleaning:
 reqPath = path.Clean(reqPath)
 if allowedPath == "" {
     return true
 }
 allowedPath = path.Clean(allowedPath)
 if allowedPath == "/" {
     return true
 }
 return reqPath == allowedPath || strings.HasPrefix(reqPath, allowedPath+"/")
  1. Minor: once allowedPath is cleaned it can never have a trailing slash, so the strings.HasSuffix(allowedPath, "/") branch below becomes dead code, the reqPath == allowedPath || HasPrefix(reqPath, allowedPath+"/") form covers it and reads more clearly.

(For what it's worth, the %2e%2e test is correct as-is, the request harness decodes it to .. before path.Clean runs.)

@AmariahAK

Copy link
Copy Markdown
Contributor Author

@iabdullah215 thanks for this, let me get to working on them

@AmariahAK

Copy link
Copy Markdown
Contributor Author

@iabdullah215 could you take a look, should be good to go now

@iabdullah215

Copy link
Copy Markdown
Contributor

Looks good, the empty allowedPath short-circuit before path.Clean and the simplified final check both address what I raised, and the test coverage is solid. LGTM from my side. Thanks for turning this around quickly.

@iabdullah215

Copy link
Copy Markdown
Contributor

Glad it worked out. Since the empty-path fix came from my local version, would you mind adding a co-author trailer to the commit? Co-authored-by: iabdullah215 <[email protected]>, that way we both show as contributors. No worries if you'd rather not.

…zation

path.Clean("") returns ".", so cleaning allowedPath unconditionally
silently broke the allow-all behavior when Paths: [""] is configured.
Short-circuit the empty case before cleaning to preserve that behavior.

Also remove the dead strings.HasSuffix(allowedPath, "/") branch —
after path.Clean the path never has a trailing slash, so the unified
reqPath == allowedPath || HasPrefix(reqPath, allowedPath+"/") form
covers exact match, subpath boundary, and trailing-slash requests.

Co-authored-by: atlarix-agent <[email protected]>
Co-authored-by: iabdullah215 <[email protected]>
@AmariahAK

AmariahAK commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@iabdullah215 done and thanks for the review
also your in the latest commit as well

steadytao

This comment was marked as outdated.

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See the comment from my previous review, wrong button 🥲

path.Clean(allowedPath) silently broadens misconfigured values like
// or /.. into /, which grants unintended access to all endpoints.
Reject non-canonical paths during provisioning in
replaceRemoteAdminServer so misconfigurations fail fast with a
clear error. The path.Clean in adminPathAllowed remains as
defense-in-depth but is now a safe no-op on validated inputs.

Co-authored-by: atlarix-agent <[email protected]>
@AmariahAK
AmariahAK requested a review from steadytao July 30, 2026 10:50
Comment thread admin.go Resolved
path.Clean(allowedPath) silently broadens misconfigured values like
// or /.. into /, which grants unintended access to all endpoints.
Reject non-canonical paths during provisioning in
replaceRemoteAdminServer so misconfigurations fail fast with a
clear error. The path.Clean in adminPathAllowed remains as
defense-in-depth but is now a safe no-op on validated inputs.

Co-authored-by: atlarix-agent <[email protected]>
@AmariahAK
AmariahAK requested a review from steadytao July 30, 2026 11:35
Comment thread admin.go Resolved
AmariahAK and others added 2 commits July 31, 2026 18:12
strings.TrimRight strips all trailing slashes, so a configured path
like /foo// passed validation (both slashes trimmed to /foo matching
path.Clean output) but was silently broadened to /foo at runtime.

Use strings.TrimSuffix instead, which removes exactly one trailing
slash — the only form the exemption was meant to allow (users write
/pki/ca/prod/ meaning the /pki/ca/prod scope).

Also update the // test case: with TrimSuffix, // is just / + one
trailing slash, which is valid under the exemption. Add a new test
for /foo// (double trailing slashes → wantErr: true).

Co-authored-by: atlarix-agent <[email protected]>
@AmariahAK
AmariahAK requested a review from steadytao July 31, 2026 15:24

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Updated trailing slash handling myself as the AI seems a bit incapable.

@steadytao

Copy link
Copy Markdown
Member

This now has a few subtle compatibility and authorisation-boundary considerations, particularly around /foo versus /foo/ and rejecting non-canonical configured paths during provisioning. @mholt @francislavoie @mohammed90, could one of you give the final path-matching semantics a second review before merge?

@mohammed90

Copy link
Copy Markdown
Member

@mholt @francislavoie @mohammed90, could one of you give the final path-matching semantics a second review before merge?

I feel like there was nuance around using canonical path versus not in admin API. I have to double check.

@AmariahAK

Copy link
Copy Markdown
Contributor Author

@mohammed90 @steadytao is there any more things youd like me to fix ?

@iabdullah215

iabdullah215 commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Hi @AmariahAK bumping this in case it got buried. It's a small defense-in-depth change to adminPathAllowed referencing #7909 that I opened. Happy to rework the approach if normalizing isn't the direction you'd prefer here. Is there anything blocking review?

@mholt mholt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is Matt's Codex agent, GPT-5.6 Sol, replying on his behalf.

I completed the requested second review of the path-matching semantics. Cleaning the decoded request path closes the traversal/boundary gap, while provisioning rejects configured paths whose cleaning could broaden their authority. The existing distinction between /foo and /foo/ is preserved, including exact-match behavior, and empty/root scopes retain their prior allow-all meaning. The focused tests cover the important traversal, duplicate-slash, segment-boundary, and trailing-slash cases. CI is green on this head.

@mholt
mholt merged commit d663793 into caddyserver:master Aug 25, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants