Skip to content
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

Cleanup namespace validation #3723

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

apostasie
Copy link
Contributor

@apostasie apostasie commented Dec 3, 2024

Currently, namespace name is only validated in certain circumstances (eg: if we are going to access a container statedir). This may lead to inconsistencies, where we would allow forbidden namespaces in certain scenarios but properly restrict them in other scenarios.

Also, the current set of rules for validation are inadequate, and fail to cover a range of conditions that will fail on the filesystem.

So, suggesting:

  • we always validate namespace, once, during argument parsing
  • we use the known good validator we have in place already in the store (ValidatePathComponent) instead of the current validator

This is introducing changes. Specifically, it will restrict previously allowed namespaces:

  • namespaces that are longer than 255 characters
  • namespaces containing protected OS/filesystem patterns (specifically: on windows, con[0-9], etc)

And on the other hand, relax restrictions on certain patterns that were previously forbidden for no apparent reason (tilde, etc).

Most importantly though, note that containerd will anyhow identifier-validate the namespace name whenever it touches it... with the much more restrictive^[A-Za-z0-9]+(?:[._-](?:[A-Za-z0-9]+))*$ - so, this is merely about cleaning things up on our side.

Let me know what you think.

@apostasie apostasie marked this pull request as ready for review December 3, 2024 05:19
@AkihiroSuda AkihiroSuda added this to the v2.0.2 milestone Dec 3, 2024
return fmt.Errorf("namespace name cannot contain any special characters (%q): %s", pathSeparators, nsName)
}

specialAliases := []string{".", "..", "~"}
Copy link
Member

Choose a reason for hiding this comment

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

This check should be retained

Copy link
Contributor Author

Choose a reason for hiding this comment

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

. and .. are retained (

disallowedKeywords = regexp.MustCompile(`^([.]|[.][.])$`)
).

Tilde is no longer, as it is not a filesystem restriction / alias. However, as mentioned, containerd will enforce ^[A-Za-z0-9]+(?:[._-](?:[A-Za-z0-9]+))*$ on namespace creation, so, tilde is still out.

@AkihiroSuda AkihiroSuda removed this from the v2.0.2 milestone Dec 3, 2024
@AkihiroSuda AkihiroSuda requested a review from a team December 3, 2024 06:25
@AkihiroSuda AkihiroSuda added this to the v2.0.2 milestone Dec 3, 2024
pkg/nsutil/nsutil_test.go Outdated Show resolved Hide resolved
Copy link
Member

@djdongjin djdongjin left a comment

Choose a reason for hiding this comment

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

LGTM thanks

@@ -239,6 +240,9 @@ Config file ($NERDCTL_TOML): %s
return fmt.Errorf("invalid cgroup-manager %q (supported values: \"systemd\", \"cgroupfs\", \"none\")", cgroupManager)
}
}
if err = store.ValidatePathComponent(globalOptions.Namespace); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

nit:

can we add a comment stating something like namespace should be a single valid path component.

https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#path-segment-names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Let me do that real quick.

@apostasie
Copy link
Contributor Author

Thanks @djdongjin

Latest push addresses your comment, and has been rebased to account for newly added unit tests.

@apostasie
Copy link
Contributor Author

TestComposeUp failure seems unrelated.

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda merged commit ab704e9 into containerd:main Dec 5, 2024
30 checks passed
apostasie pushed a commit to apostasie/nerdctl that referenced this pull request Dec 6, 2024
apostasie pushed a commit to apostasie/nerdctl that referenced this pull request Dec 6, 2024
apostasie pushed a commit to apostasie/nerdctl that referenced this pull request Dec 6, 2024
apostasie pushed a commit to apostasie/nerdctl that referenced this pull request Dec 6, 2024
apostasie pushed a commit to apostasie/nerdctl that referenced this pull request Dec 6, 2024
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.

3 participants