Repository navigation
Expand file tree
/
Copy pathshared_builds_test.rb
More file actions
460 lines (394 loc) · 18.1 KB
/
Copy pathshared_builds_test.rb
File metadata and controls
460 lines (394 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
require "test_helper"
require "tmpdir"
require_relative "../support/share_link_lifetime_helpers"
class SharedBuildsTest < ActionDispatch::IntegrationTest
include ShareLinkLifetimeHelpers
setup do
@previous_artifact_store = Rails.configuration.x.artifact_store
@previous_artifact_root = Rails.configuration.x.artifact_root
@artifact_root = Pathname(Dir.mktmpdir("replica-share-preview-"))
Rails.configuration.x.artifact_store = "local"
Rails.configuration.x.artifact_root = @artifact_root
host! "replica.localhost:19500"
seed "repositories/github_prototype"
@repository = repositories.github_prototype
@prototype = prototypes.welcome
@build = builds.welcome
actor = users.octocat
actor.update!(hub_admin: true)
browser_session = actor.sessions.create!(fresh_authorized_at: Time.current)
SharingSetting.change!(actor:, browser_session:, operation: "enable")
@share_link = ShareLink.create_public! \
prototype: @prototype,
build: @build,
created_by: users.octocat,
label: "Partner review",
expiration: "seven_days"
write_build_files
end
test "disabling permanently stops anonymous pages assets controls and pending handoffs" do
authorize_share_preview
host! "replica.localhost:19500"
pending = SharePreviewGrant.issue!(share_link: @share_link)
actor = users.octocat
actor.update!(hub_admin: true)
session = actor.sessions.create!(fresh_authorized_at: Time.current)
SharingSetting.change!(actor:, browser_session: session, operation: "disable")
SharingSetting.change!(actor:, browser_session: session, operation: "enable")
get shared_build_path(@share_link.public_id)
assert_response :not_found
get share_preview_authorization_path(@share_link.public_id)
assert_response :not_found
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: pending }
assert_response :not_found
[ "/", "/assets/app.js", "/settings", PreviewPathRouting::PUBLIC_CONTROLS_PATH ].each do |path|
get path
assert_response :not_found
assert_equal "private, no-store", response.headers["Cache-Control"]
end
assert @share_link.reload.revoked_at
end
test "disabling during artifact preparation returns no public content" do
authorize_share_preview
actor = users.octocat
actor.update!(hub_admin: true)
session = actor.sessions.create!(fresh_authorized_at: Time.current)
original = ArtifactStore::Local.instance_method(:read)
ArtifactStore::Local.define_method(:read) do |*args, **kwargs|
result = original.bind_call(self, *args, **kwargs)
SharingSetting.change!(actor:, browser_session: session, operation: "disable")
result
end
get "/"
assert_response :not_found
assert_empty response.body
ensure
ArtifactStore::Local.define_method(:read, original) if original
end
%w[never thirty_days].each do |expiration|
test "enabling the maximum lifetime immediately blocks an existing #{expiration} share and its preview credentials" do
@share_link.update_columns \
created_at: 8.days.ago,
expires_at: expiration == "never" ? nil : 30.days.from_now
pending = with_share_link_maximum_lifetime(nil) do
authorize_share_preview
get "/"
assert_response :success
SharePreviewGrant.issue!(share_link: @share_link)
end
with_share_link_maximum_lifetime(7.days) do
ShareLink.enforce_maximum_lifetime!
host! "replica.localhost:19500"
get shared_build_path(@share_link.public_id)
assert_response :not_found
get share_preview_authorization_path(@share_link.public_id)
assert_response :not_found
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: pending }
assert_response :not_found
[ "/", "/assets/app.js", "/settings", PreviewPathRouting::PUBLIC_CONTROLS_PATH ].each do |path|
get path
assert_response :not_found
assert_equal "private, no-store", response.headers["Cache-Control"]
end
end
host! "replica.localhost:19500"
get shared_build_path(@share_link.public_id)
assert_response :not_found
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/assets/app.js"
assert_response :not_found
end
end
test "a previously issued cookie and pending handoff stop at the exact creation time cap" do
travel_to Time.current.change(usec: 0) do
@share_link.update_columns(created_at: 7.days.ago + 1.minute, expires_at: nil)
authorize_share_preview
pending = SharePreviewGrant.issue!(share_link: @share_link)
deadline = @share_link.created_at + 7.days
with_share_link_maximum_lifetime(7.days) do
get "/assets/app.js"
assert_response :success
travel_to deadline
get "/assets/app.js"
assert_response :not_found
get PreviewPathRouting::PUBLIC_CONTROLS_PATH
assert_response :not_found
get "/__replica/authorize", params: { grant: pending }
assert_response :not_found
host! "replica.localhost:19500"
get shared_build_path(@share_link.public_id)
assert_response :not_found
end
end
end
teardown do
Rails.configuration.x.artifact_store = @previous_artifact_store
Rails.configuration.x.artifact_root = @previous_artifact_root
FileUtils.rm_rf(@artifact_root)
end
test "shows a public share without GitHub sign-in" do
get shared_build_path(@share_link.public_id)
assert_response :success
assert_includes response.body, "Partner review"
assert_includes response.body, share_preview_authorization_path(@share_link.public_id)
assert_not_includes response.body, @repository.name_with_owner
assert_equal "private, no-store", response.headers["Cache-Control"]
assert_equal "no-referrer", response.headers["Referrer-Policy"]
nonce = css_select("meta[name='csp-nonce']").first["content"]
assert_equal \
HubContentSecurityPolicy.build(development: false).build(Object.new, nonce, %w[script-src]),
response.headers["Content-Security-Policy"]
assert_select "script[nonce]", text: /SUPERGLUE_INITIAL_PAGE_STATE=/
end
test "anonymous share pages and preview files do not observe creator permission loss" do
creator = @share_link.created_by
permission_url = "#{GITHUB_API_URL}/repos/#{@repository.name_with_owner}/collaborators/#{creator.login}/permission"
lookup = stub_request(:get, permission_url).to_return \
status: 200,
body: JSON.generate(permission: "none", user: { id: creator.github_id }),
headers: { "Content-Type" => "application/json" }
get shared_build_path(@share_link.public_id), as: :json
assert_response :success
authorize_share_preview
get "/assets/app.js"
assert_response :success
assert_not_requested lookup
assert_nil @share_link.reload.revoked_at
end
test "a public share keeps its original Build after retirement and branch name reuse" do
@prototype.update!(current_build: @build)
@repository.branch_presence.record_missing!(
branch_name: @prototype.branch_name,
observed: @repository.branch_presence.observations.fetch(@prototype.branch_name)
)
replacement = prototypes.create(repository: @repository, branch_name: @prototype.branch_name)
builds.create_successful(prototype: replacement, commit_sha: "b" * 40)
get shared_build_path(@share_link.public_id)
assert_response :success
assert_equal @build, @share_link.reload.build
authorize_share_preview
get "/assets/app.js"
assert_response :success
assert_equal "console.log('shared')", response.body
end
test "deployment session revocation leaves an existing public preview and unused handoff usable" do
get share_preview_authorization_path(@share_link.public_id)
handoff = Rack::Utils.parse_query(URI(response.location).query).fetch("grant")
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: handoff }
get "/"
assert_response :success
unused = SharePreviewGrant.issue!(share_link: @share_link)
actor = users.octocat
browser_session = actor.sessions.sole
revision = SharingSetting.revision
SessionRevocation.end_all!(actor:, browser_session:)
get "/"
assert_response :success
assert_not_includes response.body, PreviewFilesController::BRIDGE_TAG.strip
get "/__replica/authorize", params: { grant: unused }
assert_response :redirect
assert @share_link.reload.available?
assert_equal revision, SharingSetting.revision
assert SharingSetting.enabled?
end
test "authorizes and serves the exact shared Build without the review bridge" do
get share_preview_authorization_path(@share_link.public_id)
assert_response :redirect
grant = Rack::Utils.parse_query(URI(response.location).query).fetch("grant")
assert_equal \
"s#{@build.id}.preview.replica.localhost",
URI(response.location).host
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: }
assert_redirected_to "/"
assert_match(/replica_share_preview=/, response.headers.fetch("Set-Cookie"))
assert_match(/httponly/i, response.headers.fetch("Set-Cookie"))
refute_match(/;\s*domain=/i, response.headers.fetch("Set-Cookie"))
assert_not_includes response.headers.fetch("Set-Cookie"), "#{LocalDevelopment.session_cookie_key}="
get "/"
assert_response :success
assert_includes response.body, "Shared preview"
assert_not_includes response.body, PreviewFilesController::BRIDGE_TAG.strip
assert_includes response.body, PreviewControlsRespondable::CONTROLS_TAG.strip
assert_equal \
"frame-ancestors http://replica.localhost:19500; worker-src blob:",
response.headers["Content-Security-Policy"]
assert_nil response.headers["Content-Security-Policy-Report-Only"]
assert_nil response.headers["X-Frame-Options"]
assert_equal "private, no-store", response.headers["Cache-Control"]
assert_equal "no-referrer", response.headers["Referrer-Policy"]
assert_equal "nosniff", response.headers["X-Content-Type-Options"]
assert_not_includes response.body, "SUPERGLUE_INITIAL_PAGE_STATE"
assert_select "meta[name='csp-nonce']", count: 0
get PreviewPathRouting::PUBLIC_CONTROLS_PATH,
headers: { "HTTP_ACCEPT" => "*/*", "HTTP_SEC_FETCH_DEST" => "script" }
assert_response :success
assert_equal "text/javascript", response.media_type
assert_equal "private, no-store", response.headers["Cache-Control"]
assert_equal "same-origin", response.headers["Cross-Origin-Resource-Policy"]
assert_not_includes response.body, @repository.name_with_owner
assert_not_includes response.body, "replica:preview:"
assert_not_includes response.body, "replica:canvas:"
get "/assets/app.js", headers: { "HTTP_ACCEPT" => "*/*", "HTTP_SEC_FETCH_DEST" => "script" }
assert_response :success
assert_equal "console.log('shared')", response.body
get "/settings", headers: { "HTTP_ACCEPT" => "text/html", "HTTP_SEC_FETCH_DEST" => "document" }
assert_response :success
assert_includes response.body, "Shared preview"
assert_not_includes response.body, PreviewFilesController::BRIDGE_TAG.strip
end
test "does not expose the review bridge on a shared preview host" do
authorize_share_preview
get PreviewPathRouting::PUBLIC_BRIDGE_PATH,
headers: { "HTTP_ACCEPT" => "*/*", "HTTP_SEC_FETCH_DEST" => "script" }
assert_response :not_found
end
test "controls requests cannot bypass public share availability" do
host! "s#{@build.id}.preview.replica.localhost:19500"
get PreviewPathRouting::PUBLIC_CONTROLS_PATH
assert_response :not_found
host! "replica.localhost:19500"
authorize_share_preview
get PreviewPathRouting::PUBLIC_CONTROLS_PATH
assert_response :success
@share_link.destroy!
get PreviewPathRouting::PUBLIC_CONTROLS_PATH
assert_response :not_found
end
test "public controls identities contain no private metadata and are separated by share" do
get shared_build_path(@share_link.public_id), as: :json
data = JSON.parse(response.body).fetch("data")
assert_match(/\A[0-9a-f]{64}\z/, data.fetch("controlsIdentity"))
assert_equal "http://s#{@build.id}.preview.replica.localhost:19500", data.fetch("controlsOrigin")
other = ShareLink.create_public! \
prototype: @prototype,
build: @build,
created_by: users.octocat,
label: "Another review"
get shared_build_path(other.public_id), as: :json
assert_not_equal data.fetch("controlsIdentity"),
JSON.parse(response.body).fetch("data").fetch("controlsIdentity")
assert_not_includes response.body, @repository.name_with_owner
assert_not_includes response.body, @prototype.branch_name
end
test "serves storage-incompatible shared document routes without adding the review bridge" do
authorize_share_preview
[ "/caf%C3%A9/v2.0", "/docs/", "/a//b", "/#{'a' * 300}", "/#{'a' * 1100}",
"/#{'nested/' * 24}page" ].each do |path|
get path, headers: { "HTTP_ACCEPT" => "text/html", "HTTP_SEC_FETCH_DEST" => "iframe" }
assert_response :success, path
assert_includes response.body, "Shared preview"
assert_not_includes response.body, PreviewFilesController::BRIDGE_TAG.strip
end
get "/caf%C3%A9.png", headers: { "HTTP_ACCEPT" => "*/*", "HTTP_SEC_FETCH_DEST" => "image" }
assert_response :not_found
[ "/bad%0a", "/%2e%2e/secret", "/.replica-internal/file", "/%2fexample.com" ].each do |path|
get path, headers: { "HTTP_ACCEPT" => "text/html", "HTTP_SEC_FETCH_DEST" => "iframe" }
assert_response :not_found, path
end
end
test "starts public previews on the saved route and preserves explicit destinations" do
@prototype.update!(start_page: "/settings?tab=profile#details")
[ [ nil, "/settings?tab=profile#details" ], [ "/specific?q=1#comment", "/specific?q=1#comment" ],
[ "/", "/" ] ].each do |return_path, expected|
host! "replica.localhost:19500"
get share_preview_authorization_path(@share_link.public_id), params: { return_path: }
assert_response :redirect
grant = Rack::Utils.parse_query(URI(response.location).query).fetch("grant")
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: }
assert_redirected_to expected
end
end
test "public shells forward explicit routes and reject external destinations" do
get shared_build_path(@share_link.public_id, return_path: "/specific#details"), as: :json
assert_response :success
url = JSON.parse(response.body).fetch("data").fetch("previewUrl")
assert_equal "/specific#details", Rack::Utils.parse_query(URI(url).query).fetch("return_path")
get share_preview_authorization_path(@share_link.public_id, return_path: "//example.com")
assert_response :not_found
end
test "rejects control characters and whitespace forwarded through public shells" do
[ "/\t/example.com", "/path\0", "/path\x1f", "/path\x7f", "/path with spaces" ].each do |return_path|
get shared_build_path(@share_link.public_id, return_path:), as: :json
assert_response :success
url = JSON.parse(response.body).fetch("data").fetch("previewUrl")
assert_no_difference "SharePreviewGrant.count" do
get url
assert_response :not_found
assert_nil response.location
end
end
end
test "stops an existing preview cookie after deletion or expiration" do
authorize_share_preview
@share_link.destroy!
get "/"
assert_response :not_found
host! "replica.localhost:19500"
replacement = ShareLink.create_public! \
prototype: @prototype,
build: @build,
created_by: users.mona,
label: "Short review",
expiration: "one_day"
get share_preview_authorization_path(replacement.public_id)
grant = Rack::Utils.parse_query(URI(response.location).query).fetch("grant")
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: }
travel 2.days
get "/"
assert_response :not_found
end
test "returns not found while the repository is removed and works after reinstall" do
@repository.update! installation_state: "removed", removed_at: Time.current
get shared_build_path(@share_link.public_id)
assert_response :not_found
@repository.update! installation_state: "active", removed_at: nil
get shared_build_path(@share_link.public_id)
assert_response :success
end
test "returns not found while the GitHub App installation is unavailable" do
GithubAppConfiguration.current.update!(installation_state: "removed")
get shared_build_path(@share_link.public_id)
assert_response :not_found
end
test "rejects a share grant on a different Build host" do
other_build = builds.create_successful \
prototype: @prototype,
number: 2,
commit_sha: "b" * 40
get share_preview_authorization_path(@share_link.public_id)
grant = Rack::Utils.parse_query(URI(response.location).query).fetch("grant")
host! "s#{other_build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: }
assert_response :not_found
assert SharePreviewGrant.find_valid(grant, build_id: @build.id)
end
test "returns not found for expired and unknown links" do
travel 8.days
get shared_build_path(@share_link.public_id)
assert_response :not_found
get shared_build_path(SecureRandom.uuid)
assert_response :not_found
end
private
def authorize_share_preview
get share_preview_authorization_path(@share_link.public_id)
grant = Rack::Utils.parse_query(URI(response.location).query).fetch("grant")
host! "s#{@build.id}.preview.replica.localhost:19500"
get "/__replica/authorize", params: { grant: }
assert_redirected_to "/"
end
def write_build_files
ArtifactStore.current.write \
"#{@build.artifact_prefix}/dist/index.html",
source: StringIO.new("<!doctype html><title>Shared preview</title>")
ArtifactStore.current.write \
"#{@build.artifact_prefix}/dist/assets/app.js",
source: StringIO.new("console.log('shared')")
end
end