Skip to content

Commit c36bb78

Browse files
authored
Merge pull request #1123 from travis-ci/as-state-missmatch
Fix state mismatch issue
2 parents e6f566e + 895f542 commit c36bb78

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ 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
170+
return
170171
end
171172

172173
endpoint.path = config[:access_token_path]
@@ -196,7 +197,8 @@ def remote_vcs_user
196197
def vcs_handshake
197198
if params[:code]
198199
unless state_ok?(params[:state], params[:provider])
199-
halt 400, 'state mismatch'
200+
handle_invalid_response
201+
return
200202
end
201203

202204
vcs_data = remote_vcs_user.authenticate(
@@ -259,6 +261,18 @@ def cookie_name(provider = :github)
259261

260262
# VCS HANDSHAKE END
261263

264+
def clear_state_cookies
265+
response.delete_cookie cookie_name(:github)
266+
response.delete_cookie cookie_name(:gitlab)
267+
response.delete_cookie cookie_name(:bitbucket)
268+
response.delete_cookie cookie_name(:assembla)
269+
end
270+
271+
def handle_invalid_response
272+
clear_state_cookies
273+
redirect to("https://#{Travis.config.host}/")
274+
end
275+
262276
def create_state
263277
state = SecureRandom.urlsafe_base64(16)
264278
redis.sadd('github:states', state)

spec/unit/endpoint/authorization_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,25 @@
4848
end
4949

5050
describe 'evil hackers messing with the state' do
51-
it 'does not succeed if state cookie mismatches' do
51+
before do
52+
WebMock.stub_request(:post, "https://foobar.com/access_token_path").
53+
with(
54+
body: "{\"client_id\":\"client-id\",\"scope\":\"public_repo,user:email,new_scope\",\"redirect_uri\":\"http://example.org/auth/handshake\",\"state\":\"github-state\",\"code\":\"oauth-code\",\"client_secret\":\"client-secret\"}",
55+
headers: {
56+
'Accept' => '*/*',
57+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
58+
'Connection' => 'keep-alive',
59+
'Content-Type' => 'application/json',
60+
'Keep-Alive' => '30',
61+
'User-Agent' => 'Faraday v0.17.3'
62+
}).
63+
to_return(status: 200, body: "", headers: {})
64+
end
65+
66+
it 'does not succeed if state cookie mismatches (redirects)' do
5267
Travis.redis.sadd('github:states', 'github-state')
5368
response = get '/auth/handshake?state=github-state&code=oauth-code'
54-
expect(response.status).to eq(400)
55-
expect(response.body).to eq("state mismatch")
69+
expect(response.status).to eq(302)
5670
Travis.redis.srem('github:states', 'github-state')
5771
end
5872
end

0 commit comments

Comments
 (0)