Skip to content

Commit 4893b5d

Browse files
committed
Fixed hard and soft validation
2 parents 5173f45 + a95f86a commit 4893b5d

4 files changed

Lines changed: 33 additions & 3 deletions

File tree

lib/onelogin/ruby-saml/response.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def success?
9797
end
9898
end
9999

100+
def status_message
101+
@status_message ||= begin
102+
node = REXML::XPath.first(document, "/p:Response/p:Status/p:StatusMessage", { "p" => PROTOCOL, "a" => ASSERTION })
103+
node.text if node
104+
end
105+
end
106+
100107
# Conditions (if any) for the assertion to run
101108
def conditions
102109
@conditions ||= xpath_first_from_signed_assertion('/a:Conditions')
@@ -129,7 +136,15 @@ def validate(soft = true)
129136
validate_response_state(soft) &&
130137
validate_conditions(soft) &&
131138
document.validate_document(get_fingerprint, soft) &&
132-
success?
139+
validate_success_status(soft)
140+
end
141+
142+
def validate_success_status(soft = true)
143+
if success?
144+
true
145+
else
146+
soft ? false : validation_error(status_message)
147+
end
133148
end
134149

135150
def validate_structure(soft = true)

lib/xml_security.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ def initialize(response)
4747
def validate_document(idp_cert_fingerprint, soft = true)
4848
# get cert from response
4949
cert_element = REXML::XPath.first(self, "//ds:X509Certificate", { "ds"=>DSIG })
50-
raise OneLogin::RubySaml::ValidationError.new("Certificate element missing in response (ds:X509Certificate)") unless cert_element
50+
unless cert_element
51+
if soft
52+
return false
53+
else
54+
raise OneLogin::RubySaml::ValidationError.new("Certificate element missing in response (ds:X509Certificate)")
55+
end
56+
end
5157
base64_cert = cert_element.text
5258
cert_text = Base64.decode64(base64_cert)
5359
cert = OpenSSL::X509::Certificate.new(cert_text)

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def r1_response_document_6
4646
end
4747

4848
def ampersands_response
49-
@ampersands_resposne ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'response_with_ampersands.xml.base64'))
49+
@ampersands_response ||= File.read(File.join(File.dirname(__FILE__), 'responses', 'response_with_ampersands.xml.base64'))
5050
end
5151

5252
def response_document_6

test/xml_security_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ class XmlSecurityTest < Test::Unit::TestCase
2626
end
2727
end
2828

29+
should "not raise an error when softly validating the document and the X509Certificate is missing" do
30+
response = Base64.decode64(response_document)
31+
response.sub!(/<ds:X509Certificate>.*<\/ds:X509Certificate>/, "")
32+
document = XMLSecurity::SignedDocument.new(response)
33+
assert_nothing_raised do
34+
assert !document.validate_document("a fingerprint", true) # The fingerprint isn't relevant to this test
35+
end
36+
end
37+
2938
should "should raise Fingerprint mismatch" do
3039
exception = assert_raise(OneLogin::RubySaml::ValidationError) do
3140
@document.validate_document("no:fi:ng:er:pr:in:t", false)

0 commit comments

Comments
 (0)