{"action":"created","issue":{"url":"https://api.github.com/repos/SCPR/SCPRv4/issues/263","labels_url":"https://api.github.com/repos/SCPR/SCPRv4/issues/263/labels{/name}","comments_url":"https://api.github.com/repos/SCPR/SCPRv4/issues/263/comments","events_url":"https://api.github.com/repos/SCPR/SCPRv4/issues/263/events","html_url":"https://github.com/SCPR/SCPRv4/issues/263","id":74125394,"number":263,"title":"Non-UTF8 character in filename causes file uploads to fail","user":{"login":"ewr","id":204350,"avatar_url":"https://avatars.githubusercontent.com/u/204350?v=3","gravatar_id":"","url":"https://api.github.com/users/ewr","html_url":"https://github.com/ewr","followers_url":"https://api.github.com/users/ewr/followers","following_url":"https://api.github.com/users/ewr/following{/other_user}","gists_url":"https://api.github.com/users/ewr/gists{/gist_id}","starred_url":"https://api.github.com/users/ewr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ewr/subscriptions","organizations_url":"https://api.github.com/users/ewr/orgs","repos_url":"https://api.github.com/users/ewr/repos","events_url":"https://api.github.com/users/ewr/events{/privacy}","received_events_url":"https://api.github.com/users/ewr/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-05-07T20:55:43Z","updated_at":"2015-05-09T00:35:52Z","closed_at":null,"body":"After the Rails 4.2 upgrade, if an audio file is uploaded with a funky filename, it will raise an exception.\r\n\r\nThe actual failing code is in `action_dispatch/http/upload.rb:31`:\r\n\r\n```ruby\r\n    @original_filename &&= @original_filename.encode \"UTF-8\"\r\n```\r\n\r\nWe either need this to be fixed in Rails, or we need to clean the filename before it gets to this point."},"comment":{"url":"https://api.github.com/repos/SCPR/SCPRv4/issues/comments/100399809","html_url":"https://github.com/SCPR/SCPRv4/issues/263#issuecomment-100399809","issue_url":"https://api.github.com/repos/SCPR/SCPRv4/issues/263","id":100399809,"user":{"login":"Ravenstine","id":5193330,"avatar_url":"https://avatars.githubusercontent.com/u/5193330?v=3","gravatar_id":"","url":"https://api.github.com/users/Ravenstine","html_url":"https://github.com/Ravenstine","followers_url":"https://api.github.com/users/Ravenstine/followers","following_url":"https://api.github.com/users/Ravenstine/following{/other_user}","gists_url":"https://api.github.com/users/Ravenstine/gists{/gist_id}","starred_url":"https://api.github.com/users/Ravenstine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Ravenstine/subscriptions","organizations_url":"https://api.github.com/users/Ravenstine/orgs","repos_url":"https://api.github.com/users/Ravenstine/repos","events_url":"https://api.github.com/users/Ravenstine/events{/privacy}","received_events_url":"https://api.github.com/users/Ravenstine/received_events","type":"User","site_admin":false},"created_at":"2015-05-09T00:35:52Z","updated_at":"2015-05-09T00:35:52Z","body":"I haven't found any existing solution that does what we need.  I've also tried utf8-cleaner.  My guess is that if any of these utf8 sanitizing gems work, they aren't going deep into the response object; since our file upload is nested inside of form params, they may not even find the file name.\r\n\r\nI hacked up this Rack middleware that finds nested hash keys called \"filename\" and removes invalid byte characters from their string values.  I'm not sure if this how we'd want to go about it, but it's my only proof of concept for how we could fix file names without monkeypatching ActionDispatch.\r\n\r\n```ruby\r\nclass Sanitizer\r\n  def initialize app\r\n    @app = app\r\n  end\r\n  def call env\r\n    env[\"rack.request.form_hash\"] = sanitize(env[\"rack.request.form_hash\"])\r\n    @app.call env\r\n  end\r\n  def sanitize input\r\n    if input.respond_to? :each_key\r\n      input.each_pair do |k, v|\r\n        input[k] = sanitize_pair k, v\r\n      end\r\n    end\r\n    input\r\n  end\r\n  private\r\n  def sanitize_pair key, value\r\n    if key.to_s.match(/file[_\\s]*name/i) && value.is_a?(String)\r\n      value.encode \"UTF-8\", invalid: :replace, undef: :replace, replace: \"_\"\r\n    elsif value.is_a? Hash\r\n      sanitize value\r\n    else\r\n      value\r\n    end\r\n  end\r\nend\r\n```"}}