Skip to content

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

FEATURE: allow adding anchor tag to 404 page title (#35318)

Meghnac87277b2a5e8
diff --git a/app/views/exceptions/not_found.html.erb b/app/views/exceptions/not_found.html.erb
index cfce61ea383..bc249318323 100644
--- a/app/views/exceptions/not_found.html.erb
+++ b/app/views/exceptions/not_found.html.erb
@@ -29,7 +29,7 @@
</div>
<div class="title_wrapper">
- <h1 class="title"><%= @title %></h1>
+ <h1 class="title"><%= sanitize(@title, tags: %w[a], attributes: %w[href class target rel]) %></h1>
<a class="btn btn-primary btn-large --home" href="<%= Discourse.base_url %>"> <%= SvgSprite.raw_svg('house') %> <%= I18n.t('page_not_found.home') %>
</a>
</div>
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index 04ae21442de..e49806fe205 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -442,6 +442,29 @@ RSpec.describe ApplicationController do
expect(response.body).to_not include("google.com/search")
end
+ it "should allow anchor tags in title" do
+ I18n.backend.store_translations(
+ :en,
+ { page_not_found: { title: 'Visit <a href="/search">search</a> page' } },
+ )
+
+ get "/t/nope-nope/99999999"
+ expect(response.status).to eq(404)
+ expect(response.body).to include('<a href="/search">search</a>')
+ end
+
+ it "should sanitize unsafe HTML in title" do
+ I18n.backend.store_translations(
+ :en,
+ { page_not_found: { title: 'Page <script>alert("xss")</script> not found' } },
+ )
+
+ get "/t/nope-nope/99999999"
+ expect(response.status).to eq(404)
+ expect(response.body).to_not include("<script>")
+ expect(response.body).to include("Page")
+ end
+
describe "no logspam" do
let(:fake_logger) { FakeLogger.new }
@@ -1199,8 +1222,14 @@ RSpec.describe ApplicationController do
it "serves a 404 page in the preferred locale" do
get "/missingroute", headers: headers("fr")
expect(response.status).to eq(404)
- expected_title = I18n.t("page_not_found.title", locale: :fr)
- expect(response.body).to include(CGI.escapeHTML(expected_title))
+ expect(response.body).to include(
+ # converts non-breaking space to &nbsp;
+ ActionController::Base.helpers.sanitize(
+ I18n.t("page_not_found.title", locale: :fr),
+ tags: %w[a],
+ attributes: %w[href class target rel],
+ ),
+ )
end
it "serves a RenderEmpty page in the preferred locale" do