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

fix: secp256r1 json missing quotes #20060

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crypto/keys/secp256r1/privkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func (sk ecdsaSK) Marshal() ([]byte, error) {
// MarshalJSON implements customProtobufType.
func (sk ecdsaSK) MarshalJSON() ([]byte, error) {
b64 := base64.StdEncoding.EncodeToString(sk.PrivKey.Bytes())
return []byte(b64), nil
return []byte("\"" + b64 + "\""), nil
}

// UnmarshalJSON implements customProtobufType.
func (sk *ecdsaSK) UnmarshalJSON(data []byte) error {
bz, err := base64.StdEncoding.DecodeString(string(data))
bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider verifying that the input JSON string is correctly quoted to avoid runtime errors.

+ if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
+     return errors.New("input JSON string is not properly quoted")
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return errors.New("input JSON string is not properly quoted")
}
bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))

if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions crypto/keys/secp256r1/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ func (pk ecdsaPK) Marshal() ([]byte, error) {
// MarshalJSON implements customProtobufType.
func (pk ecdsaPK) MarshalJSON() ([]byte, error) {
b64 := base64.StdEncoding.EncodeToString(pk.PubKey.Bytes())
return []byte(b64), nil
return []byte("\"" + b64 + "\""), nil
}

// UnmarshalJSON implements customProtobufType.
func (pk *ecdsaPK) UnmarshalJSON(data []byte) error {
bz, err := base64.StdEncoding.DecodeString(string(data))
// the string is quoted so we need to remove them
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
if err != nil {
return err
}
Expand Down
Loading