<分区>
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,
visit the help center 指导。
关闭 10 年前 。
我碰巧读到了 XSS 以及如何避免它。从我读到的内容中,我开始知道我们需要输入过滤、正确处理应用程序代码和输出编码,以使 Web 应用程序在一定程度上 XSS 安全。看了好几篇文章,还是有几个疑惑。
当我尝试 jQuery.text("untrusted_data") 或 element.text=untrusted_data 时,
浏览器似乎完美地编码了内容,但我在其他地方读到客户端编码不应该被信任,你必须“总是 ”在服务器上编码-边。 为什么认为客户端编码不安全?
每当我尝试使用 jQuery.val(untrusted_data) 或 element.value=untrusted_data 设置值时,它似乎足够安全。那么它真的是 XSS 安全的还是我在这里遗漏了任何案例?
>
另外,我碰巧在某处读到 jQuery.setAttribute(attrName,untrusted_data) 和 setAttribute(attrName,untrusted_data) 只有当 Attribute Names 不存在时才通常被认为是安全的'不包括基于 URL 上下文的属性 (src、href 等)或事件处理程序 (onClick、onMouseOver 等)。如果是这样,我应该如何使用 setAttribute("href",untrusted_data) 设置 href 属性?服务器端的 encodeForHtmlAttribute(untrusted_data) 是正确的方法吗?
>
我应该如何处理动态 html 创建。考虑下面的例子
<div id="divName1" onClick="copyData()">untrusted_data</div>
<div id="divName2"></div>
function copyData()
{
var divValue = jQuery("#divName1").html();
jQuery("#divName2").html(divValue);/XSS Here
}
我想在这里实现的是从divName1 中获取文本并将其粘贴到divName2 的内容中。我上面写的代码存在 XSS 漏洞。我可以将其更改为
jQuery("#divName2").text(divValue)
所以,这将确保编码,但据我了解,我们说客户端编码是不安全的,应该只使用服务器端编码。我应该如何在不使用客户端编码的情况下编写 XSS 安全的代码?我在这里有点困惑:(。
请帮我解开这些疑惑。
这是一次很多问题。
Why is client-side encoding considered not safe ?
只要正确且一致地完成,客户端编码就很好。客户端编码通常具有较低的阈值,因为它不需要担心字符编码级别的攻击,如 UTF-7 攻击。
Whenever I tried to set value using jQuery.val(untrusted_data) or element.value=untrusted_data it seems to be safe enough. So is it really XSS safe or did I miss any case here ?
假设untrusted_data
是一个字符串,并且要设置其值的元素是流或 block 元素中的常规文本节点,那么您就可以了。如果要赋值的节点是 <script>
中的文本节点,您可能会遇到麻烦。元素或 URL、事件处理程序或样式属性节点或与 <object>
相关的任何内容.
Also, I happen to read somewhere that jQuery.setAttribute(attrName,untrusted_data) and setAttribute(attrName,untrusted_data) are generally considered safe only if the Attribute Names doesn't include URL Context based attributes(src,href etc) or Event Handlers(onClick,onMouseOver etc). If that is the case,
这是部分正确的。其他属性有尖锐的边缘,如 style
以及许多与 <object>
有关的事情和 <embed>
, 和 <meta>
.
如果您不太了解该属性,则不要将其暴露给不受信任的数据。
通常安全的东西是包含文本内容的属性,如 title
,或者具有类似 dir=ltr
的枚举值和 dir=rtl
.
一旦您处理的属性具有更复杂的值,您就面临着攻击者利用晦涩的浏览器扩展(例如 -moz-binding
)的风险。在style
属性。
it seems to be safe enough
在您踩到地雷之前,地雷似乎是安全的。
您不能从“看似安全”的事物中得出太多结论。您确实需要查看它并了解可能出现的问题,并安排事情以便只有在出现完美 Storm 时才会面临风险(P(灾难)= P(failure0)* P(failure1 ) * ...) 并且当只有一件事出错时您没有风险 (P(disaster) = P(failure0) + P(failure1)*P(!failure0) + ...)。
how am I supposed to set an href attribute by using setAttribute("href",untrusted_data)?
不要在没有将协议(protocol)列入白名单的情况下这样做。
if (!/^https?:\/\//i.test(untrusted_data) && !/^mailto:/i.test(untrusted_data)) {
throw new Error('unsafe');
}
Is encodeForHtmlAttribute(untrusted_data) from server-side the right way of approach ?
没有。 HTML 编码传递给 setAttribute
的值是多余的,无助于保留任何安全属性。 <iframe srcdoc>
如果我对最近规范更改的内存是正确的,它的内容可能是一个罕见的异常(exception),因为它的内容是 HTML。
What I want to achieve here is to get the text from divName1 and paste it to divName2's content. The code I wrote above is XSS vulnerable.
不要乱用 HTML。浏览器的 .innerHTML
setter/getter 是有问题的,有时会导致利用反引号作为 IE 中的值分隔符。只需将节点从一个节点克隆到另一个节点即可。像下面这样的东西应该这样做:
var div1 = $('#divName1');
for (var child = div1[0].firstChild(); child; child = child.nextSibling) {
$('#divName2').append([child.cloneNode(true)]);
}
I can change this to
jQuery("#divName2").text(divValue)
如果您只需要文本内容,那很好。