Skip to content

Commit 28956b8

Browse files
committed
Experiment with state mismatch
1 parent c040a15 commit 28956b8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/travis/api/app/endpoint/authorization.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def handshake
166166
if params[:code]
167167
unless state_ok?(params[:state])
168168
log_with_request_id("[handshake] Handshake failed (state mismatch)")
169-
halt 400, 'state mismatch'
169+
handle_invalid_response
170170
end
171171

172172
endpoint.path = config[:access_token_path]
@@ -196,7 +196,7 @@ def remote_vcs_user
196196
def vcs_handshake
197197
if params[:code]
198198
unless state_ok?(params[:state], params[:provider])
199-
halt 400, 'state mismatch'
199+
handle_invalid_response
200200
end
201201

202202
vcs_data = remote_vcs_user.authenticate(
@@ -259,6 +259,19 @@ def cookie_name(provider = :github)
259259

260260
# VCS HANDSHAKE END
261261

262+
def clear_state_cookies
263+
response.delete_cookie cookie_name(:github)
264+
response.delete_cookie cookie_name(:gitlab)
265+
response.delete_cookie cookie_name(:bitbucket)
266+
response.delete_cookie cookie_name(:assembla)
267+
end
268+
269+
def handle_invalid_response
270+
clear_state_cookies
271+
back_url = request.get_header('Referer') || Travis.config.host
272+
response.redirect(back_url)
273+
end
274+
262275
def create_state
263276
state = SecureRandom.urlsafe_base64(16)
264277
redis.sadd('github:states', state)

spec/unit/endpoint/authorization_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
it 'does not succeed if state cookie mismatches' do
5252
Travis.redis.sadd('github:states', 'github-state')
5353
response = get '/auth/handshake?state=github-state&code=oauth-code'
54-
expect(response.status).to eq(400)
54+
expect(response.status).to eq(302)
5555
expect(response.body).to eq("state mismatch")
5656
Travis.redis.srem('github:states', 'github-state')
5757
end

0 commit comments

Comments
 (0)