简体   繁体   English

我如何确保没有人可以访问Websever中文档根目录之外的文件

[英]How do I make sure nobody gets access to files out of my document root in my websever

I'm currently working on a webserver in C. I want to make sure that it's impossible to access files that are outside of my document root and I need to write a function that checks the request file / path to that file (for example /index.html) and makes sure there is no way to get out of my document root(for example /../ would make it go out of document root). 我目前正在使用C语言在网络服务器上工作。我想确保无法访问文档根目录之外的文件,并且我需要编写一个函数来检查请求文件/该文件的路径(例如/ index.html),并确保没有办法摆脱我的文档根目录(例如/../会使它脱离文档根目录)。 I was thinking about just checking for ../ and removing that, but then somebody could request ....//, which would be ../ again if we remove ../ out of it. 我当时正在考虑只检查../并删除它,但随后有人可以请求.... //,如果我们从其中删除../,那将再次是../。 Is there some clever way to do this that I'm not thinking about? 有我没有想到的一些聪明方法吗?

Use realpath : 使用realpath

realpath - return the canonicalized absolute pathname realpath-返回规范化的绝对​​路径名

It will return the absolute pathname, with all symlinks, all ./ and all /../ resolved. 它将返回绝对路径名,并带有所有符号链接,所有./和所有/../解析。 Store your "document root" after resolving it with realpath too. realpath解决它后,也可以存储“文档根目录”。 After that you can simply memcmp or strcmp resolved path to document root with the resolved pathname to the path you want to test.. 之后,您可以简单地使用memcmpstrcmp解析路径来文档根目录,并使用要测试的路径的解析路径名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM