为了确定附加文件的文件类型,我使用了操作系统中的“file”实用工具:
很不幸,brakeman 表示这是一个命令行注入的机会。我猜这意味着有人想出了一个聪明的文件名,比如:
class AttachedFileTypeValidator < ActiveModel::Validator
def validate(record)
file = record.resource.uploaded_file
attached_file = Rails.root + file.path
file_type = `file #{attached_file}`
Rails.logger.info "Attached file type determined to be: #{file_type}"
unless file_type.split(',').first =~ /ASCII|UTF/
record.errors[:resource_content_type] << "Attachment does not appear to be a text CSV file, please ensure it was saved correctly."
end
end
end
很不幸,brakeman 表示这是一个命令行注入的机会。我猜这意味着有人想出了一个聪明的文件名,比如:
; rm -rf /;
开始吧。如何有效地清理文件名?
abc def
,那么\
file #{attached_file}``显然会失败。 - Asherah