2010-12-19 44 views
0

嘿,我正面臨着一個相當嚴重的安全錯誤。讓我先概述一下我的代碼。編輯評論 - javascript,php一起工作

<li class="comment"> 
    <form action="" method="POST" name="edit-form" class="edit-area"> 
     <textarea style="width: 100%; height: 150px;"><?php echo $response->comment; ?></textarea> 
    </form> 

    <div class="comment-area" style="padding-top: 2px"><?php echo (parseResponse($response->comment)); ?></div> 

     <p class="ranking"> 
      <?php if ($response->user_id == $user_id) : ?> 
        <a href="" class="editting" data-user="<?php echo md5(convert($response->user_id)); ?>" data-edit="<?php echo $response->short; ?>">Edit</a> &bull; <a href="#d">Delete</a> 
      <?php else : ?> 
       <a href="#">Like (<?php echo $response->likes; ?>)</a> &bull; <a href="#">Dislike (<?php echo $response->dislikes; ?>)</a> 
      <?php endif; ?> 
     </p>           
</li> 

是我在我的身上了,和這裏的相關JS

$('.editting').bind('click', function(event) { 
      var num = $(this).data('edit'); 
      var user = $(this).data('user'); 

      if ($(this).hasClass('done')) { 

       var newComment = $('#comment-' + num + ' .edit-area textarea').val(); 
       var dataString = 'newComment='+ newComment + '&num=' + num; 

       if(newComment == '') 
       { 
        alert('Comment Cannot Be Empty!'); 
       } 
       else 
       { 
        $.ajax({ 
         type: "POST", 
         url: "edit.php", 
         data: dataString, 
         success: function(){} 
        }); 

        $('#comment-' + num + ' .edit-area').slideDown('slow', function() { 
        $('#comment-' + num + ' .edit-area').addClass('invisible'); 
        });  
        $('#comment-' + num + ' .comment-area').slideUp('slow', function() { 
         $('#comment-' + num + ' .comment-area').removeClass('invisible'); 
        }); 
        $(this).removeClass('done'); 
        $(this).html('Edit'); 
       } 



      } 

      else {     
       $('#comment-' + num + ' .comment-area').slideDown('slow', function() { 
        $('#comment-' + num + ' .comment-area').addClass('invisible'); 
       }); 

       $('#comment-' + num + ' .edit-area').slideUp('slow', function() { 
        $('#comment-' + num + ' .edit-area').removeClass('invisible'); 
       }); 

       $(this).html('Done'); 
       $(this).addClass('done'); 


      } 

      return false; 


     }); 

,工作正常,但我有一個問題。如果用戶發現評論(而不是他們)並使用像螢火蟲一樣的插件,他們可以用另一個替換response-> short,並編輯任何評論。當然,在edit.php中,我可以檢查響應表中的短小內容,看看用戶是否簽出,但是我想找到一種不顯示文本區域的方式,除非該用戶確定該響應。

這可能嗎?

由於提前, 請問

回答

3

這可能嗎?

當然......但它不會阻止用戶/修復您的安全漏洞。修復此問題檢查服務器端總是仔細檢查任何應該安全的服務器端,永遠不要相信您的輸入。用戶嘗試做一些惡意的事情不會被JavaScript中的任何東西攔截......發送數據到你的服務器,他們不應該是剛好他們會先做什麼。

+0

好,我打算做的事情edit.php反正所以我確定它是有效的,但是無論如何我還可以強化js/ – willium 2010-12-19 10:03:21

+0

@tap - 沒有太多的事情你還沒有做,JavaScript是**開放的,這只是它是怎麼回事......我可以打開一個編輯窗口,對這個頁面的任何評論以及... *服務器*不會允許它,這是重要的。回到這裏,看看你正在嘗試做什麼......你試圖讓這個界面更好一些,以幫助某人嘗試*操縱你的網站,我不會做任何額外的事情爲此,只需在服務器端確保它的安全性,爲其他99.9%的用戶設計接口(並且仍然安全,重要!) – 2010-12-19 10:07:03

+0

好的!謝謝尼克! – willium 2010-12-19 10:10:42

0

像尼克說的那樣;永遠不要相信JavaScript測試!

它可能適用於「普通用戶」,但是當它涉及到避免黑客入侵時,您還可以要求黑客單擊按鈕來「證明」他的輸入是有效的!

您的驗證腳本被別人的電腦上運行,那麼他/她就可以操縱它(甚至把它使用的NoScript等)