From: Matt Amos Date: Thu, 6 Aug 2009 17:28:49 +0000 (+0000) Subject: Made XML parsing routines raise an exception if the document is valid XML, but not... X-Git-Tag: live~6738 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/bb84a78a090db5db95963653ccba89233415c508 Made XML parsing routines raise an exception if the document is valid XML, but not valid as an OSM document. This is now the same behaviour as when the document isn't valid XML. --- diff --git a/app/models/node.rb b/app/models/node.rb index dd8d96d12..df6442b83 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -70,6 +70,7 @@ class Node < ActiveRecord::Base doc.find('//osm/node').each do |pt| return Node.from_xml_node(pt, create) end + raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/node element.") rescue LibXML::XML::Error, ArgumentError => ex raise OSM::APIBadXMLError.new("node", xml, ex.message) end diff --git a/app/models/relation.rb b/app/models/relation.rb index 76cd86729..e3ba69b56 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -33,6 +33,7 @@ class Relation < ActiveRecord::Base doc.find('//osm/relation').each do |pt| return Relation.from_xml_node(pt, create) end + raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/relation element.") rescue LibXML::XML::Error, ArgumentError => ex raise OSM::APIBadXMLError.new("relation", xml, ex.message) end diff --git a/app/models/way.rb b/app/models/way.rb index e26418732..639f4e69a 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -34,6 +34,7 @@ class Way < ActiveRecord::Base doc.find('//osm/way').each do |pt| return Way.from_xml_node(pt, create) end + raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/way element.") rescue LibXML::XML::Error, ArgumentError => ex raise OSM::APIBadXMLError.new("way", xml, ex.message) end diff --git a/test/functional/node_controller_test.rb b/test/functional/node_controller_test.rb index 0595f3d08..b5f93c458 100644 --- a/test/functional/node_controller_test.rb +++ b/test/functional/node_controller_test.rb @@ -75,6 +75,12 @@ class NodeControllerTest < ActionController::TestCase lat = 3.434 lon = 3.23 + # test that the upload is rejected when xml is valid, but osm doc isn't + content("") + put :create + assert_response :bad_request, "node upload did not return bad_request status" + assert_equal "Cannot parse valid node from xml string . XML doesn't contain an osm/node element.", @response.body + # test that the upload is rejected when no lat is supplied # create a minimal xml file content("") @@ -184,6 +190,12 @@ class NodeControllerTest < ActionController::TestCase assert_response :bad_request, "should not be able to delete a node with a different ID from the XML" + # try to delete a node rubbish in the payloads + content("") + delete :delete, :id => current_nodes(:visible_node).id + assert_response :bad_request, + "should not be able to delete a node without a valid XML payload" + # valid delete now takes a payload content(nodes(:public_visible_node).to_xml) delete :delete, :id => current_nodes(:public_visible_node).id @@ -353,6 +365,12 @@ class NodeControllerTest < ActionController::TestCase assert_response :bad_request, "should not be able to update a node with a different ID from the XML" + ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc. + content "" + put :update, :id => current_nodes(:visible_node).id + assert_response :bad_request, + "should not be able to update a node with non-OSM XML doc." + ## finally, produce a good request which should work content current_nodes(:public_visible_node).to_xml put :update, :id => current_nodes(:public_visible_node).id