Skip to content

fix: passkey model type - #1930

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:feature/passkey
Sep 30, 2025
Merged

seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:feature/passkey

Conversation

@seefs001

@seefs001 seefs001 commented Sep 30, 2025 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of passkey sign-in by resolving encoding-related recognition issues.
    • Enhanced accuracy when looking up credentials during authentication.
  • Refactor

    • Updated internal storage format for passkey data to improve compatibility and data integrity across environments.
  • Chores

    • Streamlined data handling for passkey registration and authentication to reduce edge-case errors and improve stability.

@coderabbitai

coderabbitai Bot commented Sep 30, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This 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

Cohort / File(s) Summary
Passkey model and conversions
model/passkey.go
Changed PasskeyCredential fields from []byte to string with new GORM types; added base64 encode/decode in NewPasskeyCredentialFromWebAuthn, ApplyValidatedCredential, ToWebAuthnCredential; updated GetPasskeyByCredentialID to query with base64-encoded ID; imported encoding/base64.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I nibbled some bytes, then strings I chose,
Wrapped them in base64 bows.
Into the DB they safely hop,
Out they decode with a gentle plop.
Credentials snug in their cozy pen—
Thump! says the rabbit, “It works again.” 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly indicates that the pull request fixes the model type for passkeys, which corresponds directly to updating the PasskeyCredential fields from byte arrays to base64-encoded strings. It references the key area of change (the passkey model) without unnecessary detail, keeping it concise. This makes it clear to reviewers that the core change involves correcting the model’s field types.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🧪 Early access (Sonnet 4.5): enabled

We 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:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9f20e49 and ab16664.

📒 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, EncodeToString cannot 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:

  1. 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.
  2. Performance: String comparison in the uniqueIndex may be slightly slower than binary comparison, though the impact should be negligible for this use case.
  3. Benefits: The migration to strings makes debugging easier (values are readable) and may improve cross-database compatibility.

Comment thread model/passkey.go
Comment on lines +25 to +28
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 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:

  1. A migration script exists to convert existing binary data to base64-encoded strings
  2. The migration handles existing records without data loss
  3. 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 migration

Length 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
done

Length 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

Comment thread model/passkey.go
Comment on lines +82 to +84
credID, _ := base64.StdEncoding.DecodeString(p.CredentialID)
pubKey, _ := base64.StdEncoding.DecodeString(p.PublicKey)
aaguid, _ := base64.StdEncoding.DecodeString(p.AAGUID)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

Suggested change
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.

@seefs001
seefs001 merged commit d026edc into QuantumNous:main Sep 30, 2025
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants