Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Filter one-time codes from logs and hide raw DB errors from the API (#3831)

GFR58c012cfb48e
diff --git a/app/controllers/api/v1/auth_controller.rb b/app/controllers/api/v1/auth_controller.rb
index 16c3d852c0..7a8df89a49 100644
--- a/app/controllers/api/v1/auth_controller.rb
+++ b/app/controllers/api/v1/auth_controller.rb
@@ -294,11 +294,14 @@ module Api
identity = OidcIdentity.create_from_omniauth(build_omniauth_hash(cached), user)
true
end
- rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique => e
+ rescue ActiveRecord::RecordInvalid => e
# Expected persistence failures (e.g. a duplicate identity) roll the
# whole onboarding back and return the error response. Unexpected
# errors propagate so they surface instead of being hidden.
user.errors.add(:base, e.message)
+ rescue ActiveRecord::RecordNotUnique
+ # PostgreSQL's message names the index and echoes the conflicting values.
+ user.errors.add(:base, "An account or sign-in identity with these details already exists")
end
if account_created
diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb
index 413aa659ce..f173226b8c 100644
--- a/config/initializers/filter_parameter_logging.rb
+++ b/config/initializers/filter_parameter_logging.rb
@@ -8,6 +8,12 @@ Rails.application.config.filter_parameters += [
:client_id, :consumer_key, :snaptrade_user_id, :snaptrade_user_secret,
:oauth_access_token, :oauth_refresh_token, :code_verifier, :code_challenge,
/\Apin\z/i,
+ # One-time codes that act as credentials: MFA TOTP/backup codes, OAuth and
+ # bank authorization codes and the desktop SSO exchange all arrive as
+ # "code". Anchored so currency_code, country_code and postal_code stay
+ # readable. linking_code binds a mobile SSO login to an account,
+ # invite_code opens registration, and access_url is SimpleFIN's credential.
+ /\Acode\z/i, :linking_code, :invite_code, :access_url,
# A device code redeems into tokens on its own, so it is a bearer credential in
# transit; verification_uri_complete embeds the user code, hence all three.
:device_code, :user_code, :verification_uri_complete,
diff --git a/test/controllers/api/v1/auth_controller_test.rb b/test/controllers/api/v1/auth_controller_test.rb
index 18f11d0087..a58a336030 100644
--- a/test/controllers/api/v1/auth_controller_test.rb
+++ b/test/controllers/api/v1/auth_controller_test.rb
@@ -1069,7 +1069,11 @@ class Api::V1::AuthControllerTest < ActionDispatch::IntegrationTest
device_info: @device_info.stringify_keys,
allow_account_creation: true
}, expires_in: 10.minutes)
- OidcIdentity.stubs(:create_from_omniauth).raises(ActiveRecord::RecordNotUnique, "duplicate identity")
+ OidcIdentity.stubs(:create_from_omniauth).raises(
+ ActiveRecord::RecordNotUnique,
+ 'PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_oidc_identities_on_provider_and_uid" ' \
+ "DETAIL: Key (provider, uid)=(google_oauth2, google-uid-rollback) already exists."
+ )
assert_no_difference([ "User.count", "OidcIdentity.count", "Family.count" ]) do
post "/api/v1/auth/sso_create_account", params: {
@@ -1081,6 +1085,12 @@ class Api::V1::AuthControllerTest < ActionDispatch::IntegrationTest
assert_response :unprocessable_entity
assert_nil User.find_by(email: email)
+
+ # The database's own message names the index and echoes the values.
+ errors = JSON.parse(response.body)["errors"]
+ assert_equal [ "An account or sign-in identity with these details already exists" ], errors
+ assert_not_includes response.body, "index_oidc_identities"
+ assert_not_includes response.body, "google-uid-rollback"
end
test "sso_create_account linking_code single-use under race" do
diff --git a/test/initializers/filter_parameter_logging_test.rb b/test/initializers/filter_parameter_logging_test.rb
new file mode 100644
index 0000000000..dfe7b454a1
--- /dev/null
+++ b/test/initializers/filter_parameter_logging_test.rb
@@ -0,0 +1,33 @@
+require "test_helper"
+
+class FilterParameterLoggingTest < ActiveSupport::TestCase
+ setup do
+ @filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)
+ end
+
+ # MFA codes, OAuth/bank authorization codes and the desktop SSO exchange all
+ # arrive as a bare "code" and are credentials until they are spent.
+ test "filters one-time codes and other bearer values from logs" do
+ filtered = @filter.filter(
+ code: "123456",
+ linking_code: "abc",
+ invite_code: "deadbeef",
+ user: { invite_code: "deadbeef" },
+ simplefin_item: { access_url: "https://user:[email protected]/simplefin" }
+ )
+
+ assert_equal "[FILTERED]", filtered[:code]
+ assert_equal "[FILTERED]", filtered[:linking_code]
+ assert_equal "[FILTERED]", filtered[:invite_code]
+ assert_equal "[FILTERED]", filtered.dig(:user, :invite_code)
+ assert_equal "[FILTERED]", filtered.dig(:simplefin_item, :access_url)
+ end
+
+ test "keeps descriptive codes readable" do
+ filtered = @filter.filter(currency_code: "EUR", country_code: "DE", postal_code: "10115")
+
+ assert_equal "EUR", filtered[:currency_code]
+ assert_equal "DE", filtered[:country_code]
+ assert_equal "10115", filtered[:postal_code]
+ end
+end