Skip to content

Commit

Permalink
Fix exception when soap:Fault contains invalid encoding (#923)
Browse files Browse the repository at this point in the history
Co-authored-by: David Escala <[email protected]>
  • Loading branch information
2 people authored and pcai committed Jul 15, 2024
1 parent 87e87eb commit f52f645
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/savon/soap_fault.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ module Savon
class SOAPFault < Error

def self.present?(http, xml = nil)
xml ||= http.body
xml_orig ||= http.body
if xml_orig.valid_encoding?
xml = xml_orig
else
xml = xml_orig.encode(
'UTF-8', "ISO-8859-1", invalid: :replace, undef: :replace, replace: ''
)
end
fault_node = xml.include?("Fault>")
soap1_fault = xml.match(/faultcode\/?>/) && xml.match(/faultstring\/?>/)
soap2_fault = xml.include?("Code>") && xml.include?("Reason>")
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/response/soap_fault_invalid_encoding.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Response with invalid encoding ñ ñ</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
5 changes: 5 additions & 0 deletions spec/savon/soap_fault_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
it "returns false unless the HTTP response contains a SOAP fault" do
expect(Savon::SOAPFault.present? new_response).to be_falsey
end

it "returns true even if the HTTP response contains a SOAP fault with invalid encoding" do
http = new_response(:body => Fixture.response(:soap_fault_invalid_encoding))
expect(Savon::SOAPFault.present? http).to be_truthy
end
end

[:message, :to_s].each do |method|
Expand Down

0 comments on commit f52f645

Please sign in to comment.