Skip to content

Commit 9652b84

Browse files
committed
More cleanups
1 parent 2304fba commit 9652b84

File tree

4 files changed

+16
-29
lines changed

4 files changed

+16
-29
lines changed

lib/ruby_saml/response.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,9 @@ def validate_name_id
797797
end
798798

799799
def doc_to_validate
800-
# If the response contains the signature, and the assertion was encrypted, validate the original SAML Response
801-
# otherwise, review if the decrypted assertion contains a signature
800+
# Validate the original SAML Response if the response contains the signature,
801+
# and the assertion was encrypted. Otherwise, review if the decrypted assertion
802+
# contains a signature.
802803
subject_id = RubySaml::XML::SignedDocumentValidator.subject_id(document)
803804
return decrypted_document unless subject_id
804805

@@ -963,7 +964,6 @@ def xpath_from_signed_assertion(subpath = nil)
963964
# @return [RubySaml::XML::SignedDocument] The SAML Response with the assertion decrypted
964965
#
965966
def generate_decrypted_document
966-
# TODO: try decrypt_document! instead
967967
RubySaml::XML::Decryptor.decrypt_document(document, settings&.get_sp_decryption_keys)
968968
end
969969

lib/ruby_saml/xml/decryptor.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,8 @@ module Decryptor
1111
# @param decryption_keys [Array] Array of private keys for decryption
1212
# @return [Nokogiri::XML::Document] The SAML document with assertions decrypted
1313
def decrypt_document(document, decryption_keys)
14-
document_copy = RubySaml::XML.safe_load_nokogiri(document.to_s)
15-
decrypt_document!(document_copy, decryption_keys)
16-
end
17-
18-
# Modifies a SAML document to decrypt its EncryptedAssertion element into an Assertion element.
19-
# @param document [Nokogiri::XML::Document] The SAML document with the encrypted assertion
20-
# @param decryption_keys [Array] Array of private keys for decryption
21-
# @return [Nokogiri::XML::Document] The SAML document with the assertion decrypted
22-
def decrypt_document!(document, decryption_keys)
14+
# Copy the document
15+
document = RubySaml::XML.safe_load_nokogiri(document.to_s)
2316
validate_decryption_keys!(decryption_keys)
2417

2518
response_node = document.at_xpath(

test/response_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,9 +1787,7 @@ def generate_audience_error(expected, actual)
17871787
end
17881788
end
17891789

1790-
idp_key_algo = :rsa
1791-
idp_hash_algo = :sha256
1792-
# each_signature_algorithm do |idp_key_algo, idp_hash_algo|
1790+
each_signature_algorithm do |idp_key_algo, idp_hash_algo|
17931791
describe "#validate_signature" do
17941792
let(:xml_signed) do
17951793
doc = read_response('response_unsigned2.xml')
@@ -1869,7 +1867,7 @@ def generate_audience_error(expected, actual)
18691867
assert_includes response_sign_test.errors, 'Invalid Signature on SAML Response'
18701868
end
18711869
end
1872-
# end
1870+
end
18731871
end
18741872
end
18751873
end

test/xml/decryptor_test.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,22 @@ class XmlDecryptorTest < Minitest::Test
4242

4343
refute_nil decrypted_doc.at_xpath('/p:Response/a:Assertion', { 'p' => RubySaml::XML::NS_PROTOCOL, 'a' => RubySaml::XML::NS_ASSERTION })
4444
end
45-
end
46-
47-
describe '#decrypt_document!' do
48-
it 'should decrypt an encrypted assertion in a document' do
49-
decrypted_doc = RubySaml::XML::Decryptor.decrypt_document!(noko_encrypted_assertion_doc, decryption_keys)
50-
51-
# The encrypted assertion should be removed
52-
assert_nil decrypted_doc.at_xpath('/p:Response/EncryptedAssertion', { 'p' => RubySaml::XML::NS_PROTOCOL })
53-
54-
# An assertion should now be present
55-
refute_nil decrypted_doc.at_xpath('/p:Response/a:Assertion', { 'p' => RubySaml::XML::NS_PROTOCOL, 'a' => RubySaml::XML::NS_ASSERTION })
56-
end
5745

5846
it 'should handle documents without an encrypted assertion' do
5947
doc_without_encrypted_assertion = Nokogiri::XML("<Response xmlns='urn:oasis:names:tc:SAML:2.0:protocol'><Assertion xmlns='urn:oasis:names:tc:SAML:2.0:assertion'></Assertion></Response>")
60-
result = RubySaml::XML::Decryptor.decrypt_document!(doc_without_encrypted_assertion, decryption_keys)
48+
result = RubySaml::XML::Decryptor.decrypt_document(doc_without_encrypted_assertion, decryption_keys)
6149

6250
# Should return the document unmodified
6351
assert_equal doc_without_encrypted_assertion.to_s, result.to_s
6452
end
53+
54+
it 'does not modify the original document' do
55+
original = document_encrypted_assertion.to_s
56+
RubySaml::XML::Decryptor.decrypt_document(document_encrypted_assertion, decryption_keys)
57+
58+
# The original document should not be modified
59+
assert_equal original, document_encrypted_assertion.to_s
60+
end
6561
end
6662

6763
describe '#decrypt_assertion' do

0 commit comments

Comments
 (0)