Skip to content

Commit 88b620e

Browse files
cmcnallyCopilotCopilotCopilot
authored
fix(hub): preserve requested page through sign-in (#250)
<!-- Use a conventional commit-style title, such as "fix(hub): retain prototype history". --> ## Why <!-- Explain the problem and why this change matters in a short, plain-English paragraph. Link the issue when applicable. --> Opening a Hub Create link while signed out loses the requested page. After GitHub sign-in, Hub sends the user to its default destination instead. Hub only saved canvas pairing requests, and its sign-in callback only accepted preview authorization or pairing destinations. ## What changes <!-- Describe what becomes different for a first-time reader. Explain necessary terms. Include accurate Mermaid diagrams when they clarify the proposal, using before/after diagrams when helpful. Add screenshots for meaningful UI changes. --> <!-- Omit validation sections, checklists, and routine test/CI summaries such as "CI is green" or "tests passed". Mention only known verification gaps that affect the change, not disclaimers about drafting this description. --> <!-- For UI changes, prune obsolete and duplicate stories in the affected catalogues as part of the change. Explain removed UI or consolidated coverage where needed; see hub/docs/storybook.md#ui-changes-and-pull-request-evidence. --> Hub remembers the requested internal page and query through sign-in, including Create. GitHub and local sign-in share destination validation, which rejects external URLs and unsafe paths. Only GET destinations are saved, and the destination still requires its usual access checks. Sign-ins without a saved destination keep their existing welcome or repository landing page. Adds regression coverage for Create links, default destinations, repository access, unsafe redirects, and local sign-in. This changes Replica Hub only; it does not change the migration modal in github/prototyping#3604. The full Create flow and normal-browser authentication remain unverified. Local checks were blocked by npm TLS connection failures that left Vite assets unavailable; local repository fixtures also reported Git errors. Browser verification requires this branch on a configured GitHub-backed Hub. --------- Co-authored-by: Catharine McNally <2089974+cmcnally@users.noreply.github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent a19e23c commit 88b620e

7 files changed

Lines changed: 148 additions & 14 deletions

‎hub/app/controllers/concerns/authentication.rb‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def resume_session
2626
def require_authentication
2727
resume_session
2828
if Current.user.nil?
29-
session[:authentication_return_to] = request.path if request.path.match?(%r{\A/canvas/pair/[0-9A-F]{10}\z})
29+
# Leave cookie space for OAuth state and the rest of the session.
30+
if request.get? && request.fullpath.bytesize <= 1.kilobyte
31+
session[:authentication_return_to] = request.fullpath
32+
end
3033
redirect_to root_path
3134
end
3235
response.headers["Cache-Control"] = "private, no-store" if Current.user
@@ -37,6 +40,14 @@ def authenticated?
3740
Current.user.present?
3841
end
3942

43+
# Consumes the destination before sign-in rotates the browser session.
44+
def authentication_return_to
45+
value = session.delete(:authentication_return_to)
46+
return if !value.is_a?(String) || !value.match?(%r{\A/(?!/)[^\x00-\x20\\]*\z})
47+
48+
url_from(value)
49+
end
50+
4051
# Rotates the encrypted cookie and records a revocable browser session.
4152
def start_new_session_for(user, fresh_authorized_at: nil,
4253
generation: SessionRevocation.current.generation, configuration_id: nil, credential_revision: nil)

‎hub/app/controllers/github_authentications_controller.rb‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,6 @@ def discard_oauth_flow
113113
end
114114
end
115115

116-
# Returns the saved preview authorization path when it has the expected shape.
117-
def authentication_return_to
118-
value = session.delete(:authentication_return_to)
119-
return if !value.to_s.match?(%r{\A(?:/preview/builds/[1-9]\d*/authorize(?:\?[^\r\n]*)?|/canvas/pair/[0-9A-F]{10})\z})
120-
121-
value
122-
end
123-
124116
# Returns one generic failure that keeps setup and repository state private.
125117
def authentication_failed
126118
redirect_to root_path, alert: I18n.t("authentication.failure")

‎hub/app/controllers/local_authentications_controller.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def create
1616
status: :see_other
1717
end
1818

19-
return_to = session.delete(:authentication_return_to)
19+
return_to = authentication_return_to
2020
user = User.local_developer!
2121
start_new_session_for(user)
2222
show_welcome = user.consume_welcome_sign_in!

‎hub/docs/authentication-and-repository-authorization.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ Session -> Hub asks GitHub for current permission -> show repository or deny acc
1212

1313
Hub admits repository users when they can read at least one installed repository. A `none` permission on another installed repository does not prevent sign-in. Explicitly enrolled Hub operators can also sign in to administer the deployment without repository access. Hub stores their GitHub ID and current profile, but not their OAuth token. The browser cookie points to a `Session` record that Hub deletes when the person signs out.
1414

15-
Signed-in visits to `/` and successful sign-ins go to `/repositories`, regardless of Hub administrator status. Sign-in preserves a saved preview authorization or canvas pairing destination. The repository entry point opens the first readable repository or returns not found when none is available. Hub administrators can still open `/security_audit` directly.
15+
Signed-out visits to protected pages save the requested internal path and query for after sign-in, including Create, preview authorization, and canvas pairing. Hub consumes the saved destination before rotating the browser session and rejects external URLs, protocol-relative paths, backslashes, whitespace, and control characters. Only GET destinations are saved; sign-in never replays a mutation. The destination still requires its normal access checks.
16+
17+
Without a saved destination, a successful sign-in opens the welcome page for a new user or `/repositories` for a returning user, regardless of Hub administrator status. Signed-in visits to `/` go to `/repositories`. The repository entry point opens the first readable repository or returns not found when none is available. Hub administrators can still open `/security_audit` directly.
1618

1719
Before showing a repository, Hub confirms that its App is still installed there and asks GitHub whether the signed-in person can access it. Hub also confirms that GitHub's answer belongs to that person.
1820

‎hub/test/integration/authentication_test.rb‎

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,108 @@ class AuthenticationTest < ActionDispatch::IntegrationTest
1818
assert_includes response.body, I18n.t("authentication.failure")
1919
end
2020

21-
test "explains when GitHub sign-in succeeds without repository access" do
21+
test "returns to Create with its query after signing in through the public home" do
22+
github_app_configurations.create_installed
23+
repository = repository_payload(1379291646, "github/prototype")
24+
stub_github_sign_in(repositories: [ repository ], permissions: { 1379291646 => "pull" })
25+
destination = "#{repository_new_prototype_path(1379291646)}?template=default&brief=hello%20world"
26+
27+
get destination
28+
29+
assert_redirected_to root_path
30+
follow_redirect! headers: { "Accept" => "application/json" }
31+
assert_response :success
32+
33+
post github_authentication_path
34+
state = Rack::Utils.parse_query(URI(response.location).query).fetch("state")
35+
get github_authentication_callback_path, params: { code: "code", state: }
36+
37+
assert_redirected_to destination
38+
follow_redirect! headers: { "Accept" => "application/json" }
39+
assert_response :success
40+
assert_equal "new", response.parsed_body.fetch("data").fetch("selectedSection")
41+
42+
delete session_path
43+
sign_in_to_hub
44+
assert_redirected_to repositories_path
45+
end
46+
47+
test "a saved Create destination still requires repository access after sign-in" do
48+
github_app_configurations.create_installed
49+
installed = [
50+
repository_payload(1379291646, "github/private").merge("private" => true),
51+
repository_payload(2002, "github/readable").merge("private" => true)
52+
]
53+
stub_github_sign_in(repositories: installed, permissions: { 1379291646 => nil, 2002 => "pull" })
54+
destination = repository_new_prototype_path(1379291646)
55+
56+
get destination
57+
assert_redirected_to root_path
58+
follow_redirect! headers: { "Accept" => "application/json" }
59+
post github_authentication_path
60+
state = Rack::Utils.parse_query(URI(response.location).query).fetch("state")
61+
get github_authentication_callback_path, params: { code: "code", state: }
62+
63+
assert_redirected_to destination
64+
follow_redirect!
65+
assert_response :not_found
66+
assert_not_includes response.body, "github/private"
67+
end
68+
69+
[ "new", "returning" ].each do |visitor|
70+
test "does not replay a signed-out POST and keeps the #{visitor} user's default destination" do
71+
github_app_configurations.create_installed
72+
users.create(welcome_sign_in_pending: false) if visitor == "returning"
73+
repository = repository_payload(1379291646, "github/prototype")
74+
stub_github_sign_in(repositories: [ repository ], permissions: { 1379291646 => "pull" })
75+
76+
post poll_repository_path(1379291646)
77+
78+
assert_redirected_to root_path
79+
follow_redirect! headers: { "Accept" => "application/json" }
80+
post github_authentication_path
81+
state = Rack::Utils.parse_query(URI(response.location).query).fetch("state")
82+
get github_authentication_callback_path, params: { code: "code", state: }
83+
84+
assert_redirected_to(visitor == "new" ? welcome_path : repositories_path)
85+
end
86+
end
87+
88+
{
89+
"external URL" => "https://outside.example/repositories/1379291646/new",
90+
"protocol-relative URL" => "//outside.example/repositories/1379291646/new",
91+
"backslash" => "/\\outside.example/repositories/1379291646/new",
92+
"control character" => "/repositories/1379291646/new\n",
93+
"space" => "/repositories/1379291646/new?brief=hello world",
94+
"relative path" => "repositories/1379291646/new",
95+
"non-string value" => [ "/repositories/1379291646/new" ]
96+
}.each do |description, destination|
97+
test "rejects a saved authentication destination with #{description}" do
98+
github_app_configurations.create_installed
99+
repository = repository_payload(2001, "github/prototype")
100+
stub_github_sign_in(repositories: [ repository ], permissions: { 2001 => "pull" })
101+
102+
with_saved_authentication_destination(destination) { post github_authentication_path }
103+
state = Rack::Utils.parse_query(URI(response.location).query).fetch("state")
104+
get github_authentication_callback_path, params: { code: "code", state: }
105+
106+
assert_redirected_to welcome_path
107+
end
108+
end
109+
110+
test "a saved destination does not bypass sign-in admission without repository access" do
22111
github_app_configurations.create_installed
23112
repository = repository_payload(2001, "github/private")
24113
stub_github_sign_in(repositories: [ repository ], permissions: { 2001 => nil })
25114

115+
get repository_new_prototype_path(2001)
116+
assert_redirected_to root_path
117+
follow_redirect!
26118
post github_authentication_path
27119
state = Rack::Utils.parse_query(URI(response.location).query).fetch("state")
28-
get github_authentication_callback_path, params: { code: "code", state: }
120+
assert_no_difference "Session.count" do
121+
get github_authentication_callback_path, params: { code: "code", state: }
122+
end
29123

30124
assert_redirected_to root_path
31125
follow_redirect!
@@ -204,4 +298,18 @@ class AuthenticationTest < ActionDispatch::IntegrationTest
204298
assert_redirected_to root_path
205299
assert_not Session.exists?(browser_session.id)
206300
end
301+
302+
private
303+
304+
# Seeds forged server-side state without adding a public return-to parameter or bypassing OAuth.
305+
def with_saved_authentication_destination(destination)
306+
original = GithubAuthenticationsController.instance_method(:create)
307+
GithubAuthenticationsController.define_method(:create) do
308+
session[:authentication_return_to] = destination
309+
original.bind_call(self)
310+
end
311+
yield
312+
ensure
313+
GithubAuthenticationsController.define_method(:create, original) if original
314+
end
207315
end

‎hub/test/integration/local_development_test.rb‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ class LocalDevelopmentTest < ActionDispatch::IntegrationTest
2222
assert_raises(LocalDevelopment::Error) { Repository::LocalDiscovery.refresh_all! }
2323
end
2424

25+
test "local sign-in returns to the requested Create page" do
26+
path = create_repository("sign-in-destination")
27+
28+
in_local_mode do
29+
Repository::LocalDiscovery.register_repositories!
30+
repository = Repository.find_by!(local_path: path.to_s)
31+
destination = repository_new_prototype_path(repository.route_id, template: "default")
32+
33+
get destination
34+
assert_redirected_to root_path
35+
follow_redirect! headers: { "Accept" => "application/json" }
36+
assert_response :success
37+
post local_authentication_path
38+
39+
assert_redirected_to destination
40+
follow_redirect! headers: { "Accept" => "application/json" }
41+
assert_response :success
42+
assert_equal "new", response.parsed_body.fetch("data").fetch("selectedSection")
43+
end
44+
end
45+
2546
test "a GitHub browser cookie cannot resume a local session" do
2647
github_app_configurations.create_installed
2748
payload = repository_payload(2001, "github/prototype")

‎hub/test/integration/session_revocations_test.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class SessionRevocationsTest < ActionDispatch::IntegrationTest
6868
get session_revocation_path
6969
assert_redirected_to root_path
7070
sign_in
71-
assert_redirected_to repositories_path
71+
assert_redirected_to session_revocation_path
7272
get session_revocation_path
7373
assert_response :success
7474
replacement = pair

0 commit comments

Comments
 (0)