Skip to content

Commit f501a29

Browse files
authored
Merge branch 'master' into as-add-email-to-user-renderer
2 parents f260f06 + 1af3f63 commit f501a29

File tree

8 files changed

+82
-11
lines changed

8 files changed

+82
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Authorization < Endpoint
104104
#
105105
# * **redirect_uri**: URI to redirect to after handshake.
106106
get '/handshake/?:provider?' do
107-
method = Travis::Features.enabled_for_all?(:vcs_login) ? :vcs_handshake : :handshake
107+
method = org? ? :handshake : :vcs_handshake
108108
params[:provider] ||= 'github'
109109

110110
send(method) do |user, token, redirect_uri|

lib/travis/api/v3/models/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class RequestConfigs < Struct.new(:data)
44
def raw_configs
55
Array(data[:raw_configs]).map do |attrs|
66
raw_config = RequestRawConfig.new(config: attrs[:config])
7-
RequestRawConfiguration.new(source: attrs[:source], raw_config: raw_config)
7+
RequestRawConfiguration.new(source: attrs[:source], merge_mode: attrs[:mode], raw_config: raw_config)
88
end
99
end
1010

lib/travis/api/v3/renderer/request_raw_configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Travis::API::V3
22
class Renderer::RequestRawConfiguration < ModelRenderer
3-
representation(:minimal, :config, :source)
3+
representation(:minimal, :config, :source, :merge_mode)
44
representation(:standard, *representations[:minimal])
55

66
def config

lib/travis/api/v3/services/requests/create.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Services::Requests::Create < Service
55
private_constant :TIME_FRAME, :LIMIT
66

77
result_type :request
8-
params "request", "user", :merge_mode, :config, :message, :branch, :sha, :token
8+
params "request", "user", :merge_mode, :config, :configs, :message, :branch, :sha, :token
99

1010
def run
1111
repository = check_login_and_find(:repository)

spec/unit/endpoint/authorization_spec.rb

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
end
4141

4242
describe "GET /auth/handshake" do
43+
before do
44+
ENV['TRAVIS_SITE'] = 'org'
45+
end
46+
after do
47+
ENV['TRAVIS_SITE'] = nil
48+
end
49+
4350
describe 'evil hackers messing with the state' do
4451
it 'does not succeed if state cookie mismatches' do
4552
Travis.redis.sadd('github:states', 'github-state')
@@ -50,7 +57,7 @@
5057
end
5158
end
5259

53-
describe 'evil hackers messing with redirection' do
60+
describe 'On org, evil hackers messing with redirection' do
5461
before do
5562
WebMock.stub_request(:post, "https://foobar.com/access_token_path")
5663
.to_return(status: 200, body: 'access_token=token&token_type=bearer')
@@ -109,6 +116,66 @@
109116
end
110117
end
111118

