-
-
Notifications
You must be signed in to change notification settings - Fork 589
Several improvements about how XML is parsed. #782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2.x
Are you sure you want to change the base?
Changes from all commits
e3b1542
a7c847f
600e62f
5286274
dddb738
c8b7ada
6798442
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,8 @@ def initialize(response, settings = nil, options = {}) | |
| raise ArgumentError.new("Logoutresponse cannot be nil") if response.nil? | ||
| @settings = settings | ||
|
|
||
| raise ValidationError.new("Invalid settings type: expected RubySaml::Settings, got #{@settings.class.name}") if [email protected]_a?(Settings) && [email protected]? | ||
|
|
||
| if settings.nil? || settings.soft.nil? | ||
| @soft = true | ||
| else | ||
|
|
@@ -41,7 +43,14 @@ def initialize(response, settings = nil, options = {}) | |
|
|
||
| @options = options | ||
| @response = RubySaml::XML::Decoder.decode_message(response, @settings&.message_max_bytesize) | ||
| @document = RubySaml::XML.safe_load_nokogiri(@response) | ||
| begin | ||
| @document = RubySaml::XML.safe_load_xml(@response, check_malformed_doc: @soft) | ||
| rescue StandardError => e | ||
| @errors << "XML load failed: #{e.message}" if e.message != "Empty document" | ||
| return if @soft | ||
| raise ValidationError.new("XML load failed: #{e.message}") if e.message != "Empty document" | ||
| end | ||
|
|
||
| super() | ||
| end | ||
|
|
||
|
|
@@ -136,9 +145,13 @@ def validate_success_status | |
| # @raise [ValidationError] if soft == false and validation fails | ||
| # | ||
| def validate_structure | ||
| structure_error_msg = "Invalid SAML Logout Response. Not match the saml-schema-protocol-2.0.xsd" | ||
|
|
||
| doc_to_analize = @document.nil? ? @response : @document | ||
|
|
||
| check_malformed_doc = check_malformed_doc?(settings) | ||
| unless valid_saml?(document, soft, check_malformed_doc: check_malformed_doc) | ||
| return append_error("Invalid SAML Logout Response. Not match the saml-schema-protocol-2.0.xsd") | ||
| unless valid_saml?(doc_to_analize, soft, check_malformed_doc: check_malformed_doc) | ||
| return append_error(structure_error_msg) | ||
| end | ||
|
|
||
| true | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "ruby_saml/settings" | ||
| require "ruby_saml/xml" | ||
| require "ruby_saml/attributes" | ||
| require "time" | ||
|
|
@@ -52,18 +53,27 @@ def initialize(response, options = {}) | |
|
|
||
| @options = options | ||
| @soft = true | ||
| message_max_bytesize = nil | ||
| unless options[:settings].nil? | ||
| @settings = options[:settings] | ||
| unless @settings.soft.nil? | ||
| @soft = @settings.soft | ||
| end | ||
|
|
||
| raise ValidationError.new("Invalid settings type: expected RubySaml::Settings, got #{@settings.class.name}") if [email protected]_a?(Settings) && [email protected]? | ||
|
|
||
| @soft = @settings.respond_to?(:soft) && [email protected]? ? @settings.soft : true | ||
| message_max_bytesize = @settings.message_max_bytesize if @settings.respond_to?(:message_max_bytesize) | ||
| end | ||
|
|
||
| @response = RubySaml::XML::Decoder.decode_message(response, @settings&.message_max_bytesize) | ||
| @document = RubySaml::XML.safe_load_nokogiri(@response) | ||
| @response = RubySaml::XML::Decoder.decode_message(response, message_max_bytesize) | ||
| begin | ||
| @document = RubySaml::XML.safe_load_xml(@response, check_malformed_doc: @soft) | ||
| rescue StandardError => e | ||
| @errors << "XML load failed: #{e.message}" if e.message != 'Empty document' | ||
| return if @soft | ||
| raise ValidationError.new("XML load failed: #{e.message}") if e.message != 'Empty document' | ||
| end | ||
|
|
||
| if assertion_encrypted? | ||
| @decrypted_document = generate_decrypted_document | ||
| if [email protected]? && assertion_encrypted? | ||
| @decrypted_document = generate_decrypted_document | ||
| end | ||
|
|
||
| super() | ||
|
|
@@ -131,6 +141,8 @@ def sessionindex | |
| # @raise [ValidationError] if there are 2+ Attribute with the same Name | ||
| # | ||
| def attributes | ||
| return nil if @document.nil? | ||
|
|
||
| @attr_statements ||= begin | ||
| attributes = Attributes.new | ||
|
|
||
|
|
@@ -367,6 +379,9 @@ def assertion_id | |
| # | ||
| def validate(collect_errors = false) | ||
| reset_errors! | ||
|
|
||
| return append_error("Blank response") if @document.nil? | ||
|
|
||
| return false unless validate_response_state | ||
|
|
||
| validations = %i[ | ||
|
|
@@ -417,8 +432,10 @@ def validate_success_status | |
| def validate_structure | ||
| structure_error_msg = "Invalid SAML Response. Not match the saml-schema-protocol-2.0.xsd" | ||
|
|
||
| doc_to_analize = @document.nil? ? @response : @document | ||
|
|
||
| check_malformed_doc = check_malformed_doc_enabled? | ||
| unless valid_saml?(document, soft, check_malformed_doc: check_malformed_doc) | ||
| unless valid_saml?(doc_to_analize, soft, check_malformed_doc: check_malformed_doc) | ||
| return append_error(structure_error_msg) | ||
| end | ||
|
|
||
|
|
@@ -900,6 +917,8 @@ def validate_signature | |
| end | ||
|
|
||
| def name_id_node | ||
| return nil if @document.nil? | ||
|
|
||
| @name_id_node ||= | ||
| begin | ||
| encrypted_node = xpath_first_from_signed_assertion('/a:Subject/a:EncryptedID') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,11 +36,22 @@ def initialize(request, options = {}) | |
| @soft = true | ||
| unless options[:settings].nil? | ||
| @settings = options[:settings] | ||
| @soft = @settings.soft unless @settings.soft.nil? | ||
|
|
||
| raise ValidationError.new("Invalid settings type: expected RubySaml::Settings, got #{@settings.class.name}") if [email protected]_a?(Settings) && [email protected]? | ||
|
|
||
| @soft = @settings.respond_to?(:soft) && [email protected]? ? @settings.soft : true | ||
| message_max_bytesize = @settings.message_max_bytesize if @settings.respond_to?(:message_max_bytesize) | ||
| end | ||
|
|
||
| @request = RubySaml::XML::Decoder.decode_message(request, message_max_bytesize) | ||
| begin | ||
| @document = RubySaml::XML.safe_load_xml(@request, check_malformed_doc: @soft) | ||
| rescue StandardError => e | ||
| @errors << "XML load failed: #{e.message}" if e.message != 'Empty document' | ||
| return if @soft | ||
| raise ValidationError.new("XML load failed: #{e.message}") if e.message != 'Empty document' | ||
| end | ||
|
|
||
| @request = RubySaml::XML::Decoder.decode_message(request, @settings&.message_max_bytesize) | ||
| @document = RubySaml::XML.safe_load_nokogiri(@request) | ||
| super() | ||
| end | ||
|
|
||
|
|
@@ -157,6 +168,8 @@ def validate(collect_errors = false) | |
| # @return [Boolean] True if the Logout Request contains an ID, otherwise returns False | ||
| # | ||
| def validate_id | ||
| return append_error("Missing ID attribute on Logout Request") if document.nil? | ||
|
|
||
| return true if id | ||
| append_error("Missing ID attribute on Logout Request") | ||
| end | ||
|
|
@@ -166,6 +179,8 @@ def validate_id | |
| # @return [Boolean] True if the Logout Request is 2.0, otherwise returns False | ||
| # | ||
| def validate_version | ||
| return append_error("Unsupported SAML version") if document.nil? | ||
|
|
||
| return true if version(document) == "2.0" | ||
| append_error("Unsupported SAML version") | ||
| end | ||
|
|
@@ -191,8 +206,10 @@ def validate_not_on_or_after | |
| # @raise [ValidationError] if soft == false and validation fails | ||
| # | ||
| def validate_structure | ||
| doc_to_analize = @document.nil? ? @request : @document | ||
|
|
||
| check_malformed_doc = check_malformed_doc?(settings) | ||
| unless valid_saml?(document, soft, check_malformed_doc: check_malformed_doc) | ||
| unless valid_saml?(doc_to_analize, soft, check_malformed_doc: check_malformed_doc) | ||
| return append_error("Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd") | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,49 +49,34 @@ module XML | |
| NOKOGIRI_OPTIONS = Nokogiri::XML::ParseOptions::STRICT | | ||
| Nokogiri::XML::ParseOptions::NONET | ||
|
|
||
| # TODO: safe_load_message (rename safe_load_nokogiri --> safe_load_xml) | ||
| # def safe_load_message(message, check_malformed_doc: true) | ||
| # message = Decoder.decode(message) | ||
| # begin | ||
| # safe_load_nokogiri(message, check_malformed_doc: check_malformed_doc) | ||
| # rescue RubySaml::Errors::XMLLoadError | ||
| # Nokogiri::XML::Document.new | ||
| # end | ||
| # end | ||
|
|
||
| # Safely load the SAML Message XML. | ||
| # @param document [String | Nokogiri::XML::Document] The message to be loaded | ||
| # @param check_malformed_doc [Boolean] check_malformed_doc Enable or Disable the check for malformed XML | ||
| # @return [Nokogiri::XML::Document] The nokogiri document | ||
| # @raise [ValidationError] If there was a problem loading the SAML Message XML | ||
| def safe_load_nokogiri(document, check_malformed_doc: true) | ||
| # @raise [StandardError] If there was a problem loading the SAML Message XML | ||
| def safe_load_xml(document, check_malformed_doc: true) | ||
| doc_str = document.to_s | ||
| error = nil | ||
| error = StandardError.new('Dangerous XML detected. No Doctype nodes allowed') if doc_str.include?('<!DOCTYPE') | ||
|
|
||
| xml = nil | ||
| unless error | ||
| begin | ||
| xml = Nokogiri::XML(doc_str) do |config| | ||
| config.options = NOKOGIRI_OPTIONS | ||
| end | ||
| rescue StandardError => e | ||
| error ||= e | ||
| # raise StandardError.new(e.message) | ||
| raise StandardError.new('Dangerous XML detected. No Doctype nodes allowed') if doc_str.include?('<!DOCTYPE') | ||
|
|
||
| begin | ||
| doc = Nokogiri::XML(doc_str) do |config| | ||
| config.options = NOKOGIRI_OPTIONS | ||
| end | ||
| rescue StandardError => e | ||
| raise StandardError.new(e.message) | ||
| rescue SyntaxError => e | ||
| raise StandardError.new(e.message) if check_malformed_doc && e.message != 'Empty document' | ||
| end | ||
|
|
||
| # TODO: This is messy, its shims how the old REXML parser works | ||
| if xml | ||
| error ||= StandardError.new('Dangerous XML detected. No Doctype nodes allowed') if xml.internal_subset | ||
| error ||= StandardError.new("There were XML errors when parsing: #{xml.errors}") if check_malformed_doc && !xml.errors.empty? | ||
| if doc.is_a?(Nokogiri::XML::Document) | ||
| StandardError.new('Dangerous XML detected. No Doctype nodes allowed') if doc.internal_subset | ||
| StandardError.new("There were XML errors when parsing: #{doc.errors}") if check_malformed_doc && !doc.errors.empty? | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should probably be specialized Error classes, rather than StandardError |
||
| end | ||
| return Nokogiri::XML::Document.new if error || !xml | ||
|
|
||
| xml | ||
| doc | ||
| end | ||
|
|
||
| def copy_nokogiri(noko) | ||
| def copy_xml(noko) | ||
| Nokogiri::XML(noko.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)) do |config| | ||
| config.options = NOKOGIRI_OPTIONS | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,11 @@ module DocumentSigner | |
| # <Object /> | ||
| # </Signature> | ||
| def sign_document(document, private_key, certificate, signature_method = RubySaml::XML::RSA_SHA256, digest_method = RubySaml::XML::SHA256) | ||
| noko = RubySaml::XML.safe_load_nokogiri(document.to_s) | ||
| begin | ||
| noko = RubySaml::XML.safe_load_xml(document.to_s, check_malformed_doc: true) | ||
| rescue StandardError => e | ||
| raise ValidationError.new("XML load failed: #{e.message}") if e.message != 'Empty document' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error raising should probably be part of the |
||
| end | ||
|
|
||
| sign_document!(noko, private_key, certificate, signature_method, digest_method) | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: should be
doc_to_analyze(y instead of i)