是否可以通过 request.url 进行目录遍历?

con*_*com 3 node.js

我来自 PHP,您可以在其中向 URL 中注入双点以尝试目录遍历。在 NodeJS 中,您似乎让Http网络服务器自动从 URL 中删除双点。

例如,如果您转到http://example.com/static/../app.js,则 Node 似乎重定向到http://example.com/app.js,然后在我的情况下抛出 404,因为对于不以 开头的 URL 没有回调/static/

它安全通过的假设,目录遍历request.url不是可以在HTTP的NodeJS web服务器使用创建的http包?

And*_*ren 5

我想说您可以确定这是不可能的,然后我尝试了,我不得不说不,http 模块似乎没有删除“/../”。您看到的重定向是在浏览器中完成的。因此,是否存在安全风险取决于您的静态处理程序是如何实现的。

概念证明:

// Server
var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end(req.url);
}).listen(1337);
Run Code Online (Sandbox Code Playgroud)

卷曲它:

curl  --path-as-is "http://localhost:1337/static/../app.js"
# /static/../app.js
Run Code Online (Sandbox Code Playgroud)

因此,如果您使用仅使用 path.resolve() 的自制静态处理程序,您就完蛋了。希望像 express.static 这样受欢迎的人已经考虑过这一点,但我还没有尝试过。

更新

Express 确实以 404“错误:禁止”响应。

  • 请注意,curl 将删除“/../”。尝试使用 `curl --path-as-is "http://localhost:1337/static/../app.js"` (6认同)