Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitelist allowed classes for federated statuses #3810

Merged
merged 2 commits into from
Jun 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion app/lib/sanitize_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ class Sanitize
module Config
HTTP_PROTOCOLS ||= ['http', 'https', :relative].freeze

CLASS_WHITELIST_TRANSFORMER = lambda do |env|
node = env[:node]
class_list = node['class']&.split(' ')

return unless class_list

class_list.keep_if do |e|
return true if e =~ /^(h|p|u|dt|e)-/ # microformats classes
return true if e =~ /^(mention|hashtag)$/ # semantic classes
return true if e =~ /^(ellipsis|invisible)$/ # link formatting classes
end

node['class'] = class_list.join(' ')
end

MASTODON_STRICT ||= freeze_config(
elements: %w(p br span a),

Expand All @@ -21,7 +36,11 @@ module Config

protocols: {
'a' => { 'href' => HTTP_PROTOCOLS },
}
},

transformers: [
CLASS_WHITELIST_TRANSFORMER,
]
)

MASTODON_OEMBED ||= freeze_config merge(
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@
is_expected.to_not include '<script>alert("Hello")</script>'
end
end

context 'contains malicious classes' do
let(:text) { '<span class="status__content__spoiler-link">Show more</span>' }

it 'strips malicious classes' do
is_expected.to_not include 'status__content__spoiler-link'
end
end
end

describe '#plaintext' do
Expand Down