@@ -13,7 +13,11 @@ class SignedDocumentInfo
1313 # @param noko [Nokogiri::XML] The XML document to validate
1414 # @param check_malformed_doc [Boolean] Whether to check for malformed documents
1515 def initialize ( noko , check_malformed_doc : true )
16- noko = RubySaml ::XML . safe_load_nokogiri ( noko , check_malformed_doc : check_malformed_doc ) unless noko . is_a? ( Nokogiri ::XML ::Document )
16+ noko = if noko . is_a? ( Nokogiri ::XML ::Document )
17+ RubySaml ::XML . copy_nokogiri ( noko )
18+ else
19+ RubySaml ::XML . safe_load_nokogiri ( noko , check_malformed_doc : check_malformed_doc )
20+ end
1721 @noko = noko
1822 @check_malformed_doc = check_malformed_doc
1923 end
@@ -52,16 +56,8 @@ def validate_signature(cert)
5256
5357 # Compare digest
5458 calculated_digest = digest_algorithm . digest ( canonicalized_subject )
55- # puts "calculated_digest: #{calculated_digest.bytes}"
56- # puts "digest_value: #{digest_value.bytes}"
57- # puts "subject" + canonicalized_subject.inspect
58- # puts "\n\n\n\n\n\n"
5959 raise RubySaml ::ValidationError . new ( 'Digest mismatch' ) unless calculated_digest == digest_value
6060
61- # puts "signature_hash_algorithm: #{signature_hash_algorithm}"
62- # puts "signature_value: #{signature_value.bytes}"
63- # puts "canonicalized_signed_info: #{canonicalized_signed_info.inspect}"
64-
6561 # Verify signature
6662 signature_verified = false
6763 begin
@@ -113,10 +109,8 @@ def reference_node
113109 # Get the ID of the signed element
114110 # @return [String] The ID of the signed element
115111 def subject_id
116- id = uri_from_reference_node || signature_node . parent &.[]( 'ID' )
117- return id unless !id || id . empty?
118-
119- raise RubySaml ::ValidationError . new ( 'No signed subject ID found' )
112+ # TODO: The error here is problematic, perhaps it can be checked elsewhere
113+ @subject_id ||= extract_subject_id || ( raise RubySaml ::ValidationError . new ( 'No signed subject ID found' ) )
120114 end
121115
122116 # Get the subject node (the node being signed)
@@ -136,8 +130,10 @@ def canonicalized_subject
136130 # TODO: Destructive side-effect!! signature_node.remove
137131 # should possibly deep copy the noko object initially
138132 def remove_signature_node!
139- inclusive_namespaces # memoize this
140- canonicalized_signed_info # memoize this
133+ # memoize various elements
134+ subject_id
135+ inclusive_namespaces
136+ canonicalized_signed_info
141137
142138 signature_node . remove
143139 end
@@ -205,6 +201,12 @@ def inclusive_namespaces
205201
206202 private
207203
204+ def extract_subject_id
205+ return unless reference_node
206+
207+ reference_node [ 'URI' ] [ 1 ..] || signature_node . parent [ 'ID' ]
208+ end
209+
208210 # Get the ds:Signature element from the document
209211 # @return [Nokogiri::XML::Element] The Signature element
210212 def signature_node
@@ -236,11 +238,6 @@ def canon_algorithm_from_transforms
236238 transform_element = transforms . reverse . detect { |el | el [ 'Algorithm' ] }
237239 RubySaml ::XML . canon_algorithm ( transform_element , default : false )
238240 end
239-
240- def uri_from_reference_node
241- uri = reference_node &.[]( 'URI' ) &.delete_prefix ( '#' )
242- uri unless !uri || uri . empty?
243- end
244241 end
245242 end
246243end
0 commit comments