Skip to content

Commit dbb0258

Browse files
committed
Ensure HTTP Digest auth uses appropriate HTTP method [#2490 state:resolved] [Steve Madsen]
1 parent 2b5e4f3 commit dbb0258

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

actionpack/lib/action_controller/http_authentication.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,10 @@ def validate_digest_response(request, realm, &password_procedure)
192192

193193
if valid_nonce && realm == credentials[:realm] && opaque == credentials[:opaque]
194194
password = password_procedure.call(credentials[:username])
195+
method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']
195196

196197
[true, false].any? do |password_is_ha1|
197-
expected = expected_response(request.env['REQUEST_METHOD'], request.env['REQUEST_URI'], credentials, password, password_is_ha1)
198+
expected = expected_response(method, request.env['REQUEST_URI'], credentials, password, password_is_ha1)
198199
expected == credentials[:response]
199200
end
200201
end

actionpack/test/controller/http_digest_authentication_test.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ def authenticate_with_request
151151
assert_equal 'Definitely Maybe', @response.body
152152
end
153153

154+
test "authentication request with _method" do
155+
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please', :method => :post)
156+
@request.env['rack.methodoverride.original_method'] = 'POST'
157+
put :display
158+
159+
assert_response :success
160+
assert assigns(:logged_in)
161+
assert_equal 'Definitely Maybe', @response.body
162+
end
163+
154164
private
155165

156166
def encode_credentials(options)
@@ -161,15 +171,22 @@ def encode_credentials(options)
161171
# to prevent tampering of timestamp
162172
ActionController::Base.session_options[:secret] = "session_options_secret"
163173

164-
# Perform unauthenticated GET to retrieve digest parameters to use on subsequent request
165-
get :index
174+
# Perform unauthenticated request to retrieve digest parameters to use on subsequent request
175+
method = options.delete(:method) || 'GET'
176+
177+
case method.to_s.upcase
178+
when 'GET'
179+
get :index
180+
when 'POST'
181+
post :index
182+
end
166183

167184
assert_response :unauthorized
168185

169186
credentials = decode_credentials(@response.headers['WWW-Authenticate'])
170187
credentials.merge!(options)
171188
credentials.reverse_merge!(:uri => "#{@request.env['REQUEST_URI']}")
172-
ActionController::HttpAuthentication::Digest.encode_credentials("GET", credentials, password, options[:password_is_ha1])
189+
ActionController::HttpAuthentication::Digest.encode_credentials(method, credentials, password, options[:password_is_ha1])
173190
end
174191

175192
def decode_credentials(header)

0 commit comments

Comments
 (0)