I made a custom ActiveModel::Type to encrypt a field before it goes to the database. The context is that multiple apps use the same database. Some write to it, some read from it. Now my side is write-only. I should never read the field, neither can I decrypt it.
How do we handle write-only fields with ActiveRecord?
Should I raise?
Should I return nil?
Should I return the ciphertext stored in the database?
Is an ActiveModel::Type not made for this? e.g. Devise doesn’t use it.
What do other people do?
This is my sketch. It raises:
class RsaEncryptedType < ActiveModel::Type::String
def initialize(public_key: nil, private_key: nil)
raise "At least public_key or private_key must be present" unless public_key.present? || private_key.present?
@private_key = OpenSSL::PKey::RSA.new(private_key) if private_key.present?
@public_key = OpenSSL::PKey::RSA.new(public_key) if public_key.present?
end
def type
:rsa_encrypted
end
def serialize(value)
return nil if value.blank?
raise "Cannot encrypt. Public key missing." if @public_key.nil?
ciphertext = @public_key.public_encrypt(value.to_s)
Base64.strict_encode64(ciphertext)
end
def deserialize(value)
return nil if value.blank?
raise "Cannot decrypt. Private key missing." if @private_key.nil?
ciphertext = Base64.decode64(value)
@private_key.private_decrypt(ciphertext)
end
end
readonly? and attr_readonly are exactly the opposite of what I want. I’m searching for writeonly? and attr_writeonly. Is there a good reason why this doesn’t exist in Rails?
This could be a solution which doesn’t interfere too deeply.
class Account < ApplicationRecord
include WriteonlyEncryption
attr_encrypt_writeonly :pin
end
Provided functionality with a module like that. It adds the getter and setter #pin and #pin=.
module WriteonlyEncryption
extend ActiveSupport::Concern
class_methods do
def attr_encrypt_writeonly(*names, public_key: Rails.application.credentials.writeonly_encryption_public_key)
raise ArgumentError, "attr_encrypt_writeonly needs at least one attribute" if names.empty?
raise ArgumentError, "attr_encrypt_writeonly needs a public key" if public_key.blank?
sealer = RbNaCl::Boxes::Sealed.from_public_key(RbNaCl::PublicKey.new(Base64.strict_decode64(public_key)))
self.filter_attributes += names
names.each do |name|
define_method(:"#{name}=") do |plain|
super(plain.presence && Base64.strict_encode64(sealer.box(plain)))
end
define_method(name) do
raise NoMethodError, "#{self.class}##{name} is write-only"
end
end
end
end
end