0

I’m working with a Rails application that uses the attr_encrypted gem to handle encryption for sensitive data. Previously, we only stored the last 4 digits of the SSN. We’re migrating to store the full SSN while keeping the last 4 digits accessible as ssn_last_four for legacy compatibility.

class Person < ApplicationRecord
  prepend SsnLastFourConcern

  attr_encrypted :ssn, key: 'some_secret_key'
  attr_encrypted :ssn, key: 'some_secret_key'
end

and I have the concern:

module SsnLastFourConcern
  extend ActiveSupport::Concern

  def ssn_last_four
    return ssn[-4..] if ssn?
    super
  end

  def ssn=(value)
    self.ssn_last_four = value[-4..] if value.present? && value.length == 9
    super(value)
  end
end

The issue is that calling super(value) in the overridden ssn= setter leads to an infinite loop. But calling the super in ssn_last_four it works as expected

The workaround could be to use the before_save callback on ssn_last_four and make sure it reflects SSN. Also, by overriding the getter, the rest of the app will work properly, but I want to make sure that in case someone gets encrypted data from the column and manually decrypts it, he still gets the valid data.

EDIT: I do not override the ssn_last_four setter.

EDIT: Removed extend ActiveSupport::Concern as @engineersmnky suggested

Here is the stack trace: SystemStackError: stack level too deep from ruby-2.7.8/gems/activemodel-6.0.4/lib/active_model/attribute_set.rb:33:in `key?'

5
  • why are you setting self.ssn_last_four in the ssn setter? that's where you are getting your loop Commented Nov 7, 2024 at 17:29
  • I want to update the value in that column as well, in case someone directly accesses it. Commented Nov 7, 2024 at 17:50
  • 1
    Please edit your question and add the stacktrace of the error (you can remove the repeating parts, but add enough to show exactly where it loops). Also, please make sure that you have shown all relevant code, including e.g. an overwritten ssn_last_four= setter. Commented Nov 7, 2024 at 17:57
  • you need a third attribute Commented Nov 7, 2024 at 18:09
  • 3
    As an aside there is no reason to extend ActiveSupport::Concern since you don't use any part of the Concern interface. Commented Nov 7, 2024 at 20:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.