Skip to content

Commit 53508a9

Browse files
authored
Merge pull request from GHSA-vfvj-3m3g-m532
fix: enforce max index value for paths
2 parents a470152 + 7560fbc commit 53508a9

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

pkg/fieldpath/paved.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
"github.com/crossplane/crossplane-runtime/pkg/errors"
2626
)
2727

28+
// DefaultMaxFieldPathIndex is the max allowed index in a field path.
29+
const DefaultMaxFieldPathIndex = 1024
30+
2831
type errNotFound struct {
2932
error
3033
}
@@ -46,19 +49,39 @@ func IsNotFound(err error) bool {
4649

4750
// A Paved JSON object supports getting and setting values by their field path.
4851
type Paved struct {
49-
object map[string]any
52+
object map[string]any
53+
maxFieldPathIndex uint
5054
}
5155

56+
type PavedOption func(paved *Paved)
57+
5258
// PaveObject paves a runtime.Object, making it possible to get and set values
5359
// by field path. o must be a non-nil pointer to an object.
54-
func PaveObject(o runtime.Object) (*Paved, error) {
60+
func PaveObject(o runtime.Object, opts ...PavedOption) (*Paved, error) {
5561
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(o)
56-
return Pave(u), errors.Wrap(err, "cannot convert object to unstructured data")
62+
return Pave(u, opts...), errors.Wrap(err, "cannot convert object to unstructured data")
5763
}
5864

5965
// Pave a JSON object, making it possible to get and set values by field path.
60-
func Pave(object map[string]any) *Paved {
61-
return &Paved{object: object}
66+
func Pave(object map[string]any, opts ...PavedOption) *Paved {
67+
p := &Paved{object: object, maxFieldPathIndex: DefaultMaxFieldPathIndex}
68+
69+
for _, opt := range opts {
70+
opt(p)
71+
}
72+
73+
return p
74+
}
75+
76+
// WithMaxFieldPathIndex returns a PavedOption that sets the max allowed index for field paths, 0 means no limit.
77+
func WithMaxFieldPathIndex(max uint) PavedOption {
78+
return func(paved *Paved) {
79+
paved.maxFieldPathIndex = max
80+
}
81+
}
82+
83+
func (p *Paved) maxFieldPathIndexEnabled() bool {
84+
return p.maxFieldPathIndex > 0
6285
}
6386

6487
// MarshalJSON to the underlying object.
@@ -358,6 +381,10 @@ func (p *Paved) setValue(s Segments, value any) error {
358381
return errors.Errorf("%s is not an array", s[:i])
359382
}
360383

384+
if p.maxFieldPathIndexEnabled() && current.Index > p.maxFieldPathIndex {
385+
return errors.Errorf("index %d is greater than max allowed index %d", current.Index, p.maxFieldPathIndex)
386+
}
387+
361388
if final {
362389
array[current.Index] = v
363390
return nil

pkg/fieldpath/paved_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package fieldpath
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/google/go-cmp/cmp"
@@ -593,6 +594,7 @@ func TestSetValue(t *testing.T) {
593594
type args struct {
594595
path string
595596
value any
597+
opts []PavedOption
596598
}
597599
type want struct {
598600
object map[string]any
@@ -737,6 +739,38 @@ func TestSetValue(t *testing.T) {
737739
},
738740
},
739741
},
742+
"RejectsHighIndexes": {
743+
reason: "Paths having indexes above the maximum default value are rejected",
744+
data: []byte(`{"data":["a"]}`),
745+
args: args{
746+
path: fmt.Sprintf("data[%v]", MaxFieldPathIndex+1),
747+
value: "c",
748+
},
749+
want: want{
750+
object: map[string]any{
751+
"data": []any{"a"}},
752+
err: errors.Wrap(errors.Errorf("found index above max (%[1]v > %[2]v): data[%[1]v]",
753+
MaxFieldPathIndex+1, MaxFieldPathIndex), "invalid segments"),
754+
},
755+
},
756+
"NotRejectsHighIndexesIfNoDefaultOptions": {
757+
reason: "Paths having indexes above the maximum default value are not rejected if default disabled",
758+
data: []byte(`{"data":["a"]}`),
759+
args: args{
760+
path: fmt.Sprintf("data[%v]", MaxFieldPathIndex+1),
761+
value: "c",
762+
opts: []PavedOption{},
763+
},
764+
want: want{
765+
object: map[string]any{
766+
"data": func() []any {
767+
res := make([]any, MaxFieldPathIndex+2)
768+
res[0] = "a"
769+
res[MaxFieldPathIndex+1] = "c"
770+
return res
771+
}()},
772+
},
773+
},
740774
"MapStringString": {
741775
reason: "A map of string to string should be converted to a map of string to any",
742776
data: []byte(`{"metadata":{}}`),
@@ -817,7 +851,7 @@ func TestSetValue(t *testing.T) {
817851
t.Run(name, func(t *testing.T) {
818852
in := make(map[string]any)
819853
_ = json.Unmarshal(tc.data, &in)
820-
p := Pave(in)
854+
p := Pave(in, tc.args.opts...)
821855

822856
err := p.SetValue(tc.args.path, tc.args.value)
823857
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {

0 commit comments

Comments
 (0)