Repository navigation
fix: passkey model type - #1930
Conversation
WalkthroughThis change migrates passkey credential fields from binary ([]byte) to base64-encoded strings, updates GORM column types, and adds encoding/decoding in creation, validation, conversion, and lookup paths to ensure correct storage and retrieval, including querying by base64-encoded credential ID. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Client
participant Model as Passkey Model
participant DB as Database
rect rgb(235, 245, 255)
note over Client,Model: Create/Validate and Persist
Client->>Model: NewPasskeyCredentialFromWebAuthn(webauthn.Credential)
Model->>Model: base64-encode CredentialID, PublicKey, AAGUID
Model->>DB: INSERT PasskeyCredential (strings)
DB-->>Model: OK
Model-->>Client: Stored credential
end
rect rgb(245, 235, 255)
note over Client,Model: Lookup by CredentialID
Client->>Model: GetPasskeyByCredentialID([]byte credID)
Model->>Model: base64-encode credID
Model->>DB: SELECT WHERE CredentialID = encoded
DB-->>Model: PasskeyCredential (strings)
Model-->>Client: Record
end
rect rgb(235, 255, 245)
note over Client,Model: Use in WebAuthn flow
Client->>Model: ToWebAuthnCredential(record)
Model->>Model: base64-decode CredentialID, PublicKey, AAGUID
Model-->>Client: webauthn.Credential
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🧪 Early access (Sonnet 4.5): enabledWe are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience. Note:
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
model/passkey.go(6 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
model/passkey.go (1)
model/main.go (1)
DB(63-63)
🔇 Additional comments (5)
model/passkey.go (5)
4-4: LGTM!The base64 import is correctly added to support the encoding/decoding operations throughout the file.
107-110: LGTM!The base64 encoding operations are correctly implemented. Unlike decoding,
EncodeToStringcannot fail, so no error handling is needed here.
127-130: LGTM!The base64 encoding in the update path is correctly implemented, consistent with the creation path.
165-167: LGTM!The credential lookup correctly encodes the binary credential ID to base64 before querying, ensuring consistency with the stored format.
1-210: Summary: Migration looks good with one critical concern.The migration from binary to base64-encoded strings is well-executed overall, with consistent encoding/decoding across creation, update, and lookup paths. However, the lack of error handling in
ToWebAuthnCredential(lines 82-84) could lead to silent authentication failures if stored data becomes corrupted.Additional considerations:
- Storage overhead: Base64 encoding increases storage size by ~33%. For CredentialID (typically 32-64 bytes) this is acceptable, but monitor if you have a large number of passkeys.
- Performance: String comparison in the uniqueIndex may be slightly slower than binary comparison, though the impact should be negligible for this use case.
- Benefits: The migration to strings makes debugging easier (values are readable) and may improve cross-database compatibility.
| CredentialID string `json:"credential_id" gorm:"type:varchar(512);uniqueIndex;not null"` // base64 encoded | ||
| PublicKey string `json:"public_key" gorm:"type:text;not null"` // base64 encoded | ||
| AttestationType string `json:"attestation_type" gorm:"type:varchar(255)"` | ||
| AAGUID []byte `json:"aaguid" gorm:"type:blob"` | ||
| AAGUID string `json:"aaguid" gorm:"type:varchar(512)"` // base64 encoded |
There was a problem hiding this comment.
🧩 Analysis chain
Verify database migration for field type changes.
The field type changes from []byte to string with GORM column type changes will require a database migration. Ensure that:
- A migration script exists to convert existing binary data to base64-encoded strings
- The migration handles existing records without data loss
- The uniqueIndex on CredentialID is properly maintained during migration
Run the following script to check for migration files:
🏁 Script executed:
#!/bin/bash
# Description: Look for database migration files related to passkey changes
# Search for migration files
fd -e sql -e go migration
# Search for passkey-related migrations
rg -i "passkey.*credential" --type-add 'migration:*.{sql,go}' --type migrationLength of output: 3747
🏁 Script executed:
#!/bin/bash
for file in bin/migration_v0.2-v0.3.sql bin/migration_v0.3-v0.4.sql; do
echo "===== $file ====="
grep -nE "CREATE TABLE|ALTER TABLE|passkey_credentials" "$file" || echo "no relevant statements"
echo
doneLength of output: 127
Add missing database migration for passkey credential type changes
No ALTER/CREATE statements were found in existing migrations to convert credential_id, public_key or aaguid from BLOB/[]byte to string/VARCHAR/TEXT. Add a migration that:
- Alters passkey_credentials columns:
- credential_id → VARCHAR(512)
- public_key → TEXT
- aaguid → VARCHAR(512)
- Converts existing binary data to base64-encoded strings without loss
- Rebuilds the unique index on credential_id
| credID, _ := base64.StdEncoding.DecodeString(p.CredentialID) | ||
| pubKey, _ := base64.StdEncoding.DecodeString(p.PublicKey) | ||
| aaguid, _ := base64.StdEncoding.DecodeString(p.AAGUID) |
There was a problem hiding this comment.
Add error handling for base64 decoding failures.
The base64 decoding operations silently ignore errors, which could lead to authentication failures with empty credential data. If the stored base64 strings are corrupted or invalid, this method will construct a webauthn.Credential with zero-length byte slices, potentially causing hard-to-debug authentication failures.
Consider modifying ToWebAuthnCredential to return an error:
-func (p *PasskeyCredential) ToWebAuthnCredential() webauthn.Credential {
+func (p *PasskeyCredential) ToWebAuthnCredential() (webauthn.Credential, error) {
flags := webauthn.CredentialFlags{
UserPresent: p.UserPresent,
UserVerified: p.UserVerified,
BackupEligible: p.BackupEligible,
BackupState: p.BackupState,
}
- credID, _ := base64.StdEncoding.DecodeString(p.CredentialID)
- pubKey, _ := base64.StdEncoding.DecodeString(p.PublicKey)
- aaguid, _ := base64.StdEncoding.DecodeString(p.AAGUID)
+ credID, err := base64.StdEncoding.DecodeString(p.CredentialID)
+ if err != nil {
+ return webauthn.Credential{}, fmt.Errorf("failed to decode credential ID: %w", err)
+ }
+ pubKey, err := base64.StdEncoding.DecodeString(p.PublicKey)
+ if err != nil {
+ return webauthn.Credential{}, fmt.Errorf("failed to decode public key: %w", err)
+ }
+ aaguid, err := base64.StdEncoding.DecodeString(p.AAGUID)
+ if err != nil {
+ return webauthn.Credential{}, fmt.Errorf("failed to decode AAGUID: %w", err)
+ }
return webauthn.Credential{
ID: credID,
PublicKey: pubKey,
AttestationType: p.AttestationType,
Transport: p.TransportList(),
Flags: flags,
Authenticator: webauthn.Authenticator{
AAGUID: aaguid,
SignCount: p.SignCount,
CloneWarning: p.CloneWarning,
Attachment: protocol.AuthenticatorAttachment(p.Attachment),
},
- }
+ }, nil
}Then update all callers to handle the returned error.
📝 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.
| credID, _ := base64.StdEncoding.DecodeString(p.CredentialID) | |
| pubKey, _ := base64.StdEncoding.DecodeString(p.PublicKey) | |
| aaguid, _ := base64.StdEncoding.DecodeString(p.AAGUID) | |
| func (p *PasskeyCredential) ToWebAuthnCredential() (webauthn.Credential, error) { | |
| flags := webauthn.CredentialFlags{ | |
| UserPresent: p.UserPresent, | |
| UserVerified: p.UserVerified, | |
| BackupEligible: p.BackupEligible, | |
| BackupState: p.BackupState, | |
| } | |
| credID, err := base64.StdEncoding.DecodeString(p.CredentialID) | |
| if err != nil { | |
| return webauthn.Credential{}, fmt.Errorf("failed to decode credential ID: %w", err) | |
| } | |
| pubKey, err := base64.StdEncoding.DecodeString(p.PublicKey) | |
| if err != nil { | |
| return webauthn.Credential{}, fmt.Errorf("failed to decode public key: %w", err) | |
| } | |
| aaguid, err := base64.StdEncoding.DecodeString(p.AAGUID) | |
| if err != nil { | |
| return webauthn.Credential{}, fmt.Errorf("failed to decode AAGUID: %w", err) | |
| } | |
| return webauthn.Credential{ | |
| ID: credID, | |
| PublicKey: pubKey, | |
| AttestationType: p.AttestationType, | |
| Transport: p.TransportList(), | |
| Flags: flags, | |
| Authenticator: webauthn.Authenticator{ | |
| AAGUID: aaguid, | |
| SignCount: p.SignCount, | |
| CloneWarning: p.CloneWarning, | |
| Attachment: protocol.AuthenticatorAttachment(p.Attachment), | |
| }, | |
| }, nil | |
| } |
🤖 Prompt for AI Agents
In model/passkey.go around lines 82 to 84, the base64.Decoding calls ignore
errors which can produce zero-length byte slices and lead to silent
authentication failures; update ToWebAuthnCredential to check each
base64.StdEncoding.DecodeString error, return a descriptive error (wrap with
context, e.g., which field failed) instead of discarding it, and change the
function signature to return (webauthn.Credential, error); then update every
caller to handle the returned error and propagate or log it appropriately.
fix: passkey model type
fix: passkey model type
fix: passkey model type
Summary by CodeRabbit
Bug Fixes
Refactor
Chores