replace invalid characters from xml

This commit is contained in:
Benjamin Neff 2016-05-31 04:06:41 +02:00
parent 653b0fe276
commit e5203182bf
2 changed files with 12 additions and 1 deletions

View file

@ -36,6 +36,10 @@ module DiasporaFederation
extend PropertiesDSL
include Logging
# Invalid XML characters
# @see https://www.w3.org/TR/REC-xml/#charsets "Extensible Markup Language (XML) 1.0"
INVALID_XML_REGEX = /[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/
# Initializes the Entity with the given attribute hash and freezes the created
# instance it returns.
#
@ -230,7 +234,7 @@ module DiasporaFederation
def simple_node(doc, name, value)
xml_name = self.class.xml_names[name]
Nokogiri::XML::Element.new(xml_name ? xml_name.to_s : name, doc).tap do |node|
node.content = value unless value.empty?
node.content = value.gsub(INVALID_XML_REGEX, "\uFFFD") unless value.empty?
end
end

View file

@ -91,6 +91,13 @@ module DiasporaFederation
expect(%w(test1 test2 test3 test4)).to include(node.name)
end
end
it "replaces invalid XML characters" do
entity = Entities::TestEntity.new(test: "asdfasdf asdf💩asdf\nasdf")
xml = entity.to_xml.to_xml
parsed = Entities::TestEntity.from_xml(Nokogiri::XML::Document.parse(xml).root).test
expect(parsed).to eq("asdf<EFBFBD>asdf asdf💩asdf\nasdf")
end
end
describe ".from_xml" do