Assigning integer to attr_encrypted attribute raises TypeError #16

Closed
ianejames opened this Issue Mar 11, 2011 · 2 comments

Projects

None yet

2 participants

@ianejames

When assigning an integer to an attr_encrypted attribute the following error is raised:

TypeError: can't convert Fixnum into String

This is because the integer value is passed straight through to Encryptor which passes it to the OpenSSL::Cipher::Cipher instance. The error only occurs when marshaling is not used; I assume the marshaling procedure always outputs a string acceptable to the cipher.

As a solution I suggest type casting the assigned value. This can be achieved by updating line 192 of attr_encrypted.rb from...

value = options[:marshaler].send(options[:dump_method], value) if options[:marshal]

...to...

value = options[:marshal] ? options[:marshaler].send(options[:dump_method], value) : value.to_s

...or...

if options[:marshal]
  value = options[:marshaler].send(options[:dump_method], value)
else
  value = value.to_s
end

...depending on whether you prefer clarity or brevity.

Alternately:

  • The type casting could be performed in the Encryptor.
  • The user could hack a marshaller into doing the type casting.
  • The user could wrap the assignment in a type casting block using alias_method_chain.

I think it's reasonable to assume the user intends all assigned values to be cast to string when marshaling is not used, which is why I propose doing it here in the AttrEncrypted module.

Owner
shuber commented Mar 12, 2011

type cast values as strings before encrypting, closed by d325268

Owner
shuber commented Mar 12, 2011

Thanks for the clear and thorough writeup! attr-encrypted/attr_encrypted@d325268

@rcook rcook pushed a commit to rcook/attr_encrypted that referenced this issue Jul 26, 2013
@shuber shuber type cast values as strings before encrypting, closes #16 d325268
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment