mod_rewrite NE 标志 - 什么时候对 URL 中的特殊字符进行编码有帮助?

发布于 2024-10-21 17:07:26 字数 353 浏览 1 评论 0原文

我一直在查看 mod_rewrite 中的 [NE] (noescape) 标志。经过一番思考,我无法弄清楚什么情况下我不想想要使用该标志。这意味着,在几乎每个 RewriteRule 中保持启用该标志似乎最有帮助。在某些情况下,不调用此标志给我带来了问题。

我处理的大多数规则都是 HTTP 重定向 ([R]),而不是传递。

有人会透露一下什么时候让 mod_rewrite 对 URL 进行编码有帮助吗?

启用此标志通常是一种好的做法,还是使用允许 mod_rewrite 转义这些特殊字符的默认行为?为什么?

I've been looking at the [NE] (noescape) flag in mod_rewrite. After some thought I couldn't figure out a situation when I would NOT want to use the flag. Meaning, it seems most helpful to keep the flag enabled in almost every RewriteRule. Not invoking this flag has caused me problems in a few circumstances.

Most of the rules that I deal with are HTTP redirects ([R]), rather than passing through.

Would someone shed some light as to when it is helpful to have mod_rewrite encode the URL?

Is it generally good practice to enable this flag, or use the default behavior of allowing mod_rewrite escape these special characters? Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2024-10-28 17:07:26

如果您查看 mod_rewrite 的源代码,您会注意到,如果启用了 noescape,它会设置一个 proxy-nocanon 标志。

修订版中该行首次添加的位置,还包含以下注释:

确保 mod_proxy_http 不会规范化 URI,并在 mod_proxy_http:proxy_http_canon() 的文件名中保留任何(可能是 qsappend'd)查询字符串

接下来,如果您阅读 mod_proxy 文档,您将看到以下提及 nocanon 的内容:

通常情况下,mod_proxy 会将 ProxyPassed URL 规范化。但这可能与某些后端不兼容,特别是那些使用 PATH_INFO 的后端。可选的 nocanon 关键字可以抑制这种情况,并将 URL 路径“raw”传递到后端。请注意,这可能会影响后端的安全性,因为它消除了代理提供的针对基于 URL 的攻击的正常有限保护。

我可能是错的,但这对我来说意味着在 mod_proxy 中使用 nocanon (以及在 mod_rewrite 中扩展 noescape)具有潜在的安全影响。这可以解释为什么它默认被禁用,即使在大多数情况下启用它似乎更有用。

If you look at the source code for mod_rewrite, you'll notice that it sets a proxy-nocanon flag if noescape is enabled.

In the revision where that line was first added, it also included this comment:

make sure that mod_proxy_http doesn't canonicalize the URI, and preserve any (possibly qsappend'd) query string in the filename for mod_proxy_http:proxy_http_canon()

Following on from that, if you read the mod_proxy documentation, you'll see the following mention of nocanon:

Normally, mod_proxy will canonicalise ProxyPassed URLs. But this may be incompatible with some backends, particularly those that make use of PATH_INFO. The optional nocanon keyword suppresses this, and passes the URL path "raw" to the backend. Note that may affect the security of your backend, as it removes the normal limited protection against URL-based attacks provided by the proxy.

I'm may be mistaken, but that implies to me that the use of nocanon in mod_proxy (and by extension noescape in mod_rewrite) has potential security ramifications. That would explain why it is disabled by default, even thought it seems like it would be more useful to have it enabled in most cases.

季末如歌 2024-10-28 17:07:26

当您将请求 URL 添加为授权签名的一部分时,[NE] 标志非常有用。

我刚刚遇到一个错误,授权在 .htaccess 关闭时有效,但在 .htaccess 打开时无效。事实证明,原因是重定向对最终出现在 php $_GET 参数中的项目进行了 url 编码。为了解决这个bug,我将:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/0-9])$ $1/ [R=301,L]

改为

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/0-9])$ $1/ [NE,R=301,L]

(授权签名由很多东西组成,其中之一就是请求url)

The [NE] flag is extremely useful when you are adding the request url as part of - let's say - an authorization signature.

I just had a bug where authorization was working with .htaccess off but not with it on. It turned out the cause was that redirection was url encoding the items that ended up in the php $_GET parameter. To solve the bug I changed:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/0-9])$ $1/ [R=301,L]

to

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/0-9])$ $1/ [NE,R=301,L]

(the authorization signature is composed of many things, one of these is the request url)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文