Files
CaIon 385d2dfd10 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.
2026-09-11 23:15:29 +08:00

170 lines
5.2 KiB
Go

package system_setting
import (
"errors"
"net"
"net/url"
"slices"
"strings"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/setting/config"
"golang.org/x/net/idna"
"golang.org/x/net/publicsuffix"
)
type PasskeySettings struct {
Enabled bool `json:"enabled"`
RPDisplayName string `json:"rp_display_name"`
RPID string `json:"rp_id"`
LegacyRPIDs string `json:"legacy_rp_ids"`
Origins string `json:"origins"`
AllowInsecureOrigin bool `json:"allow_insecure_origin"`
UserVerification string `json:"user_verification"`
AttachmentPreference string `json:"attachment_preference"`
}
var ErrPasskeyRPIDInvalid = errors.New("Invalid Passkey domain. Enter a domain without a scheme, port, path or wildcard.")
var ErrPasskeyRPIDUnavailable = errors.New("This Passkey domain is not available on this website. Use its original website or another verification method.")
// PasskeySettingsSnapshot includes the effective defaults without sharing a
// mutable settings pointer with a WebAuthn ceremony.
func PasskeySettingsSnapshot() PasskeySettings {
common.OptionMapRWMutex.RLock()
settings, serverAddress := defaultPasskeySettings, ServerAddress
common.OptionMapRWMutex.RUnlock()
return settings.WithDefaults(serverAddress)
}
// EffectiveRPID preserves the existing default and port handling for settings
// created before explicit Passkey domains were introduced.
func (s PasskeySettings) EffectiveRPID() string {
rpID := strings.TrimSpace(s.RPID)
if rpID == "" {
for origin := range strings.SplitSeq(s.Origins, ",") {
if parsed, err := url.Parse(strings.TrimSpace(origin)); err == nil && parsed.Host != "" {
rpID = parsed.Host
break
}
}
}
if host, _, err := net.SplitHostPort(rpID); err == nil {
return host
}
return rpID
}
func NormalizePasskeyRPID(value string, configuredOrigins ...string) (string, error) {
rpID, err := idna.Lookup.ToASCII(strings.TrimSpace(value))
rpID = strings.ToLower(rpID)
if err != nil || rpID == "" || len(rpID) > 253 || strings.ContainsAny(rpID, ":/*@?#\\") || net.ParseIP(rpID) != nil {
return "", ErrPasskeyRPIDInvalid
}
for label := range strings.SplitSeq(rpID, ".") {
if label == "" || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
return "", ErrPasskeyRPIDInvalid
}
for _, c := range label {
if c != '-' && (c < 'a' || c > 'z') && (c < '0' || c > '9') {
return "", ErrPasskeyRPIDInvalid
}
}
}
if rpID == "localhost" {
return rpID, nil
}
if _, err := publicsuffix.EffectiveTLDPlusOne(rpID); err == nil {
return rpID, nil
}
// An unknown single-label hostname is usable only at its exact, explicitly
// trusted HTTPS origin, never as a suffix of other hosts.
_, icann := publicsuffix.PublicSuffix(rpID)
if icann || strings.Contains(rpID, ".") {
return "", ErrPasskeyRPIDInvalid
}
for _, origins := range configuredOrigins {
for origin := range strings.SplitSeq(origins, ",") {
parsed, err := url.Parse(strings.TrimSpace(origin))
if err != nil || parsed.Scheme != "https" || parsed.User != nil || (parsed.Path != "" && parsed.Path != "/") || parsed.RawQuery != "" || parsed.Fragment != "" {
continue
}
if strings.EqualFold(parsed.Hostname(), rpID) {
return rpID, nil
}
}
}
return "", ErrPasskeyRPIDInvalid
}
func ParsePasskeyRPIDs(value string, configuredOrigins ...string) ([]string, error) {
ids := []string{}
for _, value := range strings.FieldsFunc(value, func(c rune) bool { return c == ',' || c == '\n' || c == '\r' }) {
if strings.TrimSpace(value) == "" {
continue
}
_, err := NormalizePasskeyRPID(value, configuredOrigins...)
if err != nil {
return nil, err
}
// Legacy credentials hash the exact RP ID supplied at registration.
// Validate the domain without changing that historical value.
id := strings.TrimSpace(value)
if !slices.Contains(ids, id) {
ids = append(ids, id)
}
}
return ids, nil
}
func (s PasskeySettings) RelyingPartyIDs() []string {
ids := []string{}
if primary := s.EffectiveRPID(); primary != "" {
ids = append(ids, primary)
}
legacy, err := ParsePasskeyRPIDs(s.LegacyRPIDs, s.Origins)
if err != nil {
return ids
}
for _, id := range legacy {
if !slices.Contains(ids, id) {
ids = append(ids, id)
}
}
return ids
}
var defaultPasskeySettings = PasskeySettings{
Enabled: false,
RPDisplayName: common.SystemName,
RPID: "",
Origins: "",
AllowInsecureOrigin: false,
UserVerification: "preferred",
AttachmentPreference: "",
}
func init() {
config.GlobalConfig.Register("passkey", &defaultPasskeySettings)
}
func GetPasskeySettings() *PasskeySettings {
return &defaultPasskeySettings
}
func (s PasskeySettings) WithDefaults(serverAddress string) PasskeySettings {
if s.RPID == "" && serverAddress != "" {
// 从ServerAddress提取域名作为RPID
// ServerAddress可能是 "https://newapi.pro" 这种格式
serverAddr := strings.TrimSpace(serverAddress)
if parsed, err := url.Parse(serverAddr); err == nil && parsed.Host != "" {
s.RPID = parsed.Host
} else {
s.RPID = serverAddr
}
}
if s.Origins == "" || s.Origins == "[]" {
s.Origins = serverAddress
}
return s
}