Skip to content

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

SECURITY: prevent permalink redirects from leaking restricted slugs

Régis Hanol250c54e302a0
diff --git a/app/controllers/permalinks_controller.rb b/app/controllers/permalinks_controller.rb
index fd4264ba2dd..03302e5aff7 100644
--- a/app/controllers/permalinks_controller.rb
+++ b/app/controllers/permalinks_controller.rb
@@ -4,11 +4,10 @@ class PermalinksController < ApplicationController
skip_before_action :check_xhr, :preload_json, only: [:show]
def show
- url = request.fullpath
+ permalink = Permalink.find_by_url(request.fullpath)
- permalink = Permalink.find_by_url(url)
-
- raise Discourse::NotFound unless permalink
+ raise Discourse::NotFound if permalink.nil?
+ raise Discourse::NotFound unless guardian.can_see_permalink_target?(permalink)
if permalink.target_url
redirect_to permalink.target_url, status: :moved_permanently, allow_other_host: true
@@ -18,23 +17,15 @@ class PermalinksController < ApplicationController
end
def check
- begin
- raise Discourse::NotFound if params[:path].blank?
-
- permalink = Permalink.find_by_url(params[:path])
+ permalink = Permalink.find_by_url(params[:path]) if params[:path].present?
- raise Discourse::NotFound unless permalink
+ data =
+ if permalink && guardian.can_see_permalink_target?(permalink)
+ { found: true, internal: permalink.internal?, target_url: permalink.target_url }
+ else
+ { found: false, html: build_not_found_page(status: 200) }
+ end
- data = {
- found: true,
- internal: permalink.external_url.nil?,
- target_url: permalink.target_url,
- }
-
- render json: MultiJson.dump(data)
- rescue Discourse::NotFound
- data = { found: false, html: build_not_found_page(status: 200) }
- render json: MultiJson.dump(data)
- end
+ render json: MultiJson.dump(data)
end
end
diff --git a/app/models/permalink.rb b/app/models/permalink.rb
index da3465f19f8..e01fd9bffc9 100644
--- a/app/models/permalink.rb
+++ b/app/models/permalink.rb
@@ -12,6 +12,14 @@ class Permalink < ActiveRecord::Base
validates :url, uniqueness: true
validate :exactly_one_association
+ def internal?
+ external_url.blank?
+ end
+
+ def external?
+ external_url.present?
+ end
+
class Normalizer
attr_reader :source
diff --git a/lib/guardian.rb b/lib/guardian.rb
index b86ee517108..f9d9c0368d0 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -6,6 +6,7 @@ require "guardian/ensure_magic"
require "guardian/group_guardian"
require "guardian/invite_guardian"
require "guardian/flag_guardian"
+require "guardian/permalink_guardian"
require "guardian/post_guardian"
require "guardian/post_revision_guardian"
require "guardian/sidebar_guardian"
@@ -22,6 +23,7 @@ class Guardian
include FlagGuardian
include GroupGuardian
include InviteGuardian
+ include PermalinkGuardian
include PostGuardian
include PostRevisionGuardian
include LocalizationGuardian
diff --git a/lib/guardian/permalink_guardian.rb b/lib/guardian/permalink_guardian.rb
new file mode 100644
index 00000000000..f35174b151f
--- /dev/null
+++ b/lib/guardian/permalink_guardian.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module PermalinkGuardian
+ def can_see_permalink_target?(permalink)
+ return true if permalink.external?
+ return can_see_topic?(permalink.topic) if permalink.topic_id.present?
+ return can_see_post?(permalink.post) if permalink.post_id.present?
+ return can_see_category?(permalink.category) if permalink.category_id.present?
+ return can_see_tag?(permalink.tag) if permalink.tag_id.present?
+ return can_see_user?(permalink.user) if permalink.user_id.present?
+ false
+ end
+end
diff --git a/lib/guardian/tag_guardian.rb b/lib/guardian/tag_guardian.rb
index 7db6984796d..0bfff5392d8 100644
--- a/lib/guardian/tag_guardian.rb
+++ b/lib/guardian/tag_guardian.rb
@@ -1,8 +1,9 @@
# frozen_string_literal: true
-#mixin for all guardian methods dealing with tagging permissions
module TagGuardian
- def can_see_tag?(_tag)
+ def can_see_tag?(tag)
+ return false if !SiteSetting.tagging_enabled
+ return false if hidden_tag_names.include?(tag.name)
true
end
diff --git a/spec/lib/guardian/tag_guardian_spec.rb b/spec/lib/guardian/tag_guardian_spec.rb
index a2694c56033..3a60ea722b5 100644
--- a/spec/lib/guardian/tag_guardian_spec.rb
+++ b/spec/lib/guardian/tag_guardian_spec.rb
@@ -9,15 +9,31 @@ RSpec.describe TagGuardian do
fab!(:trust_level_2)
fab!(:trust_level_3)
- ###### VISIBILITY ######
-
describe "#can_see_tag?" do
- it "returns true for everyone" do
- expect(Guardian.new(nil).can_see_tag?(anything)).to be_truthy
+ it "returns false when tagging is disabled" do
+ SiteSetting.tagging_enabled = false
+
+ expect(Guardian.new(admin).can_see_tag?(tag)).to be_falsey
end
- end
- ###### CREATION ######
+ it "returns true for a visible tag" do
+ expect(Guardian.new(nil).can_see_tag?(tag)).to be_truthy
+ expect(Guardian.new(user).can_see_tag?(tag)).to be_truthy
+ end
+
+ it "returns false for a hidden tag when user is not in permitted group" do
+ Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [tag.name])
+
+ expect(Guardian.new(nil).can_see_tag?(tag)).to be_falsey
+ expect(Guardian.new(user).can_see_tag?(tag)).to be_falsey
+ end
+
+ it "returns true for a hidden tag when user is staff" do
+ Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [tag.name])
+
+ expect(Guardian.new(admin).can_see_tag?(tag)).to be_truthy
+ end
+ end
describe "#can_create_tag?" do
it "returns false when tagging is disabled" do
@@ -35,8 +51,6 @@ RSpec.describe TagGuardian do
end
end
- ###### EDITING ######
-
describe "#can_edit_tag?" do
it "returns false when tagging is disabled" do
SiteSetting.tagging_enabled = false
@@ -55,8 +69,6 @@ RSpec.describe TagGuardian do
end
end
- ###### TAGGING ######
-
describe "#can_tag_topics?" do
it "returns false when tagging is disabled" do
SiteSetting.tagging_enabled = false
@@ -103,8 +115,6 @@ RSpec.describe TagGuardian do
end
end
- ###### ADMIN ######
-
describe "#can_admin_tags?" do
it "returns false when tagging is disabled" do
SiteSetting.tagging_enabled = false
diff --git a/spec/models/permalink_spec.rb b/spec/models/permalink_spec.rb
index 848c0d35c8c..3ec86c73f97 100644
--- a/spec/models/permalink_spec.rb
+++ b/spec/models/permalink_spec.rb
@@ -257,4 +257,42 @@ RSpec.describe Permalink do
expect(permalink.target_url).to be_nil
end
end
+
+ describe "#internal? and #external?" do
+ it "returns internal for topic permalinks" do
+ permalink = Fabricate.build(:permalink, topic:)
+ expect(permalink.internal?).to eq(true)
+ expect(permalink.external?).to eq(false)
+ end
+
+ it "returns internal for post permalinks" do
+ permalink = Fabricate.build(:permalink, post:)
+ expect(permalink.internal?).to eq(true)
+ expect(permalink.external?).to eq(false)
+ end
+
+ it "returns internal for category permalinks" do
+ permalink = Fabricate.build(:permalink, category:)
+ expect(permalink.internal?).to eq(true)
+ expect(permalink.external?).to eq(false)
+ end
+
+ it "returns internal for tag permalinks" do
+ permalink = Fabricate.build(:permalink, tag:)
+ expect(permalink.internal?).to eq(true)
+ expect(permalink.external?).to eq(false)
+ end
+
+ it "returns internal for user permalinks" do
+ permalink = Fabricate.build(:permalink, user:)
+ expect(permalink.internal?).to eq(true)
+ expect(permalink.external?).to eq(false)
+ end
+
+ it "returns external for external URL permalinks" do
+ permalink = Fabricate.build(:permalink, external_url: "https://example.com")
+ expect(permalink.internal?).to eq(false)
+ expect(permalink.external?).to eq(true)
+ end
+ end
end
diff --git a/spec/requests/permalinks_controller_spec.rb b/spec/requests/permalinks_controller_spec.rb
index 367c209b417..de3d9b1b5fb 100644
--- a/spec/requests/permalinks_controller_spec.rb
+++ b/spec/requests/permalinks_controller_spec.rb
@@ -2,7 +2,17 @@
RSpec.describe PermalinksController do
fab!(:topic)
- fab!(:permalink) { Fabricate(:permalink, url: "deadroute/topic/546", topic_id: topic.id) }
+ fab!(:permalink) { Fabricate(:permalink, url: "deadroute/topic/546", topic:) }
+
+ fab!(:group)
+ fab!(:private_category) { Fabricate(:private_category, group:) }
+ fab!(:private_topic) do
+ Fabricate(:topic, category: private_category, title: "This is a secret title")
+ end
+
+ fab!(:group_member) { Fabricate(:user, groups: [group]) }
+ fab!(:non_member, :user)
+ fab!(:admin)
describe "show" do
it "should redirect to a permalink's target_url with status 301" do
@@ -50,5 +60,109 @@ RSpec.describe PermalinksController do
expect(response).to redirect_to(permalink.external_url)
end
end
+
+ context "when permalink points to a restricted topic" do
+ it "returns 404 for anonymous users" do
+ permalink = Fabricate(:permalink, url: "secret", topic: private_topic)
+
+ get "/#{permalink.url}"
+
+ expect(response.status).to eq(404)
+ expect(response.headers["Location"]).to be_nil
+ end
+
+ it "returns 404 for logged-in users without access" do
+ sign_in(non_member)
+ permalink = Fabricate(:permalink, url: "secret-logged-in", topic: private_topic)
+
+ get "/#{permalink.url}"
+
+ expect(response.status).to eq(404)
+ expect(response.headers["Location"]).to be_nil
+ end
+
+ it "redirects users who have access to the topic" do
+ sign_in(group_member)
+ permalink = Fabricate(:permalink, url: "secret-member", topic: private_topic)
+
+ get "/#{permalink.url}"
+
+ expect(response.status).to eq(301)
+ expect(response).to redirect_to(private_topic.relative_url)
+ end
+
+ it "redirects admins to the topic" do
+ sign_in(admin)
+ permalink = Fabricate(:permalink, url: "secret-admin", topic: private_topic)
+
+ get "/#{permalink.url}"
+
+ expect(response.status).to eq(301)
+ expect(response).to redirect_to(private_topic.relative_url)
+ end
+ end
+
+ context "when permalink points to a restricted category" do
+ it "returns 404 for anonymous users" do
+ permalink = Fabricate(:permalink, url: "hidden", category: private_category)
+
+ get "/#{permalink.url}"
+
+ expect(response.status).to eq(404)
+ expect(response.headers["Location"]).to be_nil
+ end
+ end
+
+ context "when permalink points to a post in a restricted topic" do
+ it "returns 404 for anonymous users" do
+ private_post = Fabricate(:post, topic: private_topic)
+ permalink = Fabricate(:permalink, url: "secret-post", post: private_post)
+
+ get "/#{permalink.url}"
+
+ expect(response.status).to eq(404)
+ expect(response.headers["Location"]).to be_nil
+ end
+ end
+
+ context "when permalink points to a hidden tag" do
+ it "returns 404 for anonymous users" do
+ tag = Fabricate(:tag, name: "secret-internal-tag")
+ Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [tag.name])
+
+ permalink = Fabricate(:permalink, url: "internal-tag", tag:)
+
+ get "/#{permalink.url}"
+
+ expect(response.status).to eq(404)
+ expect(response.headers["Location"]).to be_nil
+ end
+ end
+ end
+
+ describe "check" do
+ context "when permalink points to a restricted topic" do
+ it "reports not found for anonymous users" do
+ permalink = Fabricate(:permalink, url: "secret-check", topic: private_topic)
+
+ get "/permalink-check.json", params: { path: permalink.url }
+
+ json = response.parsed_body
+ expect(json["found"]).to eq(false)
+ expect(json["target_url"]).to be_nil
+ end
+ end
+
+ context "when permalink points to a restricted category" do
+ it "reports not found for anonymous users" do
+ permalink = Fabricate(:permalink, url: "hidden-check", category: private_category)
+
+ get "/permalink-check.json", params: { path: permalink.url }
+
+ json = response.parsed_body
+ expect(json["found"]).to eq(false)
+ expect(json["target_url"]).to be_nil
+ end
+ end
end
end