Introduce the request_store gem to hold User.current and prevent data leakage in error messages
|Category:||Accounts / authentication|
Recently, there were several issues where the User.current was not properly initialized and thus the setting from the request before it was used, e.g. #16511 with r13041 which can lead to data disclosure. This issue and all similar ones can be fully circumvented by ensuring that the current user is reset before the request even reaches our rails code.
This patch which was extracted from Planio proposes the introduction of the request_store gem. It adds a middleware to provide a true request-local data store based on
Thread.current. This ensures that the current user always needs to be set explicitly and can't be taken from previous requests, even if the logic to setup the user somehow fails or is circumvented due to early error handlers. The fail-safe default is always
User.anonymous which should be sufficient even in the face of an auth error.
This patch is against the current master branch on Github.
introduce request_store to ensure that the current user doesn't leak across request boundaries (#16685)
Contributed by Holger Just.
#3 Updated by Toshi MARUYAMA over 7 years ago
Test fails on drone.io.
1) Failure: test_api_should_trigger_basic_http_auth_with_basic_authorization_header(Redmine::ApiTest::AuthenticationTest) [/xxxxxxxx/redmine/test/integration/api_test/authentication_test.rb:34]: Expected response to be a <401>, but was <500>
Tests on CI Server pass and I cannot reproduce on my CentOS 6.5.
#4 Updated by Toshi MARUYAMA over 7 years ago
On Ubuntu 12.04.4 LTS,
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-linux]
1) Failure: test_api_should_trigger_basic_http_auth_with_basic_authorization_header(Redmine::ApiTest::AuthenticationTest) [test/integration/api_test/authentication_test.rb:34]: Expected response to be a <401>, but was <500>
Mocha::ExpectationError (unexpected invocation: #<AnyInstance:UsersController>.authenticate_with_http_basic() unsatisfied expectations: - expected exactly once, not yet invoked: #<AnyInstance:ApplicationController>.authenticate_with_http_basic(any_parameters) ): app/controllers/application_controller.rb:125:in `find_current_user' app/controllers/application_controller.rb:100:in `user_setup' test/integration/api_test/authentication_test.rb:33:in `test_api_should_trigger_basic_http_auth_with_basic_authorization_header'
#6 Updated by Toshi MARUYAMA over 7 years ago
Etienne Massip wrote:
Is it really useful to add a new runtime dependency?
mocha is in "test" group.
Wouldn't a complete test be enough instead?
It depends web servers, so we cannot add new tests.
And if not, could that change be targeted to next major release?
This issue is security fix depends on #16511 with r13041,
so I think it should be targeted to minor release.
#12 Updated by Holger Just over 7 years ago
Etienne: It's way too easy to introduce data leaks when dealing with before filters, either in the core or in Plugins, so this adds an additional safety net.
Also, the gem is actually about 10 lines of code, so not that much of a dependency burden. You could add it into the core if you would be willing to support this code (which I think is pointless here). If you read the code on github, you'll notice that there isn't much room for bugs. Also, the library is well tested.
In my opinion, it adds a rather easy-to-use safty net under some easy to break code paths.
#13 Updated by Toshi MARUYAMA over 7 years ago
request_store 1.0.6 breaks many tests.
1) Failure: test_api_should_accept_switch_user_header_for_admin_user(Redmine::ApiTest::AuthenticationTest) ...
#14 Updated by Holger Just over 7 years ago
This is probably caused by this commit. In 1.0.6, the thread store is cleared after a request has finished (and not before a request started as before). That means, that after the middleware was passed on the way up again,
User.current is not valid anymore.
The solution for this would be to fix the integration tests to not rely on an "old"
User.current. In fact, not having this old value still set after a request is a good thing and solves many potential leakage issues.