119+
describe 'On com and enterprise, evil hackers messing with redirection' do
120+
before do
121+
WebMock.stub_request(:post, "https://foobar.com/access_token_path")
122+
.to_return(status: 200, body: 'access_token=token&token_type=bearer')
123+
124+
WebMock.stub_request(:get, "https://api.github.com/user?per_page=100")
125+
.to_return(
126+
status: 200,
127+
body: JSON.dump(name: 'Piotr Sarnacki', login: 'drogus', gravatar_id: '123', id: 456, foo: 'bar'), headers: {'X-OAuth-Scopes' => 'repo, user, new_scope'}
128+
)
129+
130+
cookie_jar['travis.state-github'] = state
131+
Travis.redis.sadd('github:states', state)
132+
ENV['TRAVIS_SITE'] = nil
133+
end
134+
135+
after do
136+
Travis.redis.srem('github:states', state)
137+
end
138+
139+
context 'when redirect uri is correct' do
140+
let(:state) { 'github-state:::https://travis-ci.com/?any=params' }
141+
142+
it 'it does allow redirect' do
143+
response = get "/auth/handshake/github?code=1234&state=#{URI.encode(state)}"
144+
expect(response.status).to eq(200)
145+
end
146+
end
147+
148+
context 'when redirect uri is not allowed' do
149+
let(:state) { 'github-state:::https://dark-corner-of-web.com/' }
150+
151+
it 'does not allow redirect' do
152+
response = get "/auth/handshake/github?code=1234&state=#{URI.encode(state)}"
153+
expect(response.status).to eq(401)
154+
expect(response.body).to eq("target URI not allowed")
155+
end
156+
end
157+
158+
context 'when script tag is injected into redirect uri' do
159+
let(:state) { 'github-state:::https://travis-ci.com/<sCrIpt' }
160+
161+
it 'does not allow redirect' do
162+
response = get "/auth/handshake/github?code=1234&state=#{URI.encode(state)}"
163+
expect(response.status).to eq(401)
164+
expect(response.body).to eq("target URI not allowed")
165+
end
166+
end
167+
168+
context 'when onerror tag is injected into redirect uri' do
169+
let(:state) { 'github-state:::https://travis-ci.com/<img% src="" onerror="badcode()"' }
170+
171+
it 'does not allow redirect' do
172+
response = get "/auth/handshake/github?code=1234&state=#{URI.encode(state)}"
173+
expect(response.status).to eq(401)
174+
expect(response.body).to eq("target URI not allowed")
175+
end
176+
end
177+
end
178+
112179
describe 'with insufficient oauth permissions' do
113180
before do
114181
Travis.redis.sadd('github:states', 'github-state')

spec/v3/service_index_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module Routes
7777
"accepted_params" => %w(
7878
request.merge_mode
7979
request.config
80+
request.configs
8081
request.message
8182
request.branch
8283
request.sha

spec/v3/services/request/find_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
let(:one) { request.raw_configs.build(key: '123', config: 'language: ruby') }
4141
let(:two) { request.raw_configs.build(key: '234', config: 'rvm: 2.5.1') }
4242

43-
before { request.raw_configurations.create!(raw_config: one, source: '.travis.yml') }
44-
before { request.raw_configurations.create!(raw_config: two, source: 'other.yml') }
45-
before { request.raw_configurations.create!(raw_config: one, source: '.travis.yml') } # accidental duplicate
43+
before { request.raw_configurations.create!(raw_config: one, source: '.travis.yml', merge_mode: 'one') }
44+
before { request.raw_configurations.create!(raw_config: two, source: 'other.yml', merge_mode: 'two') }
45+
before { request.raw_configurations.create!(raw_config: one, source: '.travis.yml', merge_mode: 'one') } # accidental duplicate
4646

4747
before { get("/v3/repo/#{repo.id}/request/#{request.id}?include=request.raw_configs") }
4848

@@ -55,13 +55,15 @@
5555
'@type' => 'request_raw_configuration',
5656
'@representation' => 'standard',
5757
'config' => 'language: ruby',
58-
'source' => '.travis.yml'
58+
'source' => '.travis.yml',
59+
'merge_mode' => 'one',
5960
},
6061
{
6162
'@type' => 'request_raw_configuration',
6263
'@representation' => 'standard',
6364
'config' => 'rvm: 2.5.1',
64-
'source' => 'other.yml'
65+
'source' => 'other.yml',
66+
'merge_mode' => 'two',
6567
}
6668
]
6769
)

spec/v3/services/request/preview_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
let(:configs) do
1010
{
11-
raw_configs: [source: 'travis-ci/travis-yml:.travis.yml@ref', config: 'script: true'],
11+
raw_configs: [source: 'travis-ci/travis-yml:.travis.yml@ref', config: 'script: true', mode: 'replace'],
1212
config: { script: ['true'] },
1313
matrix: [script: ['true']],
1414
messages: [type: :type, level: :info, key: :key, code: :code, args: { one: 'one' }, src: '.travis.yml', line: 1],
@@ -53,6 +53,7 @@ def parse(str)
5353
'@type': 'request_raw_configuration',
5454
'@representation': 'minimal',
5555
source: 'travis-ci/travis-yml:.travis.yml@ref',
56+
merge_mode: 'replace',
5657
config: 'script: true'
5758
],
5859
request_config: {

0 commit comments

Comments
 (0)