[英]nodejs express automatic url normalization and redirection
似乎表達的默認行為是規范化包含向上目錄 (/../) 的 URL
對於下面的代碼,如果我請求這樣的 URL
http://localhost:8080/foo/../../bar
請求被重定向到
http://localhost:8080/bar
我找不到有關此行為的任何詳細文檔。
我的問題是:
經過
const express = require("express")
const app=express()
app.get("/*", (req,res) => {
console.log("url:",req.url);
console.log("path:",req.path);
res.send('echo for url='+req.url+'; path='+req.path')
}
const port=8080;
app.listen(port,() =>{
console.log(`listening on port ${port}`);
});
請求不會被重定向,而是客戶端在發出請求之前重建 URL
>curl -v http://localhost:8080/foo/../../bar
* Rebuilt URL to: http://localhost:8080/bar
因此,服務器永遠不會看到“原始 URL 路徑”。
此 URL 重建是解決過程的一部分。 另請參閱https://url.spec.whatwg.org/#concept-basic-url-parser 。
然而,惡意客戶端(例如,telnet 客戶端)可以發送帶有“未解決”的 URL 的 HTTP 請求。 以下中間件演示了如何在服務器上重建 URL:
function(req, res) {
res.json({path: req.path,
rebuilt_path: new URL("s://" + req.path).pathname});
}
惡意請求
GET /foo/../../bar HTTP/1.1
Host: localhost:8080
然后返回
{"path": "/foo/../../bar", "rebuilt_path": "/bar"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.