From 66a20701b7626e31e4a843eda2c38dbda9b405ca Mon Sep 17 00:00:00 2001 From: Andrea Faulds Date: Wed, 23 Nov 2016 21:00:00 +0000 Subject: [PATCH 1/2] Rename media to avoid exposing filename (fixes #207) --- app/controllers/api/v1/media_controller.rb | 5 ++++- app/controllers/settings/profiles_controller.rb | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/media_controller.rb b/app/controllers/api/v1/media_controller.rb index bb8e8d9eed..23cc9bb7cb 100644 --- a/app/controllers/api/v1/media_controller.rb +++ b/app/controllers/api/v1/media_controller.rb @@ -7,7 +7,10 @@ class Api::V1::MediaController < ApiController respond_to :json def create - @media = MediaAttachment.create!(account: current_user.account, file: params[:file]) + file = params[:file] + # Change so Paperclip won't expose the actual filename + file.original_filename = "media" + File.extname(file.original_filename) + @media = MediaAttachment.create!(account: current_user.account, file: file) rescue Paperclip::Errors::NotIdentifiedByImageMagickError render json: { error: 'File type of uploaded media could not be verified' }, status: 422 rescue Paperclip::Error diff --git a/app/controllers/settings/profiles_controller.rb b/app/controllers/settings/profiles_controller.rb index 4b2b5a1316..9d9c0bb729 100644 --- a/app/controllers/settings/profiles_controller.rb +++ b/app/controllers/settings/profiles_controller.rb @@ -20,7 +20,18 @@ class Settings::ProfilesController < ApplicationController private def account_params - params.require(:account).permit(:display_name, :note, :avatar, :header, :silenced) + p = params.require(:account).permit(:display_name, :note, :avatar, :header, :silenced) + if p[:avatar] + avatar = p[:avatar] + # Change so Paperclip won't expose the actual filename + avatar.original_filename = "media" + File.extname(avatar.original_filename) + end + if p[:header] + header = p[:header] + # Change so Paperclip won't expose the actual filename + header.original_filename = "media" + File.extname(header.original_filename) + end + p end def set_account From cb06801b213a2021b3be21e4dbf68632842c9d48 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Wed, 23 Nov 2016 23:31:38 +0000 Subject: [PATCH 2/2] Extract filename obfuscation into module --- app/controllers/api/v1/media_controller.rb | 8 ++++---- app/controllers/settings/profiles_controller.rb | 17 +++++------------ app/models/concerns/obfuscate_filename.rb | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 app/models/concerns/obfuscate_filename.rb diff --git a/app/controllers/api/v1/media_controller.rb b/app/controllers/api/v1/media_controller.rb index 23cc9bb7cb..f8139ade77 100644 --- a/app/controllers/api/v1/media_controller.rb +++ b/app/controllers/api/v1/media_controller.rb @@ -4,13 +4,13 @@ class Api::V1::MediaController < ApiController before_action -> { doorkeeper_authorize! :write } before_action :require_user! + include ObfuscateFilename + obfuscate_filename :file + respond_to :json def create - file = params[:file] - # Change so Paperclip won't expose the actual filename - file.original_filename = "media" + File.extname(file.original_filename) - @media = MediaAttachment.create!(account: current_user.account, file: file) + @media = MediaAttachment.create!(account: current_user.account, file: params[:file]) rescue Paperclip::Errors::NotIdentifiedByImageMagickError render json: { error: 'File type of uploaded media could not be verified' }, status: 422 rescue Paperclip::Error diff --git a/app/controllers/settings/profiles_controller.rb b/app/controllers/settings/profiles_controller.rb index 9d9c0bb729..21fbba2afd 100644 --- a/app/controllers/settings/profiles_controller.rb +++ b/app/controllers/settings/profiles_controller.rb @@ -6,6 +6,10 @@ class Settings::ProfilesController < ApplicationController before_action :authenticate_user! before_action :set_account + include ObfuscateFilename + obfuscate_filename [:account, :avatar] + obfuscate_filename [:account, :header] + def show end @@ -20,18 +24,7 @@ class Settings::ProfilesController < ApplicationController private def account_params - p = params.require(:account).permit(:display_name, :note, :avatar, :header, :silenced) - if p[:avatar] - avatar = p[:avatar] - # Change so Paperclip won't expose the actual filename - avatar.original_filename = "media" + File.extname(avatar.original_filename) - end - if p[:header] - header = p[:header] - # Change so Paperclip won't expose the actual filename - header.original_filename = "media" + File.extname(header.original_filename) - end - p + params.require(:account).permit(:display_name, :note, :avatar, :header, :silenced) end def set_account diff --git a/app/models/concerns/obfuscate_filename.rb b/app/models/concerns/obfuscate_filename.rb new file mode 100644 index 0000000000..dc25cdbc2e --- /dev/null +++ b/app/models/concerns/obfuscate_filename.rb @@ -0,0 +1,16 @@ +module ObfuscateFilename + extend ActiveSupport::Concern + + class_methods do + def obfuscate_filename(*args) + before_action { obfuscate_filename(*args) } + end + end + + def obfuscate_filename(path) + file = params.dig(*path) + return if file.nil? + + file.original_filename = "media" + File.extname(file.original_filename) + end +end