-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fail when the JSON input has extra fields (#211)
- Loading branch information
Showing
7 changed files
with
215 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package jsondecoder | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrExtraDataAfterDecoding = errors.New("extra data after decoding") | ||
ErrUnknownField = errors.New("unknown field in JSON input") | ||
) | ||
|
||
// UnmarshalDisallowUnknownFieldsUnmarshal parses the JSON-encoded data | ||
// and stores the result in the value pointed to by v like json.Unmarshal, | ||
// but with DisallowUnknownFields() set by default for extra security. | ||
func UnmarshalDisallowUnknownFields(data []byte, v interface{}) error { | ||
dec := json.NewDecoder(bytes.NewReader(data)) | ||
dec.DisallowUnknownFields() | ||
|
||
if err := dec.Decode(v); err != nil { | ||
// Ugly, but the encoding/json uses a dynamic error here | ||
if strings.HasPrefix(err.Error(), "json: unknown field ") { | ||
return ErrUnknownField | ||
} | ||
|
||
// we want to provide an alternative implementation, with the | ||
// unwrapped errors | ||
//nolint:wrapcheck | ||
return err | ||
} | ||
|
||
// Check if there's any data left in the decoder's buffer. | ||
// This ensures that there's no extra JSON after the main object | ||
// otherwise something like '{"foo": 1}{"bar": 2}' or even '{}garbage' | ||
// will not error out. | ||
if dec.More() { | ||
return ErrExtraDataAfterDecoding | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.