Skip to content

Commit ab704e9

Browse files
authored
Merge pull request #3723 from apostasie/namespace-validate
Cleanup namespace validation
2 parents 3206b49 + b8f4d9c commit ab704e9

File tree

6 files changed

+17
-117
lines changed

6 files changed

+17
-117
lines changed

cmd/nerdctl/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/containerd/nerdctl/v2/pkg/errutil"
5050
"github.com/containerd/nerdctl/v2/pkg/logging"
5151
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
52+
"github.com/containerd/nerdctl/v2/pkg/store"
5253
"github.com/containerd/nerdctl/v2/pkg/version"
5354
)
5455

@@ -239,6 +240,16 @@ Config file ($NERDCTL_TOML): %s
239240
return fmt.Errorf("invalid cgroup-manager %q (supported values: \"systemd\", \"cgroupfs\", \"none\")", cgroupManager)
240241
}
241242
}
243+
244+
// Since we store containers' stateful information on the filesystem per namespace, we need namespaces to be
245+
// valid, safe path segments. This is enforced by store.ValidatePathComponent.
246+
// Note that the container runtime will further enforce additional restrictions on namespace names
247+
// (containerd treats namespaces as valid identifiers - eg: alphanumericals + dash, starting with a letter)
248+
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#path-segment-names for
249+
// considerations about path segments identifiers.
250+
if err = store.ValidatePathComponent(globalOptions.Namespace); err != nil {
251+
return err
252+
}
242253
if appNeedsRootlessParentMain(cmd, args) {
243254
// reexec /proc/self/exe with `nsenter` into RootlessKit namespaces
244255
return rootlessutil.ParentMain(globalOptions.HostGatewayIP)

pkg/containerutil/containerutil.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
"github.com/containerd/nerdctl/v2/pkg/ipcutil"
4747
"github.com/containerd/nerdctl/v2/pkg/labels"
4848
"github.com/containerd/nerdctl/v2/pkg/labels/k8slabels"
49-
"github.com/containerd/nerdctl/v2/pkg/nsutil"
5049
"github.com/containerd/nerdctl/v2/pkg/portutil"
5150
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
5251
"github.com/containerd/nerdctl/v2/pkg/signalutil"
@@ -529,9 +528,6 @@ func Unpause(ctx context.Context, client *containerd.Client, id string) error {
529528

530529
// ContainerStateDirPath returns the path to the Nerdctl-managed state directory for the container with the given ID.
531530
func ContainerStateDirPath(ns, dataStore, id string) (string, error) {
532-
if err := nsutil.ValidateNamespaceName(ns); err != nil {
533-
return "", fmt.Errorf("invalid namespace name %q for determining state dir of container %q: %s", ns, id, err)
534-
}
535531
return filepath.Join(dataStore, "containers", ns, id), nil
536532
}
537533

pkg/nsutil/nsutil.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

pkg/nsutil/nsutil_test.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

pkg/store/filestore.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (vs *fileStore) List(key ...string) ([]string, error) {
204204

205205
// Unlike Get, Set and Delete, List can have zero length key
206206
for _, k := range key {
207-
if err := validatePathComponent(k); err != nil {
207+
if err := ValidatePathComponent(k); err != nil {
208208
return nil, err
209209
}
210210
}
@@ -333,8 +333,8 @@ func (vs *fileStore) GroupSize(key ...string) (int64, error) {
333333
return size, nil
334334
}
335335

336-
// validatePathComponent will enforce os specific filename restrictions on a single path component
337-
func validatePathComponent(pathComponent string) error {
336+
// ValidatePathComponent will enforce os specific filename restrictions on a single path component
337+
func ValidatePathComponent(pathComponent string) error {
338338
// https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
339339
if len(pathComponent) > 255 {
340340
return errors.Join(ErrInvalidArgument, errors.New("identifiers must be stricly shorter than 256 characters"))
@@ -358,7 +358,7 @@ func validateAllPathComponents(pathComponent ...string) error {
358358
}
359359

360360
for _, key := range pathComponent {
361-
if err := validatePathComponent(key); err != nil {
361+
if err := ValidatePathComponent(key); err != nil {
362362
return err
363363
}
364364
}

pkg/store/filestore_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ func TestFileStoreFilesystemRestrictions(t *testing.T) {
267267
}
268268

269269
for _, v := range invalid {
270-
err := validatePathComponent(v)
270+
err := ValidatePathComponent(v)
271271
assert.ErrorIs(t, err, ErrInvalidArgument, v)
272272
}
273273

274274
for _, v := range valid {
275-
err := validatePathComponent(v)
275+
err := ValidatePathComponent(v)
276276
assert.NilError(t, err, v)
277277
}
278278

0 commit comments

Comments
 (0)