select * from table where id = 1
`select * from table where id = ${id};` `1 or 1 = 1` `select * from table where id =1 or 1 =1;`
select * from user where username = '${data.username}' and password = '${data.password}' 1 'or '1'='1 select * from user where username = 'username' and password = '1' or '1'='1'
SQL 的注入本质是将数据变成了具备逻辑的程序javascript
select * from table where id="10" and 1=0 select * from table where id="10" and 1=1 select * from table where id="10" and mid(version(),1,1)=5--猜数据库的版本 select 1,2,3 from table select id,1,2,3 from table select * from table union select 1,2,3 from table2--猜字段个数 select * from table where min(username,1,1)="t"--猜用户名
console.log("[/site/post] error:", e.message, e.stack); ctx.body = { status: -1, body: "出错了" };
let id = ctx.parmas.id; id = parseInt(id, 10);
const post = await query{ `select * from post where id =${connecttion.escape(id)}`//escape进行转义 // 有的时候支持下面这种操做 `select * from post where id = ?`, [id] }
npm install mysql2
这个时候就要改一下引入的 mysql 库,还有 queryjava
const query = bluebird.promisify( connection.execte.bind(connectionModel).getConnection() ); //原来是 const query = bluebird.promisify( connection.query.bind(connectionModel).getConnection() );
npm install sequelize --save
初始化 ORM 实例mysql
var Sequelize = require("sequelize"); var sequelize = new Sequelize({ host: "localhost", database: "safety", username: "root", define: { freezeTableName: ture } }); module.exports = sequelize;
处理数据表sql
var sequelize = require("./sequelize"); var Sequelize = require("sequelize"); var Post = sequelize.define( "post", { id: { type: Sequelize.INTERGER, primaryKey: ture }, title: Sequelize.STRING(256), imgUrl: Sequelize.STRING(256), content: Sequelize.TEXT }, { tableName: "post" } ); module.export = Post;
查询操做数据库
let post = await Post.findById(id); let comment = await Comment.findAll({ where: { postId: post.id } });
看一段 nosql 代码npm
var mongoose = require('mongoose'); login async function(ctx) { var username = ctx.request.body.username; var password = ctx.request.body.password; mongoose.findOne({ username: username, password: password }) }
看似没有什么问题,实际上是有问题的,nosql
好比:{"name":"user""password""{"$gt":0}}
async
这样密码当密码大于 0 时就能够进行登陆,也就是任意密码都行,固然用户名也是能够这样操做的mongoose
跟关系型同样,从这几方面入手post