跳到主要内容
ecdsa.com
WebAuthn navigator.credentials.create() / get()WebAuthn 与通行密钥

错误信息

SecurityError: The relying party ID is not a registrable domain suffix of, nor equal to the current domain. Subsequently, an attempt to fetch the .well-known/webauthn resource of the claimed RP ID failed.

也可能输出为

  • SecurityError: The relying party ID is not a registrable domain suffix of, nor equal to the current domain.

不支持相关源请求的 Chromium 版本,句子在“…nor equal to the current domain.”之后就结束了。其他浏览器的措辞不同;DOMException 名称 `SecurityError` 是固定不变的部分。

WebAuthn navigator.credentials.create() / get() — DOMException SecurityError (Chromium)

含义

你在选项中设置的 RP ID 既不是页面自身的主机名,也不是它的可注册上级域名,因此浏览器拒绝为它创建或使用凭证。通行密钥绑定在其 RP ID 上;如果任何页面都能声称任意 RP ID,一个站点就能使用另一个站点的通行密钥。Chromium 还尝试了 WebAuthn 相关源请求(Related Origin Requests)机制(获取 `https://<rp-id>/.well-known/webauthn`,查看该 RP ID 是否列出了你的源),但这也没有让请求获得授权。

原因

修复方法

  1. 1.

    按环境配置 RP ID

    把 RP ID 和预期的源一起放在环境配置中,让开发、预览和生产环境各自使用自己的主机——并且记住,通行密钥不会在不同的 RP ID 之间迁移。

    js
    // Server: derive the RP ID from configuration per environment, never hard-code production.
    const rpID = process.env.WEBAUTHN_RP_ID;          // "localhost" in dev, "example.com" in production
    const expectedOrigin = process.env.WEBAUTHN_ORIGIN; // "http://localhost:3000" / "https://app.example.com"
    const options = await generateRegistrationOptions({ rpName: "Example", rpID, userName });
  2. 2.

    明确发布相关源

    如果你的多个域名必须共享通行密钥,就在 RP ID 所在的域名上提供 `/.well-known/webauthn`,并在其中列出这些域名(当前的 Chromium 和 Safari 支持这一机制)。

    json
    // https://example.com/.well-known/webauthn  (served as application/json)
    {
      "origins": ["https://example.co.uk", "https://example-shop.com"]
    }

相关错误

浏览完整的签名错误库