The personal finance app for everyone (by everyone)
Ruby
Prevent MFA enrollment for the shared sure.am demo family (#5220)
Sure Admin (bot)9363d657f20a
diff --git a/app/controllers/concerns/demo_mfa_restriction.rb b/app/controllers/concerns/demo_mfa_restriction.rbnew file mode 100644index 0000000000..826ffa46e4--- /dev/null+++ b/app/controllers/concerns/demo_mfa_restriction.rb@@ -0,0 +1,32 @@+module DemoMfaRestriction+ extend ActiveSupport::Concern++ included do+ helper_method :demo_family_mfa_restricted?+ end++ private+ def demo_family_mfa_restricted?+ # Include the deployment domain so an alternate host cannot bypass the+ # restriction on a sure.am installation. Do not match lookalike domains.+ sure_domain = [ request.host, ENV["APP_DOMAIN"] ].compact.any? do |value|+ host = value.downcase.delete_suffix(".")+ host == "sure.am" || host.end_with?(".sure.am")+ end+ return false unless sure_domain && Current.family++ # The generator marks demo subscriptions independently of editable user+ # emails and their encrypted/legacy storage representation.+ Current.family.subscription&.synthetic? || false+ end++ def require_mfa_enrollment_allowed+ return unless demo_family_mfa_restricted?++ respond_to do |format|+ format.html { redirect_to settings_security_path, alert: t("mfa.demo_family_restricted") }+ format.json { render json: { error: t("mfa.demo_family_restricted") }, status: :forbidden }+ format.any { head :forbidden }+ end+ end+enddiff --git a/app/controllers/mfa_controller.rb b/app/controllers/mfa_controller.rbindex 672a134842..d5cb1abb36 100644--- a/app/controllers/mfa_controller.rb+++ b/app/controllers/mfa_controller.rb@@ -1,8 +1,9 @@class MfaController < ApplicationController- include WebauthnRelyingParty+ include WebauthnRelyingParty, DemoMfaRestrictionlayout :determine_layoutskip_authentication only: [ :verify, :verify_code, :webauthn_options, :verify_webauthn ]+ before_action :require_mfa_enrollment_allowed, only: [ :new, :create ]before_action :require_local_password, only: [ :new, :create, :disable ]def newdiff --git a/app/controllers/settings/securities_controller.rb b/app/controllers/settings/securities_controller.rbindex 2dd280af77..03d0edb6df 100644--- a/app/controllers/settings/securities_controller.rb+++ b/app/controllers/settings/securities_controller.rb@@ -1,4 +1,6 @@class Settings::SecuritiesController < ApplicationController+ include DemoMfaRestriction+layout "settings"def showdiff --git a/app/controllers/settings/webauthn_credentials_controller.rb b/app/controllers/settings/webauthn_credentials_controller.rbindex 8cc297e38f..0fcbfde0c1 100644--- a/app/controllers/settings/webauthn_credentials_controller.rb+++ b/app/controllers/settings/webauthn_credentials_controller.rb@@ -1,8 +1,9 @@class Settings::WebauthnCredentialsController < ApplicationController- include WebauthnRelyingParty+ include WebauthnRelyingParty, DemoMfaRestrictionlayout "settings"+ before_action :require_mfa_enrollment_allowed, only: [ :options, :create ]before_action :ensure_mfa_enabled# Adding a passkey is confirmed with an MFA code; keep a stolen session fromdiff --git a/app/views/settings/securities/show.html.erb b/app/views/settings/securities/show.html.erbindex 04e1fa4848..9e0f4dd2ac 100644--- a/app/views/settings/securities/show.html.erb+++ b/app/views/settings/securities/show.html.erb@@ -64,6 +64,8 @@<%= f.submit t(".disable_mfa") %><% end %><% end %>+ <% elsif demo_family_mfa_restricted? %>+ <p class="text-sm text-subdued"><%= t("mfa.demo_family_restricted") %></p><% else %><%= render DS::Link.new(text: t(".enable_mfa"),@@ -120,40 +122,44 @@</div><% end %>- <%= styled_form_with scope: :webauthn_credential,- url: settings_webauthn_credentials_path,- method: :post,- class: "space-y-3",- data: {- controller: "webauthn-registration",- action: "submit->webauthn-registration#register",- webauthn_registration_options_url_value: options_settings_webauthn_credentials_path,- webauthn_registration_create_url_value: settings_webauthn_credentials_path,- webauthn_registration_unsupported_message_value: t(".webauthn_unsupported"),- webauthn_registration_error_fallback_value: t("webauthn_credentials.failure")- } do |form| %>- <%= form.text_field :nickname,- placeholder: t(".webauthn_name_placeholder"),- label: t(".webauthn_name_label"),- data: { webauthn_registration_target: "nickname" } %>+ <% if demo_family_mfa_restricted? %>+ <p class="text-sm text-subdued"><%= t("mfa.demo_family_restricted") %></p>+ <% else %>+ <%= styled_form_with scope: :webauthn_credential,+ url: settings_webauthn_credentials_path,+ method: :post,+ class: "space-y-3",+ data: {+ controller: "webauthn-registration",+ action: "submit->webauthn-registration#register",+ webauthn_registration_options_url_value: options_settings_webauthn_credentials_path,+ webauthn_registration_create_url_value: settings_webauthn_credentials_path,+ webauthn_registration_unsupported_message_value: t(".webauthn_unsupported"),+ webauthn_registration_error_fallback_value: t("webauthn_credentials.failure")+ } do |form| %>+ <%= form.text_field :nickname,+ placeholder: t(".webauthn_name_placeholder"),+ label: t(".webauthn_name_label"),+ data: { webauthn_registration_target: "nickname" } %>- <%= form.text_field :code,- required: true,- autocomplete: "one-time-code",- label: t(".webauthn_code_label"),- placeholder: t(".webauthn_code_placeholder"),- data: { webauthn_registration_target: "code" } %>+ <%= form.text_field :code,+ required: true,+ autocomplete: "one-time-code",+ label: t(".webauthn_code_label"),+ placeholder: t(".webauthn_code_placeholder"),+ data: { webauthn_registration_target: "code" } %>- <p class="text-sm text-destructive" role="alert" aria-live="assertive" aria-atomic="true" aria-hidden="true" hidden data-webauthn-registration-target="error"></p>+ <p class="text-sm text-destructive" role="alert" aria-live="assertive" aria-atomic="true" aria-hidden="true" hidden data-webauthn-registration-target="error"></p>- <div>- <%= render DS::Button.new(- text: t(".webauthn_add"),- variant: "secondary",- icon: "fingerprint",- type: "submit"- ) %>- </div>+ <div>+ <%= render DS::Button.new(+ text: t(".webauthn_add"),+ variant: "secondary",+ icon: "fingerprint",+ type: "submit"+ ) %>+ </div>+ <% end %><% end %></div><% end %>diff --git a/config/locales/views/mfa/en.yml b/config/locales/views/mfa/en.ymlindex 186cdae714..65bf8c3e9f 100644--- a/config/locales/views/mfa/en.yml+++ b/config/locales/views/mfa/en.yml@@ -1,6 +1,7 @@---en:mfa:+ demo_family_restricted: MFA enrollment is disabled for the shared demo family to keep demo access available to everyone.local_password_required: Two-factor authentication needs a local password.Yours is managed through your identity provider.backup_codes:diff --git a/test/controllers/demo_mfa_restriction_test.rb b/test/controllers/demo_mfa_restriction_test.rbnew file mode 100644index 0000000000..584b444800--- /dev/null+++ b/test/controllers/demo_mfa_restriction_test.rb@@ -0,0 +1,188 @@+require "test_helper"+require "webauthn/fake_client"++class DemoMfaRestrictionTest < ActionDispatch::IntegrationTest+ setup do+ @demo_user = users(:new_email)+ @member = users(:empty)+ @member.family.start_subscription!(Subscription::DEMO_STRIPE_ID)+ assert_equal Rails.application.config_for(:demo).fetch(:email), @demo_user.email+ assert_equal @demo_user.family, @member.family+ end++ %w[sure.am app.sure.am demo.sure.am nested.demo.sure.am APP.SURE.AM].each do |host|+ test "blocks enrollment for demo family members on #{host}" do+ host! host+ sign_in @member++ get new_mfa_path++ assert_redirected_to settings_security_path+ assert_equal I18n.t("mfa.demo_family_restricted"), flash[:alert]+ assert_nil @member.reload.otp_secret+ assert_not @member.otp_required?+ end+ end++ test "blocks a valid direct enrollment POST with an existing setup secret" do+ host! "app.sure.am"+ sign_in @demo_user+ @demo_user.setup_mfa!+ code = ROTP::TOTP.new(@demo_user.otp_secret).now++ assert_no_difference "SecurityAuditLog.count" do+ post mfa_path, params: { code: code, password: user_password_test }+ end++ assert_redirected_to settings_security_path+ assert_not @demo_user.reload.otp_required?+ assert_empty @demo_user.otp_backup_codes+ end++ test "email changes do not unlock demo family enrollment" do+ @demo_user.update!(email: "[email protected]")+ host! "app.sure.am"+ sign_in @member++ get new_mfa_path+ assert_redirected_to settings_security_path+ assert_nil @member.reload.otp_secret++ @member.setup_mfa!+ code = ROTP::TOTP.new(@member.otp_secret).now+ post mfa_path, params: { code: code, password: user_password_test }+ assert_redirected_to settings_security_path+ assert_not @member.reload.otp_required?++ @member.enable_mfa!+ post options_settings_webauthn_credentials_path,+ params: { webauthn_credential: { code: code } }, as: :json+ assert_response :forbidden+ end++ test "legacy plaintext demo email does not unlock demo family enrollment" do+ store_legacy_plaintext_email(@demo_user)+ host! "app.sure.am"+ sign_in @member++ get new_mfa_path+ assert_redirected_to settings_security_path+ assert_nil @member.reload.otp_secret++ @member.setup_mfa!+ code = ROTP::TOTP.new(@member.otp_secret).now+ post mfa_path, params: { code: code, password: user_password_test }+ assert_redirected_to settings_security_path+ assert_not @member.reload.otp_required?+ end++ test "configured email alone does not restrict an unmarked family" do+ @member.family.subscription.update!(stripe_id: "sub_regular")+ host! "app.sure.am"+ sign_in @member++ get new_mfa_path+ assert_response :success+ assert @member.reload.otp_secret.present?+ end++ test "deployment domain also protects enrollment through an alternate hostname" do+ with_env_overrides("APP_DOMAIN" => "app.sure.am") do+ host! "service.onrender.com"+ sign_in @member+ get new_mfa_path+ assert_redirected_to settings_security_path+ assert_nil @member.reload.otp_secret+ end+ end++ %w[finance.example.com notsure.am sure.am.example.com sure-demo.pikapod.net].each do |host|+ test "allows the demo family to enroll outside sure.am on #{host}" do+ with_env_overrides("APP_DOMAIN" => nil) do+ host! host+ sign_in @member+ get new_mfa_path+ assert_response :success+ code = ROTP::TOTP.new(@member.reload.otp_secret).now+ post mfa_path, params: { code: code, password: user_password_test }+ assert_response :success+ assert @member.reload.otp_required?+ end+ end+ end++ test "allows another family to enroll on sure.am even with the demo family name" do+ user = users(:family_admin)+ user.family.update!(name: @demo_user.family.name)+ host! "app.sure.am"+ sign_in user+ get new_mfa_path+ assert_response :success+ code = ROTP::TOTP.new(user.reload.otp_secret).now+ post mfa_path, params: { code: code, password: user_password_test }+ assert_response :success+ assert user.reload.otp_required?+ end++ test "blocks passkey options for a demo member with existing MFA" do+ host! "app.sure.am"+ sign_in @member+ @member.setup_mfa!+ @member.enable_mfa!++ post options_settings_webauthn_credentials_path,+ params: { webauthn_credential: { code: ROTP::TOTP.new(@member.otp_secret).now } }, as: :json++ assert_response :forbidden+ assert_equal I18n.t("mfa.demo_family_restricted"), response.parsed_body["error"]+ assert_nil @member.reload.webauthn_id+ end++ test "blocks passkey creation even with a previously issued valid challenge" do+ host! "app.sure.am"+ sign_in @member+ @member.setup_mfa!+ @member.enable_mfa!+ # Obtain a real challenge before this family is marked as a demo.+ @demo_user.update!(email: "[email protected]")+ @member.family.subscription.update!(stripe_id: "sub_regular")+ code = ROTP::TOTP.new(@member.otp_secret).now+ post options_settings_webauthn_credentials_path,+ params: { webauthn_credential: { code: code } }, as: :json+ assert_response :success+ client = WebAuthn::FakeClient.new("http://app.sure.am")+ credential = client.create(challenge: response.parsed_body.fetch("challenge"), rp_id: "app.sure.am")+ @member.family.start_subscription!(Subscription::DEMO_STRIPE_ID)++ assert_no_difference "WebauthnCredential.count" do+ post settings_webauthn_credentials_path,+ params: { credential: credential, webauthn_credential: { nickname: "Demo key", code: code } }, as: :json+ end+ assert_response :forbidden+ assert_equal I18n.t("mfa.demo_family_restricted"), response.parsed_body["error"]+ end++ test "hides enrollment controls but keeps MFA and passkey removal available" do+ host! "app.sure.am"+ sign_in @member+ get settings_security_path+ assert_response :success+ assert_select "a[href=?]", new_mfa_path, count: 0+ assert_includes response.body, I18n.t("mfa.demo_family_restricted")++ @member.setup_mfa!+ @member.enable_mfa!+ credential = @member.webauthn_credentials.create!(nickname: "Old key", credential_id: "old-demo-key", public_key: "key")+ get settings_security_path+ assert_response :success+ assert_select "form[action=?]", settings_webauthn_credentials_path, count: 0+ assert_select "form[action=?]", disable_mfa_path, count: 1++ delete settings_webauthn_credential_path(credential)+ assert_redirected_to settings_security_path+ assert_not WebauthnCredential.exists?(credential.id)+ delete disable_mfa_path, params: { password: user_password_test }+ assert_redirected_to settings_security_path+ assert_not @member.reload.otp_required?+ end+enddiff --git a/test/system/demo_mfa_test.rb b/test/system/demo_mfa_test.rbnew file mode 100644index 0000000000..703a6108a7--- /dev/null+++ b/test/system/demo_mfa_test.rb@@ -0,0 +1,21 @@+require "application_system_test_case"++class DemoMfaTest < ApplicationSystemTestCase+ test "demo family cannot start MFA enrollment on a sure.am installation" do+ with_env_overrides("APP_DOMAIN" => "app.sure.am") do+ users(:empty).family.start_subscription!(Subscription::DEMO_STRIPE_ID)+ users(:new_email).update!(email: "[email protected]")+ sign_in users(:empty)+ visit settings_security_path++ assert_text I18n.t("mfa.demo_family_restricted")+ assert_no_link href: new_mfa_path++ visit new_mfa_path+ assert_current_path settings_security_path+ assert_text I18n.t("mfa.demo_family_restricted")+ assert_nil users(:empty).reload.otp_secret+ save_screenshot Rails.root.join("tmp/screenshots/demo-mfa-restricted.png")+ end+ end+end