Skip to content

Globally strip null bytes for application #42696

@joshbuker

Description

@joshbuker

Okay, so my Google foo is failing me, apologies if this has been answered before and I just can't find it.

I'm wanting to strip null bytes from all text columns because Postgres cannot accept null bytes, and my application doesn't care about storing them.

One approach that you can take is to override each attribute setter individually:

class User < ApplicationRecord
  def name=(value)
    if value.is_a?(String) && value.present?
      super(value.delete("\u0000"))
    else
      super(value)
    end
  end
end

This works, but it quickly gets unwieldy if you need to do this for every string attribute in the application.

Ideally, I would be able to override it globally, e.g. something like:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def attribute=(value)
    if value.is_a?(String) && value.present?
      super(value.delete("\u0000"))
    else
      super(value)
    end
  end
end

So my questions are:

  1. Is wanting to do this sane? How do others approach users providing null bytes when you're using Postgres?
  2. Are there any ways to override the attribute setters for all setters in an application? I would be fine with monkey patching / meta programming if that's the only option available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions