spikex / strongbox

Secures ActiveRecord attributes with public key encryption
http://stuff-things.net/2009/04/17/introducing-strongbox/
MIT License
398 stars 42 forks source link

string contains null byte #44

Closed prashantjois closed 9 years ago

prashantjois commented 9 years ago

Hey there,

I'm getting this error when trying to save my model: string contains null byte

Here's the stack trace from the console:

ArgumentError: string contains null byte
        from /usr/local/bundle/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql/oid.rb:30:in `unescape_bytea'
        from /usr/local/bundle/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql/oid.rb:30:in `type_cast'
        from /usr/local/bundle/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql_adapter.rb:156:in `type_cast'
        from /usr/local/bundle/gems/activerecord-4.1.6/lib/active_record/attribute_methods/dirty.rb:102:in `_field_changed?'
        from /usr/local/bundle/gems/activerecord-4.1.6/lib/active_record/attribute_methods/dirty.rb:78:in `save_changed_attribute'
        from /usr/local/bundle/gems/activerecord-4.1.6/lib/active_record/attribute_methods/dirty.rb:66:in `write_attribute'
        from /usr/local/bundle/gems/activerecord-4.1.6/lib/active_record/attribute_methods.rb:395:in `[]='
        from /usr/local/bundle/gems/strongbox-0.7.0/lib/strongbox/lock.rb:50:in `encrypt'
        from /usr/local/bundle/gems/strongbox-0.7.0/lib/strongbox.rb:80:in `block in encrypt_with_public_key'
        from (irb):2
        from /usr/local/bundle/gems/railties-4.1.6/lib/rails/commands/console.rb:90:in `start'
        from /usr/local/bundle/gems/railties-4.1.6/lib/rails/commands/console.rb:9:in `start'
        from /usr/local/bundle/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:69:in `console'
        from /usr/local/bundle/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
        from /usr/local/bundle/gems/railties-4.1.6/lib/rails/commands.rb:17:in `<top (required)>'
        from /usr/local/bundle/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
... 4 levels...
        from /usr/local/bundle/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `load'
        from /usr/local/bundle/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `block in load'
        from /usr/local/bundle/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
        from /usr/local/bundle/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `load'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/commands/rails.rb:6:in `call'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/command_wrapper.rb:38:in `call'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/application.rb:183:in `block in serve'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/application.rb:156:in `fork'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/application.rb:156:in `serve'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
        from /usr/local/bundle/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
        from /usr/local/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /usr/local/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'

Any ideas?

prashantjois commented 9 years ago

Looks like adding the base64: true option to encrypt_with_public_key fixed the problem. I think this may have something to do with the bytea type in postgres? Not sure if this is a new problem or whether my encrypted data in the past just happened to not contain a null byte.

spikex commented 9 years ago

Looks like it's a bug in Rails when using the bytea type. https://bitbucket.org/ged/ruby-pg/issue/198/argumenterror-string-contains-null-byte Using base64:. as you found type will let you you work around it.