Skip to content

reference: reject .. path components that rewrite the host - #13781

Merged
samuelkarp merged 1 commit into
containerd:mainfrom
abdul-khaliq-khalid:reference-reject-dotdot-path
Oct 1, 2026
Merged

samuelkarp merged 1 commit into
containerd:mainfrom
abdul-khaliq-khalid:reference-reject-dotdot-path

Conversation

@abdul-khaliq-khalid

Copy link
Copy Markdown
Contributor

reference.Parse builds the locator with path.Join(u.Host, u.Path), which cleans the result and lets a .. element climb past the host. example.com/../other/image parses to a locator of other/image, so Hostname() returns other and the docker resolver, the auth scope, and the distribution-source label all follow the reference to a different registry than the one that was written (url.Parse decodes %2e%2e, so the percent-encoded form behaves the same). The CRI pull path runs references through distribution/reference first, which rejects these, but the transfer service and client.Pull hand the reference straight to the resolver.

Reject any reference whose path carries a .. component. Keeping the check in Parse means every consumer of a Spec stays on the host that was actually written rather than each re-validating, and since valid references never contain .. this only turns previously-misrouted input into an error.

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.

Pull request overview

This PR hardens pkg/reference parsing by rejecting image references whose path contains a .. component, preventing path.Join(u.Host, u.Path) from cleaning the path in a way that can “climb past” the written host and misroute pulls/auth scope/labels to a different registry.

Changes:

  • Add validation in reference.Parse to reject any reference path containing a .. segment (including percent-encoded forms that decode to ..).
  • Add unit tests covering host-rewrite and in-path .. cases to ensure the parser returns ErrInvalid.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/reference/reference.go Rejects .. path components before path.Join can rewrite the effective host/locator.
pkg/reference/reference_test.go Adds test cases ensuring .. (including %2e%2e) causes ErrInvalid.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@samuelkarp

Copy link
Copy Markdown
Member

Please address the linter failure. Thanks!

@abdul-khaliq-khalid
abdul-khaliq-khalid force-pushed the reference-reject-dotdot-path branch from 97ae99e to 808bdc5 Compare July 16, 2026 05:57
Copilot AI review requested due to automatic review settings July 16, 2026 05:57
@abdul-khaliq-khalid

Copy link
Copy Markdown
Contributor Author

Fixed - the modernize linter flagged the manual loop, so I swapped it for slices.Contains. Lint and go test ./pkg/reference/... are green now.

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@samuelkarp samuelkarp 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.

It looks like you've used an LLM to assist in creating this PR. Would you mind disclosing that? (We haven't quite merged containerd/.project#166 yet but that suggests how we'd like to have that disclosure.)

Comment thread pkg/reference/reference.go Outdated
Comment on lines +120 to +121
// references never contain one, so reject it instead of silently
// rewriting the host.

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.

"instead of silently rewriting the host" is historical framing that isn't necessary to retain in a code comment. LLMs love to do this, but it's not actually a good idea. The rest of the comment is fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, dropped that clause. The comment now just explains what a .. would do to the join.

Copilot AI review requested due to automatic review settings July 20, 2026 06:36
@abdul-khaliq-khalid
abdul-khaliq-khalid force-pushed the reference-reject-dotdot-path branch from 808bdc5 to c2aeb6b Compare July 20, 2026 06:36
@abdul-khaliq-khalid

abdul-khaliq-khalid commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor Author

This issue was identified using BugQore, our proprietary Multi-Agent Remediation Engine, which leverages repository-scale semantic program analysis, contextual dependency modeling, execution-path reasoning, and multi-stage validation to identify and remediate high-confidence security findings.

BugQore is available as a commercial platform. If it aligns with your engineering or application security requirements, we'd be pleased to discuss deployment and licensing with your team.
[email protected]

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@abdul-khaliq-khalid

Copy link
Copy Markdown
Contributor Author

@samuelkarp fair to ask. I do use an LLM a little, mostly for research and sanity-checking, nothing heavy, and I worked through the reproducer and this fix myself before opening it. Happy to add a disclosure line in whatever format you land on once that policy merges. On the change itself, the check sits in Parse so every consumer of a Spec stays on the host that was actually written, and valid references never carry a .. so it only turns previously-misrouted input into an error.

@samuelkarp

Copy link
Copy Markdown
Member

Would you mind rebasing (so CI runs) and adding the Assisted-by trailer recommended in our contributing guide?

@samuelkarp
samuelkarp force-pushed the reference-reject-dotdot-path branch from c2aeb6b to 731c62b Compare September 29, 2026 22:07
Copilot AI balanced review requested due to automatic review settings September 29, 2026 22:07
@samuelkarp

Copy link
Copy Markdown
Member

samuelkarp force-pushed the reference-reject-dotdot-path branch from c2aeb6b to 731c62b

Rebased to kick CI

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.

Copilot review overview

🟢 Approval recommended

The validation closes the described parsing issue with focused test coverage.

Review effort: Balanced
Findings: None

Comment thread pkg/reference/reference.go Outdated
Comment on lines +117 to +120
// A ".." element in the path would let the path.Join below climb past
// the host and repoint the reference at a different registry, e.g.
// "example.com/../other/image" resolves to "other/image". Valid
// references never contain one.

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.

The comment is a bit superfluous - not sure if it's needed (and the test-case covers it?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Trimmed it to a one-liner. The test cases cover the behavior, so I just kept a short note on why the guard is there.

