- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我们有一个返回的 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?
最佳答案
这里有两种情况。
如果攻击者可以操纵顶级 URL 来响应格式错误的请求,例如
HTTP/1.1 400 Bad Request
...
<script>alert(document.cookie)</script>
然后设置 Content-Type: text/plain
减轻 XSS 攻击(参见下面的详细答案)。
但是,如果攻击者可以在目标网页中操纵某些 AJAX 函数并诱使它执行类似 $("#result").html(xss_request_result)
的操作,那么这将有效地加载文本进入 web 上下文,它将被浏览器(包括 JS)解析,一切都结束了。
明智的做法是entity-encode或 tag-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)? 的浏览器嗅探调查,结果为:
所以是的,将内容类型设置为带有显式字符集的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/
我们使用 Spring Cloud Config (Dalston.SR5),云客户端使用 Spring Boot 2.x、Spring Cloud Bus 和 Finchley.SR1。 我从thi
由于 Spectre 缓解,所有当前 CPU 都禁用了硬件锁定省略是否正确,并且任何尝试使用 HLE 内在函数/指令进行互斥都会导致通常的互斥? 这是否有可能在 future 不会有类似 HLE 互斥
每当我在Visual Studio上启动新的C++项目并进行编译时,都会遇到此错误。然后,我必须去设置并禁用幽灵缓解。 Error MSB8040 Spectre-mitigated librarie
我有一个我无法解决的问题。我的代码: #include "ntddk.h" #include NTSTATUS DriverEntry(IN PDRIVER_OBJECT theDriverObje
我想看看 Spectre 缓解标志如何解决一个问题,以说服我的团队我们需要将此标志添加到我们即将进行的项目的大型代码库中。 我正在搜索具有 Spectre 漏洞并通过添加上周在 Visual Stud
我们有一个返回的 web 应用程序 HTTP/1.1 400 Bad Request ... Content-Type: text/plain;charset=UTF-8 Content-Length
我是一名优秀的程序员,十分优秀!