Skip to content

A platform for community discussion. Free, open, simple.

FIX: DiscourseConnect provider redirect to forum instead of the sso_redirect_url in the payload (#34580)

Juan David Martínez Cubillos587a180a6631
diff --git a/app/assets/javascripts/discourse/app/controllers/login.js b/app/assets/javascripts/discourse/app/controllers/login.js
index 427304a6946..1172568d0a4 100644
--- a/app/assets/javascripts/discourse/app/controllers/login.js
+++ b/app/assets/javascripts/discourse/app/controllers/login.js
@@ -260,6 +260,7 @@ export default class LoginPageController extends Controller {
set("password", this.loginPassword);
const destinationUrl = cookie("destination_url");
+
if (destinationUrl) {
removeCookie("destination_url");
set("redirect", destinationUrl);
diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb
index bbae3bb5603..4fb62ee4917 100644
--- a/app/controllers/session_controller.rb
+++ b/app/controllers/session_controller.rb
@@ -73,14 +73,16 @@ class SessionController < ApplicationController
end
if request.xhr?
- cookies[:destination_url] = data[:sso_redirect_url]
+ # This is needed for DiscourseConnect provider to redirect users correctly
+ cookies[:sso_destination_url] = data[:sso_redirect_url]
render json: success_json.merge(redirect_url: data[:sso_redirect_url])
else
redirect_to data[:sso_redirect_url], allow_other_host: true
end
elsif result.no_second_factors_enabled?
if request.xhr?
- cookies[:destination_url] = result.data[:sso_redirect_url]
+ # This is needed for DiscourseConnect provider to redirect users correctly
+ cookies[:sso_destination_url] = result.data[:sso_redirect_url]
else
redirect_to result.data[:sso_redirect_url], allow_other_host: true
end
diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb
index 960556a19cc..b4f3c6bf724 100644
--- a/app/controllers/static_controller.rb
+++ b/app/controllers/static_controller.rb
@@ -151,7 +151,15 @@ class StaticController < ApplicationController
params.delete(:password)
destination = extract_redirect_param
- redirect_to(destination, allow_other_host: false)
+
+ allow_other_hosts = false
+
+ if cookies[:sso_destination_url]
+ destination = cookies.delete(:sso_destination_url)
+ allow_other_hosts = true
+ end
+
+ redirect_to(destination, allow_other_host: allow_other_hosts)
end
FAVICON = -"favicon"
diff --git a/spec/requests/session_controller_spec.rb b/spec/requests/session_controller_spec.rb
index 1562cd578b4..87347a8fcbe 100644
--- a/spec/requests/session_controller_spec.rb
+++ b/spec/requests/session_controller_spec.rb
@@ -1482,7 +1482,7 @@ RSpec.describe SessionController do
xhr: true,
headers: headers
- location = response.cookies["destination_url"]
+ location = response.cookies["sso_destination_url"]
# javascript code will handle redirection of user to return_sso_url
expect(location).to match(%r{^http://somewhere.over.rainbow/sso})
@@ -1806,8 +1806,8 @@ RSpec.describe SessionController do
xhr: true,
headers: headers
expect(response.status).to eq(200)
- # the frontend will take care of actually redirecting the user
- redirect_url = response.cookies["destination_url"]
+ # the backend will take care of actually redirecting the user
+ redirect_url = response.cookies["sso_destination_url"]
expect(redirect_url).to start_with("http://somewhere.over.rainbow/sso?sso=")
sso = DiscourseConnectProvider.parse(URI(redirect_url).query)
expect(sso.confirmed_2fa).to eq(true)
@@ -1829,7 +1829,7 @@ RSpec.describe SessionController do
},
xhr: true,
headers: headers
- redirect_url = response.cookies["destination_url"]
+ redirect_url = response.cookies["sso_destination_url"]
expect(redirect_url).to start_with("http://somewhere.over.rainbow/sso?sso=")
sso = DiscourseConnectProvider.parse(URI(redirect_url).query)
expect(sso.confirmed_2fa).to eq(nil)
diff --git a/spec/support/sso_support_helpers.rb b/spec/support/sso_support_helpers.rb
new file mode 100644
index 00000000000..aeed7435a0a
--- /dev/null
+++ b/spec/support/sso_support_helpers.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+require "webrick"
+
+module SsoHelpers
+ def build_sso_payload(return_url)
+ secret = SiteSetting.discourse_connect_provider_secrets.split("|")[1]
+ nonce = SecureRandom.hex
+
+ payload = "nonce=#{CGI.escape(nonce)}&return_sso_url=#{CGI.escape(return_url)}"
+ sso = Base64.strict_encode64(payload)
+ sig = OpenSSL::HMAC.hexdigest("sha256", secret, sso)
+
+ [sso, sig]
+ end
+
+ def setup_test_sso_server(user:, sso_secret:, sso_port:, sso_url:)
+ @server =
+ WEBrick::HTTPServer.new(
+ Port: sso_port,
+ Logger: WEBrick::Log.new(File.open(File::NULL, "w")),
+ AccessLog: [],
+ )
+
+ @server.mount_proc "/sso" do |req, res|
+ decoded = Base64.decode64(req.query["sso"])
+ params = Rack::Utils.parse_query(decoded)
+
+ response_sso = DiscourseConnectBase.new
+ response_sso.nonce = params["nonce"]
+ response_sso.sso_secret = sso_secret
+ response_sso.external_id = "foo-bar"
+ response_sso.email = user.email
+ response_sso.username = user.username
+
+ res.status = 302
+ res["Location"] = "#{params["return_sso_url"]}?#{response_sso.payload}"
+ end
+
+ @server_thread = Thread.new { @server.start }
+
+ sleep 0.1 until server_responding?(sso_url)
+ end
+
+ def shutdown_test_sso_server
+ @server&.shutdown
+ @server_thread&.kill
+ end
+
+ def server_responding?(sso_url)
+ Net::HTTP.get_response(URI(sso_url))
+ true
+ rescue StandardError
+ false
+ end
+end
diff --git a/spec/system/discourse_connect_provider_spec.rb b/spec/system/discourse_connect_provider_spec.rb
new file mode 100644
index 00000000000..62883d0fb43
--- /dev/null
+++ b/spec/system/discourse_connect_provider_spec.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+require "rotp"
+
+describe "Discourse Connect Provider", type: :system do
+ include SsoHelpers
+
+ let(:sso_secret) { SecureRandom.alphanumeric(32) }
+ let(:sso_port) { 9876 }
+ let(:sso_url) { "http://localhost:#{sso_port}/sso" }
+
+ fab!(:user) { Fabricate(:user, username: "john", password: "supersecurepassword") }
+ let(:login_form) { PageObjects::Pages::Login.new }
+ let!(:return_url) { "http://localhost:#{sso_port}/test/url" }
+ before do
+ SiteSetting.enable_discourse_connect_provider = true
+ SiteSetting.discourse_connect_provider_secrets = "localhost|Test"
+ SiteSetting.enable_discourse_connect = false
+ Jobs.run_immediately!
+
+ setup_test_sso_server(user: user, sso_secret:, sso_port:, sso_url:)
+ end
+
+ after { shutdown_test_sso_server }
+
+ it "redirects back to the return_sso_url after successful login" do
+ sso, sig = build_sso_payload(return_url)
+ EmailToken.confirm(Fabricate(:email_token, user: user).token)
+
+ visit "/"
+ visit "/session/sso_provider?sso=#{CGI.escape(sso)}&sig=#{sig}"
+ expect(page).to have_current_path("/login")
+
+ login_form.fill(username: "john", password: "supersecurepassword").click_login
+
+ expect(page).to have_current_path(
+ /#{Regexp.escape(return_url)}\?sso=.*&sig=[0-9a-f]+/,
+ url: true,
+ ignore_query: false,
+ )
+ end
+ context "with two-factor authentication" do
+ let!(:user_second_factor) { Fabricate(:user_second_factor_totp, user: user) }
+ let!(:user_second_factor_backup) { Fabricate(:user_second_factor_backup, user: user) }
+ fab!(:other_user) { Fabricate(:user, username: "jane", password: "supersecurepassword") }
+
+ it "redirects back to the return_sso_url" do
+ sso, sig = build_sso_payload(return_url)
+ EmailToken.confirm(Fabricate(:email_token, user: user).token)
+
+ visit "/"
+ visit "/session/sso_provider?sso=#{CGI.escape(sso)}&sig=#{sig}"
+ expect(page).to have_current_path("/login")
+
+ login_form.fill(username: "john", password: "supersecurepassword").click_login
+
+ expect(page).to have_css(".second-factor")
+
+ totp = ROTP::TOTP.new(user_second_factor.data).now
+ find("#login-second-factor").fill_in(with: totp)
+ login_form.click_login
+
+ expect(page).to have_current_path(
+ /#{Regexp.escape(return_url)}\?sso=.*&sig=[0-9a-f]+/,
+ url: true,
+ ignore_query: false,
+ )
+ end
+ end
+end
diff --git a/spec/system/discourse_connect_spec.rb b/spec/system/discourse_connect_spec.rb
index b367f350d4e..2e5f75ed3e6 100644
--- a/spec/system/discourse_connect_spec.rb
+++ b/spec/system/discourse_connect_spec.rb
@@ -1,24 +1,15 @@
# frozen_string_literal: true
-require "webrick"
-
describe "Discourse Connect", type: :system do
+ include SsoHelpers
+
let(:sso_secret) { SecureRandom.alphanumeric(32) }
let(:sso_port) { 9876 }
let(:sso_url) { "http://localhost:#{sso_port}/sso" }
- fab!(:user)
- fab!(:private_group) { Fabricate(:group, users: [user]) }
- fab!(:private_category) { Fabricate(:private_category, group: private_group) }
- fab!(:private_topic) { Fabricate(:topic, category: private_category) }
- fab!(:private_post) { Fabricate(:post, topic: private_topic) }
-
- fab!(:topic)
- fab!(:post) { Fabricate(:post, topic:) }
-
before do
- setup_test_sso_server
configure_discourse_connect
+ setup_test_sso_server(user:, sso_secret:, sso_port:, sso_url:)
end
after { shutdown_test_sso_server }
@@ -42,149 +33,120 @@ describe "Discourse Connect", type: :system do
end
end
- context "when login_required is false" do
- before { SiteSetting.login_required = false }
+ context "when using vanilla DiscourseConnect" do
+ fab!(:user)
+ fab!(:private_group) { Fabricate(:group, users: [user]) }
+ fab!(:private_category) { Fabricate(:private_category, group: private_group) }
+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }
+ fab!(:private_post) { Fabricate(:post, topic: private_topic) }
- context "when auth_immediately is false" do
- before { SiteSetting.auth_immediately = false }
+ fab!(:topic)
+ fab!(:post) { Fabricate(:post, topic:) }
- context "when visiting /" do
- before { visit "/" }
- it_behaves_like "shows the homepage"
- end
+ context "when login_required is false" do
+ before { SiteSetting.login_required = false }
- context "when visiting / and clicking the login button" do
- before do
- visit "/"
- find(".login-button").click
- end
+ context "when auth_immediately is false" do
+ before { SiteSetting.auth_immediately = false }
- it_behaves_like "redirects to SSO"
- end
+ context "when visiting /" do
+ before { visit "/" }
+ it_behaves_like "shows the homepage"
+ end
- context "when visiting /login" do
- before { visit "/login" }
- it_behaves_like "redirects to SSO"
- end
- end
+ context "when visiting / and clicking the login button" do
+ before do
+ visit "/"
+ find(".login-button").click
+ end
- context "when auth_immediately is true" do
- before { SiteSetting.auth_immediately = true }
+ it_behaves_like "redirects to SSO"
+ end
- context "when visiting /" do
- before { visit "/" }
- it_behaves_like "shows the homepage"
+ context "when visiting /login" do
+ before { visit "/login" }
+ it_behaves_like "redirects to SSO"
+ end
end
- context "when visiting / and clicking the login button" do
- before do
- visit "/"
- find(".login-button").click
+ context "when auth_immediately is true" do
+ before { SiteSetting.auth_immediately = true }
+
+ context "when visiting /" do
+ before { visit "/" }
+ it_behaves_like "shows the homepage"
end
- it_behaves_like "redirects to SSO"
- end
+ context "when visiting / and clicking the login button" do
+ before do
+ visit "/"
+ find(".login-button").click
+ end
- context "when visiting /login" do
- before { visit "/login" }
- it_behaves_like "redirects to SSO"
- end
+ it_behaves_like "redirects to SSO"
+ end
- it "redirects the user back to the landing URL" do
- visit private_topic.url
+ context "when visiting /login" do
+ before { visit "/login" }
+ it_behaves_like "redirects to SSO"
+ end
- find(".login-button").click
+ it "redirects the user back to the landing URL" do
+ visit private_topic.url
- wait_for { has_css?("#current-user") }
+ find(".login-button").click
- expect(page).to have_current_path(private_topic.relative_url)
+ wait_for { has_css?("#current-user") }
+
+ expect(page).to have_current_path(private_topic.relative_url)
+ end
end
end
- end
-
- context "when login_required is true" do
- before { SiteSetting.login_required = true }
- context "when auth_immediately is false" do
- before { SiteSetting.auth_immediately = false }
+ context "when login_required is true" do
+ before { SiteSetting.login_required = true }
- context "when visiting /" do
- before { visit "/" }
- it_behaves_like "shows the login splash"
- end
+ context "when auth_immediately is false" do
+ before { SiteSetting.auth_immediately = false }
- context "when visiting / and clicking the login button" do
- before do
- visit "/"
- find(".login-button").click
+ context "when visiting /" do
+ before { visit "/" }
+ it_behaves_like "shows the login splash"
end
- it_behaves_like "redirects to SSO"
- end
+ context "when visiting / and clicking the login button" do
+ before do
+ visit "/"
+ find(".login-button").click
+ end
- context "when visiting /login" do
- before { visit "/login" }
- it_behaves_like "redirects to SSO"
+ it_behaves_like "redirects to SSO"
+ end
+
+ context "when visiting /login" do
+ before { visit "/login" }
+ it_behaves_like "redirects to SSO"
+ end
end
- end
- context "when auth_immediately is true" do
- before { SiteSetting.auth_immediately = true }
+ context "when auth_immediately is true" do
+ before { SiteSetting.auth_immediately = true }
- context "when visiting /" do
- before { visit "/" }
- it_behaves_like "redirects to SSO"
- end
+ context "when visiting /" do
+ before { visit "/" }
+ it_behaves_like "redirects to SSO"
+ end
- context "when visiting /login" do
- before { visit "/login" }
- it_behaves_like "redirects to SSO"
+ context "when visiting /login" do
+ before { visit "/login" }
+ it_behaves_like "redirects to SSO"
+ end
end
end
end
private
- def setup_test_sso_server
- @server =
- WEBrick::HTTPServer.new(
- Port: sso_port,
- Logger: WEBrick::Log.new(File.open(File::NULL, "w")),
- AccessLog: [],
- )
-
- @server.mount_proc "/sso" do |req, res|
- decoded = Base64.decode64(req.query["sso"])
- params = Rack::Utils.parse_query(decoded)
-
- response_sso = DiscourseConnectBase.new
- response_sso.nonce = params["nonce"]
- response_sso.sso_secret = sso_secret
- response_sso.external_id = "foo-bar"
- response_sso.email = user.email
- response_sso.username = user.username
-
- res.status = 302
- res["Location"] = "#{params["return_sso_url"]}?#{response_sso.payload}"
- end
-
- @server_thread = Thread.new { @server.start }
-
- sleep 0.1 until server_responding?
- end
-
- def shutdown_test_sso_server
- @server&.shutdown
- @server_thread&.kill
- end
-
- def server_responding?
- Net::HTTP.get_response(URI(sso_url))
- true
- rescue StandardError
- false
- end
-
def configure_discourse_connect
SiteSetting.discourse_connect_url = sso_url
SiteSetting.discourse_connect_secret = sso_secret