Commit d5fa616f authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Handle missing reference parsers gracefully

When a redacting references and the particular reference is no longer
valid, we should just treat it as an inaccessible reference instead of
raising an exception.

This can happen when are redacting stale HTML that has some references
that we already removed.
parent 8b161d60
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
module Banzai module Banzai
module ReferenceParser module ReferenceParser
InvalidReferenceType = Class.new(StandardError)
# Returns the reference parser class for the given type # Returns the reference parser class for the given type
# #
# Example: # Example:
...@@ -11,6 +13,8 @@ module Banzai ...@@ -11,6 +13,8 @@ module Banzai
# This would return the `Banzai::ReferenceParser::IssueParser` class. # This would return the `Banzai::ReferenceParser::IssueParser` class.
def self.[](name) def self.[](name)
const_get("#{name.to_s.camelize}Parser", false) const_get("#{name.to_s.camelize}Parser", false)
rescue NameError
raise InvalidReferenceType
end end
end end
end end
...@@ -111,6 +111,7 @@ module Banzai ...@@ -111,6 +111,7 @@ module Banzai
parser = Banzai::ReferenceParser[type].new(context) parser = Banzai::ReferenceParser[type].new(context)
visible.merge(parser.nodes_visible_to_user(user, nodes)) visible.merge(parser.nodes_visible_to_user(user, nodes))
rescue Banzai::ReferenceParser::InvalidReferenceType
end end
visible visible
......
...@@ -182,5 +182,12 @@ RSpec.describe Banzai::ReferenceRedactor do ...@@ -182,5 +182,12 @@ RSpec.describe Banzai::ReferenceRedactor do
expect(redactor.nodes_visible_to_user([node])).to eq(Set.new([node])) expect(redactor.nodes_visible_to_user([node])).to eq(Set.new([node]))
end end
it 'handles invalid references gracefully' do
doc = Nokogiri::HTML.fragment('<a data-reference-type="some_invalid_type"></a>')
node = doc.children[0]
expect(redactor.nodes_visible_to_user([node])).to be_empty
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment