path-traversal
A file read or written at a path input chose.
- Group
- Code
- Runs
- Runs under ur scan:
ur scan --rule path-traversal - Level
- warning
- Reads
- Every file
- Fix
- None: the safe form depends on what the value is meant to be
Why
A value from outside the program, such as a request's parameter or a message another process sends, names the file read, written, sent, or removed, so whoever chose it can reach files outside the directory meant, such as with ../. Take only the file's name, as with Ruby's File.basename, and check the path it resolves to stays inside the directory meant.
The fix, as Ur suggests it: take the base name and check where the path resolves.
For agents
Never open a file at a path taken from input; take its base name and check the resolved path.
Example
app/controllers/downloads_controller.rbclass DownloadsController < ApplicationController
def show
send_file File.join("public/files", params[:name])
end
end
$ ur scan --rule path-traversal
warning[path-traversal]: `params`, a request parameter, reaches `send_file`
--> ./app/controllers/downloads_controller.rb:3:5 method `show`
= step 1: ./app/controllers/downloads_controller.rb:3:41 `params`, a request parameter,
= step 2: ./app/controllers/downloads_controller.rb:3:15 `.join` gives it
= hint: take the base name and check where the path resolves
= see: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/22.html
scanned 1 file (no framework recognized)
path-traversal: 1 high, 0 medium
1 finding and 0 of medium confidence
Source
CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), MITRE (2024-11-19).