Skip to content

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

SECURITY: Existing category uploads remain publicly accessible after category permissions are restricted

David Taylor98f1c021d0e9
diff --git a/app/jobs/onceoff/backfill_category_upload_security.rb b/app/jobs/onceoff/backfill_category_upload_security.rb
new file mode 100644
index 00000000000..a034f0720d1
--- /dev/null
+++ b/app/jobs/onceoff/backfill_category_upload_security.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+module Jobs
+ class BackfillCategoryUploadSecurity < ::Jobs::Onceoff
+ def execute_onceoff(_args)
+ return if !SiteSetting.secure_uploads? || SiteSetting.secure_uploads_pm_only?
+
+ topics_with_insecure_uploads.find_each do |topic|
+ Jobs.enqueue(:update_topic_upload_security, topic_id: topic.id)
+ end
+ end
+
+ private
+
+ def topics_with_insecure_uploads
+ insecure_uploads = Upload.where(secure: false)
+ owned_topic_ids =
+ Post
+ .with_deleted
+ .where(id: insecure_uploads.select(:access_control_post_id))
+ .select(:topic_id)
+ unowned_post_ids =
+ UploadReference.where(
+ target_type: "Post",
+ upload: insecure_uploads.where(access_control_post_id: nil),
+ ).select(:target_id)
+ unowned_topic_ids = Post.with_deleted.where(id: unowned_post_ids).select(:topic_id)
+ private_topics =
+ Topic.with_deleted.joins(:category).where(categories: { read_restricted: true })
+
+ private_topics.where(id: owned_topic_ids).or(private_topics.where(id: unowned_topic_ids))
+ end
+ end
+end
diff --git a/app/jobs/regular/update_category_upload_security.rb b/app/jobs/regular/update_category_upload_security.rb
new file mode 100644
index 00000000000..311a2851630
--- /dev/null
+++ b/app/jobs/regular/update_category_upload_security.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+module Jobs
+ class UpdateCategoryUploadSecurity < ::Jobs::Base
+ def execute(args)
+ category = Category.find_by(id: args[:category_id])
+ return if category.blank?
+
+ category
+ .topics
+ .with_deleted
+ .select(:id)
+ .find_each { |topic| Jobs.enqueue(:update_topic_upload_security, topic_id: topic.id) }
+ end
+ end
+end
diff --git a/app/jobs/regular/update_topic_upload_security.rb b/app/jobs/regular/update_topic_upload_security.rb
index dd2eceffe56..f7cbdd147f0 100644
--- a/app/jobs/regular/update_topic_upload_security.rb
+++ b/app/jobs/regular/update_topic_upload_security.rb
@@ -3,7 +3,7 @@
module Jobs
class UpdateTopicUploadSecurity < ::Jobs::Base
def execute(args)
- topic = Topic.find_by(id: args[:topic_id])
+ topic = Topic.with_deleted.find_by(id: args[:topic_id])
if topic.blank?
Rails.logger.info(
"Could not find topic #{args[:topic_id]} for topic upload security updater.",
diff --git a/app/models/category.rb b/app/models/category.rb
index 56759e4da31..3c4e76d4515 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -183,6 +183,7 @@ class Category < ActiveRecord::Base
end
end
+ after_commit :enqueue_upload_security_updates, on: :update, if: :saved_change_to_read_restricted?
after_commit :trigger_category_created_event, on: :create
after_commit :trigger_category_updated_event, on: :update
after_commit :trigger_category_destroyed_event, on: :destroy
@@ -1298,6 +1299,10 @@ class Category < ActiveRecord::Base
saved_change_to_slug? || saved_change_to_parent_category_id?
end
+ def enqueue_upload_security_updates
+ Jobs.enqueue(:update_category_upload_security, category_id: id)
+ end
+
def enqueue_category_hashtag_remap
old_slug = saved_change_to_slug? ? slug_before_last_save : slug
old_parent_category_id =
diff --git a/lib/topic_upload_security_manager.rb b/lib/topic_upload_security_manager.rb
index 1b0be86998a..f2bdb3353e1 100644
--- a/lib/topic_upload_security_manager.rb
+++ b/lib/topic_upload_security_manager.rb
@@ -28,14 +28,15 @@ class TopicUploadSecurityManager
Rails.logger.debug("Updating upload security in topic #{@topic.id} - post #{post.id}")
post.topic = @topic
- secure_status_did_change =
- post.owned_uploads_via_access_control.any? do |upload|
- # We already have the post preloaded so we may as well
- # attach it here to avoid another load in UploadSecurity
- # (which is called via update_secure_status)
- upload.access_control_post = post
- upload.update_secure_status(source: "topic upload security")
- end
+ secure_status_did_change = false
+ post.owned_uploads_via_access_control.each do |upload|
+ # We already have the post preloaded so we may as well
+ # attach it here to avoid another load in UploadSecurity
+ # (which is called via update_secure_status)
+ upload.access_control_post = post
+ upload_did_change = upload.update_secure_status(source: "topic upload security")
+ secure_status_did_change = upload_did_change || secure_status_did_change
+ end
if secure_status_did_change
post.rebake!
@@ -69,17 +70,15 @@ class TopicUploadSecurityManager
)
post.topic = @topic
- secure_status_did_change =
- post.uploads.any? do |upload|
- first_post_upload_appeared_in =
- upload.upload_references.where(target_type: "Post").first.target
- if first_post_upload_appeared_in == post
- upload.update(access_control_post: post)
- upload.update_secure_status(source: "topic upload security")
- else
- false
- end
+ secure_status_did_change = false
+ post.uploads.each do |upload|
+ first_post_upload_reference = upload.upload_references.where(target_type: "Post").first
+ if first_post_upload_reference.target_id == post.id
+ upload.update(access_control_post: post)
+ upload_did_change = upload.update_secure_status(source: "topic upload security")
+ secure_status_did_change = upload_did_change || secure_status_did_change
end
+ end
if secure_status_did_change
post.rebake!
@@ -98,17 +97,20 @@ class TopicUploadSecurityManager
private
def posts_owning_uploads
- Post.where(topic_id: @topic.id, id: Upload.select(:access_control_post_id))
+ Post.with_deleted.where(topic_id: @topic.id, id: Upload.select(:access_control_post_id))
end
def posts_with_unowned_uploads
- Post.where(
- topic_id: @topic.id,
- id:
- UploadReference.where(
- target_type: "Post",
- upload: Upload.where(access_control_post_id: nil),
- ).select(:target_id),
- ).includes(:uploads)
+ Post
+ .with_deleted
+ .where(
+ topic_id: @topic.id,
+ id:
+ UploadReference.where(
+ target_type: "Post",
+ upload: Upload.where(access_control_post_id: nil),
+ ).select(:target_id),
+ )
+ .includes(:uploads)
end
end
diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rb
index 0a969968295..6b286f035b7 100644
--- a/script/bulk_import/generic_bulk.rb
+++ b/script/bulk_import/generic_bulk.rb
@@ -1329,6 +1329,7 @@ class BulkImport::Generic < BulkImport::Base
if current_read_restricted != expected_read_restricted
category.update_column(:read_restricted, expected_read_restricted)
+ Jobs.enqueue(:update_category_upload_security, category_id: category.id)
updated_count += 1
else
skipped_count += 1
diff --git a/spec/jobs/onceoff/backfill_category_upload_security_spec.rb b/spec/jobs/onceoff/backfill_category_upload_security_spec.rb
new file mode 100644
index 00000000000..3472000204e
--- /dev/null
+++ b/spec/jobs/onceoff/backfill_category_upload_security_spec.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+RSpec.describe Jobs::BackfillCategoryUploadSecurity do
+ it "secures stale uploads only in read-restricted categories" do
+ setup_s3
+ SiteSetting.secure_uploads = true
+ Jobs.run_immediately!
+
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ owned_topic = Fabricate(:topic, category: private_category)
+ owned_post = Fabricate(:post, topic: owned_topic)
+ owned_upload = Fabricate(:upload_s3, access_control_post: owned_post)
+ UploadReference.create!(upload: owned_upload, target: owned_post)
+ owned_post.trash!
+ owned_topic.trash!
+
+ unowned_post = Fabricate(:post, topic: Fabricate(:topic, category: private_category))
+ unowned_upload = Fabricate(:upload_s3)
+ UploadReference.create!(upload: unowned_upload, target: unowned_post)
+ unowned_post.trash!
+
+ public_post = Fabricate(:post, topic: Fabricate(:topic, category: Fabricate(:category)))
+ public_upload = Fabricate(:upload_s3, access_control_post: public_post)
+ UploadReference.create!(upload: public_upload, target: public_post)
+
+ stub_upload(owned_upload)
+ stub_upload(unowned_upload)
+
+ described_class.new.execute_onceoff(nil)
+
+ expect(
+ [owned_upload.reload.secure, unowned_upload.reload.secure, public_upload.reload.secure],
+ ).to eq([true, true, false])
+ end
+end
diff --git a/spec/jobs/regular/update_category_upload_security_spec.rb b/spec/jobs/regular/update_category_upload_security_spec.rb
new file mode 100644
index 00000000000..8bfc61b9dda
--- /dev/null
+++ b/spec/jobs/regular/update_category_upload_security_spec.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+RSpec.describe Jobs::UpdateCategoryUploadSecurity do
+ it "updates upload security for soft-deleted topics and posts in the category" do
+ setup_s3
+ SiteSetting.secure_uploads = true
+ Jobs.run_immediately!
+ category = Fabricate(:private_category, group: Fabricate(:group))
+ topic = Fabricate(:topic, category: category)
+ post = Fabricate(:post, topic: topic)
+ upload = Fabricate(:upload_s3, access_control_post: post)
+ UploadReference.create!(upload: upload, target: post)
+ post.trash!
+ topic.trash!
+ stub_upload(upload)
+
+ described_class.new.execute(category_id: category.id)
+
+ expect(upload.reload.secure).to eq(true)
+ end
+end
diff --git a/spec/lib/topic_upload_security_manager_spec.rb b/spec/lib/topic_upload_security_manager_spec.rb
index 8d1a580aa74..7d8ef39cadc 100644
--- a/spec/lib/topic_upload_security_manager_spec.rb
+++ b/spec/lib/topic_upload_security_manager_spec.rb
@@ -39,6 +39,20 @@ RSpec.describe TopicUploadSecurityManager do
expect_upload_status_not_to_change
end
+ it "updates every stale upload owned by the same post" do
+ uploads =
+ 2.times.map do
+ upload = Fabricate(:upload_s3, access_control_post: post1)
+ UploadReference.create!(upload: upload, target: post1)
+ stub_upload(upload)
+ upload
+ end
+
+ manager.run
+
+ expect(uploads.map { |upload| upload.reload.secure? }).to eq([true, true])
+ end
+
context "when changing the topic to a non-private category" do
before { topic.update(category: Fabricate(:category)) }
@@ -134,6 +148,20 @@ RSpec.describe TopicUploadSecurityManager do
expect(upload3.reload.access_control_post).to eq(post4)
end
+ it "updates every unowned upload first referenced by the same post" do
+ another_upload = Fabricate(:upload_s3)
+ UploadReference.create!(upload: another_upload, target: post4)
+ stub_upload(another_upload)
+
+ manager.run
+
+ expect(
+ [upload3, another_upload].map do |upload|
+ [upload.reload.secure?, upload.access_control_post]
+ end,
+ ).to eq([[true, post4], [true, post4]])
+ end
+
context "when secure uploads is not enabled" do
before { SiteSetting.secure_uploads = false }
diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb
index ec4f73608f8..a090e4c582d 100644
--- a/spec/models/category_spec.rb
+++ b/spec/models/category_spec.rb
@@ -1723,6 +1723,34 @@ RSpec.describe Category do
end
end
+ describe "upload security updates" do
+ it "enqueues an update whenever read restrictions change" do
+ category = Fabricate(:category)
+
+ expect_enqueued_with(
+ job: :update_category_upload_security,
+ args: {
+ category_id: category.id,
+ },
+ ) { category.update!(permissions: { admins: :full }) }
+
+ expect_enqueued_with(
+ job: :update_category_upload_security,
+ args: {
+ category_id: category.id,
+ },
+ ) { category.update!(permissions: { everyone: :full }) }
+ end
+
+ it "does not enqueue an update when read restrictions stay unchanged" do
+ category = Fabricate(:category)
+
+ expect_not_enqueued_with(job: :update_category_upload_security) do
+ category.update!(permissions: { everyone: :readonly })
+ end
+ end
+ end
+
describe "category hashtag remapping" do
it "enqueues a remap job when the slug changes" do
category = Fabricate(:category, slug: "support")
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index bddd4a16190..4ee36ad0b4a 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -1066,6 +1066,76 @@ RSpec.describe CategoriesController do
expect(category.topic_title_placeholder).to eq("test topic title placeholder")
end
+ it "revokes anonymous access to existing uploads when a public category becomes private" do
+ setup_s3
+ SiteSetting.secure_uploads = true
+ topic = Fabricate(:topic, category: category)
+ post = Fabricate(:post, topic: topic)
+ upload = Fabricate(:upload_s3, access_control_post: post)
+ UploadReference.create!(upload: upload, target: post)
+ stub_upload(upload)
+
+ delete "/session/#{admin.username}.json"
+ get upload.short_path
+
+ expect(response).to redirect_to(upload.url)
+
+ sign_in(admin)
+ put "/categories/#{category.id}.json",
+ params: {
+ permissions: {
+ "admins" => CategoryGroup.permission_types[:full],
+ },
+ }
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body["category"]["read_restricted"]).to eq(true)
+
+ delete "/session/#{admin.username}.json"
+ get upload.short_path
+
+ expect(response).to have_http_status(:forbidden)
+ expect(response.body).to include(I18n.t("page_forbidden.title"))
+ end
+
+ it "revokes anonymous access to uploads retained in a deleted topic" do
+ setup_s3
+ SiteSetting.secure_uploads = true
+ topic = Fabricate(:topic, category: category)
+ post = Fabricate(:post, topic: topic)
+ upload = Fabricate(:upload_s3, access_control_post: post)
+ UploadReference.create!(upload: upload, target: post)
+ stub_upload(upload)
+
+ delete "/session/#{admin.username}.json"
+ get upload.short_path
+
+ expect(response).to redirect_to(upload.url)
+
+ sign_in(admin)
+ delete "/t/#{topic.id}.json"
+
+ expect(response).to have_http_status(:ok)
+ expect(Topic.with_deleted.find(topic.id)).to be_trashed
+ expect(Post.with_deleted.find(post.id)).to be_trashed
+
+ put "/categories/#{category.id}.json",
+ params: {
+ permissions: {
+ "admins" => CategoryGroup.permission_types[:full],
+ },
+ }
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body["category"]["read_restricted"]).to eq(true)
+
+ delete "/session/#{admin.username}.json"
+ get upload.short_path
+
+ expect(response).to have_http_status(:forbidden)
+ expect(response.body).to include(I18n.t("page_forbidden.title"))
+ end
+
it "updates description and revises category topic OP to stay in sync" do
cat = Fabricate(:category_with_definition, user: admin)
raw_description = "New **markdown** description here"
diff --git a/spec/script/bulk_import/generic_bulk_spec.rb b/spec/script/bulk_import/generic_bulk_spec.rb
index f84383a2f11..226f0da3cd1 100644
--- a/spec/script/bulk_import/generic_bulk_spec.rb
+++ b/spec/script/bulk_import/generic_bulk_spec.rb
@@ -1587,6 +1587,26 @@ if generic_import_dependencies_available
end
end
+ describe "#update_category_read_restricted" do
+ it "reconciles uploads when imported permissions restrict an existing category" do
+ category = Fabricate(:category)
+ CategoryGroup.create!(
+ category: category,
+ group: Group[:admins],
+ permission_type: CategoryGroup.permission_types[:full],
+ )
+
+ expect_enqueued_with(
+ job: :update_category_upload_security,
+ args: {
+ category_id: category.id,
+ },
+ ) { described_class.allocate.update_category_read_restricted }
+
+ expect(category.reload.read_restricted).to eq(true)
+ end
+ end
+
describe "#import_user_associated_groups" do
fab!(:employee, :user)
fab!(:customer, :user)