// the host and repoint the reference at a different registry, e.g.
// "example.com/../other/image" resolves to "other/image". Valid
// references never contain one.
if slices.Contains(strings.Split(u.Path, "/"), "..") {

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.

FWIW, there's other elements that may not be "valid" if we're trying to match the distribution/reference format here; slightly wondering if this package should use that as implementation under the hood (although distribution/reference is a bit over-engineered IMO);

// Grammar
//
// reference := name [ ":" tag ] [ "@" digest ]
// name := [domain '/'] remote-name
// domain := host [':' port-number]
// host := domain-name | IPv4address | \[ IPv6address \] ; rfc3986 appendix-A
// domain-name := domain-component ['.' domain-component]*
// domain-component := /([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])/
// port-number := /[0-9]+/
// path-component := alpha-numeric [separator alpha-numeric]*
// path (or "remote-name") := path-component ['/' path-component]*
// alpha-numeric := /[a-z0-9]+/
// separator := /[_.]|__|[-]*/
//
// tag := /[\w][\w.-]{0,127}/
//
// digest := digest-algorithm ":" digest-hex
// digest-algorithm := digest-algorithm-component [ digest-algorithm-separator digest-algorithm-component ]*
// digest-algorithm-separator := /[+.-_]/
// digest-algorithm-component := /[A-Za-z][A-Za-z0-9]*/
// digest-hex := /[0-9a-fA-F]{32,}/ ; At least 128 bit digest value
//
// identifier := /[a-f0-9]{64}/
package reference

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, distribution/reference is stricter and catches more than just ... I kept this one narrow to the host-rewrite hole so it stays an easy backport; moving pkg/reference onto distribution/reference under the hood is a larger behavioral change that I think belongs in its own PR. Happy to file an issue to track that if it's worth pursuing.

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.

Yes, agreed on both. It was mostly an observation, and something we may want to look at.

I have helped maintain the distribution/reference package to clean it up (and address some bugs), but am (as mentioned) not a fan of the implementation. The module is useful because it documents / defines the "spec" (warts and all, including some odd historic choices), and for that reason I prefer using it, but it could definitely use a v2 with a simplified API.

Assisted-by: Claude
Signed-off-by: syed abdul khaliq <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 1, 2026 06:45
@abdul-khaliq-khalid
abdul-khaliq-khalid force-pushed the reference-reject-dotdot-path branch from 731c62b to c20414b Compare October 1, 2026 06:45
@abdul-khaliq-khalid

Copy link
Copy Markdown
Contributor Author

Added the Assisted-by trailer to the commit, and thanks for rebasing it earlier to kick CI. Also trimmed the parse comment per @thaJeztah's note below.

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.

Copilot review overview

🟢 Approval recommended

The validation directly prevents host rewriting and is covered by focused tests.

Review effort: Balanced
Findings: None

@thaJeztah thaJeztah 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, thanks!

@samuelkarp
samuelkarp added this pull request to the merge queue Oct 1, 2026
Merged via the queue into containerd:main with commit 7b48098 Oct 1, 2026
55 checks passed
@samuelkarp

Copy link
Copy Markdown
Member

/cherry-pick release/2.4
/cherry-pick release/2.3
/cherry-pick release/2.2
/cherry-pick release/2.0

@samuelkarp samuelkarp added cherry-pick/2.0.x Change to be cherry picked to release/2.0 branch cherry-pick/2.2.x Change to be cherry picked to release/2.2 branch cherry-pick/2.3.x Change to be cherry picked to release/2.3 cherry-pick/2.4.x Change to be cherry picked to release/2.4 labels Oct 2, 2026
@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@samuelkarp: #13781 failed to apply on top of branch "release/2.0":

Applying: reference: reject `..` path components that rewrite the host
Using index info to reconstruct a base tree...
M	pkg/reference/reference.go
Falling back to patching base and 3-way merge...
Auto-merging pkg/reference/reference.go
CONFLICT (content): Merge conflict in pkg/reference/reference.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 reference: reject `..` path components that rewrite the host

Details

In response to this:

/cherry-pick release/2.4
/cherry-pick release/2.3
/cherry-pick release/2.2
/cherry-pick release/2.0

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@samuelkarp: new pull request created: #14280

Details

In response to this:

/cherry-pick release/2.4
/cherry-pick release/2.3
/cherry-pick release/2.2
/cherry-pick release/2.0

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@samuelkarp: new pull request created: #14281

Details

In response to this:

/cherry-pick release/2.4
/cherry-pick release/2.3
/cherry-pick release/2.2
/cherry-pick release/2.0

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@samuelkarp: new pull request created: #14282

Details

In response to this:

/cherry-pick release/2.4
/cherry-pick release/2.3
/cherry-pick release/2.2
/cherry-pick release/2.0

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@samuelkarp samuelkarp added cherry-picked/2.2.x PR commits are cherry-picked into release/2.2 branch cherry-picked/2.3.x PR commits are cherry picked into release/2.3 branch cherry-picked/2.4.x PR commits are cherry picked into release/2.4 branch and removed cherry-pick/2.0.x Change to be cherry picked to release/2.0 branch cherry-pick/2.2.x Change to be cherry picked to release/2.2 branch cherry-pick/2.3.x Change to be cherry picked to release/2.3 cherry-pick/2.4.x Change to be cherry picked to release/2.4 labels Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-picked/2.2.x PR commits are cherry-picked into release/2.2 branch cherry-picked/2.3.x PR commits are cherry picked into release/2.3 branch cherry-picked/2.4.x PR commits are cherry picked into release/2.4 branch size/S

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants