javascript
  • sql-server
  • node.js
  • prepared-statement
  • sql-injection
  • 2017-09-13 47 views 1 likes 
    1

    我需要在Javascript中插入一個字符串到MSSQL表中。如何在Node.JS中爲MSSQL創建預準備語句?

    這是我到目前爲止有:

    的Javascript:

    var message = "It's a great day today!"; 
    $.post('www.server.com/message='+message, function(response){ 
    console.log(response); 
    }); 
    

    的Node.js服務器:

    //..... a bunch of code has been used to accept the HTTP request and get the message... 
    // for the purpose of this example, the message is asigned to 'NodeMsg' 
    var mssqldb = require("../core/mssql"); 
    var NodeMsg = theMessageReceivedFromHTTPRequest; 
    function saveMessage(message) { 
        mssqldb.executeMssql("insert into messages (message, status) VALUES('"+message+"', 'new')", function (err) { 
         if (err) { 
         httpMsgs.show500(req, resp, err); 
         }//end if 
         else { 
         httpMsgs.sendJson(req, resp, 'success'); 
         }//end else 
        }); 
    }; 
    

    mssql.js(node.js的文件):

    var mssqldb = require("mssql"); 
    var settings = require("../settings"); 
    
    exports.executeMssql = function(sql, callback) { 
        var conn = new mssqldb.Connection(settings.mssqlConfig); 
        conn.connect() 
        .then(function(){ 
        var req = new mssqldb.Request(conn); 
        req.query(sql) 
        .then(function (recordset) { 
         callback(recordset); 
        }) 
        .catch(function(err){ 
         console.log(err); 
         callback(null, err); 
        }); 
        }) 
        .catch(function(err){ 
        console.log(err); 
        callback(null, err); 
        }); 
    };//end executeMssql 
    

    摘要是怎麼回事的:

    1. 我定義一個消息字符串。 (請注意,它包含一個單引號)
    2. 我送該字符串Node.js的服務器
    3. 我使用Node.js的發送字符串到MSSQL

    問題: 在瞭解了更多關於面向對象的程序之後,我意識到我執行插入的方式受到高度的關注,因爲它對SQL注入是開放的。而且,由於字符串中的單引號,代碼將在執行SQL查詢期間中斷。

    解決方案: 根據我發現的大多數資源,解決方案是使用「準備好的語句」。

    我的問題:

    如何在地球上我轉換我已經做了,利用準備好的發言?我已經搜索網絡HOURS,我找不到一個很好的例子,說明如何使用Node.JS爲MSSQL(而不是MySQL)準備好的語句,這對初學者來說是易於理解的。Node.Js

    回答

    0

    如果您使用繁瑣的跨平臺MSSQL驅動程序實現,文檔如下:http://tediousjs.github.io/tedious/parameters.html

    基本上,您需要爲需要注入的值準備一個帶有'@xxx'佔位符的SQL語句,將這些參數的實際值綁定到請求,然後執行你的請求。

    相關問題