簡體   English   中英

將mysql where子句列名稱和運算符傳遞給PHP ajax腳本是否安全?

[英]is it safe to pass mysql where clause column names and operators to PHP ajax script

我正在嘗試構建一種健壯,簡單,安全,全面的方法,以通過ajax將動態SQL查詢傳遞給php腳本。 我認為我還沒有想出贏家,但是我現在看來可以為我的應用程序工作。 我希望在我的Web服務器上只有一個php腳本,該腳本可以使用輸入變量來構建SQL查詢,執行查詢並將結果返回到網站。 執行查詢並返回結果不是問題。 問題是將這些輸入變量傳遞到腳本的正確且安全的方法。

我最大的困惑是如何產生WHERE子句,但仍然能夠考慮所有可能的WHERE子句。

假設服務器上的PHP文件稱為master.php

從Javascirpt我有這個:

$.ajax({
  type: "POST",
  url: "master.php",
  dataType: "xml",
  data: { 
      schema: "the_schema", 
      table: "the_table",
      select: JSON.stringify(['col_name_1','col_name_2','...']),
      where: JSON.stringify(["status","!=","Some Status","and","STR_TO_DATE(date,","%m/%d/%Y",")",">","(date(now())","-","INTERVAL","30","day)"])
  },
  success: function (data){
      alert(data);
      // of course here I will actually do something with the data, this is just for illustration of how the data is returned to the web page.
  }
});

data變量將是來自數據庫的XML數據,其字符串為:“ failure:error msg”。

在服務器上的master.php腳本中,我有以下內容:

<?php
    include '/config_file.php'; //checks user permissions and establishes mysqli connection

    /*
     * Table and schema are required 
     */
    $table = $mysqli->real_escape_string($_POST['table']);
    $schema = $mysqli->real_escape_string($_POST['schema']);

    /*
     * Select section
     * if there is no select then just use *
     */
    if (isset($_POST['select'])){
        $select_string = 'select ';
        $select = json_decode($_POST['select']);
        foreach($select as $key => $value){
            $select_string .= '`'.$mysqli->real_escape_string($value).'`, ';
        }
    }else{
        $select_string = 'select * ';
    }

    /*
     * Where section
     */
    if (isset($_POST['where'])){
        $where = json_decode($_POST['where']);
    }

    if (isset($where)){
        $where_string = ' where ';
        foreach ($where as $key => $value){
            if (strpos($value, ' ') !== false or strpos($value, '%') !== false){
                // if there is a space or % in the value, it must be a string so enclose it in quotes
                $where_string .= '"'.$mysqli->real_escape_string($value).'" ';
            }else{
                $where_string .= $mysqli->real_escape_string($value).' ';
            }
        }
        $where_string = rtrim($where_string);
    }

    if(!isset($where_string)) $where_string = '';

    /*
     * End of Where Section
     */


    /*
     * Build SQL
     */
    $SQL = $select_string.'from '.$schema.'.'.$table.$where_string;

    //...continue to execute query and echo results
?>

如您所見, where數組的每個部分都被轉義並添加到查詢中,甚至包括列名和運算符(=,!=,>,<等)。

另外,正如您所看到的那樣,它們似乎有點瘋狂,我正在為where子句的這一部分的每個部分傳遞單獨的字符串-> and STR_TO_DATE(date, "%m/%d/%Y" ) > (date(now()) - INTERVAL 30 day)

如果你們都有

  1. 將動態where子句傳遞給ajax腳本的經驗
  2. 如果你有更好的主意
  3. 看到腳本中的安全漏洞

請告訴我。 希望我們能夠獲得一個不錯的,基於腳本的通用數據。

謝謝!

我個人不建議您使用這種方法在javascript中公開數據庫列名稱,這是完全危險的。

一個簡單的例子是,您可以在瀏覽器級別操縱ajax參數,然后對數據庫執行它。 您只是使黑客能夠獲取有關您的數據的更多信息。

例如,我可以停止where關閉,以便通過執行不帶where參數的調用可以查看數據庫中的所有數據。 如果要開發這樣的動態查詢,請確保在ajax傳遞的參數和實際表列之間使用某些映射,以使這些列不會直接暴露給黑客。

那就是我可以在此腳本中看到的基本安全問題。

希望這可以幫助。

謝謝。

暫無
暫無

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

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