option.text() 在我的例子中容易受到 xss fiddle
option.text() is vulnerable to xss in my example fiddle
为什么我会在
中收到警告框
$(function() {
$('#users').each(function() {
var select = $(this);
var option = select.children('option').first();
select.after(option.text());
select.hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="users" name="users">
<option value="bad"><script>alert('xss');</script></option>
</select>
尽管我在选项文本中有编码版本 (<script>alert('xss');</script>)
??
我试图阻止显示警报,因为我在选项 html 中编码了 text.Can 有人告诉我我错过了什么吗?
I have encoded html in the option text.
这没关系,因为您得到的是它的文本表示(使用 text()
)。 (即使您得到的是 HTML 表示,浏览器如何将 DOM 中的文本转换回 HTML 的规则也可能会给您带来意想不到的结果)。
然后您使用 select.after(that_text);
。如果您将看起来像 HTML(您就是)的字符串传递给 after
,那么 jQuery 会将其视为 HTML。您需要明确地将其视为文本。
例如:
select.after(
jQuery("<span />").text(that_text)
);
为什么我会在
中收到警告框 $(function() {
$('#users').each(function() {
var select = $(this);
var option = select.children('option').first();
select.after(option.text());
select.hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="users" name="users">
<option value="bad"><script>alert('xss');</script></option>
</select>
尽管我在选项文本中有编码版本 (<script>alert('xss');</script>)
??
我试图阻止显示警报,因为我在选项 html 中编码了 text.Can 有人告诉我我错过了什么吗?
I have encoded html in the option text.
这没关系,因为您得到的是它的文本表示(使用 text()
)。 (即使您得到的是 HTML 表示,浏览器如何将 DOM 中的文本转换回 HTML 的规则也可能会给您带来意想不到的结果)。
然后您使用 select.after(that_text);
。如果您将看起来像 HTML(您就是)的字符串传递给 after
,那么 jQuery 会将其视为 HTML。您需要明确地将其视为文本。
例如:
select.after(
jQuery("<span />").text(that_text)
);