javascript
  • sql-server
  • node.js
  • prepared-statement
  • sql-injection
  • 2017-09-13 103 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语句,将这些参数的实际值绑定到请求,然后执行你的请求。

    相关问题