From a442eeb0f4bdb951e2c1d4bd443828438dff4912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Thu, 27 Jun 2024 15:09:16 +0200 Subject: [PATCH] =?UTF-8?q?FIX:=20Don=E2=80=99t=20raise=20an=20error=20on?= =?UTF-8?q?=20permalinks=20with=20external=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently redirecting to an external URL through a permalink doesn’t work because Rails raises a `ActionController::Redirecting::UnsafeRedirectError` error. This wasn’t the case before we upgraded to Rails 7.0. This patch fixes the issue by using `allow_other_host: true` on the redirect. --- app/controllers/permalinks_controller.rb | 2 +- spec/requests/permalinks_controller_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/controllers/permalinks_controller.rb b/app/controllers/permalinks_controller.rb index ee34e6bfaad..fd4264ba2dd 100644 --- a/app/controllers/permalinks_controller.rb +++ b/app/controllers/permalinks_controller.rb @@ -11,7 +11,7 @@ class PermalinksController < ApplicationController raise Discourse::NotFound unless permalink if permalink.target_url - redirect_to permalink.target_url, status: :moved_permanently + redirect_to permalink.target_url, status: :moved_permanently, allow_other_host: true else raise Discourse::NotFound end diff --git a/spec/requests/permalinks_controller_spec.rb b/spec/requests/permalinks_controller_spec.rb index 0053bb4407b..2f421ee6574 100644 --- a/spec/requests/permalinks_controller_spec.rb +++ b/spec/requests/permalinks_controller_spec.rb @@ -44,5 +44,14 @@ RSpec.describe PermalinksController do get "/not/a/valid/url" expect(response.status).to eq(404) end + + context "when permalink's target_url is an external URL" do + before { permalink.update!(external_url: "https://github.com/discourse/discourse") } + + it "redirects to it properly" do + get "/#{permalink.url}" + expect(response).to redirect_to(permalink.external_url) + end + end end end