Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some additional validation for object keys. #16311

Merged
merged 1 commit into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/tools/etcd_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ func (h *EtcdHelper) extractObj(response *etcd.Response, inErr error, objPtr run
// and 0 means forever. If no error is returned and out is not nil, out will be set to the read value
// from etcd.
func (h *EtcdHelper) CreateObj(key string, obj, out runtime.Object, ttl uint64) error {
if strings.Contains(key, "../") {
return fmt.Errorf("Illegal path, moving to parent directories not supported")
}
key = h.PrefixEtcdKey(key)
data, err := h.Codec.Encode(obj)
if err != nil {
Expand Down Expand Up @@ -454,6 +457,9 @@ func (h *EtcdHelper) SetObj(key string, obj, out runtime.Object, ttl uint64) err
if err != nil {
return err
}
if strings.Contains(key, "../") {
return fmt.Errorf("Illegal path, moving to parent directories not supported")
}
key = h.PrefixEtcdKey(key)

create := true
Expand Down Expand Up @@ -544,6 +550,9 @@ func (h *EtcdHelper) GuaranteedUpdate(key string, ptrToType runtime.Object, igno
// Panic is appropriate, because this is a programming error.
panic("need ptr to type")
}
if strings.Contains(key, "../") {
return fmt.Errorf("Illegal path, moving to parent directories not supported")
}
key = h.PrefixEtcdKey(key)
for {
obj := reflect.New(v.Type()).Interface().(runtime.Object)
Expand Down
37 changes: 37 additions & 0 deletions pkg/tools/etcd_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ func TestCreateObj(t *testing.T) {
}
}

func TestCreateObjBadPath(t *testing.T) {
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
fakeClient := NewFakeEtcdClient(t)
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
err := helper.CreateObj("/some/../key", obj, &api.Pod{}, 5)
if err == nil {
t.Error("Unexpected non error")
}
}

func TestCreateObjNilOutParam(t *testing.T) {
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
fakeClient := NewFakeEtcdClient(t)
Expand Down Expand Up @@ -448,6 +458,16 @@ func TestSetObjFailCAS(t *testing.T) {
}
}

func TestSetObjFailBadPath(t *testing.T) {
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"}}
fakeClient := NewFakeEtcdClient(t)
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
err := helper.SetObj("/some/../key", obj, nil, 5)
if err == nil {
t.Errorf("Expecting error.")
}
}

func TestSetObjWithVersion(t *testing.T) {
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"}}
fakeClient := NewFakeEtcdClient(t)
Expand Down Expand Up @@ -739,6 +759,23 @@ func TestGuaranteedUpdateKeyNotFound(t *testing.T) {
}
}

func TestGuaranteedUpdateBadPath(t *testing.T) {
fakeClient := NewFakeEtcdClient(t)
fakeClient.TestIndex = true
helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix())

obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
f := SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
return obj, nil
})

ignoreNotFound := false
err := helper.GuaranteedUpdate("/some/../key", &TestResource{}, ignoreNotFound, f)
if err == nil {
t.Errorf("Expected error for key with bad path.")
}
}

func TestGuaranteedUpdate_CreateCollision(t *testing.T) {
fakeClient := NewFakeEtcdClient(t)
fakeClient.TestIndex = true
Expand Down