Unescape tags query when to list artifact (#11148)

The query string is encoded by UI, and we have to unescape the "=" in "q=tag=nil",
otherwise, the query doesn't work, and returns 400

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2020-03-20 10:07:34 +08:00 committed by GitHub
parent 2859cd8b69
commit 63cf1fce7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -16,7 +16,9 @@ package q
import (
"fmt"
"github.com/goharbor/harbor/src/common/utils/log"
ierror "github.com/goharbor/harbor/src/internal/error"
"net/url"
"strconv"
"strings"
"time"
@ -38,6 +40,12 @@ func Build(q string, pageNumber, pageSize int64) (*Query, error) {
if len(q) == 0 {
return query, nil
}
// try to escaped the 'q=tags%3Dnil' when to filter tags.
if unescapedQuery, err := url.QueryUnescape(q); err == nil {
q = unescapedQuery
} else {
log.Errorf("failed to unescape the query %s: %v", q, err)
}
params := strings.Split(q, ",")
for _, param := range params {
strs := strings.SplitN(param, "=", 2)

View File

@ -262,4 +262,11 @@ func TestBuild(t *testing.T) {
assert.Equal(t, int64(1), query.PageNumber)
assert.Equal(t, int64(10), query.PageSize)
assert.Equal(t, "v", query.Keywords["k"].(string))
q = `q=tags%3Dnil`
query, err = Build(q, 1, 10)
require.Nil(t, err)
assert.Equal(t, int64(1), query.PageNumber)
assert.Equal(t, int64(10), query.PageSize)
assert.Equal(t, "tags=nil", query.Keywords["q"].(string))
}

View File

@ -53,6 +53,18 @@ func (b *baseHandlerTestSuite) TestBuildQuery() {
b.Equal(int64(1), q.PageNumber)
b.Equal(int64(10), q.PageSize)
b.NotNil(q.Keywords)
var (
qs1 = "q=a%3Db"
pn1 int64 = 1
ps1 int64 = 10
)
q, err = b.base.BuildQuery(nil, &qs1, &pn1, &ps1)
b.Require().Nil(err)
b.Require().NotNil(q)
b.Equal(int64(1), q.PageNumber)
b.Equal(int64(10), q.PageSize)
b.Equal(q.Keywords["q"], "a=b")
}
func (b *baseHandlerTestSuite) TestLinks() {