简体   繁体   English

node.js中的变量查询

[英]Variable queries in node.js

I am trying to modify a query on my server side (node.js) that looks like this: 我正在尝试在服务器端(node.js)上修改如下查询:

var http = require('http');
var query = "SELECT * FROM Users WHERE Email='Test.User@TestUser.com'";

This is the hardcoded query. 这是硬编码的查询。 I'm trying to do something like this (email being a variable pulled in from an input text area in an HTML doc): 我正在尝试执行以下操作(电子邮件是从HTML文档中的输入文本区域提取的变量):

var http = require('http');
var query = "SELECT * FROM Users WHERE Email=" + "'" + email + "'";

I'm using JQuery to handle the request like this from a javascript file: 我正在使用JQuery处理来自javascript文件的请求,如下所示:

$.ajax({
        url: "http://127.0.0.1:8000/",
        type: "POST",
        success: function(dataRcvd) {
            alert(dataRcvd);
        }

If I figure out how to do this I can dynamically update the var query with different SQL queries/stored procedures. 如果我知道如何执行此操作,则可以使用不同的SQL查询/存储过程来动态更新var query

// server.js
var http = require('http');
function handler(req, res){
  console.log('Server got '+req.body.email);
  var query = "SELECT * FROM Users WHERE Email=" + "'" + req.body.email + "'";
  someAsyncSQLCall(query, function(rows){
    res.send(200, rows);
  });
};

http.createServer(handler).listen(3000);

//client.js
$.ajax({
        url: "http://127.0.0.1:8000/",
        type: "POST",
        data: {email: 'value' },
        success: function(dataRcvd) {
            alert(dataRcvd);
        }

This is an incredibly bad idea (string concatenation); 这是一个非常糟糕的主意(字符串串联)。 anyone can post a SQL injection attack in your form and drop your database or do any number of other nefarious things. 任何人都可以以您的形式发布SQL注入攻击并删除您的数据库,或执行许多其他恶意操作。

I'm not sure what DB you're using, but most of them have a way of doing parameterized queries that sanitize you against these kinds of attacks. 我不确定您使用的是哪个数据库,但是大多数数据库都有一种执行参数化查询的方法,可以使您免受这类攻击。

Checkout node-postgres (pg module), which has examples of how to do it if you're using PostgreSQL. Checkout node-postgres(pg模块),其中包含有关使用PostgreSQL时如何执行此操作的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM