gpt4 book ai didi

javascript - 依赖 Content-Type : text/plain to mitigate malicious javascript execution in response? 是否安全

转载 作者:数据小太阳 更新时间:2023-10-29 04:08:57 25 4
gpt4 key购买 nike

我们有一个返回的 web 应用程序


HTTP/1.1 400 Bad Request
...
Content-Type: text/plain;charset=UTF-8
Content-Length: 57
Date: Tue, 14 Apr 2015 19:24:54 GMT
Connection: close

Invalid project area item id

<script>alert(1086)</script>

据我了解,依靠 Content-Type: text/plain;charset=UTF-8 作为防御来阻止 javascript 执行是不够的。相反,应该对输出进行编码,并且应该对输入进行输入验证并丢弃垃圾。

我正在寻找的是关于处理具有 javascript 的响应的正确方法的一些非常清晰的官方答案,其中 Content-Type 已设置为文本/纯文本。

任何人都有指向这种情况的官方示例的链接(或答案)以及处理它的正确方法?还是只需要 Content-Type: text/plain;charset=UTF-8?

最佳答案

这里有两种情况。

顶级XSS

如果攻击者可以操纵顶级 URL 来响应格式错误的请求,例如

HTTP/1.1 400 Bad Request
...
<script>alert(document.cookie)</script>

然后设置 Content-Type: text/plain 减轻 XSS 攻击(参见下面的详细答案)。

AJAX 攻击

但是,如果攻击者可以在目标网页中操纵某些 AJAX 函数并诱使它执行类似 $("#result").html(xss_request_result) 的操作,那么这将有效地加载文本进入 web 上下文,它将被浏览器(包括 JS)解析,一切都结束了

明智的做法是entity-encodetag-strip此类错误消息的响应。


关于第一种情况,根据w3.org ,

The primary subtype of text is "plain". This indicates plain (unformatted) text. The default Content-Type for Internet mail, "text/plain; charset=us-ascii", describes existing Internet practice, that is, it is the type of body defined by RFC 822.

这意味着 text/plain 不应被解释和处理。然而,Google (2011 年 3 月 30 日更新)指出,

If Content-Type matches one of generic values, such as application/octet-stream, application/unknown, or even text/plain, many browsers treat this as a permission to second-guess the value based on the aforementioned signals, and try to come up with something more specific. The rationale for this step is that some badly configured web servers fall back to these types on all returned content.

根据一项名为 Is HTML sniffed on text/plain documents (with or without file extension in URL)? 的浏览器嗅探调查,结果为:

  • Internet Explorer 9+:否
  • 火狐:没有
  • 歌剧:没有
  • Chrome:否
  • 安卓:没有
  • Safari:

所以是的,将内容类型设置为带有显式字符集的text/plain 将减轻 XSS 攻击,但从上面的调查来看,Safari 可能是一个阻碍。

测试您的浏览器是否存在此漏洞。转到一个更慷慨的在线 PHP fiddle 站点(例如 http://phptester.net/)并执行以下命令。你不应该得到一个弹出窗口。

<?php
header("Content-Type: text/plain");
echo "<script>alert(1)</script>";

更多阅读 Content-sniffing XSS attacks (PDF)

关于javascript - 依赖 Content-Type : text/plain to mitigate malicious javascript execution in response? 是否安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com