Possible Methods To Prevent Directory Traversal
A possible algorithm for preventing directory traversal would be to:
- Process URI requests that do not result in a file request, e.g., executing a hook into user code, before continuing below.
- When a URI request for a file/directory is to be made, build a full path to the file/directory if it exists, and normalize all characters (e.g., %20 converted to spaces).
- It is assumed that a 'Document Root' fully qualified, normalized, path is known, and this string has a length N. Assume that no files outside this directory can be served.
- Ensure that the first N characters of the fully qualified path to the requested file is exactly the same as the 'Document Root'.
- If so, allow the file to be returned.
- If not, return an error, since the request is clearly out of bounds from what the web-server should be allowed to serve.
- Check if the string contains '..' (two periods next to each other).
- Using a hard-coded predefined file extension to suffix the path does not limit the scope of the attack to files of that file extension.
Read more about this topic: Directory Traversal Attack
Famous quotes containing the words methods, prevent and/or directory:
“It would be some advantage to live a primitive and frontier life, though in the midst of an outward civilization, if only to learn what are the gross necessaries of life and what methods have been taken to obtain them.”
—Henry David Thoreau (18171862)
“It is the nature of our desires to be boundless, and many live only to gratify them. But for this purpose the first object is, not so much to establish an equality of fortune, as to prevent those who are of a good disposition from desiring more than their own, and those who are of a bad one from being able to acquire it; and this may be done if they are kept in an inferior station, and not exposed to injustice.”
—Aristotle (384322 B.C.)
“Although then a printer by trade, he listed himself in this early directory as an antiquarian. When he was asked the reason for this he replied that he always thought every town should have at least one antiquarian, and since none appeared for the post, he volunteered.”
—For the State of Iowa, U.S. public relief program (1935-1943)