Skip to content

Commit

Permalink
Double encode name parameters in requests to CAPI
Browse files Browse the repository at this point in the history
The v3 API requires that the comma character is double encoded when used
in a single query parameter. Previously, the CLI would only encode the
query request once it was fully formed. This was sufficient for v2, but
with the v3 API this would cause issues when using a resource that had
commas in its name.

This commit now encodes query parameters with the
`names` filter before encoding the entire query. This ensures that
resource names that may contain commas are properly double encoded.

[Github issue #1938](#1938)
[#172891571](https://www.pivotaltracker.com/story/show/172891571)

Authored-by: Belinda Liu <bliu@pivotal.io>
  • Loading branch information
belinda-liu committed Jun 19, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent e5d7012 commit 45b2481
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/cloudcontroller/ccv3/isolation_segment_test.go
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ var _ = Describe("Isolation Segments", func() {
JustBeforeEach(func() {
queries = []Query{
{Key: OrganizationGUIDFilter, Values: []string{"some-org-guid"}},
{Key: NameFilter, Values: []string{"iso1,iso2,iso3"}},
{Key: NameFilter, Values: []string{"iso1", "iso2", "iso3"}},
}
segments, warnings, executeErr = client.GetIsolationSegments(queries...)
})
8 changes: 8 additions & 0 deletions api/cloudcontroller/ccv3/query.go
Original file line number Diff line number Diff line change
@@ -116,6 +116,14 @@ type Query struct {
func FormatQueryParameters(queries []Query) url.Values {
params := url.Values{}
for _, query := range queries {
if query.Key == NameFilter {
encodedParamValues := []string{}
for _, valString := range query.Values {
encodedParamValues = append(encodedParamValues, url.QueryEscape(valString))
}
query.Values = encodedParamValues
}

params.Add(string(query.Key), strings.Join(query.Values, ","))
}

52 changes: 52 additions & 0 deletions api/cloudcontroller/ccv3/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ccv3_test

import (
"net/url"

. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Query Helpers", func() {
Describe("FormatQueryParameters", func() {
var (
inputQueries []Query
outputParameters url.Values
)

BeforeEach(func() {
inputQueries = []Query{
{
Key: SpaceGUIDFilter,
Values: []string{"space-guid1", "space-guid2"},
},
}
outputParameters = FormatQueryParameters(inputQueries)
})

It("encodes the param values and reformats the query", func() {
Expect(outputParameters).To(Equal(url.Values{
"space_guids": []string{"space-guid1,space-guid2"},
}))
})

When("the name filter is used", func() {
BeforeEach(func() {
inputQueries = []Query{
{
Key: NameFilter,
Values: []string{"name1", "name,2"},
},
}
outputParameters = FormatQueryParameters(inputQueries)
})

It("encodes the param values before formatting", func() {
Expect(outputParameters).To(Equal(url.Values{
"names": []string{"name1,name%2C2"},
}))
})
})
})
})

0 comments on commit 45b2481

Please sign in to comment.