Merge pull request #7697 from betagouv/fix-champ-null-byte

fix(champ): remove null byte before save
This commit is contained in:
Colin Darie 2022-08-31 11:58:06 +02:00 committed by GitHub
commit 8059abb85e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View file

@ -75,6 +75,7 @@ class Champ < ApplicationRecord
before_create :set_dossier_id, if: :needs_dossier_id? before_create :set_dossier_id, if: :needs_dossier_id?
before_validation :set_dossier_id, if: :needs_dossier_id? before_validation :set_dossier_id, if: :needs_dossier_id?
before_save :cleanup_if_empty before_save :cleanup_if_empty
before_save :normalize
after_update_commit :fetch_external_data_later after_update_commit :fetch_external_data_later
validates :type_de_champ_id, uniqueness: { scope: [:dossier_id, :row] } validates :type_de_champ_id, uniqueness: { scope: [:dossier_id, :row] }
@ -245,6 +246,12 @@ class Champ < ApplicationRecord
end end
end end
def normalize
return if value.nil?
self.value = value.delete("\u0000")
end
class NotImplemented < ::StandardError class NotImplemented < ::StandardError
def initialize(method) def initialize(method)
super(":#{method} not implemented") super(":#{method} not implemented")

View file

@ -26,6 +26,13 @@ describe Champ do
end end
end end
describe "normalization" do
it "should remove null bytes before save" do
champ = create(:champ, value: "foo\u0000bar")
expect(champ.value).to eq "foobar"
end
end
describe '#public?' do describe '#public?' do
let(:type_de_champ) { build(:type_de_champ) } let(:type_de_champ) { build(:type_de_champ) }
let(:champ) { type_de_champ.champ.build } let(:champ) { type_de_champ.champ.build }