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

spec: fix regexp #426

Merged
merged 1 commit into from
Jun 23, 2023
Merged

Conversation

rogpeppe
Copy link
Contributor

As pointed out by @andaaron in #425, the regular expression proposed there was bogus. It was actually wrong in two different ways. Mea culpa, my apologies.

This fixes it. I wrote a few test cases to check: https://go.dev/play/p/4_iYH4Hao1-

package main

import (
	"regexp"
	"testing"
)

func TestRepoPattern(t *testing.T) {
	r := regexp.MustCompile(`^[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*$`)
	for _, test := range []struct {
		s    string
		want bool
	}{
		{"foo", true},
		{"foo/bar", true},
		{"foo/bar__baz", true},
		{"foo/bar___baz", false},
		{"/foo", false},
		{"foo//bar", false},
		{"foo-------b__x.com", true},
		{"foo-------b__x.com/p----x", true},
		{"foo-", false},
		{"-foo", false},
		{"foo/-bar", false},
		{"foo/bar-", false},
		{"foo-bar", true},
		{"foo----_bar", false},
		{"foo----bar_/x", false},
	} {
		if got, want := r.MatchString(test.s), test.want; got != want {
			t.Errorf("mismatch on %q; got %v want %v", test.s, got, want)
		}
	}
}

Ideally something like the above would be committed for CI testing but perhaps that's a stage too far.

As pointed out by @andaaron in opencontainers#425, the regular expression
proposed there was bogus. It was actually wrong in two different ways.
Mea culpa, my apologies.

This fixes it. I wrote a few test cases to check: https://go.dev/play/p/4_iYH4Hao1-

```go
package main

import (
	"regexp"
	"testing"
)

func TestRepoPattern(t *testing.T) {
	r := regexp.MustCompile(`^[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*$`)
	for _, test := range []struct {
		s    string
		want bool
	}{
		{"foo", true},
		{"foo/bar", true},
		{"foo/bar__baz", true},
		{"foo/bar___baz", false},
		{"/foo", false},
		{"foo//bar", false},
		{"foo-------b__x.com", true},
		{"foo-------b__x.com/p----x", true},
		{"foo-", false},
		{"-foo", false},
		{"foo/-bar", false},
		{"foo/bar-", false},
		{"foo-bar", true},
		{"foo----_bar", false},
		{"foo----bar_/x", false},
	} {
		if got, want := r.MatchString(test.s), test.want; got != want {
			t.Errorf("mismatch on %q; got %v want %v", test.s, got, want)
		}
	}
}
```

Ideally something like the above would be committed for CI testing
but perhaps that's a stage too far.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

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

I have no power to approve, but I 100% agree this should be included in code with tests. The regexp doesn't even have to be exported, so long as it's kept in sync with spec.md.

Tests let us catch bugs and regressions, and act as runnable documentation to showcase unusual valid and invalid cases.

@sudo-bmitch
Copy link
Contributor

I'm not sure when it happened, but it looks like we've relaxed the restrictions from the Docker spec:

A repository name is broken up into path components. A component of a repository name must be at least one lowercase, alpha-numeric characters, optionally separated by periods, dashes or underscores. More strictly, it must match the regular expression [a-z0-9]+(?:[._-][a-z0-9]+)*.

https://docs.docker.com/registry/spec/api/

@jdolitsky jdolitsky merged commit efe2de0 into opencontainers:main Jun 23, 2023
4 checks passed
@jdolitsky jdolitsky mentioned this pull request Jun 27, 2023
8 tasks
@jdolitsky jdolitsky mentioned this pull request Jul 6, 2023
8 tasks
sudo-bmitch pushed a commit to sudo-bmitch/distribution-spec that referenced this pull request Aug 18, 2023
As pointed out by @andaaron in opencontainers#425, the regular expression
proposed there was bogus. It was actually wrong in two different ways.
Mea culpa, my apologies.

This fixes it. I wrote a few test cases to check: https://go.dev/play/p/4_iYH4Hao1-

```go
package main

import (
	"regexp"
	"testing"
)

func TestRepoPattern(t *testing.T) {
	r := regexp.MustCompile(`^[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*$`)
	for _, test := range []struct {
		s    string
		want bool
	}{
		{"foo", true},
		{"foo/bar", true},
		{"foo/bar__baz", true},
		{"foo/bar___baz", false},
		{"/foo", false},
		{"foo//bar", false},
		{"foo-------b__x.com", true},
		{"foo-------b__x.com/p----x", true},
		{"foo-", false},
		{"-foo", false},
		{"foo/-bar", false},
		{"foo/bar-", false},
		{"foo-bar", true},
		{"foo----_bar", false},
		{"foo----bar_/x", false},
	} {
		if got, want := r.MatchString(test.s), test.want; got != want {
			t.Errorf("mismatch on %q; got %v want %v", test.s, got, want)
		}
	}
}
```

Ideally something like the above would be committed for CI testing
but perhaps that's a stage too far.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
@sudo-bmitch sudo-bmitch mentioned this pull request Feb 1, 2024
8 tasks
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.

5 participants