2014-04-14 16:55:57 -04:00
|
|
|
require_dependency "file_helper"
|
|
|
|
|
|
|
|
module Validators; end
|
|
|
|
|
|
|
|
class Validators::UploadValidator < ActiveModel::Validator
|
|
|
|
|
|
|
|
def validate(upload)
|
2017-06-12 16:41:29 -04:00
|
|
|
# staff can upload any file in PM
|
|
|
|
if upload.for_private_message && SiteSetting.allow_staff_to_upload_any_file_in_pm
|
|
|
|
return true if upload.user&.staff?
|
|
|
|
end
|
|
|
|
|
2016-08-03 11:55:54 -04:00
|
|
|
# check the attachment blacklist
|
2017-06-12 16:41:29 -04:00
|
|
|
if upload.for_group_message && SiteSetting.allow_all_attachments_for_group_messages
|
2016-08-03 11:55:54 -04:00
|
|
|
return upload.original_filename =~ SiteSetting.attachment_filename_blacklist_regex
|
2016-06-27 16:26:05 -04:00
|
|
|
end
|
2016-02-29 16:39:24 -05:00
|
|
|
|
2014-07-14 21:15:17 -04:00
|
|
|
extension = File.extname(upload.original_filename)[1..-1] || ""
|
2014-04-14 16:55:57 -04:00
|
|
|
|
|
|
|
if is_authorized?(upload, extension)
|
|
|
|
if FileHelper.is_image?(upload.original_filename)
|
|
|
|
authorized_image_extension(upload, extension)
|
|
|
|
maximum_image_file_size(upload)
|
|
|
|
else
|
|
|
|
authorized_attachment_extension(upload, extension)
|
|
|
|
maximum_attachment_file_size(upload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_authorized?(upload, extension)
|
2018-02-19 04:44:24 -05:00
|
|
|
extension_authorized?(upload, extension, authorized_extensions(upload))
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorized_image_extension(upload, extension)
|
2018-02-19 04:44:24 -05:00
|
|
|
extension_authorized?(upload, extension, authorized_images(upload))
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def maximum_image_file_size(upload)
|
|
|
|
maximum_file_size(upload, "image")
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorized_attachment_extension(upload, extension)
|
2018-02-19 04:44:24 -05:00
|
|
|
extension_authorized?(upload, extension, authorized_attachments(upload))
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def maximum_attachment_file_size(upload)
|
|
|
|
maximum_file_size(upload, "attachment")
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-02-19 04:44:24 -05:00
|
|
|
def extensions_to_set(exts)
|
|
|
|
extensions = Set.new
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2018-02-19 04:44:24 -05:00
|
|
|
exts
|
2016-10-20 13:53:41 -04:00
|
|
|
.gsub(/[\s\.]+/, "")
|
|
|
|
.downcase
|
2014-04-14 16:55:57 -04:00
|
|
|
.split("|")
|
2018-02-19 04:44:24 -05:00
|
|
|
.each { |extension| extensions << extension unless extension.include?("*") }
|
|
|
|
|
|
|
|
extensions
|
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2018-02-19 04:44:24 -05:00
|
|
|
def authorized_extensions(upload)
|
2018-04-19 07:30:31 -04:00
|
|
|
extensions = if upload.for_theme
|
|
|
|
SiteSetting.theme_authorized_extensions
|
|
|
|
elsif upload.for_export
|
|
|
|
SiteSetting.export_authorized_extensions
|
|
|
|
else
|
|
|
|
SiteSetting.authorized_extensions
|
|
|
|
end
|
2018-02-19 04:44:24 -05:00
|
|
|
extensions_to_set(extensions)
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
def authorized_images(upload)
|
2018-02-19 04:44:24 -05:00
|
|
|
authorized_extensions(upload) & FileHelper.images
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
def authorized_attachments(upload)
|
2018-02-19 04:44:24 -05:00
|
|
|
authorized_extensions(upload) - FileHelper.images
|
2014-04-29 13:12:35 -04:00
|
|
|
end
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
def authorizes_all_extensions?(upload)
|
2018-02-19 04:44:24 -05:00
|
|
|
if upload.user&.staff?
|
|
|
|
return true if SiteSetting.authorized_extensions_for_staff.include?("*")
|
|
|
|
end
|
2018-04-19 07:30:31 -04:00
|
|
|
extensions = if upload.for_theme
|
|
|
|
SiteSetting.theme_authorized_extensions
|
|
|
|
elsif upload.for_export
|
|
|
|
SiteSetting.export_authorized_extensions
|
|
|
|
else
|
|
|
|
SiteSetting.authorized_extensions
|
|
|
|
end
|
2017-05-09 17:20:28 -04:00
|
|
|
extensions.include?("*")
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
2018-02-19 04:44:24 -05:00
|
|
|
def extension_authorized?(upload, extension, extensions)
|
2017-05-09 17:20:28 -04:00
|
|
|
return true if authorizes_all_extensions?(upload)
|
2014-04-29 13:12:35 -04:00
|
|
|
|
2018-02-19 04:44:24 -05:00
|
|
|
staff_extensions = Set.new
|
|
|
|
if upload.user&.staff?
|
|
|
|
staff_extensions = extensions_to_set(SiteSetting.authorized_extensions_for_staff)
|
|
|
|
return true if staff_extensions.include?(extension.downcase)
|
|
|
|
end
|
|
|
|
|
2014-04-29 13:12:35 -04:00
|
|
|
unless authorized = extensions.include?(extension.downcase)
|
2018-02-19 04:44:24 -05:00
|
|
|
message = I18n.t("upload.unauthorized", authorized_extensions: (extensions | staff_extensions).to_a.join(", "))
|
2014-04-14 16:55:57 -04:00
|
|
|
upload.errors.add(:original_filename, message)
|
|
|
|
end
|
2014-04-29 13:12:35 -04:00
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def maximum_file_size(upload, type)
|
2018-07-31 01:45:31 -04:00
|
|
|
max_size_kb = if upload.for_export
|
|
|
|
SiteSetting.max_export_file_size_kb
|
|
|
|
else
|
|
|
|
SiteSetting.send("max_#{type}_size_kb")
|
|
|
|
end
|
2015-05-03 13:26:54 -04:00
|
|
|
max_size_bytes = max_size_kb.kilobytes
|
2014-04-29 13:12:35 -04:00
|
|
|
|
2015-05-03 13:26:54 -04:00
|
|
|
if upload.filesize > max_size_bytes
|
2014-04-14 16:55:57 -04:00
|
|
|
message = I18n.t("upload.#{type}s.too_large", max_size_kb: max_size_kb)
|
|
|
|
upload.errors.add(:filesize, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|