Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

アバター画像のバリデーションにMime-Typeを使うようにした #7759

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/current_user_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def edit
end

def update
@user.uploaded_avatar = user_params[:avatar]

if @user.update(user_params)
redirect_to @user, notice: 'ユーザー情報を更新しました。'
else
Expand Down
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def create
@user.course_id ||= Course.first.id
@user.free = true if @user.trainee?
@user.build_discord_profile
@user.uploaded_avatar = user_params[:avatar]
Newspaper.publish(:user_create, { user: @user })
if @user.staff? || @user.trainee?
create_free_user!
Expand Down
16 changes: 11 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ class User < ApplicationRecord

validates :login_name, length: { minimum: 3, message: 'は3文字以上にしてください。' }

validates :avatar, attached: false,
content_type: {
in: %w[image/png image/jpg image/jpeg image/gif image/heic image/heif],
message: 'はPNG, JPG, GIF, HEIC, HEIF形式にしてください'
}
attribute :uploaded_avatar, :binary
validate :validate_uploaded_avatar_content_type

with_options if: -> { %i[create update].include? validation_context } do
validates :login_name, presence: true, uniqueness: true,
Expand Down Expand Up @@ -820,4 +817,13 @@ def category_having_active_practice
def category_having_unstarted_practice
unstarted_practices&.first&.categories&.first
end

def validate_uploaded_avatar_content_type
return unless uploaded_avatar

mime_type = Marcel::Magic.by_magic(uploaded_avatar.read)&.type
return if mime_type&.start_with?('image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/heic', 'image/heif')

errors.add(:avatar, 'は指定された拡張子(PNG, JPG, GIF, HEIC, HEIF形式)になっていないか、あるいは画像が破損している可能性があります')
end
end
1 change: 1 addition & 0 deletions test/fixtures/files/images/broken_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/system/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ class UsersTest < ApplicationSystemTestCase
assert_match(/#{user.id}\.png$/, img.native['src'])
end

test 'can not upload broken image as user avatar' do
visit_with_auth '/current_user/edit', 'hajime'
attach_file 'user[avatar]', 'test/fixtures/files/images/broken_image.jpg', make_visible: true
click_button '更新する'

assert_text 'ユーザーアイコンは指定された拡張子(PNG, JPG, GIF, HEIC, HEIF形式)になっていないか、あるいは画像が破損している可能性があります'
end

test 'mentor can see retired and hibernated tabs' do
visit_with_auth '/users', 'mentormentaro'
assert_link '休会', href: '/users?target=hibernated'
Expand Down