javascript
  • jsp
  • xss
  • cross-site
  • 2013-07-18 105 views 0 likes 
    0

    我在同一個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; 
        } 
    

    所以我認爲編碼鏈接veriable使用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; 
    

    感謝您的幫助!

    回答

    0

    你需要一塊使用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解釋。以這種方式傳遞惡意用戶輸入字符串是完全正確的,如果網站在收錄時沒有保護它,它最終會返回到您網站的某個頁面上,這會非常惡毒。

    +0

    感謝您的幫助。那我該如何預防XSS?我想使用ESAPI.encoder()。encodeForURL(String str),但我無法將鏈接變量作爲ESAPI方法的字符串參數傳遞。它說鏈接無法解決。我嘗試過使用ESAPI parent.window.open(ESAPI.encoder()。encodeForURL(link.toString()),'detail','width = 640,height = 480,toolbar = no,location = no,directories = no ,狀態=是,菜單欄=沒有,滾動條=是,可調整大小=是,alwaysRaised =是); –

    相關問題