rack path traversal
Description
Rack::Directory#get decides whether a request path containing .. stays inside the root by expanding it lexically with File.expand_path, but then joins the original, unnormalized path onto the root and passes it to File.stat and Dir.foreach. The operating system resolves a symbolic link before applying a .. that follows it, so when a directory under the root is a symlink, a request such as /system/../ passes the check while the filesystem lists the parent of the link's target, and /system/../config/ lists a sibling of the target. Both are outside the root. This results in disclosure of file and directory names, sizes and modification times outside the served tree.
Details
The check and the filesystem access use different paths:
def get(env)
path_info = Utils.unescape_path(env[PATH_INFO])
if client_error_response = check_bad_request(path_info) || check_forbidden(path_info)
client_error_response
else
path = ::File.join(@root, path_info) # unnormalized
list_path(env, path, path_info, script_name) # File.stat, Dir.foreach
end
end
def check_forbidden(path_info)
return unless path_info.include? ".."
expanded_path = ::File.expand_path(::File.join(@root, path_info)) # lexical
return if expanded_path == @root || expanded_path.start_with?(@root_with_separator)
# 403
endWith public/system linked to shared/system, File.expand_path("public/system/../") is public, so the request is allowed, but Dir.foreach("public/system/../") is evaluated by the kernel as shared/system/.., which is shared. The climb is bounded to one level above each symlink target, but any further symlink discovered in a listing can be used to climb again. Percent-encoded forms such as %2E%2E behave the same way. The Rack::Files application that Rack::Directory delegates to for files normalizes the path with Utils.clean_path_info before touching the filesystem, so file contents outside the root are not readable through this path; only directory listings leak.
Rack::Directory documents that it follows symbolic links, meaning that the link's target is served. This issue goes further: the target's parent and siblings become browsable.
All supported versions since 2.2.0 are affected. Earlier versions rejected every path containing ...
Impact
Applications that mount Rack::Directory, directly or through rackup with a directory application, with any symbolic link to a directory under the root. The Capistrano convention of linking public/system, public/uploads or public/assets into a release-independent shared/ directory is the common case, and shared/ typically also holds config/database.yml, config/master.key, logs and PID files. An unauthenticated attacker obtains the names, sizes and modification times of those entries, and can walk further through any other symlinks found. No authentication, malformed HTTP, or large payload is required. File contents are not disclosed.
Mitigation
- Update to a patched version of Rack, in which
Rack::Directorynormalizes the request path withUtils.clean_path_infobefore accessing the filesystem, asRack::Filesalready does. - Avoid symbolic links to directories under a root served by
Rack::Directory, or avoid exposingRack::Directoryto untrusted clients. - At a reverse proxy, reject request paths containing
..or%2E%2Esegments.