我正在使用 OWASP ESAPI encodeForHTMLAttribute 但是符号显示为它们的 html 实体编号而不是符号
I am using the OWASP ESAPI encodeForHTMLAttribute however symbols are displaying as their html entity number instead of symbol
我刚刚了解用于 XSS 预防的 OWASP ESAPI,我正在我的应用程序中使用 Javascipt version
根据 XSS 预防作弊 sheet 中的 Rule #2,您应该 "Attribute Escape" 在将不受信任的数据插入 "width, name or value" 之类的属性值之前,我正在片刻。
然而,在将电子邮件地址作为联系表单中输入字段的值插入之前使用 encodeForHTMLAttribute() 时,@ 符号显示为对应的 html 实体 #&x40 ;如您在此屏幕截图中所见:
我是不是漏掉了什么?
我已确保文档的字符集设置为 utf-8,如下所示:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
我还在 accept-charset="UTF-8" 属性中添加了相关表单。
HTML形式:
<form id="contact_form" name="contact_form" method="post" action="" accept-charset="UTF-8">
<div class="form_divider">
<label for="contact_form_email"><span class="asterisk">*</span>E-mail</label>
<input id="contact_form_email" tabindex="1" name="contact_form_email" type="email" data-role="none" maxlength="254" placeholder="E-mail" required/>
</div><!--/form_divider-->
</form>
我的应用的数据流:
在我的应用程序中,执行 ajax 请求以提交登录详细信息,在身份验证成功后,我从数据库中检索他们的详细信息并以 json 格式发送回客户端。
然后我将这些详细信息插入到 html 适用的地方
Javascript:
$.ajax({
url: app_root_url + 'login_registration/user_processing.php',
data: JSON.stringify(params),
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function(data){
//data will be the user details such as userID, display name, email
var result = data;
prepareAppAfterLogin(result);
}
});//end ajax
function prepareAppAfterLogin(result){
var userDetails = result.userDetails;
generateUserProfilePage(userDetails);
}
function generateUserProfilePage(userDetails){
var userID = userDetails.userID;
var display_name = userDetails.display_name;
var email_address = userDetails.email_address;
var profile_image = userDetails.profile_image;
$("#profile_pic").attr('src', profile_image);
var safe_email_address_for_html = $ESAPI.encoder().encodeForHTML(email_address);
$("#profile_email_address").html(safe_email_address_for_html);
var safe_email_for_attribute = $ESAPI.encoder().encodeForHTMLAttribute(email_address);
$("#hidden_input_for_email").val(safe_email_for_attribute);
$("#contact_form_email").val(safe_email_for_attribute);
}
编辑:
我刚刚找到了支持@Cheran Shunmugavel 在他下面的评论中所说内容的文档:
这里的规则#2:https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet
"It is important to note that when setting an HTML attribute which does not execute code, the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up."..."The appropriate encoding to use would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag."
遵循此规则,我刚刚尝试对 Javascript 的电子邮件地址进行编码,而不是对 html 属性进行编码,如下所示:
function generateUserProfilePage(userDetails){
var safe_email_address = $ESAPI.encoder().encodeForJavaScript(userDetails.email_address);
$("#contact_form_email").val(safe_email_address);
}
然而,这样做似乎也会产生显示问题:
你能确认我需要javascript编码吗?谢谢
如果您使用 jQuery val
方法在文档中插入数据,则不需要 encodeForHTML 和 encodeForHTML 属性函数。我在官方文档中找不到任何内容,但对这个 Whosebug 问题有很好的解释:Do jQuery's val() and prop() methods html-escape values?。重要的是 val
在内部设置 DOM value
属性,并且 属性 永远不会尝试将文本解释为 HTML 标记,所以转义是没有必要的。
我刚刚了解用于 XSS 预防的 OWASP ESAPI,我正在我的应用程序中使用 Javascipt version
根据 XSS 预防作弊 sheet 中的 Rule #2,您应该 "Attribute Escape" 在将不受信任的数据插入 "width, name or value" 之类的属性值之前,我正在片刻。
然而,在将电子邮件地址作为联系表单中输入字段的值插入之前使用 encodeForHTMLAttribute() 时,@ 符号显示为对应的 html 实体 #&x40 ;如您在此屏幕截图中所见:
我是不是漏掉了什么?
我已确保文档的字符集设置为 utf-8,如下所示:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
我还在 accept-charset="UTF-8" 属性中添加了相关表单。
HTML形式:
<form id="contact_form" name="contact_form" method="post" action="" accept-charset="UTF-8">
<div class="form_divider">
<label for="contact_form_email"><span class="asterisk">*</span>E-mail</label>
<input id="contact_form_email" tabindex="1" name="contact_form_email" type="email" data-role="none" maxlength="254" placeholder="E-mail" required/>
</div><!--/form_divider-->
</form>
我的应用的数据流:
在我的应用程序中,执行 ajax 请求以提交登录详细信息,在身份验证成功后,我从数据库中检索他们的详细信息并以 json 格式发送回客户端。 然后我将这些详细信息插入到 html 适用的地方
Javascript:
$.ajax({
url: app_root_url + 'login_registration/user_processing.php',
data: JSON.stringify(params),
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function(data){
//data will be the user details such as userID, display name, email
var result = data;
prepareAppAfterLogin(result);
}
});//end ajax
function prepareAppAfterLogin(result){
var userDetails = result.userDetails;
generateUserProfilePage(userDetails);
}
function generateUserProfilePage(userDetails){
var userID = userDetails.userID;
var display_name = userDetails.display_name;
var email_address = userDetails.email_address;
var profile_image = userDetails.profile_image;
$("#profile_pic").attr('src', profile_image);
var safe_email_address_for_html = $ESAPI.encoder().encodeForHTML(email_address);
$("#profile_email_address").html(safe_email_address_for_html);
var safe_email_for_attribute = $ESAPI.encoder().encodeForHTMLAttribute(email_address);
$("#hidden_input_for_email").val(safe_email_for_attribute);
$("#contact_form_email").val(safe_email_for_attribute);
}
编辑: 我刚刚找到了支持@Cheran Shunmugavel 在他下面的评论中所说内容的文档: 这里的规则#2:https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet "It is important to note that when setting an HTML attribute which does not execute code, the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up."..."The appropriate encoding to use would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag."
遵循此规则,我刚刚尝试对 Javascript 的电子邮件地址进行编码,而不是对 html 属性进行编码,如下所示:
function generateUserProfilePage(userDetails){
var safe_email_address = $ESAPI.encoder().encodeForJavaScript(userDetails.email_address);
$("#contact_form_email").val(safe_email_address);
}
然而,这样做似乎也会产生显示问题:
你能确认我需要javascript编码吗?谢谢
如果您使用 jQuery val
方法在文档中插入数据,则不需要 encodeForHTML 和 encodeForHTML 属性函数。我在官方文档中找不到任何内容,但对这个 Whosebug 问题有很好的解释:Do jQuery's val() and prop() methods html-escape values?。重要的是 val
在内部设置 DOM value
属性,并且 属性 永远不会尝试将文本解释为 HTML 标记,所以转义是没有必要的。