[PATCH conductor] Deployable XML parsing switched to strict mode

jprovazn at redhat.com jprovazn at redhat.com
Mon Dec 19 10:02:58 UTC 2011


From: Jan Provaznik <jprovazn at redhat.com>

https://bugzilla.redhat.com/show_bug.cgi?id=766993
It's not possible to have now wrongly formatted document (missing tags),
DeployableXML raises exception now in this case.
---
 src/app/models/deployable.rb       |   10 +++++++---
 src/app/models/deployment.rb       |    2 ++
 src/app/util/deployable_xml.rb     |    2 +-
 src/spec/models/deployable_spec.rb |    8 ++++++++
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/app/models/deployable.rb b/src/app/models/deployable.rb
index 59c4378..c9446f2 100644
--- a/src/app/models/deployable.rb
+++ b/src/app/models/deployable.rb
@@ -41,9 +41,13 @@ class Deployable < ActiveRecord::Base
   PRESET_FILTERS_OPTIONS = []
 
   def valid_deployable_xml?
-    deployable_xml = DeployableXML.new(xml)
-    unless deployable_xml.validate!
-      errors.add(:xml, I18n.t('catalog_entries.flash.warning.not_valid'))
+    begin
+      deployable_xml = DeployableXML.new(xml)
+      unless deployable_xml.validate!
+        errors.add(:xml, I18n.t('catalog_entries.flash.warning.not_valid'))
+      end
+    rescue Nokogiri::XML::SyntaxError => e
+      errors.add(:base, I18n.t("deployments.errors.not_valid_deployable_xml", :msg => "#{e.message}"))
     end
   end
 
diff --git a/src/app/models/deployment.rb b/src/app/models/deployment.rb
index 7b5ce76..ea62c33 100644
--- a/src/app/models/deployment.rb
+++ b/src/app/models/deployment.rb
@@ -79,6 +79,8 @@ class Deployment < ActiveRecord::Base
       deployable_xml.validate!
     rescue DeployableXML::ValidationError => e
       errors.add(:deployable_xml, e.message)
+    rescue Nokogiri::XML::SyntaxError => e
+      errors.add(:base, I18n.t("deployments.errors.not_valid_deployable_xml", :msg => "#{e.message}"))
     end
   end
 
diff --git a/src/app/util/deployable_xml.rb b/src/app/util/deployable_xml.rb
index 8295340..995cfff 100644
--- a/src/app/util/deployable_xml.rb
+++ b/src/app/util/deployable_xml.rb
@@ -173,7 +173,7 @@ class DeployableXML
     if xmlstr_or_node.is_a? Nokogiri::XML::Node
       @root = xmlstr_or_node
     elsif xmlstr_or_node.is_a? String
-      @doc = Nokogiri::XML(xmlstr_or_node)
+      @doc = Nokogiri::XML(xmlstr_or_node) { |config| config.strict }
       @root = @doc.root.at_xpath("/deployable") if @doc.root
     end
     @relax_file = "#{File.dirname(File.expand_path(__FILE__))}/deployable-rng.xml"
diff --git a/src/spec/models/deployable_spec.rb b/src/spec/models/deployable_spec.rb
index 9f7cbaf..cf4bb1d 100644
--- a/src/spec/models/deployable_spec.rb
+++ b/src/spec/models/deployable_spec.rb
@@ -30,4 +30,12 @@ describe Deployable do
     doc.at_xpath('/deployable/assemblies/assembly')[:name].should eql(image.name)
     doc.at_xpath('/deployable/assemblies/assembly/image')[:id].should eql(image.uuid)
   end
+
+  it "should not be valid if xml is not parsable" do
+    deployable = FactoryGirl.build(:deployable)
+    deployable.should be_valid
+    deployable.xml << "</deployable>"
+    deployable.should_not be_valid
+  end
+
 end
-- 
1.7.6




More information about the aeolus-devel mailing list