From f862678008a2537ce0de725a11c43914ab91f68a Mon Sep 17 00:00:00 2001
From: Ahmad Farhat <ahmad.af.farhat@gmail.com>
Date: Wed, 28 Jun 2023 10:48:42 -0400
Subject: [PATCH] Fix room redirect not working for legacy links (#5293)

---
 app/controllers/external_controller.rb       |  2 +-
 spec/controllers/external_controller_spec.rb | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/app/controllers/external_controller.rb b/app/controllers/external_controller.rb
index fd36ad4a..d66c1dc6 100644
--- a/app/controllers/external_controller.rb
+++ b/app/controllers/external_controller.rb
@@ -65,7 +65,7 @@ class ExternalController < ApplicationController
 
     redirect_location = cookies.delete(:location)
 
-    return redirect_to redirect_location, allow_other_host: false if redirect_location&.match?('\/rooms\/\w{3}-\w{3}-\w{3}-\w{3}\/join\z')
+    return redirect_to redirect_location, allow_other_host: false if redirect_location&.match?('\/rooms\/\w{3}-\w{3}-\w{3}(-\w{3})?\/join\z')
 
     redirect_to root_path
   rescue ActionController::Redirecting::UnsafeRedirectError => e
diff --git a/spec/controllers/external_controller_spec.rb b/spec/controllers/external_controller_spec.rb
index 04ae4cc7..666bea26 100644
--- a/spec/controllers/external_controller_spec.rb
+++ b/spec/controllers/external_controller_spec.rb
@@ -86,6 +86,18 @@ RSpec.describe ExternalController, type: :controller do
         expect(response).to redirect_to('/rooms/o5g-hvb-s44-p5t/join')
       end
 
+      it 'redirects to the location cookie if its a legacy url (3 sections in uid)' do
+        request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect]
+
+        cookies[:location] = {
+          value: '/rooms/o5g-hvb-s44/join',
+          path: '/'
+        }
+        get :create_user, params: { provider: 'openid_connect' }
+
+        expect(response).to redirect_to('/rooms/o5g-hvb-s44/join')
+      end
+
       it 'redirects to the location cookie if a relative redirection 2' do
         request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect]
 
-- 
GitLab