feat: support authn keys#2
feat: support authn keys#2johnletey wants to merge 2 commits intov0.45.x-send-restrictions-authnfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe recent changes enhance the Cosmos SDK by supporting authentication keys, specifically through the introduction of a new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant AuthnService
participant CryptoCodec
participant SignatureVerifier
User->>AuthnService: Request Authentication
AuthnService->>CryptoCodec: Register PubKey
CryptoCodec-->>AuthnService: Registration Successful
AuthnService->>SignatureVerifier: Verify Signature
SignatureVerifier-->>AuthnService: Verification Result
AuthnService-->>User: Authentication Success/Failure
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
|
@coderabbitai review |
Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 12
Outside diff range, codebase verification and nitpick comments (1)
crypto/keys/authn/signature_test.go (1)
1-2: Add a package comment.Consider adding a comment to describe the purpose of the
authnpackage.// Package authn provides functionalities for authentication key management and signature verification. package authn
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (1)
crypto/keys/authn/keys.pb.gois excluded by!**/*.pb.go
Files selected for processing (9)
- Makefile (1 hunks)
- crypto/codec/amino.go (2 hunks)
- crypto/codec/proto.go (2 hunks)
- crypto/keys/authn/authn.go (1 hunks)
- crypto/keys/authn/signature.go (1 hunks)
- crypto/keys/authn/signature_test.go (1 hunks)
- docs/core/proto-docs.md (2 hunks)
- proto/cosmos/crypto/authn/keys.proto (1 hunks)
- x/auth/ante/sigverify.go (2 hunks)
Files skipped from review due to trivial changes (1)
- Makefile
Additional context used
golangci-lint
crypto/keys/authn/signature.go
80-80: appendAssign: append result not assigned to the same slice
(gocritic)
Gitleaks
crypto/keys/authn/signature_test.go
55-55: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
322-322: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
Markdownlint
docs/core/proto-docs.md
3541-3541: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3542-3542: Expected: 1; Actual: 3
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3548-3548: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3554-3554: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3555-3555: Expected: 1; Actual: 3
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3556-3556: Expected: 1; Actual: 4
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3557-3557: Expected: 1; Actual: 5
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3566-3566: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3567-3567: Expected: 1; Actual: 3
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3545-3545: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines(MD022, blanks-around-headings)
Additional comments not posted (39)
proto/cosmos/crypto/authn/keys.proto (6)
1-1: Syntax declaration is correct.The file correctly declares the syntax as proto3.
2-2: Package declaration is correct.The package is appropriately named
cosmos.crypto.authn.
4-4: Import statement is correct.The file correctly imports
gogoproto/gogo.protofor using gogoproto options.
8-11: Review ofgoproto_stringeroption.The
goproto_stringeroption is set to false, which disables the generation of theString()method for thePubKeymessage. This is appropriate if string representation is not needed.
12-12: Review ofkey_idfield.The
key_idfield is defined as a string with field number 1. This is appropriate for storing an identifier for the key.
13-13: Review ofkeyfield.The
keyfield is defined as bytes with field number 2. This is appropriate for storing the actual key data.crypto/codec/proto.go (3)
Line range hint
1-1:
Package declaration is correct.The package is appropriately named
codec.
Line range hint
3-11:
Import statements are correct.The file correctly imports the necessary packages, including the new
authnpackage.
17-17: Registration ofauthn.PubKeyis correct.The function correctly registers the new
authn.PubKeytype as an implementation of thecosmos.crypto.PubKeyinterface.crypto/codec/amino.go (3)
Line range hint
1-1:
Package declaration is correct.The package is appropriately named
codec.
4-7: Import statements are correct.The file correctly imports the necessary packages, including the new
authnpackage.
20-21: Registration ofauthn.PubKeyis correct.The function correctly registers the new
authn.PubKeytype alongside existing public key types.crypto/keys/authn/authn.go (7)
40-42: LGTM!The method is straightforward and correct.
44-46: LGTM!The method is straightforward and correct.
48-50: LGTM!The method is straightforward and correct.
52-54: LGTM!The method is straightforward and correct.
56-59: LGTM!The method is straightforward and correct.
71-76: LGTM!The method is straightforward and correct.
79-81: LGTM!The method is straightforward and correct.
x/auth/ante/sigverify.go (1)
390-393: LGTM!The new case for
*authn.PubKeyis consistent with existing cases and correctly handles the new public key type.crypto/keys/authn/signature_test.go (18)
22-33: LGTM!The
CollectedClientDatastructure is well-defined and follows the WebAuthn specification.
35-37: LGTM!The
TokenBindingstructure is well-defined and follows the WebAuthn specification.
61-74: LGTM!The
GenerateClientDatafunction is well-implemented and follows the WebAuthn specification.
76-112: LGTM!The
TestVerifySignaturefunction is comprehensive and covers multiple scenarios.
114-148: LGTM!The
TestVerifySignature_ChallengeStdEncodingfunction is well-implemented and covers the scenario effectively.
150-184: LGTM!The
TestVerifySignature_ChallengeHexEncodingfunction is well-implemented and covers the scenario effectively.
186-220: LGTM!The
TestVerifySignature_ChallengeEmptyfunction is well-implemented and covers the scenario effectively.
222-253: LGTM!The
TestVerifySignature_ChallengeNilfunction is well-implemented and covers the scenario effectively.
255-287: LGTM!The
TestVerifySignature_ChallengeIntegerfunction is well-implemented and covers the scenario effectively.
289-314: LGTM!The
TestVerifySignature_ClientDataJSONEmptyfunction is well-implemented and covers the scenario effectively.
316-349: LGTM!The
TestVerifySignature_UncompressedPubKeyfunction is well-implemented and covers the scenario effectively.Tools
Gitleaks
322-322: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
351-378: LGTM!The
TestVerifySignature_AnotherPubKeyfunction is well-implemented and covers the scenario effectively.
380-406: LGTM!The
TestVerifySignature_SignaureNotInASN1function is well-implemented and covers the scenario effectively.
408-433: LGTM!The
TestVerifySignature_EmptyAuthenticatorDatafunction is well-implemented and covers the scenario effectively.
435-461: LGTM!The
TestVerifySignature_SignatureEncodingInBase64function is well-implemented and covers the scenario effectively.
463-488: LGTM!The
TestVerifySignature_ClientDataJSONEncodingInBase64function is well-implemented and covers the scenario effectively.
490-494: LGTM!The
TestVerifySignature_EmptySignaturefunction is well-implemented and covers the scenario effectively.
496-520: LGTM!The
TestVerifySignature_AuthenticatorDataLengthfunction is well-implemented and covers the scenario effectively.docs/core/proto-docs.md (1)
229-231: LGTM!The reference link to
cosmos/crypto/authn/keys.protoandPubKeyhas been correctly added.
| <a name="cosmos/crypto/authn/keys.proto"></a> | ||
| <p align="right"><a href="#top">Top</a></p> | ||
|
|
||
| ## cosmos/crypto/authn/keys.proto | ||
|
|
||
|
|
||
|
|
||
| <a name="cosmos.crypto.authn.PubKey"></a> | ||
|
|
||
| ### PubKey | ||
| PubKey defines a authn public key | ||
|
|
||
|
|
||
| | Field | Type | Label | Description | | ||
| | ----- | ---- | ----- | ----------- | | ||
| | `key_id` | [string](#string) | | | | ||
| | `key` | [bytes](#bytes) | | | | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| <!-- end messages --> | ||
|
|
||
| <!-- end enums --> | ||
|
|
||
| <!-- end HasExtensions --> | ||
|
|
||
| <!-- end services --> | ||
|
|
||
|
|
||
|
|
There was a problem hiding this comment.
Fix the formatting issues.
There are multiple consecutive blank lines and missing blank lines around headings. This violates markdownlint rules.
-<a name="cosmos/crypto/authn/keys.proto"></a>
-<p align="right"><a href="#top">Top</a></p>
-
-## cosmos/crypto/authn/keys.proto
-
-
-
-<a name="cosmos.crypto.authn.PubKey"></a>
-
-### PubKey
-PubKey defines a authn public key
-
-
-| Field | Type | Label | Description |
-| ----- | ---- | ----- | ----------- |
-| `key_id` | [string](#string) | | |
-| `key` | [bytes](#bytes) | | |
-
-
-
-
<!-- end messages -->
<!-- end enums -->
<!-- end HasExtensions -->
<!-- end services -->
-
-
-
-<a name="cosmos/crypto/ed25519/keys.proto"></a>
-<p align="right"><a href="#top">Top</a></p>
+<a name="cosmos/crypto/authn/keys.proto"></a>
+<p align="right"><a href="#top">Top</a></p>
+
+## cosmos/crypto/authn/keys.proto
+
+<a name="cosmos.crypto.authn.PubKey"></a>
+
+### PubKey
+PubKey defines an authn public key
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| `key_id` | [string](#string) | | |
+| `key` | [bytes](#bytes) | | |
+
<!-- end messages -->
<!-- end enums -->
<!-- end HasExtensions -->
<!-- end services -->
+
+<a name="cosmos/crypto/ed25519/keys.proto"></a>
+<p align="right"><a href="#top">Top</a></p>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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| <a name="cosmos/crypto/authn/keys.proto"></a> | |
| <p align="right"><a href="#top">Top</a></p> | |
| ## cosmos/crypto/authn/keys.proto | |
| <a name="cosmos.crypto.authn.PubKey"></a> | |
| ### PubKey | |
| PubKey defines a authn public key | |
| | Field | Type | Label | Description | | |
| | ----- | ---- | ----- | ----------- | | |
| | `key_id` | [string](#string) | | | | |
| | `key` | [bytes](#bytes) | | | | |
| <!-- end messages --> | |
| <!-- end enums --> | |
| <!-- end HasExtensions --> | |
| <!-- end services --> | |
| <a name="cosmos/crypto/authn/keys.proto"></a> | |
| <p align="right"><a href="#top">Top</a></p> | |
| ## cosmos/crypto/authn/keys.proto | |
| <a name="cosmos.crypto.authn.PubKey"></a> | |
| ### PubKey | |
| PubKey defines an authn public key | |
| | Field | Type | Label | Description | | |
| | ----- | ---- | ----- | ----------- | | |
| | `key_id` | [string](#string) | | | | |
| | `key` | [bytes](#bytes) | | | | |
| <!-- end messages --> | |
| <!-- end enums --> | |
| <!-- end HasExtensions --> | |
| <!-- end services --> | |
| <a name="cosmos/crypto/ed25519/keys.proto"></a> | |
| <p align="right"><a href="#top">Top</a></p> |
Tools
Markdownlint
3541-3541: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3542-3542: Expected: 1; Actual: 3
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3548-3548: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3554-3554: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3555-3555: Expected: 1; Actual: 3
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3556-3556: Expected: 1; Actual: 4
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3557-3557: Expected: 1; Actual: 5
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3566-3566: Expected: 1; Actual: 2
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3567-3567: Expected: 1; Actual: 3
Multiple consecutive blank lines(MD012, no-multiple-blanks)
3545-3545: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines(MD022, blanks-around-headings)
| } | ||
|
|
||
| clientDataHash := sha256.Sum256(clientDataJSON) | ||
| payload := append(authenticatorData, clientDataHash[:]...) |
There was a problem hiding this comment.
Fix the append assignment issue.
The result of append should be assigned back to payload to ensure the new slice is used.
- payload := append(authenticatorData, clientDataHash[:]...)
+ payload = append(authenticatorData, clientDataHash[:]...)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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| payload := append(authenticatorData, clientDataHash[:]...) | |
| payload = append(authenticatorData, clientDataHash[:]...) |
Tools
golangci-lint
80-80: appendAssign: append result not assigned to the same slice
(gocritic)
| signatureBytes, err := hex.DecodeString(sig.Signature) | ||
| if err != nil { | ||
| return false | ||
| } |
There was a problem hiding this comment.
Handle hex decoding errors gracefully.
The current error handling simply returns false. Consider logging the error for debugging purposes.
- if err != nil {
- return false
- }
+ if err != nil {
+ // Log the error for debugging purposes
+ fmt.Printf("Error decoding signatureBytes: %v\n", err)
+ return false
+ }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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| signatureBytes, err := hex.DecodeString(sig.Signature) | |
| if err != nil { | |
| return false | |
| } | |
| signatureBytes, err := hex.DecodeString(sig.Signature) | |
| if err != nil { | |
| // Log the error for debugging purposes | |
| fmt.Printf("Error decoding signatureBytes: %v\n", err) | |
| return false | |
| } |
| challenge, err := base64.RawURLEncoding.DecodeString(challengeBase64) | ||
| if err != nil { | ||
| return false | ||
| } |
There was a problem hiding this comment.
Handle base64 decoding errors gracefully.
The current error handling simply returns false. Consider logging the error for debugging purposes.
- if err != nil {
- return false
- }
+ if err != nil {
+ // Log the error for debugging purposes
+ fmt.Printf("Error decoding challengeBase64: %v\n", err)
+ return false
+ }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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| challenge, err := base64.RawURLEncoding.DecodeString(challengeBase64) | |
| if err != nil { | |
| return false | |
| } | |
| challenge, err := base64.RawURLEncoding.DecodeString(challengeBase64) | |
| if err != nil { | |
| // Log the error for debugging purposes | |
| fmt.Printf("Error decoding challengeBase64: %v\n", err) | |
| return false | |
| } |
| authenticatorData, err := hex.DecodeString(sig.AuthenticatorData) | ||
| if err != nil { | ||
| return false | ||
| } |
There was a problem hiding this comment.
Handle hex decoding errors gracefully.
The current error handling simply returns false. Consider logging the error for debugging purposes.
- if err != nil {
- return false
- }
+ if err != nil {
+ // Log the error for debugging purposes
+ fmt.Printf("Error decoding authenticatorData: %v\n", err)
+ return false
+ }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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| authenticatorData, err := hex.DecodeString(sig.AuthenticatorData) | |
| if err != nil { | |
| return false | |
| } | |
| authenticatorData, err := hex.DecodeString(sig.AuthenticatorData) | |
| if err != nil { | |
| // Log the error for debugging purposes | |
| fmt.Printf("Error decoding authenticatorData: %v\n", err) | |
| return false | |
| } |
| err := json.Unmarshal(sigStr, &sig) | ||
| if err != nil { | ||
| return false | ||
| } |
There was a problem hiding this comment.
Handle JSON unmarshalling errors gracefully.
The current error handling simply returns false. Consider logging the error for debugging purposes.
- if err != nil {
- return false
- }
+ if err != nil {
+ // Log the error for debugging purposes
+ fmt.Printf("Error unmarshalling signature: %v\n", err)
+ return false
+ }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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| err := json.Unmarshal(sigStr, &sig) | |
| if err != nil { | |
| return false | |
| } | |
| err := json.Unmarshal(sigStr, &sig) | |
| if err != nil { | |
| // Log the error for debugging purposes | |
| fmt.Printf("Error unmarshalling signature: %v\n", err) | |
| return false | |
| } |
| func (pubKey *PubKey) UnmarshalAmino(bz []byte) error { | ||
| if len(bz) != PubKeySize { | ||
| return sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "invalid pubkey size") | ||
| } |
There was a problem hiding this comment.
Handle invalid pubkey size error gracefully.
The error message should be more descriptive to aid debugging.
- return sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "invalid pubkey size")
+ return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "invalid pubkey size: expected %d, got %d", PubKeySize, len(bz))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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func (pubKey *PubKey) UnmarshalAmino(bz []byte) error { | |
| if len(bz) != PubKeySize { | |
| return sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "invalid pubkey size") | |
| } | |
| func (pubKey *PubKey) UnmarshalAmino(bz []byte) error { | |
| if len(bz) != PubKeySize { | |
| return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "invalid pubkey size: expected %d, got %d", PubKeySize, len(bz)) | |
| } |
| func (pubKey *PubKey) Address() crypto.Address { | ||
| if len(pubKey.Key) != PubKeySize { | ||
| panic("length of pubkey is incorrect") | ||
| } |
There was a problem hiding this comment.
Avoid using panic statements.
Replace the panic statement with proper error handling to ensure the application does not crash unexpectedly.
- if len(pubKey.Key) != PubKeySize {
- panic("length of pubkey is incorrect")
- }
+ if len(pubKey.Key) != PubKeySize {
+ return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "length of pubkey is incorrect")
+ }Committable suggestion was skipped due to low confidence.
| cborSig := Signature{ | ||
| AuthenticatorData: hex.EncodeToString(authenticatorData), | ||
| ClientDataJSON: hex.EncodeToString(clientDataJSON), | ||
| Signature: hex.EncodeToString(sig), | ||
| } |
There was a problem hiding this comment.
Define the Signature structure.
The Signature structure is used in multiple test functions but is not defined in the provided code.
type Signature struct {
AuthenticatorData string `json:"authenticatorData"`
ClientDataJSON string `json:"clientDataJSON"`
Signature string `json:"signature"`
}Also applies to: 139-143, 175-179, 211-215, 244-248, 278-282, 340-344, 369-373, 397-401, 424-428, 452-456, 479-483, 511-515
| func GenerateAuthnKey(t *testing.T) (*ecdsa.PrivateKey, PubKey) { | ||
| curve := elliptic.P256() | ||
| privateKey, err := ecdsa.GenerateKey(curve, rand.Reader) | ||
| require.NoError(t, err) | ||
| pkBytes := elliptic.MarshalCompressed(curve, privateKey.PublicKey.X, privateKey.PublicKey.Y) | ||
| pk := PubKey{ | ||
| KeyId: "a099eda0fb05e5783379f73a06acca726673b8e07e436edcd0d71645982af65c", | ||
| Key: pkBytes, | ||
| } | ||
| return privateKey, pk | ||
| } |
There was a problem hiding this comment.
Remove hardcoded KeyId.
The hardcoded KeyId was flagged by static analysis tools. Consider generating it dynamically.
- KeyId: "a099eda0fb05e5783379f73a06acca726673b8e07e436edcd0d71645982af65c",
+ hash := sha256.Sum256(pkBytes)
+ KeyId: hex.EncodeToString(hash[:]),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. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func GenerateAuthnKey(t *testing.T) (*ecdsa.PrivateKey, PubKey) { | |
| curve := elliptic.P256() | |
| privateKey, err := ecdsa.GenerateKey(curve, rand.Reader) | |
| require.NoError(t, err) | |
| pkBytes := elliptic.MarshalCompressed(curve, privateKey.PublicKey.X, privateKey.PublicKey.Y) | |
| pk := PubKey{ | |
| KeyId: "a099eda0fb05e5783379f73a06acca726673b8e07e436edcd0d71645982af65c", | |
| Key: pkBytes, | |
| } | |
| return privateKey, pk | |
| } | |
| func GenerateAuthnKey(t *testing.T) (*ecdsa.PrivateKey, PubKey) { | |
| curve := elliptic.P256() | |
| privateKey, err := ecdsa.GenerateKey(curve, rand.Reader) | |
| require.NoError(t, err) | |
| pkBytes := elliptic.MarshalCompressed(curve, privateKey.PublicKey.X, privateKey.PublicKey.Y) | |
| hash := sha256.Sum256(pkBytes) | |
| pk := PubKey{ | |
| KeyId: hex.EncodeToString(hash[:]), | |
| Key: pkBytes, | |
| } | |
| return privateKey, pk | |
| } |
Tools
Gitleaks
55-55: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
Closes #1
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
PubKeystructure and its fields.Tests