Merge pull request #15985 from chlins/fix/replication-rule-enhancement

fix(replication): enhance the replication rule validation
This commit is contained in:
Chenyu Zhang 2021-11-12 11:12:41 +08:00 committed by GitHub
commit d260e632d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 0 deletions

View File

@ -17,6 +17,7 @@ package project
import ( import (
"context" "context"
"regexp" "regexp"
"strings"
"github.com/goharbor/harbor/src/common/utils" "github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
@ -105,6 +106,10 @@ func (m *manager) Get(ctx context.Context, idOrName interface{}) (*models.Projec
} }
name, ok := idOrName.(string) name, ok := idOrName.(string)
if ok { if ok {
// check white space in project name
if strings.Contains(name, " ") {
return nil, errors.BadRequestError(nil).WithMessage("invalid project name: '%s'", name)
}
return m.dao.GetByName(ctx, name) return m.dao.GetByName(ctx, name)
} }
return nil, errors.Errorf("invalid parameter: %v, should be ID(int64) or name(string)", idOrName) return nil, errors.Errorf("invalid parameter: %v, should be ID(int64) or name(string)", idOrName)

View File

@ -224,6 +224,8 @@ func (a *Adapter) ListProjects(filters []*model.Filter) ([]*Project, error) {
names, ok := util.IsSpecificPathComponent(projectPattern) names, ok := util.IsSpecificPathComponent(projectPattern)
if ok { if ok {
for _, name := range names { for _, name := range names {
// trim white space in project name
name = strings.TrimSpace(name)
project, err := a.Client.GetProject(name) project, err := a.Client.GetProject(name)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -251,3 +251,47 @@ func TestAbstractPublicMetadata(t *testing.T) {
require.Equal(t, 1, len(meta)) require.Equal(t, 1, len(meta))
require.Equal(t, "true", meta["public"].(string)) require.Equal(t, "true", meta["public"].(string))
} }
func TestListProjects(t *testing.T) {
server := test.NewServer(
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/api/projects",
Handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`[{"name": "p1"}, {"name": "p2"}]`))
},
},
)
defer server.Close()
registry := &model.Registry{
URL: server.URL,
}
adapter, err := New(registry)
require.Nil(t, err)
validPattern := "{p1,p2}/**"
// has " " in the p2 project name
invalidPattern := "{p1, p2}/**"
filters := []*model.Filter{
{
Type: "name",
Value: validPattern,
},
}
projects, err := adapter.ListProjects(filters)
require.Nil(t, err)
require.Len(t, projects, 2)
require.Equal(t, "p1", projects[0].Name)
require.Equal(t, "p2", projects[1].Name)
// invalid pattern, should also work with trim white space in project name.
filters[0].Value = invalidPattern
_, err = adapter.ListProjects(filters)
require.Nil(t, err)
require.Len(t, projects, 2)
require.Equal(t, "p1", projects[0].Name)
require.Equal(t, "p2", projects[1].Name)
}