Skip to content

Commit

Permalink
Merge pull request #257 from go-kivik/fv3
Browse files Browse the repository at this point in the history
Fix database name validation
  • Loading branch information
flimzy authored Nov 10, 2020
2 parents ee2317b + e731c3f commit a499a4c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 19 deletions.
4 changes: 2 additions & 2 deletions chttp/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const (
func EncodeDocID(docID string) string {
for _, prefix := range []string{prefixDesign, prefixLocal} {
if strings.HasPrefix(docID, prefix) {
return prefix + url.QueryEscape(strings.TrimPrefix(docID, prefix))
return prefix + url.PathEscape(strings.TrimPrefix(docID, prefix))
}
}
return url.QueryEscape(docID)
return url.PathEscape(docID)
}
10 changes: 6 additions & 4 deletions chttp/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ func TestEncodeDocID(t *testing.T) {
{Input: "foo/bar", Expected: "foo%2Fbar"},
{Input: "_design/foo", Expected: "_design/foo"},
{Input: "_design/foo/bar", Expected: "_design/foo%2Fbar"},
{Input: "foo@bar.com", Expected: "foo%40bar.com"},
{Input: "foo+bar@baz.com", Expected: "foo%2Bbar%40baz.com"},
{Input: "Is this a valid ID?", Expected: "Is+this+a+valid+ID%3F"},
{Input: "foo@bar.com", Expected: "foo@bar.com"},
{Input: "foo+bar@baz.com", Expected: "foo+bar@baz.com"},
{Input: "Is this a valid ID?", Expected: "Is%20this%20a%20valid%20ID%3F"},
{Input: "nón-English-çharacters", Expected: "n%C3%B3n-English-%C3%A7haracters"},
{Input: "foo+bar & páces?!*,", Expected: "foo%2Bbar+%26+p%C3%A1ces%3F%21%2A%2C"},
{Input: "foo+bar & páces?!*,", Expected: "foo+bar%20&%20p%C3%A1ces%3F%21%2A%2C"},
{Input: "kivik$1234", Expected: "kivik$1234"},
{Input: "_users", Expected: "_users"},
}
for _, test := range tests {
result := EncodeDocID(test.Input)
Expand Down
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"net/http"
"net/url"
"strings"

"github.com/go-kivik/couchdb/v3/chttp"
Expand Down Expand Up @@ -41,15 +42,15 @@ func (c *client) CreateDB(ctx context.Context, dbName string, opts map[string]in
if err != nil {
return err
}
_, err = c.DoError(ctx, http.MethodPut, dbName, &chttp.Options{Query: query})
_, err = c.DoError(ctx, http.MethodPut, url.PathEscape(dbName), &chttp.Options{Query: query})
return err
}

func (c *client) DestroyDB(ctx context.Context, dbName string, _ map[string]interface{}) error {
if dbName == "" {
return missingArg("dbName")
}
_, err := c.DoError(ctx, http.MethodDelete, dbName, nil)
_, err := c.DoError(ctx, http.MethodDelete, url.PathEscape(dbName), nil)
return err
}

Expand Down
3 changes: 2 additions & 1 deletion couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"sync"

"github.com/go-kivik/couchdb/v3/chttp"
Expand Down Expand Up @@ -79,6 +80,6 @@ func (c *client) DB(_ context.Context, dbName string, _ map[string]interface{})
}
return &db{
client: c,
dbName: dbName,
dbName: url.PathEscape(dbName),
}, nil
}
2 changes: 1 addition & 1 deletion couchdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"gitlab.com/flimzy/testy"

kivik "github.com/go-kivik/kivik/v3"
"github.com/go-kivik/kivik/v3"
)

func TestNewClient(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (d *db) Query(ctx context.Context, ddoc, view string, opts map[string]inter

// Get fetches the requested document.
func (d *db) Get(ctx context.Context, docID string, options map[string]interface{}) (*driver.Document, error) {
resp, rev, err := d.get(ctx, http.MethodGet, docID, options)
resp, rev, err := d.get(ctx, http.MethodGet, chttp.EncodeDocID(docID), options)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.13

require (
github.com/go-kivik/kivik/v3 v3.2.0
github.com/go-kivik/kiviktest/v3 v3.0.2
github.com/go-kivik/kiviktest/v3 v3.0.3
github.com/gopherjs/gopherjs v0.0.0-20200209144316-f9cef593def5
github.com/pkg/errors v0.9.1
gitlab.com/flimzy/testy v0.0.3
golang.org/x/net v0.0.0-20200202094626-16171245cfb2
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
gitlab.com/flimzy/testy v0.3.2
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
)
18 changes: 14 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/flimzy/testy v0.1.17-0.20190521133342-95b386c3ece6/go.mod h1:3szguN8N
github.com/go-kivik/kivik/v3 v3.0.1/go.mod h1:7tmQDvkta/pcijpUjLMsQ9HJUELiKD5zm6jQ3Gb9cxE=
github.com/go-kivik/kivik/v3 v3.2.0 h1:YQ6j89DrQwNdMdpWioObEP+pYTSAtF58HomS5ROfKc0=
github.com/go-kivik/kivik/v3 v3.2.0/go.mod h1:chqVuHKAU9j2C7qL0cAH2FCO26oL+0B4aIBeCRMnLa8=
github.com/go-kivik/kiviktest/v3 v3.0.2 h1:+n90Nopbrzf/tqoXu4xqAMXk1+191vLrvakBdiz7r3Y=
github.com/go-kivik/kiviktest/v3 v3.0.2/go.mod h1:sqsz3M2sJxTxAUdOj+2SU21y4phcpYc0FJIn+hbf1D0=
github.com/go-kivik/kiviktest/v3 v3.0.3 h1:4zX1F1eLTbIvyKiylzdWDgPqofJWiuQzQhKwZwUXMB0=
github.com/go-kivik/kiviktest/v3 v3.0.3/go.mod h1:sqsz3M2sJxTxAUdOj+2SU21y4phcpYc0FJIn+hbf1D0=
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20200209144316-f9cef593def5 h1:On5cS+huOk7mqad9QjklHw+BMGKykSmu6QG32X+C77o=
github.com/gopherjs/gopherjs v0.0.0-20200209144316-f9cef593def5/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand All @@ -28,11 +28,21 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
gitlab.com/flimzy/testy v0.0.3 h1:UkCz4aDa52cUX6uwvuVrwlTFZC1AesU5W6grDUcVFlg=
gitlab.com/flimzy/testy v0.0.3/go.mod h1:YObF4cq711ubd/3U0ydRQQVz7Cnq/ChgJpVwNr/AJac=
gitlab.com/flimzy/testy v0.3.2 h1:4djQFwBJ1ayM681Zx7Y3+OKns/E9zAfGFsLc967jfdk=
gitlab.com/flimzy/testy v0.3.2/go.mod h1:YObF4cq711ubd/3U0ydRQQVz7Cnq/ChgJpVwNr/AJac=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit a499a4c

Please sign in to comment.