Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(security): warn when ActiveRecord encryption is not configured (#2362)

Andrew B6945b5a29675
diff --git a/app/controllers/settings/securities_controller.rb b/app/controllers/settings/securities_controller.rb
index d53bddd775..6d32a07858 100644
--- a/app/controllers/settings/securities_controller.rb
+++ b/app/controllers/settings/securities_controller.rb
@@ -8,5 +8,7 @@ class Settings::SecuritiesController < ApplicationController
]
@oidc_identities = Current.user.oidc_identities.order(:provider)
@webauthn_credentials = Current.user.webauthn_credentials.order(created_at: :asc)
+ @encryption_unconfigured = Rails.application.config.app_mode.self_hosted? &&
+ !ActiveRecordEncryptionConfig.explicitly_configured?
end
end
diff --git a/app/views/settings/securities/show.html.erb b/app/views/settings/securities/show.html.erb
index d1c0fc8286..4ec0d554ae 100644
--- a/app/views/settings/securities/show.html.erb
+++ b/app/views/settings/securities/show.html.erb
@@ -1,5 +1,19 @@
<%= content_for :page_title, t(".page_title") %>
+<% if @encryption_unconfigured %>
+ <div class="mb-4">
+ <%= render DS::Alert.new(variant: :warning, title: t(".encryption_warning.title")) do %>
+ <p><%= t(".encryption_warning.intro") %></p>
+ <ul class="list-disc pl-5">
+ <% t(".encryption_warning.keys").each do |key| %>
+ <li><%= key %></li>
+ <% end %>
+ </ul>
+ <p><%= t(".encryption_warning.generate") %></p>
+ <% end %>
+ </div>
+<% end %>
+
<%= settings_section title: t(".mfa_title"), subtitle: t(".mfa_description") do %>
<div class="space-y-4">
<div class="p-3 shadow-border-xs bg-container rounded-lg md:flex md:justify-between md:items-center">
diff --git a/config/initializers/encryption_warning.rb b/config/initializers/encryption_warning.rb
new file mode 100644
index 0000000000..31d19b4aaa
--- /dev/null
+++ b/config/initializers/encryption_warning.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+# Warn self-hosted operators when ActiveRecord Encryption is NOT configured.
+#
+# This emits a clear startup warning so plaintext-at-rest is never silent.
+require Rails.root.join("lib/active_record_encryption_config").to_s
+
+Rails.application.config.after_initialize do
+ app_mode = Rails.application.config.app_mode
+ if app_mode.self_hosted? && !ActiveRecordEncryptionConfig.explicitly_configured?
+ Rails.logger.warn(<<~WARN)
+ [SECURITY] ActiveRecord Encryption is NOT configured. Sensitive data
+ (API keys, provider/bank tokens, MFA secrets, and PII) are being stored
+ UNENCRYPTED at rest. To enable encryption, set the following keys in your Rails credentials or environment variables:
+ ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY
+ ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY
+ ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT
+ Generate a set with: bin/rails db:encryption:init
+ WARN
+ end
+end
diff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.yml
index a1e88e4126..0280986d22 100644
--- a/config/locales/views/settings/en.yml
+++ b/config/locales/views/settings/en.yml
@@ -213,6 +213,14 @@ en:
securities:
show:
page_title: Security
+ encryption_warning:
+ title: Encryption keys missing
+ intro: "Sensitive data (API keys, provider tokens, MFA secrets, and PII) are being stored unencrypted at rest. To enable encryption, set the following keys in your environment variables or Rails credentials:"
+ keys:
+ - ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY
+ - ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY
+ - ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT
+ generate: "Generate a set with: bin/rails db:encryption:init"
mfa_title: Two-Factor Authentication
mfa_description: Add an extra layer of security to your account by requiring a code from your authenticator app when signing in
enable_mfa: Enable 2FA
diff --git a/test/controllers/settings/securities_controller_test.rb b/test/controllers/settings/securities_controller_test.rb
new file mode 100644
index 0000000000..26c12306d3
--- /dev/null
+++ b/test/controllers/settings/securities_controller_test.rb
@@ -0,0 +1,34 @@
+require "test_helper"
+
+class Settings::SecuritiesControllerTest < ActionDispatch::IntegrationTest
+ setup { sign_in users(:family_admin) }
+
+ test "shows encryption warning when self-hosted and encryption is not configured" do
+ Rails.configuration.stubs(:app_mode).returns("self_hosted".inquiry)
+ ActiveRecordEncryptionConfig.stubs(:explicitly_configured?).returns(false)
+
+ get settings_security_url
+
+ assert_response :success
+ assert_includes response.body, I18n.t("settings.securities.show.encryption_warning.title")
+ end
+
+ test "hides encryption warning when encryption is configured" do
+ Rails.configuration.stubs(:app_mode).returns("self_hosted".inquiry)
+ ActiveRecordEncryptionConfig.stubs(:explicitly_configured?).returns(true)
+
+ get settings_security_url
+
+ assert_response :success
+ assert_not_includes response.body, I18n.t("settings.securities.show.encryption_warning.title")
+ end
+
+ test "does not show encryption warning in managed mode" do
+ Rails.configuration.stubs(:app_mode).returns("managed".inquiry)
+
+ get settings_security_url
+
+ assert_response :success
+ assert_not_includes response.body, I18n.t("settings.securities.show.encryption_warning.title")
+ end
+end