Sending Variables for PHP FileSystem Functions with Form Submission

拥有回忆 提交于 2019-12-01 21:19:34

You might want to look at realpath().

$foo = realpath($foo);
if (substr($foo, 0, 8) != '/my/path') {
    return false;
}

...or something like that.

You probably need to elaborate further on the purpose of this script and why it wants to read arbitrary directories. Without this knowledge, it is difficult to comment on the security of this code, other than to point out it is an extremely dangerous thing to do, without clear limitations on accessible directories.

I, for one, would never install a PHP script to my server that allows a remote attacker to specify a directory to read - or even to be able to find out if particular files or directories even exist.

There is a strong argument for REQUIRING the user to edit a config file, read by the php, that allows the server owner to white-list a set of directories that are allowed to be opened by this script

Update after reply from OP

The problem you have here is that all "ajax" requests are made directly by and from the browser. This means that any data that the webserver sets in the html page that makes the ajax request can be overwritten by a malicious user. Your ajax request can be made by anyone at any time with any parameter, unless you secure it to do otherwise.

As soon as your data leaves your server, you can no longer trust it. The best way to make this secure is to hold a white-list of allowed directories on the server-side. If you receive a request for a directory that is not on the allowed list (or perhaps not in a subdirectory underneath something in the allowed list) then you reject the request and give a standard error response.

Your directory name can contain .. elements, so they can look outside the current working directory you're intended to limit them to. They'll still only be able to look at directories named as you intend, though.

FWIW, using parse_url() to analyze whether a directory name is relative is really bizarre. Why don't you just look for $foo[0] == '/'?

(That assumes a Unix path convention, of course, but your code already does that.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!