Skip to content
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

Introduce thread safety to SAML schema validation #175

Merged
merged 1 commit into from
Feb 11, 2015

Conversation

phlipper
Copy link
Contributor

Per the references below, use of Dir.chdir is not thread-safe. This usage was causing exceptions to be raised when running on Puma and in other multi-threaded environments.

This patch also moves the schema read up to a class instance, as this data is static and does not need to be read every time an assertion is validated. This boosts performance, especially in environments with higher throughput.

Thanks to @dannyb for the assistance!

References:

@xml = Nokogiri::XML(document.to_s)

@schema.validate(@xml).map do |error|
return false if soft

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return will raise an error if soft. You should use break or next

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daniel-g what error will this raise? If you check the previous line 20, it was doing exactly the same thing. The tests are also passing, so I'm hoping I can trust those.

My goal is not to change all the functionality, my goal is to solve the problem given the tools available.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @phlipper . In ruby 2.1.2:

2.1.2 :002 > [1, 2, 3, 4].map{|f| return(f) }
LocalJumpError: unexpected return
    from (irb):2:in `block in irb_binding'
    from (irb):2:in `map'
    from (irb):2

in ruby 1.8.7:

[1, 2, 3, 4].map{|f| return(f) }
LocalJumpError: unexpected return
    from (irb):1
    from (irb):1:in `map'
    from (irb):1

So I wonder how the tests still passed. What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this shows there is some test coverage lacking somewhere.

@phlipper
Copy link
Contributor Author

@daniel-g per your suggestions, I have updated the use of break vs. return and I have eliminated some useless instance variables. How does this look to you now?

SamlMessage.schema.validate(xml).map do |error|
break false if soft

validation_error("#{error.message}\n\n#{@xml.to_s}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess @xml => xml

@daniel-g
Copy link

other than my last comments, it looks good to me 👍

@phlipper
Copy link
Contributor Author

@daniel-g good catch on the last typos. This PR should be good to go now.

@cthornton
Copy link
Contributor

Wouldn't it make sense to not even change the directory and read the file relative to __FILE__? i.e.

@schema ||= begin
  path = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'schemas', 'saml-schema-protocol-2.0.xsd'))
  Nokogiri::XML::Schema(IO.read(path))
end

@phlipper
Copy link
Contributor Author

phlipper commented Feb 6, 2015

@Lordnibbler
Copy link
Contributor

@phlipper any chance you can rebase off master and throw a test around the new method you introduced? self.schema

We updated the test suite to use Minitest and I just want to ensure its all passing still before we merge.

Thank you for this improvement!

@phlipper
Copy link
Contributor Author

phlipper commented Feb 6, 2015

@Lordnibbler i have rebased on top of the latest master and tests are green.

@Lordnibbler
Copy link
Contributor

I'm 👍 on this but would love a simple unit test around this new self.schema method

@phlipper
Copy link
Contributor Author

phlipper commented Feb 6, 2015

@Lordnibbler what would you like to see for test coverage? I'm happy to add some, but there are no existing tests for the SamlMessage class at the moment. The subclasses are tested and passing so I felt confident in the change.

@Lordnibbler
Copy link
Contributor

@phlipper We will make a followup issue to test the SamlMessage class (unless you want to throw some tests around the class too).

@Lordnibbler
Copy link
Contributor

@cgthornt @pitbulk can you guys review?

* per the references below, use of `Dir.chdir` is not
  thread-safe. this usage was causing exceptions to
  be raised when running on Puma and in other
  multi-threaded environments.
* this patch also moves the schema read up to a
  class instance, as this data is static and does
  not need to be read every time an assertion is
  validated. this boosts performance, especially in
  environments with higher throughput.

thanks to @dannyb for the assistance!

References:

* https://www.ruby-forum.com/topic/165079
* https://bugs.ruby-lang.org/issues/9785
* http://www.justskins.com/forums/working-directory-in-thread-42304.html
* http://www.ruby-doc.org/core-2.1.5/Dir.html#method-c-chdir
Lordnibbler added a commit that referenced this pull request Feb 11, 2015
Introduce thread safety to SAML schema validation
@Lordnibbler Lordnibbler merged commit c8deef8 into SAML-Toolkits:master Feb 11, 2015
@phlipper phlipper deleted the thread-safety branch February 13, 2015 16:50
@Umofomia Umofomia mentioned this pull request Apr 1, 2015
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling ea8e552 on phlipper:thread-safety into * on onelogin:master*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants