Do not use user input to form file paths.
- Rule ID: ruby_lang_path_using_user_input
- Languages: ruby
- Source: path_using_user_input.yml
Description
Using raw unsanitized input when forming filenames or file paths is bad practice. It can lead to path manipulation, by which attackers can gain access to resources outside of the intended scope.
Remediations
❌ Avoid wherever possible
✅ Validate expected file paths using File
methods
path = File.expand("/home/" + params[:resource_name])
if path.starts_with?("/home/")
Dir.chdir(path)
else
# path is unexpected
end
Resources
Associated CWE
- CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
- CWE-73: External Control of File Name or Path