mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 01:45:41 +08:00
FIX: Correctly redirect after external login on subfolder sites (#10529)
This commit is contained in:
parent
996d5f3b17
commit
4351fa435e
@ -32,7 +32,7 @@ class Users::OmniauthCallbacksController < ApplicationController
|
|||||||
# Save to redis, with a secret token, then redirect to confirmation screen
|
# Save to redis, with a secret token, then redirect to confirmation screen
|
||||||
token = SecureRandom.hex
|
token = SecureRandom.hex
|
||||||
Discourse.redis.setex "#{Users::AssociateAccountsController::REDIS_PREFIX}_#{current_user.id}_#{token}", 10.minutes, auth.to_json
|
Discourse.redis.setex "#{Users::AssociateAccountsController::REDIS_PREFIX}_#{current_user.id}_#{token}", 10.minutes, auth.to_json
|
||||||
return redirect_to Discourse.base_uri("/associate/#{token}")
|
return redirect_to "#{Discourse.base_uri}/associate/#{token}"
|
||||||
else
|
else
|
||||||
@auth_result = authenticator.after_authenticate(auth)
|
@auth_result = authenticator.after_authenticate(auth)
|
||||||
DiscourseEvent.trigger(:after_auth, authenticator, @auth_result)
|
DiscourseEvent.trigger(:after_auth, authenticator, @auth_result)
|
||||||
@ -55,7 +55,7 @@ class Users::OmniauthCallbacksController < ApplicationController
|
|||||||
|
|
||||||
if parsed && # Valid
|
if parsed && # Valid
|
||||||
(parsed.host == nil || parsed.host == Discourse.current_hostname) && # Local
|
(parsed.host == nil || parsed.host == Discourse.current_hostname) && # Local
|
||||||
!parsed.path.starts_with?(Discourse.base_uri("/auth/")) # Not /auth URL
|
!parsed.path.starts_with?("#{Discourse.base_uri}/auth/") # Not /auth URL
|
||||||
@origin = +"#{parsed.path}"
|
@origin = +"#{parsed.path}"
|
||||||
@origin << "?#{parsed.query}" if parsed.query
|
@origin << "?#{parsed.query}" if parsed.query
|
||||||
end
|
end
|
||||||
|
@ -545,6 +545,19 @@ RSpec.describe Users::OmniauthCallbacksController do
|
|||||||
expect(cookie_data["destination_url"]).to eq('/t/123')
|
expect(cookie_data["destination_url"]).to eq('/t/123')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "redirects to internal origin on subfolder" do
|
||||||
|
set_subfolder "/subpath"
|
||||||
|
|
||||||
|
post "/auth/google_oauth2?origin=http://test.localhost/subpath/t/123"
|
||||||
|
get "/auth/google_oauth2/callback"
|
||||||
|
|
||||||
|
expect(response.status).to eq 302
|
||||||
|
expect(response.location).to eq "http://test.localhost/subpath/t/123"
|
||||||
|
|
||||||
|
cookie_data = JSON.parse(response.cookies['authentication_data'])
|
||||||
|
expect(cookie_data["destination_url"]).to eq('/subpath/t/123')
|
||||||
|
end
|
||||||
|
|
||||||
it "never redirects to /auth/ origin" do
|
it "never redirects to /auth/ origin" do
|
||||||
post "/auth/google_oauth2?origin=http://test.localhost/auth/google_oauth2"
|
post "/auth/google_oauth2?origin=http://test.localhost/auth/google_oauth2"
|
||||||
get "/auth/google_oauth2/callback"
|
get "/auth/google_oauth2/callback"
|
||||||
@ -556,6 +569,19 @@ RSpec.describe Users::OmniauthCallbacksController do
|
|||||||
expect(cookie_data["destination_url"]).to eq('/')
|
expect(cookie_data["destination_url"]).to eq('/')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "never redirects to /auth/ origin on subfolder" do
|
||||||
|
set_subfolder "/subpath"
|
||||||
|
|
||||||
|
post "/auth/google_oauth2?origin=http://test.localhost/subpath/auth/google_oauth2"
|
||||||
|
get "/auth/google_oauth2/callback"
|
||||||
|
|
||||||
|
expect(response.status).to eq 302
|
||||||
|
expect(response.location).to eq "http://test.localhost/subpath"
|
||||||
|
|
||||||
|
cookie_data = JSON.parse(response.cookies['authentication_data'])
|
||||||
|
expect(cookie_data["destination_url"]).to eq('/subpath')
|
||||||
|
end
|
||||||
|
|
||||||
it "redirects to relative origin" do
|
it "redirects to relative origin" do
|
||||||
post "/auth/google_oauth2?origin=/t/123"
|
post "/auth/google_oauth2?origin=/t/123"
|
||||||
get "/auth/google_oauth2/callback"
|
get "/auth/google_oauth2/callback"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user