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