-
-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: spec! Sanitise existing long descriptions of enterprises
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
db/migrate/20240510033206_sanitize_enterprise_long_description.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
class SanitizeEnterpriseLongDescription < ActiveRecord::Migration[7.0] | ||
class Enterprise < ApplicationRecord | ||
end | ||
|
||
# This is a copy from our application code at the time of writing. | ||
# We prefer to keep migrations isolated and not affected by changing | ||
# application code in the future. | ||
# If we need to change the sanitizer in the future we may need a new | ||
# migration (not change the old one) to sanitise the data properly. | ||
class HtmlSanitizer | ||
ALLOWED_TAGS = %w[h1 h2 h3 h4 div p br b i u a strong em del pre blockquote ul ol li hr | ||
figure].freeze | ||
ALLOWED_ATTRIBUTES = %w[href target].freeze | ||
ALLOWED_TRIX_DATA_ATTRIBUTES = %w[data-trix-attachment].freeze | ||
|
||
def self.sanitize(html) | ||
@sanitizer ||= Rails::HTML5::SafeListSanitizer.new | ||
@sanitizer.sanitize( | ||
html, tags: ALLOWED_TAGS, attributes: (ALLOWED_ATTRIBUTES + ALLOWED_TRIX_DATA_ATTRIBUTES) | ||
) | ||
end | ||
end | ||
|
||
def up | ||
Enterprise.where.not(long_description: [nil, ""]).find_each do |enterprise| | ||
enterprise.update!(long_description: sanitize(long_description)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def sanitize(html) | ||
HtmlSanitizer.sanitize(super) | ||
end | ||
end |