Skip to content

Commit d5814f5

Browse files
authored
Let a browser approve OAuth consent, and answer OAuth clients whatever their user agent (#3283)
* Keep the browser's Origin on consent's own form posts Every authorization server response sent Referrer-Policy: no-referrer, the consent page included. A browser posting a form from a no-referrer page sends Origin: null, and Rails' origin check refuses that as an InvalidCrossOriginRequest (422) before anything else is weighed, so no one could approve or deny consent, or use "Not you?", from a real browser. The test environment turns forgery protection off, so the system test that clicks Authorize passed all along. It now runs with forgery protection on, and fails without this change. The pages a GET renders now send same-origin: our own form posts keep their Origin, and nothing goes to any other site, so the authorization request's parameters still stay here. A POST's answer, the redirect carrying the code among them, keeps no-referrer, since a redirect's policy is the one its next hop is sent under. * Answer OAuth's machine endpoints whatever the user agent allow_browser versions: :modern gated every controller, so a client whose platform sends an old browser's user agent got a 406 page from the token endpoint, a page no OAuth client can act on. Token, delegation exchange, revocation, registration and discovery now answer any user agent; consent, which people see, keeps the gate. A test holds every Oauth controller to the choice, so a new one fails until it is put on one side or the other. * Answer HEAD like GET in consent's referrer policy, as Brakeman asks
1 parent 4031d98 commit d5814f5

12 files changed

Lines changed: 159 additions & 11 deletions

‎app/controllers/application_controller.rb‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ class ApplicationController < ActionController::Base
2121
fragment_cache_key RENDERING_VERSION
2222
etag { RENDERING_VERSION }
2323
stale_when_importmap_changes
24-
allow_browser versions: :modern
24+
# People's browsers, that is: see allow_any_user_agent.
25+
class_attribute :browser_versions_enforced, instance_accessor: false, default: true
26+
allow_browser versions: :modern, if: -> { self.class.browser_versions_enforced }
27+
28+
# For endpoints software calls rather than people visit: OAuth's token,
29+
# registration, revocation and discovery. Their callers send whatever
30+
# user agent their platform does, an old browser's included, and a 406 page
31+
# is no answer a client can act on.
32+
def self.allow_any_user_agent
33+
self.browser_versions_enforced = false
34+
end
2535

2636
# Here rather than in Authentication: Active Storage's controllers include
2737
# that too, and a handler declared there would shadow ones added to their

‎app/controllers/oauth/authorizations_controller.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ def denial?
8484
request.post? && params[:error] == "access_denied"
8585
end
8686

87+
# The pages a GET or HEAD renders, consent above all, hold forms that
88+
# post back to us, and a browser posting from a no-referrer page sends
89+
# Origin: null, which Rails' origin check refuses (InvalidCrossOriginRequest,
90+
# a 422) before anything else is weighed. same-origin keeps our own origin on
91+
# those posts and still sends nothing to any other site, so the request's
92+
# parameters stay here. A POST's answer, the redirect carrying the code
93+
# among them, keeps no-referrer: a redirect's policy is the one the next
94+
# hop is sent under.
95+
def referrer_policy
96+
request.post? ? super : "same-origin"
97+
end
98+
8799
# While connecting apps is in preview, nobody but previewers is asked to
88100
# consent. They're told why, rather than bounced back to an app that can't say.
89101
def require_admitted_identity

‎app/controllers/oauth/base_controller.rb‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ class Oauth::BaseController < ApplicationController
22
disallow_account_scope
33

44
# Every authorization server response, errors, consent and sign-in
5-
# redirects included, is uncacheable and sends no Referer, so a code, a
6-
# token, a secret or an authorization request's parameters reach neither a
7-
# shared cache nor the page a browser goes to next. Prepended, so it runs
8-
# ahead of every callback that can halt, the app's authentication included:
9-
# a halted chain skips after_actions (RFC 6749 §5.1, §5.2).
5+
# redirects included, is uncacheable and sends no Referer to another site,
6+
# so a code, a token, a secret or an authorization request's parameters
7+
# reach neither a shared cache nor the page a browser goes to next.
8+
# Prepended, so it runs ahead of every callback that can halt, the app's
9+
# authentication included: a halted chain skips after_actions (RFC 6749
10+
# §5.1, §5.2).
1011
prepend_before_action :prevent_caching, :prevent_referrer
1112

1213
# OAuth parameters are top-level names. Rails' JSON parameter wrapping would
@@ -161,7 +162,11 @@ def prevent_caching
161162
end
162163

163164
def prevent_referrer
164-
response.headers["Referrer-Policy"] = "no-referrer"
165+
response.headers["Referrer-Policy"] = referrer_policy
166+
end
167+
168+
def referrer_policy
169+
"no-referrer"
165170
end
166171

167172
# Every error answer is announced as error.oauth, for metrics (SaaS counts

‎app/controllers/oauth/clients_controller.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Oauth::ClientsController < Oauth::BaseController
2+
allow_any_user_agent
3+
24
allow_unauthenticated_access
35
skip_forgery_protection
46

‎app/controllers/oauth/metadata_controller.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Oauth::MetadataController < Oauth::BaseController
2+
allow_any_user_agent
3+
24
allow_unauthenticated_access
35

46
before_action :require_acceptance_enabled

‎app/controllers/oauth/protected_resource_metadata_controller.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Oauth::ProtectedResourceMetadataController < Oauth::BaseController
2+
allow_any_user_agent
3+
24
allow_unauthenticated_access
35

46
before_action :require_acceptance_enabled

‎app/controllers/oauth/revocations_controller.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
class Oauth::RevocationsController < Oauth::BaseController
1717
include Oauth::ClientAuthentication
1818

19+
allow_any_user_agent
20+
1921
allow_unauthenticated_access
2022
skip_forgery_protection
2123

‎app/controllers/oauth/token_exchanges_controller.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
class Oauth::TokenExchangesController < Oauth::BaseController
1111
include Oauth::ClientAuthentication
1212

13+
allow_any_user_agent
14+
1315
CLIENT_BUDGET = 600
1416

1517
allow_unauthenticated_access

‎app/controllers/oauth/tokens_controller.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Oauth::TokensController < Oauth::BaseController
22
include Oauth::ClientAuthentication
33

4+
allow_any_user_agent
5+
46
GRANT_TYPES = %w[ authorization_code refresh_token ]
57

68
# A grant's own requests a minute: a client refreshing on schedule spends

‎test/integration/oauth_response_headers_test.rb‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require "test_helper"
22

3-
# Every authorization server response is uncacheable and sends no Referer,
4-
# whether it succeeds, fails, or never reaches its action.
3+
# Every authorization server response is uncacheable and sends no Referer to
4+
# another site, whether it succeeds, fails, or never reaches its action. The
5+
# pages a GET renders send ours to us alone (same-origin), so a browser keeps
6+
# its Origin on the forms they post back; every other answer sends none.
57
class OauthResponseHeadersTest < ActionDispatch::IntegrationTest
68
include OauthAvailabilityTestHelper
79

@@ -32,18 +34,36 @@ class OauthResponseHeadersTest < ActionDispatch::IntegrationTest
3234

3335
assert_response :redirect
3436
assert_match %r{/session/new}, response.location
35-
assert_uncached_without_referrer
37+
assert_uncached_without_cross_site_referrer
3638
end
3739

3840
test "the consent screen and its redirect to the client" do
3941
sign_in_as :david
4042

4143
untenanted { get new_oauth_authorization_path, params: authorization_params }
4244
assert_response :success
43-
assert_uncached_without_referrer
45+
assert_uncached_without_cross_site_referrer
4446

4547
untenanted { post oauth_authorization_path, params: authorization_params }
4648
assert_response :redirect
49+
assert_match %r{\Ahttp://127\.0\.0\.1:8888/callback\?.*code=}, response.location
50+
assert_uncached_without_referrer
51+
end
52+
53+
test "an error a GET redirects to the client" do
54+
sign_in_as :david
55+
56+
untenanted { get new_oauth_authorization_path, params: authorization_params.merge(response_type: "token") }
57+
assert_response :redirect
58+
assert_match %r{\Ahttp://127\.0\.0\.1:8888/callback\?.*error=unsupported_response_type}, response.location
59+
assert_uncached_without_cross_site_referrer
60+
end
61+
62+
test "the not-you switch" do
63+
sign_in_as :david
64+
65+
untenanted { post oauth_authorization_switch_path, params: authorization_params }
66+
assert_response :redirect
4767
assert_uncached_without_referrer
4868
end
4969

@@ -78,4 +98,9 @@ def assert_uncached_without_referrer
7898
assert_equal "no-store", response.headers["Cache-Control"]
7999
assert_equal "no-referrer", response.headers["Referrer-Policy"]
80100
end
101+
102+
def assert_uncached_without_cross_site_referrer
103+
assert_equal "no-store", response.headers["Cache-Control"]
104+
assert_equal "same-origin", response.headers["Referrer-Policy"]
105+
end
81106
end

0 commit comments

Comments
 (0)