簡體   English   中英

在插入INTO MySql並從MySql獲取到屏幕時的PHP-MySql安全方法

[英]Php-MySql Security approach while INSERT’ing INTO MySql & fetching from MySql to screen

我的方法同時插入到MySql

我想我在stackoverflow.com上讀到“如果您需要轉義或類似的操作,請及時進行操作”,因此在驗證頁面中我會驗證用戶輸入(是否為空檢查,長度檢查和結構檢查(例如:郵件結構,自定義標簽結構);我使用$_POST['']變量作為輸入。在驗證過程中,即使在自定義錯誤打印部件中,我的錯誤消息中也不包含任何$_POST['']值文本。

作為臨時說明:在php-mysql交互過程中,我使用准備好的語句和參數化的查詢 輸入是否經過驗證; 在插入INTO MySql之前,我將標簽從輸入中剝離,因為我不允許除自定義結構化標簽之外的任何html標簽。 (例如**bold text** === <strong>bold text</strong> ),然后將用戶輸入插入MySql db。

從MySql提取並將輸出打印到屏幕時的“我的方法”

我只應用htmlspecialchars()命令從MySql db打印到屏幕

我的問題

我不確定自己。 我的方法是否有明顯或隱藏的弱點? 在此先感謝php gurus的寶貴意見。 BR

更新

在插入MySql db的過程中,我不會剝離標簽。 由於某些原因,請參閱下面的ÁlvaroG.Vicario的注釋。 BR。

到目前為止,討論的主題是如何避免SQL注入和持久性跨站點腳本。 聽起來您在正確的軌道上。

  • 使用准備好的語句是打擊SQL注入的“最佳實踐”。
  • htmlspecialchars()是防止XSS的一個很好的開始,但是您必須使用適合於輸出數據的編碼方案對數據進行轉義。 OWASP的綜合頁面對此進行了討論: XSS(跨站點腳本)預防速查表 簡短的答案:確保您正在使用“ the escape syntax for the part of the HTML document you're putting untrusted data into. ”。

我在trial.php中嘗試了以下代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="tr"> 
<head> 
<title>trial</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<meta http-equiv="Content-Language" content="tr" /> 
<meta name="Description" content="trial page." /> 
<meta name="Robots" content="noindex, nofollow" />
</head>
<body>
<?php
$str1 = '<script>alert(\'inside of input with quote\')</script>';
$str2 = '<script>alert("inside of input with quote")</script>';
$str3 = "<script>alert(\"inside of input with quote\")</script>";
$str4 = '<script>alert("outside of form")</script>';
?>
<form method="post" action="">
    <fieldset>
        <legend>alert attempts inside form inputs</legend>
        <label for="input1">label 1:</label><br />
        <input type="text" value="<?php echo htmlspecialchars($str1) ; ?>" name="input1" id="input1" /><br /><br />

        <legend>attempt 2</legend>
        <label for="input2">label 2:</label><br />
        <input type="text" value="<?php echo htmlspecialchars($str2) ; ?>" name="input2" id="input2" /><br /><br />

        <label for="input3">Label 3:</label><br />
        <input type="text" value="<?php echo htmlspecialchars($str3) ; ?>" name="input3" id="input3" />
    </fieldset>
</form>    
<?php echo htmlspecialchars($str4) ; ?>
</body>
</html>

trial.php的結果

  1. 用戶關心的輸入內容沒有中斷,因此用戶數據沒有損壞
  2. 我在屏幕上看到的正是用戶關心的內容
  3. 腳本警報不起作用

插入到MySQL中並從MySQL中提取並將輸出打印到屏幕時生成的過程

  1. 在php-mysql互動期間利用准備好的語句和參數查詢。 在INSERT'S INTO MySQl db時,此方法已經防止了SQL注入,因此不需要額外的轉義。 strip_tags()這樣的額外工作將破壞用戶確切關心的鍵入內容,就像htmlspecialchars()htmlentities()一樣。
  2. 從MySQL資料庫擷取資料時,也要利用准備好的陳述式和參數查詢
  3. 在屏幕上打印用戶輸入的數據時,請使用htmlspecialchars()以便用戶不會打擾的內容不會中斷,並且可能的惡意特殊字符將轉換為相應的HTML實體

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM