Skip to content

Commit

Permalink
A test to show that http_authentication needs to fail authentication …
Browse files Browse the repository at this point in the history
…if the password procedure returns nil. Also includes a fix to validate_digest_response to fail validation if the password procedure returns nil.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
nate authored and NZKoz committed Jun 9, 2009
1 parent 5fb66a3 commit 056ddbd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/http_authentication.rb
Expand Up @@ -183,7 +183,7 @@ def authorization(request)
request.env['REDIRECT_X_HTTP_AUTHORIZATION']
end

# Raises error unless the request credentials response value matches the expected value.
# Returns false unless the request credentials response value matches the expected value.
# First try the password as a ha1 digest password. If this fails, then try it as a plain
# text password.
def validate_digest_response(request, realm, &password_procedure)
Expand All @@ -192,6 +192,8 @@ def validate_digest_response(request, realm, &password_procedure)

if valid_nonce && realm == credentials[:realm] && opaque == credentials[:opaque]
password = password_procedure.call(credentials[:username])
return false unless password

method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']

[true, false].any? do |password_is_ha1|
Expand Down
14 changes: 14 additions & 0 deletions actionpack/test/controller/http_digest_authentication_test.rb
Expand Up @@ -67,6 +67,15 @@ def authenticate_with_request
assert_equal 'SuperSecret', credentials[:realm]
end

test "authentication request with nil credentials" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => nil, :password => nil)
get :index

assert_response :unauthorized
assert_equal "HTTP Digest: Access denied.\n", @response.body, "Authentication didn't fail for request"
assert_not_equal 'Hello Secret', @response.body, "Authentication didn't fail for request"
end

test "authentication request with invalid password" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'foo')
get :display
Expand Down Expand Up @@ -161,6 +170,11 @@ def authenticate_with_request
assert_equal 'Definitely Maybe', @response.body
end

test "validate_digest_response should fail with nil returning password_procedure" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => nil, :password => nil)
assert !ActionController::HttpAuthentication::Digest.validate_digest_response(@request, "SuperSecret"){nil}
end

private

def encode_credentials(options)
Expand Down

0 comments on commit 056ddbd

Please sign in to comment.