From 3f6a912af612333a7da81d645587e910727472cf Mon Sep 17 00:00:00 2001 From: Andrew Poydence Date: Tue, 31 Mar 2015 21:08:06 -0600 Subject: [PATCH] Returns an error for a key with a '/' --- api/kv.go | 4 ++++ api/kv_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/api/kv.go b/api/kv.go index ba74057fc..c1a8923be 100644 --- a/api/kv.go +++ b/api/kv.go @@ -168,6 +168,10 @@ func (k *KV) Release(p *KVPair, q *WriteOptions) (bool, *WriteMeta, error) { } func (k *KV) put(key string, params map[string]string, body []byte, q *WriteOptions) (bool, *WriteMeta, error) { + if len(key) > 0 && key[0] == '/' { + return false, nil, fmt.Errorf("Invalid key. Key must not begin with a '/': %s", key) + } + r := k.c.newRequest("PUT", "/v1/kv/"+key) r.setWriteOptions(q) for param, val := range params { diff --git a/api/kv_test.go b/api/kv_test.go index 8f2b54945..b4dcbf576 100644 --- a/api/kv_test.go +++ b/api/kv_test.go @@ -30,6 +30,14 @@ func TestClientPutGetDelete(t *testing.T) { t.Fatalf("err: %v", err) } + // Put a key that begins with a '/' + invalidKey := "/test" + value = []byte(invalidKey) + p = &KVPair{Key: key, Flags: 42, Value: value} + if _, err := kv.Put(p, nil); err == nil { + t.Fatalf("Invalid key not detected: %s", invalidKey) + } + // Get should work pair, meta, err := kv.Get(key, nil) if err != nil {