-
Notifications
You must be signed in to change notification settings - Fork 72
Improve UnknownFieldError message #669
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
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Tim Barry <[email protected]>
d5cb714 to
7b6cb77
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tim-barry Thanks for opening this PR.
I left some suggestions, but the most important ones are:
-
We shouldn't use
%sin the error message to add the field name coming from untrusted input data. -
The changes made by this PR don't handle all the use cases in the
parseMapToStruct()function. Specifically, it doesn't handle CBOR map key as integer for unknown field error.
BTW, I'm out of office for ~2 weeks starting tomorrow (Monday, June 9) but will take a look at this PR in case it is updated.
If this is urgent, please let me know (here on GitHub) so I can take a look sooner.
| Struct string // name of the struct being decoded | ||
| Field string // field of the CBOR map that was unexpected | ||
| Index int // index of the field in the CBOR map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename two fields:
| Struct string // name of the struct being decoded | |
| Field string // field of the CBOR map that was unexpected | |
| Index int // index of the field in the CBOR map | |
| StructName string // name of the struct being decoded | |
| FieldName string // unknown field name decoded from CBOR map key | |
| Index int // index of the field in the CBOR map |
|
|
||
| func (e *UnknownFieldError) Error() string { | ||
| return fmt.Sprintf("cbor: found unknown field at map element index %d", e.Index) | ||
| return fmt.Sprintf("cbor: found unknown field: struct '%s' has no field '%s', at map element index %d", e.Struct, e.Field, e.Index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using %s to add field name here to an error message requires security considerations.
In this PR, field name is decoded from untrusted data, so we need to consider the possibility it might contain non-printable characters.
We can use %q instead of %s to escape non-printable characters.
Please also reword the error message to be more consistent with the old message, like this:
| return fmt.Sprintf("cbor: found unknown field: struct '%s' has no field '%s', at map element index %d", e.Struct, e.Field, e.Index) | |
| return fmt.Sprintf("cbor: found unknown field %q at map element index %d in struct %q", e.Field, e.Index, e.Struct) |
| var k any | ||
|
|
||
| t := d.nextCBORType() | ||
| var keyBytes []byte |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes don't cover all use cases in parseMapToStruct().
Valid map keys in this function can be byte string, text string, or integer (to support keyasint feature). Currently, keyBytes is only used when map key type is byte string or text string.
So we also need to handle unknown field when map key is integer.
Also, please add a test for unknown field of integer type.
|
Hey @tim-barry 👋 Do you have any updates planned for this PR? This July 4 weekend, I need to begin preparing for v2.9.0 release by reviewing changes, updating docs, fuzz testing, etc. After fuzzing starts for v2.9.0, coding changes to codec (except regression fixes & docs) will likely be postponed until after v2.9.0 is release tagged. |
Description
Improves the error message for UnknownFieldError when a CBOR map does not match with the Go struct currently being decoded into. The message now includes struct and field names, allowing simpler debugging of issues that can occur due to mismatched struct types.
PR Was Proposed and Welcomed in Currently Open Issue
Checklist (for code PR only, ignore for docs PR)
Last line of each commit message should be in this format:
Signed-off-by: Firstname Lastname [email protected]
(see next section).
Certify the Developer's Certificate of Origin 1.1
the Developer Certificate of Origin 1.1.