discourse/lib/secure_upload_endpoint_helpers.rb
Alan Guo Xiang Tan eeaca70bc1
FIX: Enforce secure-upload ACL in AI bot prompt path (#39903)
The AI bot reads upload contents from posts and chat messages and feeds
them into the LLM prompt. The lookup is gated by whether the requester
can see the post, but not whether they can see the upload's secure
access-control post, so an attacker can paste another user's secure
short URL into their own post, summon the bot, and have it disclose the
contents. The agent tool runner's `_upload_get_base64` has the same gap
with no ACL check at all.

This commit introduces `Guardian#can_see_upload?` so upload visibility
is checked in one place, and uses it from `PromptMessagesBuilder`,
`ToolRunner::Upload`, and
`SecureUploadEndpointHelpers#check_secure_upload_permission`.

Follow-up to fa54f62348.
2026-05-13 09:55:32 +08:00

25 lines
797 B
Ruby
Vendored

# frozen_string_literal: true
module SecureUploadEndpointHelpers
include ActiveSupport::Concern
def upload_from_path_and_extension(path_with_ext)
sha1 = File.basename(path_with_ext, File.extname(path_with_ext))
# this takes care of optimized image requests
sha1 = sha1.partition("_").first if sha1.include?("_")
Upload.find_by(sha1: sha1)
end
def upload_from_full_url(url)
Upload.find_by(sha1: Upload.sha1_from_long_url(url))
end
def check_secure_upload_permission(upload)
if upload.access_control_post_id.present?
raise Discourse::InvalidAccess if current_user.nil? && SiteSetting.login_required
raise Discourse::InvalidAccess if !guardian.can_see_upload?(upload)
else
raise Discourse::NotFound if current_user.nil?
end
end
end