Apache mod_rewrite 替换 REQUEST_URI 中的字符

发布于 2024-08-30 11:47:22 字数 485 浏览 1 评论 0 原文

我正在尝试编写一些 .htaccess 规则来替换 REQUEST_URI 参数中的某些字符。具体来说,我想替换以下内容:

  • “<” = &lt;
  • “>” = &gt;
  • "'" = &apos;
  • '"' = &#x22;
  • ")" = &#x29;
  • "(" = &#x28;

示例 URL 可以是 http://www.example.com/?&q=")("

我尝试了一大堆方法但没有成功,有人能指出我正确的方向吗?

I'm trying to write some .htaccess rules that replace certain characters in the REQUEST_URI parameter. Specifically, I want to replace the following:

  • "<" = <
  • ">" = >
  • "'" = '
  • '"' = "
  • ")" = )
  • "(" = (

Example URL could be http://www.example.com/?<script>alert(1)</script>&q=")("<script')

I've tried a whole bunch of methods with no success. Can someone point me in the right direction? Thanks.

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

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

发布评论

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

评论(1

心碎无痕… 2024-09-06 11:47:22

您可以使用 mod_rewrite 来执行此替换,请参阅 < 示例:

RewriteCond %{QUERY_STRING} ^([^<]*)<([^<]*)<(.*)
RewriteRule ^ %{REQUEST_URI}?%1<%2< [N]
RewriteCond %{QUERY_STRING} ^([^<]*)<([^<]*)$
RewriteRule ^ %{REQUEST_URI}?%1<%2 [L]

第一个规则将一次替换两个 < 字符,第二个规则将结束递归。其他字符可以用相同的方式替换(只需将 << 替换为其他对)。

但是使用 mod_rewrite 来完成此类工作不太合适,因为

  1. mod_rewrite 允许一次仅替换固定数量的出现次数,并且
  2. 替换次数仅限于用于避免无限递归的内部重定向计数器。

尽管由于使用了 在这种情况下第二条语句不适用N 标志我不建议在此类工作中使用 mod_rewrite

我宁愿建议在 Web 应用程序中执行此操作,可能是在将数据放入 HTML 文档之前,而不是对每个输入都采取预防性的方式,无论数据是如何处理的。

You can use mod_rewrite to do this replacement, see this example for <:

RewriteCond %{QUERY_STRING} ^([^<]*)<([^<]*)<(.*)
RewriteRule ^ %{REQUEST_URI}?%1<%2< [N]
RewriteCond %{QUERY_STRING} ^([^<]*)<([^<]*)$
RewriteRule ^ %{REQUEST_URI}?%1<%2 [L]

The first rule will replace two < characters at a time and the second will end the recursion. The other characters can be replaced in the same way (just replace < and < with the other pairs).

But using mod_rewrite for this kind of work is not that suitable because

  1. mod_rewrite allows to replace only fixed number of occurrences at a time and
  2. the number of replacements is limited to the internal redirection counter that is used to avoid infinite recursion.

Although the second statement does not apply in this case due to the usage of the N flag, I would not recommend the usage of mod_rewrite for this kind of work.

I would rather recommend to do this in the web application, possibly just before putting your data out into an HTML document and not in a prophylactic manner for every input no matter how that data is processed.

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