C++ 中的路径清理
我正在编写一个类似 FTP 的小型只读服务器。 客户端说“给我那个文件”,然后我的服务器发送它。
是否有任何标准方法(库函数?!?)来确保请求的文件不是“../../../../../etc/passwd”或任何其他坏事? 如果我可以将所有查询限制到一个目录(及其子目录),那就太好了。
谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Chroot 可能是最好的方法,但您可以使用 realpath(3) 来确定给定文件名的规范路径。 从手册页:
从那里您可以按照您喜欢的任何其他方式限制请求。
Chroot is probably the best way to go, but you can use
realpath(3)
to determine the canonical path to a given filename. From the man page:From there you can restrict the request in any additional way you like.
另请查看 chroot
Also take a look at chroot
虽然这并不完美,但您可以在特定用户/组下运行 ftp 服务器,并且只向该用户/组授予某些目录的权限。 然而,这可能并不正是您正在寻找的。
您还可以有一个用户可以访问的目录白名单,以及他们尝试访问的任何其他目录,而您根本不允许(因此,基本上是构建您自己的权限)。
我个人更喜欢前者,因为操作系统已经为您完成了这项工作。
While this isn't perfect, you can run your ftp server under a specific user/group, and only permission certain directories to that user/group. This, however, may not be exactly what you're looking for.
You can also have a whitelist of directories users can go to, and any others they try to go to, you simply don't allow (thus, basically building your own permissioning).
I, personally, prefer the former, as the work is already done for you by the OS.
获取根 (/) 目录和服务目录(例如 /ftp/pub)的 inode。 对于他们请求的文件,请确保:
您可以使用
stat
查找任何目录的inode。 将其放入一个函数中,并在提供文件之前调用它。当然,使用具有适当权限的用户/组也可以。
Get the inode of the root (/) directory, and that of the serving directory (say /ftp/pub). For the files they request, make sure that:
You can use
stat
to find the inode of any directory. Put this in one function, and call it before serving the file.Of course using a user/group with appropriate privilege will work as well.
我不知道有哪个标准库可以实现这一点。
您可以尝试:
将运行服务器的unix用户的权限设置为仅对某个目录具有读/写权限。 (可能使用PAM、chroot 环境或使用标准unix 用户/组权限)
您可以设计程序,使其只接受绝对路径(在 unix 中,以“/”开头的路径)。 这样,您可以检查以确保它是有效路径 - 例如,禁止任何包含字符串“..”的路径
编辑:
来自 Peter:看起来有一个库函数,realpath() 这有助于解决上面的#2。
I don't know of a standard library that accomplishes this.
You could try:
Set the permissions of the unix user who is running the server to only have read/write permissions to a certain directory. (maybe using PAM, a chrooted environment, or using standard unix user/group permissions)
You could design your program so that it only accepts absolute paths (in unix, paths beginning with '/'). That way, you can check to make sure it is a valid path - for example, disallow any path which has the string ".."
edit:
From Peter: looks like there is a library function, realpath() which helps with #2 from above.
在Windows中,我会做类似的事情(尽管仍然适用于任何操作系统):
In Windows, I would do something like (still applies to any OS though):