Repository navigation
imagecache: validate image volume names before constructing mount points #3024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,67 @@ import ( | |
| "testing" | ||
| ) | ||
|
|
||
| // TestValidateImageVolumeName pins which names setupImageVolumes will build a | ||
| // mount point from. The table is the decision; the assertion under it is why | ||
| // the decision matters, that an accepted name is always a direct child of the | ||
| // bundle's volumes/ directory. | ||
| func TestValidateImageVolumeName(t *testing.T) { | ||
| const bundle = "/var/lib/ate/bundles/b1" | ||
| volumes := filepath.Join(bundle, "volumes") | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| valid bool | ||
| }{ | ||
| {name: "agent", valid: true}, | ||
| {name: "my-volume-1", valid: true}, | ||
| // Dots are ordinary characters inside a path element. | ||
| {name: "..foo", valid: true}, | ||
| {name: "foo..", valid: true}, | ||
|
|
||
| {name: "", valid: false}, | ||
| // "." resolves to volumes/ itself, which holds every other volume's | ||
| // mount point in the bundle. | ||
| {name: ".", valid: false}, | ||
| {name: "..", valid: false}, | ||
| {name: "../../../etc", valid: false}, | ||
| // A separator nests the mount somewhere below volumes/ instead; an | ||
| // absolute or dot-prefixed form is cleaned by filepath.Join into a | ||
| // different volume than the one named. Neither escapes the bundle, | ||
| // but neither is the directory the caller asked for either. | ||
| {name: "a/b", valid: false}, | ||
| {name: "./agent", valid: false}, | ||
| {name: "agent/", valid: false}, | ||
| {name: "/etc", valid: false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. Naming subtests by the volume name makes the |
||
| err := validateImageVolumeName(tc.name) | ||
| if got := err == nil; got != tc.valid { | ||
| t.Fatalf("validateImageVolumeName(%q) error = %v, want valid: %t", tc.name, err, tc.valid) | ||
| } | ||
| if !tc.valid { | ||
| return | ||
| } | ||
| if got := ImageVolumeMountPath(bundle, tc.name); filepath.Dir(got) != volumes { | ||
| t.Errorf("ImageVolumeMountPath(%q) = %q, want a direct child of %q", tc.name, got, volumes) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestImageVolumeMountPathEscapesOnTraversal records the behavior the check | ||
| // above exists to prevent: without it, the mount point leaves the bundle | ||
| // entirely, and setupImageVolumes would MkdirAll, unmount and mount over it. | ||
| func TestImageVolumeMountPathEscapesOnTraversal(t *testing.T) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd drop this test. It asserts that |
||
| const bundle = "/var/lib/ate/bundles/b1" | ||
| if got, want := ImageVolumeMountPath(bundle, "../../../etc"), "/var/lib/ate/etc"; got != want { | ||
| t.Fatalf("ImageVolumeMountPath(%q, %q) = %q, want %q", bundle, "../../../etc", got, want) | ||
| } | ||
| if got, want := ImageVolumeMountPath(bundle, "."), filepath.Join(bundle, "volumes"); got != want { | ||
| t.Fatalf("ImageVolumeMountPath(%q, %q) = %q, want %q", bundle, ".", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestOverlaySpecRoundTrip(t *testing.T) { | ||
| bundle := t.TempDir() | ||
| in := &OverlaySpec{ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two suggestions here.
Validating inside the mount loop means a bad second volume fails after the first one is already mounted. Checking every name in a loop of its own before any mount keeps the rejection free of side effects.
Also, a name that fails here means
rootfs-overlay.jsonwas changed after API validation, since the API already limits names to a DNS label. A WARN log with the bundle path and the name would leave a trace beyond the returned error. #2279 did the same for diffID mismatches.