PHP's realpath()
is well described in the manual, I'd just like to understand in which scenarios it is useful. Because it seems that my code works well enough with paths like ../../path
so I'm not really sure where realpath()
is useful or even necessary.
THE BAD NEWS IS
When you use a "dot-dot-slash" for your path you're in a warning state to be attacked using Path Traversal, This attack aims to access files and directories that are stored outside the web root folder. By manipulating variable that reference files with "dot-dot-slash" (../) sequences and it's variations, it may be possible to access arbitrary files and directories stored on file system, including application source code, configuration and critical system files, limited by system operational access control. The attacker uses "../" sequences to move up to root directory, thus permitting navigation through the file system.
This attack can be executed with an external malicious code injected on the path, like the Resource Injection attack. To perform this attack it’s not necessary to use a specific tool; attackers typically use a spider/crawler to detect all URLs available.
This attack is also known as "dot-dot-slash", "directory traversal", "directory climbing" and "backtracking".
you can check this over ..
AND, THE GOOD NEWS IS....
realpath() will let you convert any path that may contain relative information into an absolute path. You can then ensure that path is under a certain subdirectory that you want to allow access to.
For absolute path such as an URL's your subdomain and protocol can be controlled. People that enter through an obscure subdomain will be funneled into the proper subdomain. You can hop back and forth between secure and non-secure as appropriate. And, using it can be configurable, developers love things to be absolute.
You can design neat algorithms when using absolute URLs. URLs can be made configurable so that an URL can be updated site-wide with a single change in a single configuration file.
But if you look at this :
<a href=“index.php?q=”>index.php?q=</a>
<link src=“../.././../css/default.css” />
Are you not confuse of that ?
Sorry for my bad english... :)
In my experience I found realpath()
useful for
when displaying/logging/storing a full path instead of a relative one is more desiderable.
to ensure that a given file path is below (in the filesystem hierarchy) a "base" path, before doing something with that file (serving, editing, etc...)
Example:
$full_path = realpath( $relative_path );
if( $full_path !== false && strpos( $full_path, "/var/www/whatever/" ) === 0 )
{
//...
}
A couple of things worth mentioning:
realpath()
resolves symlinks.
realpath()
returns FALSE
if the file or directory doesn't exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With