Repository navigation
reference: reject .. path components that rewrite the host - #13781
Conversation
There was a problem hiding this comment.
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.Parseto 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 returnsErrInvalid.
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.
|
Please address the linter failure. Thanks! |
97ae99e to
808bdc5
Compare
|
Fixed - the modernize linter flagged the manual loop, so I swapped it for slices.Contains. Lint and go test ./pkg/reference/... are green now. |
samuelkarp
left a comment
There was a problem hiding this comment.
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.)
| // references never contain one, so reject it instead of silently | ||
| // rewriting the host. |
There was a problem hiding this comment.
"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.
There was a problem hiding this comment.
Good point, dropped that clause. The comment now just explains what a .. would do to the join.
808bdc5 to
c2aeb6b
Compare
|
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. |
|
@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 |
|
Would you mind rebasing (so CI runs) and adding the Assisted-by trailer recommended in our contributing guide? |
c2aeb6b to
731c62b
Compare
| // 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. |
There was a problem hiding this comment.
The comment is a bit superfluous - not sure if it's needed (and the test-case covers it?)
There was a problem hiding this comment.
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, "/"), "..") { |
There was a problem hiding this comment.
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);
containerd/vendor/github.com/distribution/reference/reference.go
Lines 4 to 27 in cf413d5
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
731c62b to
c20414b
Compare
|
Added the |
|
/cherry-pick release/2.4 |
|
@samuelkarp: #13781 failed to apply on top of branch "release/2.0": DetailsIn response to this:
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: new pull request created: #14280 DetailsIn response to this:
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: new pull request created: #14281 DetailsIn response to this:
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: new pull request created: #14282 DetailsIn response to this:
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. |
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/imageparses to a locator ofother/image, so Hostname() returnsotherand 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.