【问题标题】:How to deny path containing dot dot slash(relative path) in Nginx?如何在 Nginx 中拒绝包含点点斜杠(相对路径)的路径?
【发布时间】:2019-02-16 21:26:09
【问题描述】:

我在 nginx 后面的 web 应用程序不应该收到任何包含相对路径的 url,例如 ../,直接在 Nginx 级别拒绝它们会更安全。

尝试了这个配置,但不工作:

location ~ \.\.\/ {
  return 400;
}

带有测试用例:

printf "GET /web/../play/find.js HTTP/1.1\r\nHost: www.example.com\r\n\r\n" | nc 127.0.0.1 80

【问题讨论】:

  • 您的location 规则将不起作用,因为此时nginx 已经解析了任何.. 路径元素。您需要使用mapif 块检查$request_uri
  • @RichardSmith 你是对的!

标签: nginx nginx-location


【解决方案1】:

在@RichardSmith 的帮助下,人们可以在任何位置匹配之前添加这个正则表达式测试:

if ($request_uri ~ "\.\./"){
    return 403;
}

location / {
    proxy_pass http://localhost:8095;
}

【讨论】:

  • 很好,可以了
猜你喜欢
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 2014-03-30
  • 2016-01-22
相关资源
最近更新 更多