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
|
|
|
|
# when an upload already exists.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# complex queries and joins to perform.
|
|
|
|
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
|
|
|
|
category_background
|
2020-08-24 03:12:28 -04:00
|
|
|
]
|
|
|
|
|
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]
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def should_be_secure?
|
2020-02-20 18:35:16 -05:00
|
|
|
return false if !SiteSetting.secure_media?
|
2020-01-15 22:50:27 -05:00
|
|
|
return false if uploading_in_public_context?
|
2020-03-25 17:16:02 -04:00
|
|
|
uploading_in_secure_context?
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def uploading_in_public_context?
|
2020-08-24 03:12:28 -04:00
|
|
|
@upload.for_theme ||
|
|
|
|
@upload.for_site_setting ||
|
|
|
|
@upload.for_gravatar ||
|
|
|
|
public_type? ||
|
|
|
|
used_for_custom_emoji? ||
|
|
|
|
based_on_regular_emoji?
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def uploading_in_secure_context?
|
|
|
|
return true if SiteSetting.login_required?
|
|
|
|
if @upload.access_control_post_id.present?
|
|
|
|
return access_control_post_has_secure_media?
|
|
|
|
end
|
2020-01-16 22:16:27 -05:00
|
|
|
uploading_in_composer? || @upload.for_private_message || @upload.for_group_message || @upload.secure?
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2020-09-17 21:54:33 -04:00
|
|
|
private
|
|
|
|
|
2020-01-15 22:50:27 -05:00
|
|
|
# whether the upload should remain secure or not after posting depends on its context,
|
|
|
|
# which is based on the post it is linked to via access_control_post_id.
|
|
|
|
# if that post is with_secure_media? 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
|
|
|
|
#
|
|
|
|
# if there is no access control post id and the upload is currently secure, we
|
|
|
|
# do not want to make it un-secure to avoid unintentionally exposing it
|
|
|
|
def access_control_post_has_secure_media?
|
2020-01-22 21:01:10 -05:00
|
|
|
@upload.access_control_post.with_secure_media?
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2020-01-16 22:16:27 -05:00
|
|
|
def public_type?
|
2020-09-17 21:54:33 -04:00
|
|
|
PUBLIC_TYPES.include?(@upload_type) || @@custom_public_types.include?(@upload_type)
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
|
|
|
|
2020-01-16 22:16:27 -05:00
|
|
|
def uploading_in_composer?
|
|
|
|
@upload_type == "composer"
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|
2020-02-13 20:17:09 -05:00
|
|
|
|
|
|
|
def used_for_custom_emoji?
|
2020-02-16 21:30:47 -05:00
|
|
|
@upload.id.present? && CustomEmoji.exists?(upload_id: @upload.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def based_on_regular_emoji?
|
|
|
|
return false if @upload.origin.blank?
|
|
|
|
uri = URI.parse(@upload.origin)
|
2020-02-17 00:11:15 -05:00
|
|
|
return true if Emoji.all.map(&:url).include?("#{uri.path}?#{uri.query}")
|
|
|
|
uri.path.include?("images/emoji")
|
2020-02-13 20:17:09 -05:00
|
|
|
end
|
2020-01-15 22:50:27 -05:00
|
|
|
end
|