-
-
Notifications
You must be signed in to change notification settings - Fork 591
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 |
|---|---|---|
|
|
@@ -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)