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

Add valid_encoding checking before inserting into xml object. #318

Merged
merged 3 commits into from
Dec 15, 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
6 changes: 3 additions & 3 deletions lib/gems/pending/util/xml/miq_rexml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def initialize(first, second = nil, parent = nil)
@value = Text.unnormalize(second.to_s, nil)
rescue => err
if err.class == ::Encoding::CompatibilityError
second_utf8 = second.to_s.force_encoding('UTF-8')
@value = Text.unnormalize(second_utf8, nil)
second_utf8 = second.to_s.force_encoding('UTF-8').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '')
@value = Text.unnormalize(second_utf8)
else
$log.error "Encoding error: #{second_utf8}" if $log
end
Expand Down Expand Up @@ -223,7 +223,7 @@ def to_xml
def add_element(element, attrs = nil)
return add_element_orig(element) if element.kind_of?(REXML::Element)
attrs.delete_if { |_k, v| v.nil? } unless attrs.nil?
add_element_orig(element.to_s, XmlHelpers.stringify_keys(attrs))
add_element_orig(element.to_s, XmlHelpers.validate_attrs(attrs))
end

alias_method :add_attribute_orig, :add_attribute
Expand Down
17 changes: 17 additions & 0 deletions lib/gems/pending/util/xml/xml_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@ def self.findElementInt(paths, ele)
end

class XmlHelpers
def self.remove_invalid_chars(str)
str.chars.reject do |c|
case c.ord
when *REXML::Text::VALID_CHAR
else
''
end
end.join
end

# Validate attributes before inserting into xml
def self.validate_attrs(h)
return nil if h.nil?
h.each_with_object({}) { |(k, v), h| h[k.to_s] = remove_invalid_chars(v.to_s.encode('UTF-8', :undef => :replace, :invalid => :replace, :replace => '')) }
end

def self.stringify_keys(h)
return nil if h.nil?
h.inject({}) { |options, (key, value)| options[key.to_s] = value; options }
end
end

class Xml2tags

def self.walk(node, parents = "")
tags = []

Expand Down
7 changes: 7 additions & 0 deletions spec/util/xml/miq_rexml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@
xml = MiqXml.load(doc_text)
expect(xml.root.elements[1].attributes['attr1']).to eq(attr_string)
end

it "add_element with control characters" do
attr_hash = { "attr1" => "test\u000Fst\u001Fring" }
doc = MiqXml.createDoc(nil)
doc.add_element('element_1', attr_hash)
expect(doc.elements[1].attributes['attr1']).to eq("teststring")
end
end