应该使用哪种编码

which encoding should be used

本文关键字:编码      更新时间:2023-09-26

我在同一个jsp文件中有下面的javascript函数,它根据链接中传递的参数打开一个新窗口。有人告诉我,我需要编码以防止XSS攻击。

     <script language="JavaScript">function openDocWindow(report,index,reportType) {
    link = '/ocs/jsp/single_report_frameset.jsp?      
    report_id='+index+'&id=13740995910316prfXwysgrSGk2Strm7pvxC'+
    index+'&startCount=0'+'&enclosure_id='+index;
    parent.window.open(link,'detail','width=640,height=480,toolbar=no,
    location=no,directories=no,status=yes,menubar=no,scrollbars=
   yes,resizable=yes,alwaysRaised=yes');
   return;
    }

所以我想使用encodeURIComponent()或encodeURI()对链接进行编码,但我需要知道,如果我喜欢下面的内容,它能防止XSS攻击吗?

 parent.window.open(encodeURIComponent(link),'detail','width=640,height=480,toolbar=no,
    location=no,directories=no,status=yes,menubar=no,scrollbars=
   yes,resizable=yes,alwaysRaised=yes');
   return;

谢谢你的帮助!

您需要逐个使用encodeURIComponent

function openDocWindow(report,index,reportType) {
  var link = '/ocs/jsp/single_report_frameset.jsp?report_id=' +
    encodeURIComponent(index) + 
    '&id=13740995910316prfXwysgrSGk2Strm7pvxC' +
    encodeURIComponent(index) +
    '&startCount=0&enclosure_id=' +
    encodeURIComponent(index);
    parent.window.open(link,'detail','width=640,height=480,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,alwaysRaised=yes');
    return;
}

我可能只对它进行一次编码,然后重新使用它的值;那只是为了说明。基本上,页面中可能包含URI元字符的任何内容都必须进行编码。它是分块完成的,因为你将故意引入元字符,用于它们的设计用途。

现在,这会阻止XSS吗?不,一点也不;至少,根据您对XSS的定义,只针对一小部分可能的攻击。此编码仅用于URI解释。以这种方式将恶意用户输入字符串传递回是完全可以的,如果网站在包含它时没有对其进行保护,那么当它最终返回到网站的某个页面时,它也会同样具有恶意。