Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active November 23, 2017 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/4227657775e94dac40912812d10bd7c0 to your computer and use it in GitHub Desktop.
Save ik5/4227657775e94dac40912812d10bd7c0 to your computer and use it in GitHub Desktop.
Example of how to sanitize file names (base, without path) to avoid any malicious actions
# help to avoid path traversal, and execution of anything on a machine
# due to file name
def escape_file_name(name)
# regex is a s follows:
# if it's the begining of the string, or there is no escape char
# for the following chars,
# add an escape for that char
name.gsub(/(^|[^\\])([\s\!\'\"#$&\^\*\`\/\(\)\[\]\?\{\}\|\~])/) do |match|
"\\#{match[1]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment