Skip to content

Commit

Permalink
Sanitize the filename of uploaded file
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Li committed May 10, 2017
1 parent b3784c5 commit 6022d7c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/ext/string.rb
Expand Up @@ -94,7 +94,15 @@ def cama_fix_slash
end

def cama_fix_filename
"#{File.basename(self, File.extname(self)).downcase.gsub(" ", "-").parameterize}#{File.extname(self)}"
# Sanitize the filename, to prevent hacking
# https://github.com/carrierwaveuploader/carrierwave/blob/6a1445e0daef29a5d4f799a016359b62d82dbc24/lib/carrierwave/sanitized_file.rb#L322
sanitize_regexp = /[^[:word:]\.\-\+]/
name = self.tr("\\", "/") # work-around for IE
name = File.basename(name)
name = name.gsub(sanitize_regexp, "_")
name = "_#{name}" if name =~ /\A\.+\z/
name = "unnamed" if name.size == 0
name.mb_chars.to_s
end

# return cleaned model class name
Expand Down Expand Up @@ -143,4 +151,4 @@ def cama_log_style(color = :red)
colors = {red: 31, green: 32, blue: 34, pink: 35, light_blue: 36, yellow: 33}
"\e[#{colors[color]}m#{self}\e[0m"
end
end
end
2 changes: 2 additions & 0 deletions spec/dummy/.gitignore
Expand Up @@ -2,3 +2,5 @@
/db/*.sqlite3
/tmp/cache
/tmp/capybara
/public/media
/public/tmp

0 comments on commit 6022d7c

Please sign in to comment.