생활코딩/node.js-mysql

SQL injection

7he8oy 2021. 1. 12. 19:24

현재 내가 만든 홈페이지는 url에 있는 query 데이터를 통해서 알맞은 정보를 보여준다.

해당 부분의 코드는 다음과 같다.

exports.desc = function(request,response){
  var _url = request.url;
  var queryData = url.parse(_url,true/*true는 객체형식으로 받아옴*/).query;
  db.query(`select * from topic`, function(error,topics){
    if(error){
      throw error;
    }
    db.query(`select * from topic left join author on topic.author_id = author.id where topic.id=?`,[queryData.id], function(error2, topic){
      if (error2){
        throw error2;
      }
      var list = template.list(topics);
      var html = template.HTML(topic[0].title,`${topic[0].description}<p>by ${topic[0].name}</p>`,list,

      `
        <a href = '/create'>create</a>
        <a href = '/update?id=${queryData.id}'>update</a>
        <form action = "/delete" method = 'post'>
          <input type = 'hidden' name = 'trueid' value='${queryData.id}'>
          <input type='submit' value = 'delete'>
        </form>
      `);
      response.writeHead(200);
      response.end(html);
    });
  });
}

만약 주소창에

http://localhost:3000/?id=32;drop table topic; 과 같은 형태로 입력했을 때,

db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password:'rkfkehfl7300',
  database : 'baby'
  multipleStatements:True
});
db.connect();
module.exports = db;

multipleStatements부분이 True 값을 갖고, 

아래와 같이 ?에 들어갈 부분을 따로 표시하는 것이 아니라,


db.query(`insert into author(name, profile) values(?, ?)`, [post.name, post.profile], function(error, results){...

안에 ${...}  형태로 바로 넣는다면 

drop table topic;은 바로 실행되어 버린다.

따라서 위와 같이 ?로 표기하고 해당 값을 따로 표시하거나,

${db.excape(...)}

형태로 집어 넣는다면 drop table은 실행되지 않는다고 한다.