A platform for community discussion. Free, open, simple.
SECURITY: Authorize secure-upload hotlink downloads against post user
Alan Guo Xiang Tanfa74e0dec734
diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rbindex 79e61527ee4..881f485a924 100644--- a/app/jobs/regular/pull_hotlinked_images.rb+++ b/app/jobs/regular/pull_hotlinked_images.rb@@ -171,7 +171,15 @@ module Jobsif Discourse.store.has_been_uploaded?(src) || normalize_src(src).start_with?(*local_bases) ||src =~ %r{\A/[^/]}i- return false if !(src =~ %r{/uploads/} || Upload.secure_uploads_url?(src))+ if Upload.secure_uploads_url?(src)+ upload = Upload.fetch_from(sha1: Upload.sha1_from_long_url(src), url: src)+ return false if upload.nil?+ return false if !can_see_upload?(upload, guardian: post&.user&.guardian)+ elsif src.match?(%r{/uploads/})+ upload = Upload.get_from_url(src)+ else+ return false+ end# Someone could hotlink a file from a different site on the same CDN,# so check whether we have it in this database@@ -181,8 +189,7 @@ module Jobs# media was enabled, then we definitely want to redownload again otherwise# we end up reusing existing uploads which may be linked to many posts# already.- upload = Upload.consider_for_reuse(Upload.get_from_url(src), post)-+ upload = Upload.consider_for_reuse(upload, post)return !upload.present?end@@ -212,6 +219,15 @@ module Jobsprotected+ def can_see_upload?(upload, guardian:)+ return false if guardian.blank?++ access_control_post = upload.access_control_post+ return false if access_control_post.blank?++ guardian.can_see_post?(access_control_post)+ end+def replace_encoded_src(src)PostHotlinkedMedia.normalize_src(src, reset_scheme: false)enddiff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rbindex cb98b248d8a..d0b81bec879 100644--- a/spec/jobs/pull_hotlinked_images_spec.rb+++ b/spec/jobs/pull_hotlinked_images_spec.rb@@ -296,19 +296,31 @@ RSpec.describe Jobs::PullHotlinkedImages doendend- context "when the upload access_control_post is different to the current post" do- it "redownloads the upload" do- setup_s3- SiteSetting.secure_uploads = true+ context "when the post hotlinks a secure upload owned by another post" do+ fab!(:private_group, :group)+ fab!(:private_category) { Fabricate(:private_category, group: private_group) }+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }+ fab!(:private_post) { Fabricate(:post, topic: private_topic) }++ fab!(:upload) do+ enable_secure_uploads+ Fabricate(:secure_upload_s3, secure: true)+ end- upload = Fabricate(:secure_upload_s3, secure: true)+ fab!(:post) do+ secure_url = Discourse.base_url + Upload.secure_uploads_url_from_upload_url(upload.url)+ create_post(user: user, raw: "<img src='#{secure_url}'>")+ end++ before do+ enable_secure_uploadsstub_s3(upload)Upload.stubs(:signed_url_from_secure_uploads_url).returns(upload.url)- url = Upload.secure_uploads_url_from_upload_url(upload.url)- url = Discourse.base_url + url- post = Fabricate(:post, user: user, raw: "<img src='#{url}'>")- upload.update(access_control_post: Fabricate(:post))FileStore::S3Store.any_instance.stubs(:store_upload).returns(upload.url)+ end++ it "redownloads when the post user can see the owning post" do+ upload.update(access_control_post: Fabricate(:post))expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {Upload.count@@ -318,6 +330,40 @@ RSpec.describe Jobs::PullHotlinkedImages doUpload.count}end++ it "does not redownload when the post user cannot see the owning post" do+ upload.update!(access_control_post: private_post)++ expect(user.guardian.can_see_post?(private_post)).to eq(false)++ expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.not_to change {+ Upload.count+ }+ expect(post.reload.raw).to include(Upload.secure_uploads_url_from_upload_url(upload.url))+ expect(post.post_hotlinked_media).to be_empty+ end++ it "does not redownload when the post user cannot see the owning post and the URL points at the optimized variant" do+ upload.update!(access_control_post: private_post)++ optimized_secure_url =+ "#{Discourse.base_url}#{Upload.secure_uploads_url_from_upload_url(upload.url).gsub("/original/", "/optimized/")}"+ bypass_post = create_post(user: user, raw: "<img src='#{optimized_secure_url}'>")++ expect { Jobs::PullHotlinkedImages.new.execute(post_id: bypass_post.id) }.not_to change {+ Upload.count+ }+ end++ it "does not redownload when the secure URL does not match any known upload" do+ unresolved_url =+ "#{Discourse.base_url}/secure-uploads/original/1X/#{SecureRandom.hex(20)}.png"+ unresolved_post = create_post(user: user, raw: "<img src='#{unresolved_url}'>")++ expect {+ Jobs::PullHotlinkedImages.new.execute(post_id: unresolved_post.id)+ }.not_to change { Upload.count }+ endendend