Skip to content

Commit 8ed171c

Browse files
committed
Escaping document, collection & document names/keys
1 parent c7874c2 commit 8ed171c

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

client_databases_impl.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ package driver
2424

2525
import (
2626
"context"
27+
"net/url"
2728
"path"
2829
)
2930

3031
// Database opens a connection to an existing database.
3132
// If no database with given name exists, an NotFoundError is returned.
3233
func (c *client) Database(ctx context.Context, name string) (Database, error) {
33-
req, err := c.conn.NewRequest("GET", path.Join("_db", name, "_api/database/current"))
34+
escapedName := url.QueryEscape(name)
35+
req, err := c.conn.NewRequest("GET", path.Join("_db", escapedName, "_api/database/current"))
3436
if err != nil {
3537
return nil, WithStack(err)
3638
}
@@ -50,7 +52,8 @@ func (c *client) Database(ctx context.Context, name string) (Database, error) {
5052

5153
// DatabaseExists returns true if a database with given name exists.
5254
func (c *client) DatabaseExists(ctx context.Context, name string) (bool, error) {
53-
req, err := c.conn.NewRequest("GET", path.Join("_db", name, "_api/database/current"))
55+
escapedName := url.QueryEscape(name)
56+
req, err := c.conn.NewRequest("GET", path.Join("_db", escapedName, "_api/database/current"))
5457
if err != nil {
5558
return false, WithStack(err)
5659
}

collection_document_impl.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package driver
2525
import (
2626
"context"
2727
"fmt"
28+
"net/url"
2829
"path"
2930
"reflect"
3031
)
@@ -36,7 +37,8 @@ func (c *collection) ReadDocument(ctx context.Context, key string, result interf
3637
if err := validateKey(key); err != nil {
3738
return DocumentMeta{}, WithStack(err)
3839
}
39-
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("document"), key))
40+
escapedKey := url.QueryEscape(key)
41+
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("document"), escapedKey))
4042
if err != nil {
4143
return DocumentMeta{}, WithStack(err)
4244
}
@@ -164,7 +166,8 @@ func (c *collection) UpdateDocument(ctx context.Context, key string, update inte
164166
if update == nil {
165167
return DocumentMeta{}, WithStack(InvalidArgumentError{Message: "update nil"})
166168
}
167-
req, err := c.conn.NewRequest("PATCH", path.Join(c.relPath("document"), key))
169+
escapedKey := url.QueryEscape(key)
170+
req, err := c.conn.NewRequest("PATCH", path.Join(c.relPath("document"), escapedKey))
168171
if err != nil {
169172
return DocumentMeta{}, WithStack(err)
170173
}
@@ -272,7 +275,8 @@ func (c *collection) ReplaceDocument(ctx context.Context, key string, document i
272275
if document == nil {
273276
return DocumentMeta{}, WithStack(InvalidArgumentError{Message: "document nil"})
274277
}
275-
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("document"), key))
278+
escapedKey := url.QueryEscape(key)
279+
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("document"), escapedKey))
276280
if err != nil {
277281
return DocumentMeta{}, WithStack(err)
278282
}
@@ -376,7 +380,8 @@ func (c *collection) RemoveDocument(ctx context.Context, key string) (DocumentMe
376380
if err := validateKey(key); err != nil {
377381
return DocumentMeta{}, WithStack(err)
378382
}
379-
req, err := c.conn.NewRequest("DELETE", path.Join(c.relPath("document"), key))
383+
escapedKey := url.QueryEscape(key)
384+
req, err := c.conn.NewRequest("DELETE", path.Join(c.relPath("document"), escapedKey))
380385
if err != nil {
381386
return DocumentMeta{}, WithStack(err)
382387
}

collection_impl.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package driver
2424

2525
import (
2626
"context"
27+
"net/url"
2728
"path"
2829
)
2930

@@ -50,7 +51,8 @@ type collection struct {
5051

5152
// relPath creates the relative path to this collection (`_db/<db-name>/_api/<api-name>/<col-name>`)
5253
func (c *collection) relPath(apiName string) string {
53-
return path.Join(c.db.relPath(), "_api", apiName, c.name)
54+
escapedName := url.QueryEscape(c.name)
55+
return path.Join(c.db.relPath(), "_api", apiName, escapedName)
5456
}
5557

5658
// Name returns the name of the collection.

database_impl.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package driver
2424

2525
import (
2626
"context"
27+
"net/url"
2728
"path"
2829
)
2930

@@ -49,7 +50,8 @@ type database struct {
4950

5051
// relPath creates the relative path to this database (`_db/<name>`)
5152
func (d *database) relPath() string {
52-
return path.Join("_db", d.name)
53+
escapedName := url.QueryEscape(d.name)
54+
return path.Join("_db", escapedName)
5355
}
5456

5557
// Name returns the name of the database.
@@ -60,7 +62,8 @@ func (d *database) Name() string {
6062
// Collection opens a connection to an existing collection within the database.
6163
// If no collection with given name exists, an NotFoundError is returned.
6264
func (d *database) Collection(ctx context.Context, name string) (Collection, error) {
63-
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", name))
65+
escapedName := url.QueryEscape(name)
66+
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", escapedName))
6467
if err != nil {
6568
return nil, WithStack(err)
6669
}
@@ -80,7 +83,8 @@ func (d *database) Collection(ctx context.Context, name string) (Collection, err
8083

8184
// CollectionExists returns true if a collection with given name exists within the database.
8285
func (d *database) CollectionExists(ctx context.Context, name string) (bool, error) {
83-
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", name))
86+
escapedName := url.QueryEscape(name)
87+
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", escapedName))
8488
if err != nil {
8589
return false, WithStack(err)
8690
}

0 commit comments

Comments
 (0)