~whereswaldon/forest-go

b9e48a8b078db9fbc37238edd6b174d54e0ba8f2 — Chris Waldon a month ago ee23841
feat(twig): reject values with null bytes in Set
2 files changed, 18 insertions(+), 0 deletions(-)

M twig/twig.go
M twig/twig_test.go
M twig/twig.go => twig/twig.go +5 -0
@@ 82,6 82,11 @@ func New() *Data {

// Set sets a twig key-version data entry. If the entry does not exist, it is created
func (d *Data) Set(name string, version uint, value []byte) (*Data, error) {
	for i, b := range value {
		if b == 0 {
			return nil, fmt.Errorf("invalid null byte in twig value at index %d", i)
		}
	}
	d.Values[Key{Name: name, Version: version}] = value
	return d, nil
}

M twig/twig_test.go => twig/twig_test.go +13 -0
@@ 90,3 90,16 @@ func TestDataMarshalNoBytes(t *testing.T) {
		t.Fatalf("Empty data store should return an empty.")
	}
}

func TestDataSet(t *testing.T) {
	data := twig.New()
	var err error
	data, err = data.Set("foo", 1, []byte("bar"))
	if err != nil {
		t.Fatalf("failed to set legal key value pair")
	}
	data, err = data.Set("baz", 1, []byte{1, 2, 3, 4, 0, 5})
	if err == nil {
		t.Fatalf("successfully set illegal key value pair containing null byte")
	}
}