Skip to content

Commit

Permalink
Fixing "PG::UntranslatableCharacter" error when storing some Pender d…
Browse files Browse the repository at this point in the history
…ata annotations.

PostgreSQL doesn't support null characters in a JSON column, so these characters need to be removed before storing.

Fixes CV2-3582.
  • Loading branch information
caiosba committed Sep 15, 2023
1 parent 92fd5a2 commit 0ae4e59
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/annotations/dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def create_field(name, value)
f.skip_check_ability = true
f.disable_es_callbacks = self.disable_es_callbacks || value.blank?
f.field_name = name
value.gsub!('\u0000', '') if value.is_a?(String) # Avoid PG::UntranslatableCharacter exception
f.value = value
f.annotation_id = self.id
f
Expand Down
10 changes: 10 additions & 0 deletions test/models/media_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,14 @@ def setup
l = create_valid_media
assert_equal '', l.picture
end

test "should sanitize link data before storing" do
pender_url = CheckConfig.get('pender_url_private') + '/api/medias'
url = random_url
response = { type: 'media', data: { url: url, type: 'item', title: "Foo \u0000 bar" } }
WebMock.stub_request(:get, pender_url).with({ query: { url: url } }).to_return(body: response.to_json)
assert_difference 'Link.count' do
create_media url: url
end
end
end

0 comments on commit 0ae4e59

Please sign in to comment.