Skip to content

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

FIX: Handle external permalink redirects during in-app navigation (#38767)

Régis Hanol885c9eded25f
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index c9f1ac06a28..b628fd5433d 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -118,7 +118,15 @@ class TopicsController < ApplicationController
return redirect_to_correct_topic(topic, opts[:post_number]) if topic
end
- raise ex
+ topic_id = params[:topic_id] || params[:id]
+ original_path =
+ if params[:slug].present? && topic_id.present?
+ "/t/#{params[:slug]}/#{topic_id}"
+ else
+ request.path
+ end
+
+ raise Discourse::NotFound.new(ex.message, check_permalinks: true, original_path:)
rescue Discourse::NotLoggedIn => ex
raise(SiteSetting.detailed_404 ? ex : Discourse::NotFound)
rescue Discourse::InvalidAccess => ex
diff --git a/frontend/discourse/app/instance-initializers/message-bus.js b/frontend/discourse/app/instance-initializers/message-bus.js
index 72c410008cb..21deb31a3c1 100644
--- a/frontend/discourse/app/instance-initializers/message-bus.js
+++ b/frontend/discourse/app/instance-initializers/message-bus.js
@@ -57,8 +57,7 @@ function mbAjax(messageBus, opts) {
const oldComplete = opts.complete;
opts.complete = function (xhr, stat) {
- handleLogoff(xhr);
- oldComplete?.(xhr, stat);
+ handleLogoff(xhr) || oldComplete?.(xhr, stat);
};
return $.ajax(opts);
diff --git a/frontend/discourse/app/lib/ajax.js b/frontend/discourse/app/lib/ajax.js
index 8507cc769c7..5c4bfbacafb 100644
--- a/frontend/discourse/app/lib/ajax.js
+++ b/frontend/discourse/app/lib/ajax.js
@@ -3,6 +3,7 @@ import { run } from "@ember/runloop";
import $ from "jquery";
import { isTesting } from "discourse/lib/environment";
import getURL from "discourse/lib/get-url";
+import DiscourseURL from "discourse/lib/url";
import userPresent from "discourse/lib/user-presence";
import Session from "discourse/models/session";
import Site from "discourse/models/site";
@@ -49,13 +50,17 @@ export function setLogoffCallback(cb) {
export function handleLogoff(xhr) {
if (xhr && xhr.getResponseHeader("Discourse-Logged-Out") && _logoffCallback) {
_logoffCallback();
+ return true;
}
+ return false;
}
function handleRedirect(xhr) {
if (xhr && xhr.getResponseHeader("Discourse-Xhr-Redirect")) {
- window.location = xhr.responseText;
+ DiscourseURL.redirectAbsolute(xhr.responseText);
+ return true;
}
+ return false;
}
let activeCsrfRequest;
@@ -144,8 +149,9 @@ export function ajax() {
}
args.success = (data, textStatus, xhr) => {
- handleRedirect(xhr);
- handleLogoff(xhr);
+ if (handleRedirect(xhr) || handleLogoff(xhr)) {
+ return resolve();
+ }
run(() => {
Site.currentProp(
@@ -171,7 +177,9 @@ export function ajax() {
return;
}
- handleLogoff(xhr);
+ if (handleRedirect(xhr) || handleLogoff(xhr)) {
+ return resolve();
+ }
// note: for bad CSRF we don't loop an extra request right away.
// this allows us to eliminate the possibility of having a loop.
@@ -181,7 +189,11 @@ export function ajax() {
// If it's a parser error, don't reject
if (xhr.status === 200) {
- return args.success(xhr);
+ return args.success(
+ xhr.responseJSON ?? xhr.responseText,
+ textStatus,
+ xhr
+ );
}
// Fill in some extra info
diff --git a/frontend/discourse/tests/acceptance/permalink-redirect-test.js b/frontend/discourse/tests/acceptance/permalink-redirect-test.js
new file mode 100644
index 00000000000..f09adc51e09
--- /dev/null
+++ b/frontend/discourse/tests/acceptance/permalink-redirect-test.js
@@ -0,0 +1,25 @@
+import { visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import sinon from "sinon";
+import DiscourseURL from "discourse/lib/url";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("External Permalink Redirect via XHR", function (needs) {
+ needs.pretender((server) => {
+ server.get("/t/99.json", () => [
+ 200,
+ { "Discourse-Xhr-Redirect": "true", "Content-Type": "text/plain" },
+ "https://www.example.com",
+ ]);
+ });
+
+ test("redirects to external URL when topic has an external permalink", async function (assert) {
+ sinon.stub(DiscourseURL, "redirectAbsolute");
+
+ await visit("/t/a-deleted-topic/99");
+
+ assert.true(
+ DiscourseURL.redirectAbsolute.calledWith("https://www.example.com")
+ );
+ });
+});
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 039a356737d..66072d87ddb 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -3526,6 +3526,78 @@ RSpec.describe TopicsController do
end
end
+ describe "with external permalink on a soft-deleted topic" do
+ fab!(:topic) { Fabricate(:post, user: post_author1).topic }
+
+ before do
+ topic.trash!(Discourse.system_user)
+ Permalink.create!(
+ url: "t/#{topic.slug}/#{topic.id}",
+ external_url: "https://www.example.com",
+ )
+ end
+
+ it "returns Discourse-Xhr-Redirect header for XHR requests" do
+ get "/t/#{topic.slug}/#{topic.id}.json",
+ headers: {
+ "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.headers["Discourse-Xhr-Redirect"]).to eq("true")
+ expect(response.body).to eq("https://www.example.com")
+ end
+
+ it "returns Discourse-Xhr-Redirect header for XHR requests when detailed_404 is enabled" do
+ SiteSetting.detailed_404 = true
+
+ get "/t/#{topic.slug}/#{topic.id}.json",
+ headers: {
+ "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.headers["Discourse-Xhr-Redirect"]).to eq("true")
+ expect(response.body).to eq("https://www.example.com")
+ end
+
+ it "returns 301 redirect for non-XHR requests" do
+ get "/t/#{topic.slug}/#{topic.id}"
+
+ expect(response.status).to eq(301)
+ expect(response.headers["Location"]).to eq("https://www.example.com")
+ end
+ end
+
+ describe "with external permalink on a nonexistent topic" do
+ let!(:nonexistent_topic_id) { Topic.maximum(:id) + 1 }
+
+ before do
+ Permalink.create!(
+ url: "t/old-topic/#{nonexistent_topic_id}",
+ external_url: "https://www.example.com",
+ )
+ end
+
+ it "returns Discourse-Xhr-Redirect header for XHR requests" do
+ get "/t/old-topic/#{nonexistent_topic_id}.json",
+ headers: {
+ "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.headers["Discourse-Xhr-Redirect"]).to eq("true")
+ expect(response.body).to eq("https://www.example.com")
+ end
+
+ it "returns 301 redirect for non-XHR requests" do
+ get "/t/old-topic/#{nonexistent_topic_id}"
+
+ expect(response.status).to eq(301)
+ expect(response.headers["Location"]).to eq("https://www.example.com")
+ end
+ end
+
describe "#show filters" do
fab!(:post) { Fabricate(:post, user: post_author1) }
fab!(:topic) { post.topic }