diff --git a/capability/policy/selector/selector.go b/capability/policy/selector/selector.go index 1733fcb..a46388c 100644 --- a/capability/policy/selector/selector.go +++ b/capability/policy/selector/selector.go @@ -222,6 +222,24 @@ func resolve(sel Selector, subject ipld.Node, at []string) (ipld.Node, []ipld.No } else { cur = basicnode.NewString(string(str[idx])) } + } else if cur != nil && cur.Kind() == datamodel.Kind_Bytes { + b, err := cur.AsBytes() + if err != nil { + return nil, nil, err + } + idx := seg.Index() + if idx < 0 { + idx = len(b) + idx + } + if idx < 0 || idx >= len(b) { + if seg.Optional() { + cur = nil + } else { + return nil, nil, newResolutionError(fmt.Sprintf("index out of bounds: %d", seg.Index()), at) + } + } else { + cur = basicnode.NewInt(int64(b[idx])) + } } else if seg.Optional() { cur = nil } else { diff --git a/capability/policy/selector/supported_test.go b/capability/policy/selector/supported_test.go index 6a6013a..6524950 100644 --- a/capability/policy/selector/supported_test.go +++ b/capability/policy/selector/supported_test.go @@ -37,7 +37,7 @@ func TestSupportedForms(t *testing.T) { {Name: "Index", Selector: `.[0]`, Input: `[1, 2]`, Output: `1`}, {Name: "Negative Index", Selector: `.[-1]`, Input: `[1, 2]`, Output: `2`}, {Name: "String Index", Selector: `.[0]`, Input: `"Hi"`, Output: `"H"`}, - {Name: "Bytes Index", Selector: `.[0]`, Input: `{"/":{"bytes":"AAE"}`, Output: `0`}, + {Name: "Bytes Index", Selector: `.[0]`, Input: `{"/":{"bytes":"AAE"}}`, Output: `0`}, {Name: "Array Slice", Selector: `.[0:2]`, Input: `[0, 1, 2]`, Output: `[0, 1]`}, {Name: "Array Slice", Selector: `.[1:]`, Input: `[0, 1, 2]`, Output: `[1, 2]`}, {Name: "Array Slice", Selector: `.[:2]`, Input: `[0, 1, 2]`, Output: `[0, 1]`},