mirror of
https://github.com/we-promise/sure.git
synced 2026-09-02 05:11:05 +00:00
* feat(auth): sign in with a passkey, without a password Passkeys could only ever replace the TOTP code: registration required 2FA to already be on, and the WebAuthn ceremony was reachable only after User.authenticate_by had succeeded. A registered passkey can now complete sign-in on its own, from the login page. The ceremony requests userVerification: "required", so the authenticator has to confirm the person as well as the device. That makes a lone passkey two independent factors, the same bar as the password plus TOTP flow it replaces, which is why this path deliberately skips the TOTP step. A credential that can only prove presence is rejected here and still works as a second factor. Sign-in is usernameless: no email is submitted, because the browser returns the account handle with the assertion. Nothing on this path can be probed to learn whether an account exists. Registration now asks for a discoverable credential with residentKey: "preferred" so the key is offered by the picker, while authenticators without a free resident-key slot still register as a second factor. Where conditional mediation is available, saved passkeys appear in the email field's autofill menu; everywhere else the button covers it. The automatic challenge request that conditional mediation makes on every page load gets its own looser Rack::Attack budget, so ordinary page views can no longer exhaust the limit that protects the MFA endpoints. Set AUTH_PASSKEY_LOGIN_ENABLED=false to keep passkeys as a second factor only. Passkey sign-in follows the same policy as local login, so it stays closed to regular users when AUTH_LOCAL_LOGIN_ENABLED is false. * refactor(auth): group the passkey button with the other sign-in methods It sat directly under the password fields, so the forgot-password link split it from the identical SSO buttons. It is an alternative to the credential form rather than part of it. * fix(auth): close the passkey challenge races and document the upgrade Three review passes converged on the conditional-mediation flow. The AbortController was created after `isConditionalMediationAvailable()` resolved, so a button click or a Turbo disconnect landing in that window found nothing to abort: the conditional task carried on, re-minted the challenge, and the assertion the user was about to produce verified against a challenge the server had already replaced. It is created before the first await now, and held in a local, because `abortConditionalMediation()` nulls the field. Checking that one signal after each await covers both triggers, so no separate connected flag is needed. The same symptom had a second cause nobody flagged: `authenticate()` was not re-entrant. A double-click minted a fresh challenge under an open authenticator prompt and rejected a perfectly valid passkey, with no race window at all — and it was live on the MFA step-up too, which shares the method. The conditional catch was silent for every failure, including a rejected assertion the user had deliberately chosen from the autofill menu. Splitting the try draws the line where it belongs: silence before the user has been asked anything, feedback once they have picked a passkey. Filtering on `error.name` cannot draw it, since `fetchOptions` and `verifyCredential` both raise a plain Error. Also documents the upgrade: passwordless is on by default and applies to already-registered credentials, so a passkey added purely as a second factor can now sign its owner in alone. Nothing in the schema marks a credential discoverable — the authenticator decides — and the opt-out is instance-wide. The invitation test is a guard, not coverage for this change. The pending token lives in the Rack session and `complete_sign_in` reads it right after creating the session, so a `reset_session` dropped in between strands the invitee in their own family, silently and with every existing test still green. * fix(auth): cancel the in-flight conditional options request Aborting the conditional flow did not cancel its options request, because `fetchOptions` never received the signal. A click landing while that POST was in flight left it to finish, and its response could apply last. The challenge rides in the session cookie, so "the server wrote it" only counts if the Set-Cookie reaches the browser. Threading the signal means an aborted request's response is discarded, which closes the window without needing the server to hold two challenges open. Also drops the absolute claim about which existing credentials gain passwordless sign-in. `residentKey: "preferred"` is a request an authenticator may decline, and nothing records what it decided, so the honest statement is that password managers and platform authenticators generally store discoverable credentials rather than always.
176 lines
6.1 KiB
Ruby
176 lines
6.1 KiB
Ruby
require "test_helper"
|
|
require "webauthn/fake_client"
|
|
|
|
class Settings::WebauthnCredentialsControllerTest < ActionDispatch::IntegrationTest
|
|
setup do
|
|
@user = users(:family_admin)
|
|
@user.webauthn_credentials.destroy_all
|
|
sign_in @user
|
|
@user.setup_mfa!
|
|
@user.enable_mfa!
|
|
@client = WebAuthn::FakeClient.new("http://www.example.com")
|
|
end
|
|
|
|
test "options require enabled MFA" do
|
|
@user.disable_mfa!
|
|
|
|
post options_settings_webauthn_credentials_path, as: :json
|
|
|
|
assert_response :forbidden
|
|
assert_equal I18n.t("webauthn_credentials.mfa_required"), JSON.parse(response.body).fetch("error")
|
|
end
|
|
|
|
test "asks for a discoverable credential so it can be used for passwordless sign-in" do
|
|
options = registration_options
|
|
|
|
assert_equal "preferred", options.dig("authenticatorSelection", "residentKey")
|
|
assert_equal "preferred", options.dig("authenticatorSelection", "userVerification")
|
|
end
|
|
|
|
test "creates a credential from a verified registration challenge" do
|
|
options = registration_options
|
|
credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")
|
|
|
|
assert_difference -> { @user.webauthn_credentials.count }, 1 do
|
|
post settings_webauthn_credentials_path, params: {
|
|
webauthn_credential: { nickname: "MacBook Touch ID" },
|
|
credential: credential
|
|
}, as: :json
|
|
end
|
|
|
|
assert_response :success
|
|
assert_equal settings_security_path, JSON.parse(response.body).fetch("redirect_url")
|
|
|
|
stored_credential = @user.webauthn_credentials.reload.last
|
|
assert_equal "MacBook Touch ID", stored_credential.nickname
|
|
assert_equal credential.fetch("id"), stored_credential.credential_id
|
|
assert_includes stored_credential.transports, "internal"
|
|
assert @user.reload.webauthn_id.present?
|
|
end
|
|
|
|
test "uses configured relying party id and allowed origin" do
|
|
with_webauthn_config(rp_id: "example.test", allowed_origins: [ "https://app.example.test" ]) do
|
|
client = WebAuthn::FakeClient.new("https://app.example.test")
|
|
options = registration_options
|
|
|
|
assert_equal "example.test", options.dig("rp", "id")
|
|
|
|
credential = client.create(challenge: options.fetch("challenge"), rp_id: "example.test")
|
|
|
|
assert_difference -> { @user.webauthn_credentials.count }, 1 do
|
|
post settings_webauthn_credentials_path, params: {
|
|
webauthn_credential: { nickname: "Configured origin key" },
|
|
credential: credential
|
|
}, as: :json
|
|
end
|
|
|
|
assert_response :success
|
|
end
|
|
end
|
|
|
|
test "rejects a credential when registration challenge has already been used" do
|
|
options = registration_options
|
|
credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")
|
|
|
|
post settings_webauthn_credentials_path, params: {
|
|
webauthn_credential: { nickname: "MacBook Touch ID" },
|
|
credential: credential
|
|
}, as: :json
|
|
assert_response :success
|
|
|
|
assert_no_difference -> { @user.webauthn_credentials.count } do
|
|
post settings_webauthn_credentials_path, params: {
|
|
webauthn_credential: { nickname: "Replay" },
|
|
credential: credential
|
|
}, as: :json
|
|
end
|
|
|
|
assert_response :unprocessable_entity
|
|
end
|
|
|
|
test "rejects malformed credential payloads" do
|
|
registration_options
|
|
|
|
assert_no_difference -> { @user.webauthn_credentials.count } do
|
|
post settings_webauthn_credentials_path, params: {
|
|
webauthn_credential: { nickname: "Malformed" },
|
|
credential: []
|
|
}, as: :json
|
|
end
|
|
|
|
assert_response :unprocessable_entity
|
|
assert_equal I18n.t("webauthn_credentials.failure"), JSON.parse(response.body).fetch("error")
|
|
end
|
|
|
|
test "rejects database-level duplicate credential races" do
|
|
registration_options
|
|
@user.webauthn_credentials.create!(
|
|
nickname: "Existing security key",
|
|
credential_id: "duplicate-credential-id",
|
|
public_key: "public-key"
|
|
)
|
|
|
|
verified_credential = Struct.new(:id, :public_key, :sign_count).new("duplicate-credential-id", "new-public-key", 0)
|
|
relying_party = mock("webauthn_relying_party")
|
|
relying_party.expects(:verify_registration).returns(verified_credential)
|
|
Settings::WebauthnCredentialsController.any_instance.stubs(:webauthn_relying_party).returns(relying_party)
|
|
|
|
assert_no_difference -> { @user.webauthn_credentials.count } do
|
|
post settings_webauthn_credentials_path, params: {
|
|
webauthn_credential: { nickname: "Duplicate security key" },
|
|
credential: { id: "duplicate-credential-id", response: {} }
|
|
}, as: :json
|
|
end
|
|
|
|
assert_response :unprocessable_entity
|
|
assert_equal I18n.t("webauthn_credentials.failure"), JSON.parse(response.body).fetch("error")
|
|
end
|
|
|
|
test "uses localized default credential nickname" do
|
|
options = registration_options
|
|
credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")
|
|
|
|
post settings_webauthn_credentials_path, params: {
|
|
webauthn_credential: { nickname: "" },
|
|
credential: credential
|
|
}, as: :json
|
|
|
|
assert_response :success
|
|
assert_equal I18n.t("webauthn_credentials.default_name"), @user.webauthn_credentials.reload.last.nickname
|
|
end
|
|
|
|
test "destroys a credential owned by the current user" do
|
|
credential = @user.webauthn_credentials.create!(
|
|
nickname: "YubiKey",
|
|
credential_id: "credential-to-delete",
|
|
public_key: "public-key"
|
|
)
|
|
|
|
assert_difference -> { @user.webauthn_credentials.count }, -1 do
|
|
delete settings_webauthn_credential_path(credential)
|
|
end
|
|
|
|
assert_redirected_to settings_security_path
|
|
end
|
|
|
|
private
|
|
def registration_options
|
|
post options_settings_webauthn_credentials_path, as: :json
|
|
assert_response :success
|
|
JSON.parse(response.body)
|
|
end
|
|
|
|
def with_webauthn_config(rp_id:, allowed_origins:)
|
|
config = Rails.application.config.x.webauthn
|
|
previous_rp_id = config.rp_id
|
|
previous_allowed_origins = config.allowed_origins
|
|
config.rp_id = rp_id
|
|
config.allowed_origins = allowed_origins
|
|
|
|
yield
|
|
ensure
|
|
config.rp_id = previous_rp_id
|
|
config.allowed_origins = previous_allowed_origins
|
|
end
|
|
end
|