Skip to content

Commit

Permalink
Merge pull request #247 from blinklabs-io/fix/cbor-fail-on-unknown-fi…
Browse files Browse the repository at this point in the history
…elds

fix: fail CBOR decoding on unknown field
  • Loading branch information
agaffney authored May 10, 2023
2 parents 026cd18 + 0b91f5c commit 91b9815
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cbor/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ import (

func Decode(dataBytes []byte, dest interface{}) (int, error) {
data := bytes.NewReader(dataBytes)
dec := _cbor.NewDecoder(data)
err := dec.Decode(dest)
// Create a custom decoder that returns an error on unknown fields
decOptions := _cbor.DecOptions{
ExtraReturnErrors: _cbor.ExtraDecErrorUnknownField,
}
decMode, err := decOptions.DecMode()
if err != nil {
return 0, err
}
dec := decMode.NewDecoder(data)
err = dec.Decode(dest)
return dec.NumBytesRead(), err
}

Expand Down

0 comments on commit 91b9815

Please sign in to comment.