diff --git a/lib/sanitize_ext/sanitize_config.rb b/lib/sanitize_ext/sanitize_config.rb
index f30eec476c..3c3d697eda 100644
--- a/lib/sanitize_ext/sanitize_config.rb
+++ b/lib/sanitize_ext/sanitize_config.rb
@@ -83,8 +83,10 @@ class Sanitize
# next, we find the plain-text description
is_annotation_with_encoding = lambda do |encoding, node|
return false unless node.name == 'annotation'
+ encoding_attr = node.attributes['encoding']
+ return false if encoding_attr.nil?
- node.attributes['encoding'].value == encoding
+ encoding_attr.value == encoding
end
annotation = semantics.children.find(&is_annotation_with_encoding.curry['application/x-tex'])
diff --git a/spec/lib/sanitize/config_spec.rb b/spec/lib/sanitize/config_spec.rb
index b4c849c427..7baa1423b7 100644
--- a/spec/lib/sanitize/config_spec.rb
+++ b/spec/lib/sanitize/config_spec.rb
@@ -38,6 +38,14 @@ RSpec.describe Sanitize::Config do
expect(Sanitize.fragment('Test<a href="https://example.com">test</a>', subject)).to eq 'Test<a href="https://example.com">test</a>'
end
+ it 'removes math when unparsable due to missing attributes' do
+ expect(Sanitize.fragment('', subject)).to eq ''
+ end
+
+ it 'removes math when unparsable due to missing encoding attribute' do
+ expect(Sanitize.fragment('', subject)).to eq ''
+ end
+
it 'keeps a with href' do
expect(Sanitize.fragment('Test', subject)).to eq 'Test'
end