Skip to content

Commit 385d2df

Browse files
committed
feat(auth): add safe multi-RP ID passkey support
Preserve legacy credentials across primary RP ID changes and bind successful assertions to their verified RP ID. Share domain selection across sign-in, login verification and sensitive operations without replaying ceremonies. Save domain settings atomically, preview known and unknown credential impact, and require fresh confirmation before removing domains still in use. Recheck trusted RP IDs when persisting credentials and record safe operation audits. Use read-only configuration snapshots, accept trusted HTTPS intranet hosts, check registration origins early, and preserve historical capitalization. Update settings guidance and all supported translations. Validation: SQLite 3.50.4, MySQL 8.0.46 and PostgreSQL 16.15, including fresh and released-schema upgrade checks; Go regression and race checks; 79 frontend tests; TypeScript, lint, frontend build and root Go build. Security references: OWASP ASVS 5.0.0 V6.3.4 and V6.3.8, Authentication and Session Management Cheat Sheets, and the WebAuthn RP ID/origin rules.
1 parent 74629e2 commit 385d2df

48 files changed

Lines changed: 3272 additions & 433 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎controller/audit.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package controller
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
7+
"strings"
68

79
"github.com/QuantumNous/new-api/common"
810
"github.com/QuantumNous/new-api/constant"
@@ -43,6 +45,11 @@ var auditContentTemplates = map[string]string{
4345
"user.reset_passkey": "Reset the user passkey",
4446
"option.update": "Updated system setting ${key}",
4547

48+
"option.passkey_domains": "Updated Passkey domains: removed ${domains}; affected ${known}; unknown ${unknown}",
49+
"option.passkey_domains_confirmed": "Confirmed removal of Passkey domains: ${domains}; affected ${known}; unknown ${unknown}",
50+
"option.passkey_domains_blocked": "Passkey domain change blocked: ${domains}; affected ${known}; unknown ${unknown}",
51+
"option.passkey_domains_failed": "Passkey domain update failed",
52+
4653
"channel.create": "Created channel ${name} (type ${type}, count ${count})",
4754
"channel.update": "Updated channel ${name} (ID: ${id})",
4855
"channel.delete": "Deleted channel ${name} (ID: ${id})",
@@ -65,6 +72,33 @@ var auditContentTemplates = map[string]string{
6572
"subscription.user_plan_reset": "Reset active plan ${plan_id} subscriptions for user ${target_user_id}",
6673
}
6774

75+
func recordPasskeyDomainAudit(c *gin.Context, change *model.PasskeyDomainChange, confirmed bool, err error) {
76+
confirmed = confirmed && err == nil && change != nil && len(change.RemovedRPIDs) > 0
77+
params := map[string]any{"success": err == nil, "confirmed": confirmed}
78+
if change != nil {
79+
params["domains"] = strings.Join(change.RemovedRPIDs, ", ")
80+
params["removed_rp_ids"] = change.RemovedRPIDs
81+
params["known"] = change.AffectedCredentials
82+
params["unknown"] = change.UnknownCredentials
83+
params["previous_rp_id"] = change.PreviousRPID
84+
params["effective_rp_id"] = change.EffectiveRPID
85+
}
86+
action := "option.passkey_domains"
87+
if errors.Is(err, model.ErrPasskeyDomainRemovalConfirmation) {
88+
action = "option.passkey_domains_blocked"
89+
} else if err != nil {
90+
action = "option.passkey_domains_failed"
91+
} else if confirmed && change != nil && len(change.RemovedRPIDs) > 0 {
92+
action = "option.passkey_domains_confirmed"
93+
}
94+
auditInfo := &model.AuditRequestInfo{
95+
Method: c.Request.Method, Route: c.FullPath(), Path: c.FullPath(),
96+
Status: c.Writer.Status(), Success: err == nil,
97+
}
98+
model.RecordOperationAuditLog(c.GetInt("id"), c.GetInt("role"), auditContentEN(action, params), c.ClientIP(), action, params, auditOperatorInfo(c), auditInfo, c)
99+
markAuditLogged(c)
100+
}
101+
68102
// auditContentEN 按 action 模板渲染英文兜底文本;未登记的 action 退回 action 本身。
69103
func auditContentEN(action string, params map[string]any) string {
70104
tmpl, ok := auditContentTemplates[action]

‎controller/login_verification.go‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func VerifyLogin(c *gin.Context) {
4141
func LoginPasskeyBegin(c *gin.Context) {
4242
var request struct {
4343
FlowToken string `json:"flow_token"`
44+
RPID string `json:"rp_id"`
4445
}
4546
if common.DecodeJson(c.Request.Body, &request) != nil || request.FlowToken == "" {
4647
common.ApiErrorMsg(c, "参数错误")
@@ -56,7 +57,11 @@ func LoginPasskeyBegin(c *gin.Context) {
5657
writeSecurityOperationError(c, err)
5758
return
5859
}
59-
wa, err := passkeysvc.BuildWebAuthn(c.Request)
60+
credentialRPID := ""
61+
if credential.RPID != nil {
62+
credentialRPID = *credential.RPID
63+
}
64+
wa, rpIDs, err := passkeysvc.BuildLoginWebAuthn(c.Request, request.RPID, credentialRPID)
6065
if err != nil {
6166
writeSecurityOperationError(c, err)
6267
return
@@ -75,7 +80,7 @@ func LoginPasskeyBegin(c *gin.Context) {
7580
writeSecurityOperationError(c, err)
7681
return
7782
}
78-
common.ApiSuccess(c, gin.H{"flow_token": token, "expires_at": expiresAt, "options": options})
83+
common.ApiSuccess(c, gin.H{"flow_token": token, "expires_at": expiresAt, "options": options, "rp_ids": rpIDs})
7984
}
8085

8186
func LoginPasskeyFinish(c *gin.Context) {
@@ -104,6 +109,7 @@ func LoginPasskeyFinish(c *gin.Context) {
104109
writeSecurityOperationError(c, err)
105110
return
106111
}
112+
c.Set("passkey_rp_id", sessionData.RelyingPartyID)
107113
if security.LoginFlowID != verification.Flow.Id || sessionData.UserVerification != protocol.VerificationRequired {
108114
writeSecurityOperationError(c, model.ErrAuthFlowInvalid)
109115
return
@@ -113,17 +119,21 @@ func LoginPasskeyFinish(c *gin.Context) {
113119
writeSecurityOperationError(c, err)
114120
return
115121
}
116-
wa, err := passkeysvc.BuildWebAuthn(c.Request)
122+
wa, err := passkeysvc.BuildWebAuthnForRPID(c.Request, sessionData.RelyingPartyID)
117123
if err != nil {
118124
writeSecurityOperationError(c, err)
119125
return
120126
}
127+
if credential.RPID != nil && *credential.RPID != "" && *credential.RPID != sessionData.RelyingPartyID {
128+
writeSecurityOperationError(c, service.ErrVerificationFailed)
129+
return
130+
}
121131
validated, err := wa.ValidateLogin(passkeysvc.NewWebAuthnUser(&model.User{Id: identity.UserID}, credential), *sessionData, parsed)
122132
if err != nil {
123133
writeSecurityOperationError(c, err)
124134
return
125135
}
126-
if err := model.UpdatePasskeyAssertionState(identity.UserID, validated, time.Now()); err != nil {
136+
if err := model.UpdatePasskeyAssertionState(identity.UserID, validated, time.Now(), sessionData.RelyingPartyID); err != nil {
127137
writeSecurityOperationError(c, err)
128138
return
129139
}

‎controller/misc.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func TestStatus(c *gin.Context) {
4444
func GetStatus(c *gin.Context) {
4545

4646
cs := console_setting.GetConsoleSetting()
47+
passkeySetting := system_setting.PasskeySettingsSnapshot()
4748
common.OptionMapRWMutex.RLock()
4849
defer common.OptionMapRWMutex.RUnlock()
4950

50-
passkeySetting := system_setting.GetPasskeySettings()
5151
legalSetting := system_setting.GetLegalSettings()
5252

5353
data := gin.H{
@@ -117,7 +117,8 @@ func GetStatus(c *gin.Context) {
117117
"oidc_display_name": system_setting.GetOIDCSettings().GetEffectiveDisplayName(),
118118
"passkey_login": passkeySetting.Enabled,
119119
"passkey_display_name": passkeySetting.RPDisplayName,
120-
"passkey_rp_id": passkeySetting.RPID,
120+
"passkey_rp_id": passkeySetting.EffectiveRPID(),
121+
"passkey_rp_ids": passkeySetting.RelyingPartyIDs(),
121122
"passkey_origins": passkeySetting.Origins,
122123
"passkey_allow_insecure": passkeySetting.AllowInsecureOrigin,
123124
"passkey_user_verification": passkeySetting.UserVerification,

‎controller/option.go‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67
"slices"
@@ -135,6 +136,50 @@ type OptionUpdateRequest struct {
135136
Value any `json:"value"`
136137
}
137138

139+
func UpdatePasskeyDomains(c *gin.Context) {
140+
var request struct {
141+
RPID *string `json:"rp_id"`
142+
LegacyRPIDs *string `json:"legacy_rp_ids"`
143+
Origins *string `json:"origins"`
144+
Preview bool `json:"preview"`
145+
RemovalConfirmation string `json:"removal_confirmation"`
146+
}
147+
if err := common.DecodeJson(c.Request.Body, &request); err != nil || request.RPID == nil || request.LegacyRPIDs == nil || request.Origins == nil {
148+
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
149+
return
150+
}
151+
change, err := model.UpdatePasskeyDomainOptions(map[string]string{
152+
"passkey.rp_id": *request.RPID, "passkey.legacy_rp_ids": *request.LegacyRPIDs, "passkey.origins": *request.Origins,
153+
}, request.Preview, request.RemovalConfirmation)
154+
if err != nil {
155+
writePasskeyDomainSettingsError(c, err)
156+
if !request.Preview {
157+
recordPasskeyDomainAudit(c, change, request.RemovalConfirmation != "", err)
158+
}
159+
return
160+
}
161+
if !request.Preview {
162+
recordPasskeyDomainAudit(c, change, request.RemovalConfirmation != "", nil)
163+
}
164+
common.ApiSuccess(c, change)
165+
}
166+
167+
func writePasskeyDomainSettingsError(c *gin.Context, err error) {
168+
var removal *model.PasskeyDomainRemovalError
169+
if errors.As(err, &removal) {
170+
c.JSON(http.StatusConflict, gin.H{
171+
"success": false, "code": "PASSKEY_RP_ID_REMOVAL_CONFIRMATION_REQUIRED",
172+
"message": i18n.T(c, i18n.MsgPasskeyRPIDRemovalConfirmation), "data": removal.Change,
173+
})
174+
return
175+
}
176+
if errors.Is(err, system_setting.ErrPasskeyRPIDInvalid) {
177+
writeSecurityOperationError(c, err)
178+
return
179+
}
180+
common.ApiError(c, err)
181+
}
182+
138183
func UpdateOption(c *gin.Context) {
139184
var option OptionUpdateRequest
140185
err := common.DecodeJson(c.Request.Body, &option)
@@ -431,9 +476,24 @@ func UpdateOption(c *gin.Context) {
431476
return
432477
}
433478
}
479+
if model.IsPasskeyDomainOption(option.Key) {
480+
change, updateErr := model.UpdatePasskeyDomainOptions(map[string]string{option.Key: option.Value.(string)}, false, "")
481+
if updateErr != nil {
482+
writePasskeyDomainSettingsError(c, updateErr)
483+
recordPasskeyDomainAudit(c, change, false, updateErr)
484+
return
485+
}
486+
recordPasskeyDomainAudit(c, change, false, nil)
487+
common.ApiSuccess(c, change)
488+
return
489+
}
434490
err = model.UpdateOption(option.Key, option.Value.(string))
435491
if err != nil {
436-
common.ApiError(c, err)
492+
if errors.Is(err, system_setting.ErrPasskeyRPIDInvalid) {
493+
writeSecurityOperationError(c, err)
494+
} else {
495+
common.ApiError(c, err)
496+
}
437497
return
438498
}
439499
// 出于安全考虑只记录被修改的配置项名称,不记录配置值(可能含密钥等敏感信息)。

0 commit comments

Comments
 (0)