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

Validate that the partialDoc is decoded correctly #201

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions v5/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var (
ErrInvalid = errors.New("invalid state detected")
ErrInvalidIndex = errors.New("invalid index referenced")

ErrExpectedObject = errors.New("invalid value, expected object")

rawJSONArray = []byte("[]")
rawJSONObject = []byte("{}")
rawJSONNull = []byte("null")
Expand Down Expand Up @@ -134,6 +136,10 @@ func (n *lazyNode) UnmarshalJSON(data []byte) error {
}

func (n *partialDoc) TrustMarshalJSON(buf *bytes.Buffer) error {
if n.obj == nil {
return ErrExpectedObject
}

if err := buf.WriteByte('{'); err != nil {
return err
}
Expand Down Expand Up @@ -557,6 +563,10 @@ func findObject(pd *container, path string, options *ApplyOptions) (container, s
}

func (d *partialDoc) set(key string, val *lazyNode, options *ApplyOptions) error {
if d.obj == nil {
return ErrExpectedObject
}

found := false
for _, k := range d.keys {
if k == key {
Expand All @@ -579,6 +589,11 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
if key == "" {
return d.self, nil
}

if d.obj == nil {
return nil, ErrExpectedObject
}

v, ok := d.obj[key]
if !ok {
return v, errors.Wrapf(ErrMissing, "unable to get nonexistent key: %s", key)
Expand All @@ -587,6 +602,10 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
}

func (d *partialDoc) remove(key string, options *ApplyOptions) error {
if d.obj == nil {
return ErrExpectedObject
}

_, ok := d.obj[key]
if !ok {
if options.AllowMissingPathOnRemove {
Expand Down
Loading