Skip to content

Commit 20db084

Browse files
authored
Merge pull request rails#43209 from mpestov/check-basic-auth-credentials
Check basic auth credentials before authenticate
2 parents a76344f + 22e6cb2 commit 20db084

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

actionpack/lib/action_controller/metal/http_authentication.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def authenticate(request, &login_procedure)
103103
end
104104

105105
def has_basic_credentials?(request)
106-
request.authorization.present? && (auth_scheme(request).downcase == "basic")
106+
request.authorization.present? && (auth_scheme(request).downcase == "basic") && user_name_and_password(request).length == 2
107107
end
108108

109109
def user_name_and_password(request)

actionpack/test/controller/http_basic_authentication_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ def test_encode_credentials_has_no_newline
112112
assert_no_match(/\n/, result)
113113
end
114114

115+
test "has_basic_credentials? should fail with credentials without colon" do
116+
@request.env["HTTP_AUTHORIZATION"] = "Basic #{::Base64.encode64("David Goliath")}"
117+
assert_not ActionController::HttpAuthentication::Basic.has_basic_credentials?(@request)
118+
end
119+
115120
test "successful authentication with uppercase authorization scheme" do
116121
@request.env["HTTP_AUTHORIZATION"] = "BASIC #{::Base64.encode64("lifo:world")}"
117122
get :index

0 commit comments

Comments
 (0)