2020-01-15 22:50:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
##
|
|
|
|
# A note on determining whether an upload should be marked as secure:
|
|
|
|
#
|
|
|
|
# Some of these flags checked (e.g. all of the for_X flags and the opts[:type])
|
|
|
|
# are only set when _initially uploading_ via UploadCreator and are not present
|
2021-01-28 18:03:44 -05:00
|
|
|
# when an upload already exists, these will only be checked when the @creating
|
|
|
|
# option is present.
|
2020-01-15 22:50:27 -05:00
|
|
|
#
|
|
|
|
# If the upload already exists the best way to figure out whether it should be
|
|
|
|
# secure alongside the site settings is the access_control_post_id, because the
|
|
|
|
# original post the upload is linked to has far more bearing on its security context
|
|
|
|
# post-upload. If the access_control_post_id does not exist then we just rely
|
|
|
|
# on the current secure? status, otherwise there would be a lot of additional
|
2023-01-19 19:24:52 -05:00
|
|
|
# complex queries and joins to perform.
|
|
|
|
#
|
|
|
|
# These queries will be performed only if the @creating option is false. So if
|
|
|
|
# an upload is included in a post, and it's an upload from a different source
|
|
|
|
# (e.g. a category logo, site setting upload) then we will determine secure
|
|
|
|
# state _based on the first place the upload was referenced_.
|
2022-05-22 23:14:11 -04:00
|
|
|
#
|
|
|
|
# NOTE: When updating this to add more cases where uploads will be marked
|
|
|
|
# secure, consider uploads:secure_upload_analyse_and_update as well, which
|
|
|
|
# does not use this class directly but uses an SQL version of its rules for
|
|
|
|
# efficient updating of many uploads in bulk.
|
2020-01-15 22:50:27 -05:00
|
|
|
class UploadSecurity
|
2020-09-17 21:54:33 -04:00
|
|
|
@@custom_public_types = []
|
|
|
|
|
2020-08-24 03:12:28 -04:00
|
|
|
PUBLIC_TYPES = %w[
|
2020-09-17 21:54:33 -04:00
|
|
|
avatar
|
|
|
|
custom_emoji
|
|
|
|
profile_background
|
|
|
|
card_background
|
|
|
|
category_logo
|
2022-10-07 11:00:44 -04:00
|
|
|
category_logo_dark
|
2020-09-17 21:54:33 -04:00
|
|
|
category_background
|
2021-02-15 21:34:03 -05:00
|
|
|
group_flair
|
2021-05-27 22:35:52 -04:00
|
|
|
badge_image
|
2020-08-24 03:12:28 -04:00
|
|
|
]
|
|
|
|
|
2023-01-19 19:24:52 -05:00
|
|
|
PUBLIC_UPLOAD_REFERENCE_TYPES = %w[
|
|
|
|
Badge
|
|
|
|
Category
|
|
|
|
CustomEmoji
|
|
|
|
Group
|
|
|
|
SiteSetting
|
|
|
|
ThemeField
|
|
|
|
User
|
|
|
|
UserAvatar
|
|
|
|
UserProfile
|
|
|
|
]
|
|
|
|
|
2020-09-17 21:54:33 -04:00
|
|
|
def self.register_custom_public_type(type)
|
|
|
|
@@custom_public_types << type if !@@custom_public_types.include?(type)
|
|
|
|
end
|
|
|
|
|
2020-09-18 14:35:36 -04:00
|
|
|
# used in tests
|
|
|
|
def self.reset_custom_public_types
|
|
|
|
@@custom_public_types = []
|
|
|
|
end
|
|
|
|
|
2020-01-15 22:50:27 -05:00
|
|
|
def initialize(upload, opts = {})
|
|
|
|
@upload = upload
|
|
|
|
@opts = opts
|
2020-01-16 22:16:27 -05:00
|
|
|
@upload_type = @opts[:type]
|
2021-01-28 18:03:44 -05:00
|
|
|
@creating = @opts[:creating]
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def should_be_secure?
|
2021-01-28 18:03:44 -05:00
|
|
|
should_be_secure_with_reason.first
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2021-01-28 18:03:44 -05:00
|
|
|
def should_be_secure_with_reason
|
|
|
|
insecure_context_checks.each { |check, reason| return false, reason if perform_check(check) }
|
|
|
|
secure_context_checks.each do |check, reason|
|
|
|
|
return perform_check(check), reason if priority_check?(check)
|
|
|
|
return true, reason if perform_check(check)
|
|
|
|
end
|
|
|
|
|
|
|
|
[false, "no checks satisfied"]
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2023-01-19 19:24:52 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def access_control_post
|
|
|
|
@access_control_post ||=
|
|
|
|
@upload.access_control_post_id.present? ? @upload.access_control_post : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def insecure_context_checks
|
|
|
|
{
|
|
|
|
secure_uploads_disabled: "secure uploads is disabled",
|
|
|
|
insecure_creation_for_modifiers: "one or more creation for_modifiers was satisfied",
|
|
|
|
public_type: "upload is public type",
|
|
|
|
regular_emoji: "upload is used for regular emoji",
|
|
|
|
publicly_referenced_first: "upload was publicly referenced when it was first created",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def secure_context_checks
|
|
|
|
{
|
|
|
|
login_required: "login is required",
|
|
|
|
access_control_post_has_secure_uploads: "access control post dictates security",
|
|
|
|
secure_creation_for_modifiers: "one or more creation for_modifiers was satisfied",
|
|
|
|
uploading_in_composer: "uploading via the composer",
|
|
|
|
already_secure: "upload is already secure",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# The access control check is important because that is the truest indicator
|
|
|
|
# of whether an upload should be secure or not, and thus should be returned
|
|
|
|
# immediately if there is an access control post.
|
|
|
|
def priority_check?(check)
|
|
|
|
check == :access_control_post_has_secure_uploads && access_control_post
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_check(check)
|
|
|
|
send("#{check}_check")
|
|
|
|
end
|
|
|
|
|
|
|
|
#### START PUBLIC CHECKS ####
|
|
|
|
|
2022-09-28 19:24:33 -04:00
|
|
|
def secure_uploads_disabled_check
|
|
|
|
!SiteSetting.secure_uploads?
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2021-01-28 18:03:44 -05:00
|
|
|
def insecure_creation_for_modifiers_check
|
|
|
|
return false if !@creating
|
|
|
|
@upload.for_theme || @upload.for_site_setting || @upload.for_gravatar
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_type_check
|
|
|
|
PUBLIC_TYPES.include?(@upload_type) || @@custom_public_types.include?(@upload_type)
|
|
|
|
end
|
|
|
|
|
2023-01-19 19:24:52 -05:00
|
|
|
def publicly_referenced_first_check
|
|
|
|
return false if @creating
|
2023-01-23 19:01:48 -05:00
|
|
|
first_reference =
|
|
|
|
@upload
|
|
|
|
.upload_references
|
|
|
|
.joins(<<~SQL)
|
|
|
|
LEFT JOIN posts ON upload_references.target_type = 'Post' AND upload_references.target_id = posts.id
|
|
|
|
SQL
|
|
|
|
.where("posts.deleted_at IS NULL")
|
2023-03-08 20:52:26 -05:00
|
|
|
.order("upload_references.created_at ASC, upload_references.id ASC")
|
2023-01-23 19:01:48 -05:00
|
|
|
.first
|
2023-01-19 19:24:52 -05:00
|
|
|
return false if first_reference.blank?
|
|
|
|
PUBLIC_UPLOAD_REFERENCE_TYPES.include?(first_reference.target_type)
|
2021-01-28 18:03:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def regular_emoji_check
|
|
|
|
return false if @upload.origin.blank?
|
|
|
|
uri = URI.parse(@upload.origin)
|
|
|
|
return true if Emoji.all.map(&:url).include?("#{uri.path}?#{uri.query}")
|
|
|
|
uri.path.include?("images/emoji")
|
|
|
|
end
|
|
|
|
|
2023-01-19 19:24:52 -05:00
|
|
|
#### END PUBLIC CHECKS ####
|
|
|
|
|
|
|
|
#--------------------------#
|
|
|
|
|
|
|
|
#### START PRIVATE CHECKS ####
|
|
|
|
|
2021-01-28 18:03:44 -05:00
|
|
|
def login_required_check
|
2023-09-05 19:39:09 -04:00
|
|
|
SiteSetting.login_required? && !SiteSetting.secure_uploads_pm_only?
|
2021-01-28 18:03:44 -05:00
|
|
|
end
|
2020-09-17 21:54:33 -04:00
|
|
|
|
2023-01-19 19:24:52 -05:00
|
|
|
# Whether the upload should remain secure or not after posting depends on its context,
|
2020-01-15 22:50:27 -05:00
|
|
|
# which is based on the post it is linked to via access_control_post_id.
|
|
|
|
#
|
2023-01-19 19:24:52 -05:00
|
|
|
# If that post is with_secure_uploads? then the upload should also be secure.
|
|
|
|
#
|
|
|
|
# This may change to false if the upload was set to secure on upload e.g. in
|
|
|
|
# a post composer then it turned out that the post itself was not in a secure context.
|
|
|
|
#
|
|
|
|
# A post is with secure uploads if it is a private message or in a read restricted
|
|
|
|
# category. See `Post#with_secure_uploads?` for the full definition.
|
2022-09-28 19:24:33 -04:00
|
|
|
def access_control_post_has_secure_uploads_check
|
|
|
|
access_control_post&.with_secure_uploads?
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2021-01-28 18:03:44 -05:00
|
|
|
def uploading_in_composer_check
|
|
|
|
@upload_type == "composer"
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2021-01-28 18:03:44 -05:00
|
|
|
def secure_creation_for_modifiers_check
|
|
|
|
return false if !@creating
|
|
|
|
@upload.for_private_message || @upload.for_group_message
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
2020-02-13 20:17:09 -05:00
|
|
|
|
2021-01-28 18:03:44 -05:00
|
|
|
def already_secure_check
|
|
|
|
@upload.secure?
|
2020-02-16 21:30:47 -05:00
|
|
|
end
|
|
|
|
|
2023-01-19 19:24:52 -05:00
|
|
|
#### END PRIVATE CHECKS ####
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|