Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
15.9 KB
Raw Blame
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", code: @code },
        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 "creating a credential writes an audit log entry" do
    options = registration_options
    credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")

    assert_difference "SecurityAuditLog.count", 1 do
      post settings_webauthn_credentials_path, params: {
        webauthn_credential: { nickname: "MacBook Touch ID", code: @code },
        credential: credential
      }, as: :json
    end

    log = SecurityAuditLog.last
    stored_credential = @user.webauthn_credentials.reload.last
    assert_equal "webauthn_credential_added", log.event_type
    assert_equal stored_credential.id, log.metadata["credential_id"]
    assert_equal "MacBook Touch ID", log.metadata["nickname"]
  end

  test "does not persist the credential when the audit log write fails" do
    options = registration_options
    credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")
    SecurityAuditLog.stubs(:log_webauthn_credential_added!).raises(ActiveRecord::RecordInvalid.new(SecurityAuditLog.new))

    assert_no_difference -> { @user.webauthn_credentials.count } do
      post settings_webauthn_credentials_path, params: {
        webauthn_credential: { nickname: "MacBook Touch ID", code: @code },
        credential: credential
      }, as: :json
    end

    assert_response :unprocessable_entity
  end

  test "still destroys the credential when the audit log write fails" do
    # Log-and-continue, not transactional: a failed audit write must not
    # block removing a security key the user believes is compromised.
    credential = @user.webauthn_credentials.create!(
      nickname: "YubiKey",
      credential_id: "credential-audit-fail",
      public_key: "public-key"
    )
    SecurityAuditLog.stubs(:log_webauthn_credential_removed!).raises(ActiveRecord::RecordInvalid.new(SecurityAuditLog.new))

    assert_difference -> { @user.webauthn_credentials.count }, -1 do
      delete settings_webauthn_credential_path(credential)
    end

    assert_redirected_to settings_security_path
    assert_not SecurityAuditLog.exists?(user: @user, event_type: "webauthn_credential_removed")
  end

  test "a rejected registration does not write an audit log" do
    registration_options

    assert_no_difference "SecurityAuditLog.count" do
      post settings_webauthn_credentials_path, params: {
        webauthn_credential: { nickname: "Malformed" },
        credential: []
      }, as: :json
    end
  end

  test "registering a credential requires an MFA code" do
    [ nil, "", "000000" ].each do |code|
      options = registration_options
      credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")

      assert_no_difference -> { @user.webauthn_credentials.count } do
        register(credential, code: code)
      end

      assert_response :unprocessable_entity
      assert_equal I18n.t("webauthn_credentials.invalid_code"), JSON.parse(response.body).fetch("error")
    end
  end

  test "registering a credential accepts a current authenticator code" do
    travel_to Time.zone.parse("2026-10-07 12:00:10") do
      options = registration_options
      credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")

      assert_difference -> { @user.webauthn_credentials.count }, 1 do
        register(credential, code: totp_code)
      end

      assert_response :success
    end
  end

  test "an authenticator code that was already used is reported as such" do
    travel_to Time.zone.parse("2026-10-07 12:00:10") do
      code = totp_code
      assert @user.verify_otp?(code)

      options = registration_options
      credential = @client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com")

      assert_no_difference -> { @user.webauthn_credentials.count } do
        register(credential, code: code)
      end

      assert_response :unprocessable_entity
      assert_equal I18n.t("webauthn_credentials.code_already_used"), JSON.parse(response.body).fetch("error")
    end
  end

  test "a backup code confirms only one registration" do
    code = backup_code

    options = registration_options(code: code)
    register(@client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com"), code: code)
    assert_response :success

    post options_settings_webauthn_credentials_path, params: { webauthn_credential: { code: code } }, as: :json
    assert_response :unprocessable_entity
    assert_equal I18n.t("webauthn_credentials.invalid_code"), JSON.parse(response.body).fetch("error")
  end

  test "options refuse a missing or wrong code before the browser creates a passkey" do
    [ nil, "", "000000" ].each do |code|
      post options_settings_webauthn_credentials_path, params: { webauthn_credential: { code: code } }, as: :json

      assert_response :unprocessable_entity
      assert_equal I18n.t("webauthn_credentials.invalid_code"), JSON.parse(response.body).fetch("error")
      assert_nil session[:webauthn_registration_challenge]
    end
  end

  test "options report an authenticator code that was already used" do
    travel_to Time.zone.parse("2026-10-07 12:00:10") do
      code = totp_code
      assert @user.verify_otp?(code)

      post options_settings_webauthn_credentials_path, params: { webauthn_credential: { code: code } }, as: :json

      assert_response :unprocessable_entity
      assert_equal I18n.t("webauthn_credentials.code_already_used"), JSON.parse(response.body).fetch("error")
    end
  end

  test "options check the code without spending it" do
    code = backup_code

    registration_options(code: code)

    assert @user.reload.verify_otp?(code), "backup code should still be unused after options"
  end

  test "a registration that fails does not spend the backup code" do
    code = backup_code

    stale_options = registration_options(code: code)
    registration_options(code: code)
    stale_credential = @client.create(challenge: stale_options.fetch("challenge"), rp_id: "www.example.com")
    register(stale_credential, code: code)
    assert_response :unprocessable_entity
    assert_equal I18n.t("webauthn_credentials.failure"), JSON.parse(response.body).fetch("error")

    options = registration_options(code: code)
    assert_difference -> { @user.webauthn_credentials.count }, 1 do
      register(@client.create(challenge: options.fetch("challenge"), rp_id: "www.example.com"), code: code)
    end
    assert_response :success
  end

  test "a duplicate credential does not spend the backup code" do
    code = backup_code
    registration_options(code: code)
    @user.webauthn_credentials.create!(nickname: "Existing", 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)

    register({ id: "duplicate-credential-id", response: {} }, code: code)
    assert_response :unprocessable_entity

    assert @user.reload.verify_otp?(code), "backup code should still be unused"
  end

  test "code attempts are rate limited per user" do
    registration_options
    Settings::WebauthnCredentialsController.cache_store.stubs(:increment).returns(11)

    register({ id: "x", response: {} }, code: "000000")

    assert_response :too_many_requests
    assert_equal I18n.t("webauthn_credentials.rate_limited"), JSON.parse(response.body).fetch("error")
  end

  test "guessing codes through options alone is limited to 10 a minute" do
    # The test cache is a null store, so hand out the counts a real store
    # would return for the options counter, which create does not share.
    Settings::WebauthnCredentialsController.cache_store.stubs(:increment)
      .with { |key, *| key.include?("code-check") && key.exclude?("code-spend") }
      .returns(*1..11)

    10.times do
      post options_settings_webauthn_credentials_path, params: { webauthn_credential: { code: "000000" } }, as: :json
      assert_response :unprocessable_entity
    end

    post options_settings_webauthn_credentials_path, params: { webauthn_credential: { code: "000000" } }, as: :json

    assert_response :too_many_requests
    assert_equal I18n.t("webauthn_credentials.rate_limited"), JSON.parse(response.body).fetch("error")
  end

  test "every locale that translates the passkey form also translates the code confirmation" do
    I18n.available_locales.each do |locale|
      next unless I18n.exists?("webauthn_credentials.failure", locale, fallback: false)

      %w[webauthn_credentials.code_already_used webauthn_credentials.invalid_code webauthn_credentials.rate_limited
         settings.securities.show.webauthn_code_label settings.securities.show.webauthn_code_placeholder].each do |key|
        assert I18n.exists?(key, locale, fallback: false), "#{locale} is missing #{key}"
      end
    end
  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", code: @code },
          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", code: @code },
      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", code: backup_code },
        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", code: @code },
        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", code: @code },
        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: "", code: @code },
      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

    log = SecurityAuditLog.where(user: @user, event_type: "webauthn_credential_removed").last
    assert_equal credential.id, log.metadata["credential_id"]
    assert_equal "YubiKey", log.metadata["nickname"]
  end

  private
    # Options now check the code too; keep one valid code per test so the
    # create call that follows can spend the same one.
    def registration_options(code: nil)
      @code = code || @code || backup_code
      post options_settings_webauthn_credentials_path, params: { webauthn_credential: { code: @code } }, as: :json
      assert_response :success
      JSON.parse(response.body)
    end

    def backup_code
      @user.enable_mfa!.first
    end

    def totp_code
      ROTP::TOTP.new(@user.reload.otp_secret, issuer: "Sure Finances").now
    end

    def register(credential, code:)
      post settings_webauthn_credentials_path, params: {
        webauthn_credential: { nickname: "Attempt", code: code },
        credential: credential
      }, as: :json
    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