Skip to content

(RESTCONF+YANG) PUT Request with invalid JSON modifies state #100

@sibuk-harabudjasim

Description

@sibuk-harabudjasim

When making CRUD request (PUT) to RESTCONF with invalid JSON (missing bracket for ex), request is rejected with proper error message but state is also changed. I found core of a problem, but don't have proper solution.
IMO problem lays between these 3 functions:

func ReadJSONIO(rdr io.Reader) node.Node {

func ReadJSONIO(rdr io.Reader) node.Node {
	jrdr := &JSONRdr{In: rdr}
	return jrdr.Node()
}

Here function returns node.Node (which can be node.ErrorNode in case of read errors) without separate error value.
This cause code in restconf:
https://github.com/freeconf/restconf/blob/72cc856c177bf129d9c55bb2978367379209b4c6/browser_handler.go#L179-L184

		case "PUT":
			// CRUD - Remove and replace
			var input node.Node
			input, err = requestNode(r)
			if err != nil {
				handleErr(compliance, err, r, w)
				return
			}
			err = target.ReplaceFrom(input)

to not to check error and pass erroneous node.Noderight to yanglibrary.

Here, ReplaceFrom first deletes previous version of element before trying to apply new version which is an just error.

func (sel *Selection) ReplaceFrom(fromNode Node) error {

func (sel *Selection) ReplaceFrom(fromNode Node) error {
	parent := sel.parent
	if err := sel.Delete(); err != nil {
		return err
	}
	return parent.InsertFrom(fromNode)
}

The error is returned but it's too late, data is already gone.

Like I said, I've done investigation :) but I don't know how to properly fix this issue to keep code consistency.
Main question is: what is better, to return error from ReadJSONIO() or to check for node.ErrorNode in browser_handler.go?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions