수업소개
공격의 의도를 가진 자바스크립트 코드를 입력해서 이 코드를 웹 브라우저로 실행할 때 공격목적을 달성하는 공격 기법을 Cross site scripting (XSS) 이라고 합니다. 이를 막는 방법을 살펴보겠습니다.
강의
소스코드
author.js
var db = require('./db'); var template = require('./template.js'); var qs = require('querystring'); var url = require('url'); var sanitizeHtml = require('sanitize-html'); exports.home = function(request, response){ db.query(`SELECT * FROM topic`, function(error,topics){ db.query(`SELECT * FROM author`, function(error2,authors){ var title = 'author'; var list = template.list(topics); var html = template.HTML(title, list, ` ${template.authorTable(authors)} <style> table{ border-collapse: collapse; } td{ border:1px solid black; } </style> <form action="/author/create_process" method="post"> <p> <input type="text" name="name" placeholder="name"> </p> <p> <textarea name="profile" placeholder="description"></textarea> </p> <p> <input type="submit" value="create"> </p> </form> `, `` ); response.writeHead(200); response.end(html); }); }); } exports.create_process = function(request, response){ var body = ''; request.on('data', function(data){ body = body + data; }); request.on('end', function(){ var post = qs.parse(body); db.query(` INSERT INTO author (name, profile) VALUES(?, ?)`, [post.name, post.profile], function(error, result){ if(error){ throw error; } response.writeHead(302, {Location: `/author`}); response.end(); } ) }); } exports.update = function(request, response){ db.query(`SELECT * FROM topic`, function(error,topics){ db.query(`SELECT * FROM author`, function(error2,authors){ var _url = request.url; var queryData = url.parse(_url, true).query; db.query(`SELECT * FROM author WHERE id=?`,[queryData.id], function(error3,author){ var title = 'author'; var list = template.list(topics); var html = template.HTML(title, list, ` ${template.authorTable(authors)} <style> table{ border-collapse: collapse; } td{ border:1px solid black; } </style> <form action="/author/update_process" method="post"> <p> <input type="hidden" name="id" value="${queryData.id}"> </p> <p> <input type="text" name="name" value="${sanitizeHtml(author[0].name)}" placeholder="name"> </p> <p> <textarea name="profile" placeholder="description">${sanitizeHtml(author[0].profile)}</textarea> </p> <p> <input type="submit" value="update"> </p> </form> `, `` ); response.writeHead(200); response.end(html); }); }); }); } exports.update_process = function(request, response){ var body = ''; request.on('data', function(data){ body = body + data; }); request.on('end', function(){ var post = qs.parse(body); db.query(` UPDATE author SET name=?, profile=? WHERE id=?`, [post.name, post.profile, post.id], function(error, result){ if(error){ throw error; } response.writeHead(302, {Location: `/author`}); response.end(); } ) }); } exports.delete_process = function(request, response){ var body = ''; request.on('data', function(data){ body = body + data; }); request.on('end', function(){ var post = qs.parse(body); db.query( `DELETE FROM topic WHERE author_id=?`, [post.id], function(error1, result1){ if(error1){ throw error1; } db.query(` DELETE FROM author WHERE id=?`, [post.id], function(error2, result2){ if(error2){ throw error2; } response.writeHead(302, {Location: `/author`}); response.end(); } ) } ); }); }
template.js
var sanitizeHtml = require('sanitize-html'); module.exports = { HTML:function(title, list, body, control){ return ` <!doctype html> <html> <head> <title>WEB1 - ${title}</title> <meta charset="utf-8"> </head> <body> <h1><a href="/">WEB</a></h1> <a href="/author">author</a> ${list} ${control} ${body} </body> </html> `; },list:function(topics){ var list = '<ul>'; var i = 0; while(i < topics.length){ list = list + `<li><a href="/?id=${topics[i].id}">${sanitizeHtml(topics[i].title)}</a></li>`; i = i + 1; } list = list+'</ul>'; return list; },authorSelect:function(authors, author_id){ var tag = ''; var i = 0; while(i < authors.length){ var selected = ''; if(authors[i].id === author_id) { selected = ' selected'; } tag += `<option value="${authors[i].id}"${selected}>${sanitizeHtml(authors[i].name)}</option>`; i++; } return ` <select name="author"> ${tag} </select> ` },authorTable:function(authors){ var tag = '<table>'; var i = 0; while(i < authors.length){ tag += ` <tr> <td>${sanitizeHtml(authors[i].name)}</td> <td>${sanitizeHtml(authors[i].profile)}</td> <td><a href="/author/update?id=${authors[i].id}">update</a></td> <td> <form action="/author/delete_process" method="post"> <input type="hidden" name="id" value="${authors[i].id}"> <input type="submit" value="delete"> </form> </td> </tr> ` i++; } tag += '</table>'; return tag; } }
template.js
var db = require('./db'); var template = require('./template.js'); var url = require('url'); var qs = require('querystring'); var sanitizeHtml = require('sanitize-html'); exports.home = function(request, response){ db.query(`SELECT * FROM topic`, function(error,topics){ var title = 'Welcome'; var description = 'Hello, Node.js'; var list = template.list(topics); var html = template.HTML(title, list, `<h2>${title}</h2>${description}`, `<a href="/create">create</a>` ); response.writeHead(200); response.end(html); }); } exports.page = function(request, response){ var _url = request.url; var queryData = url.parse(_url, 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 title = topic[0].title; var description = topic[0].description; var list = template.list(topics); var html = template.HTML(title, list, ` <h2>${sanitizeHtml(title)}</h2> ${sanitizeHtml(description)} <p>by ${sanitizeHtml(topic[0].name)}</p> `, ` <a href="/create">create</a> <a href="/update?id=${queryData.id}">update</a> <form action="delete_process" method="post"> <input type="hidden" name="id" value="${queryData.id}"> <input type="submit" value="delete"> </form>` ); response.writeHead(200); response.end(html); }) }); } exports.create = function(request, response){ db.query(`SELECT * FROM topic`, function(error,topics){ db.query('SELECT * FROM author', function(error2, authors){ var title = 'Create'; var list = template.list(topics); var html = template.HTML(sanitizeHtml(title), list, ` <form action="/create_process" method="post"> <p><input type="text" name="title" placeholder="title"></p> <p> <textarea name="description" placeholder="description"></textarea> </p> <p> ${template.authorSelect(authors)} </p> <p> <input type="submit"> </p> </form> `, `<a href="/create">create</a>` ); response.writeHead(200); response.end(html); }); }); } exports.create_process = function(request, response){ var body = ''; request.on('data', function(data){ body = body + data; }); request.on('end', function(){ var post = qs.parse(body); db.query(` INSERT INTO topic (title, description, created, author_id) VALUES(?, ?, NOW(), ?)`, [post.title, post.description, post.author], function(error, result){ if(error){ throw error; } response.writeHead(302, {Location: `/?id=${result.insertId}`}); response.end(); } ) }); } exports.update = function(request, response){ var _url = request.url; var queryData = url.parse(_url, true).query; db.query('SELECT * FROM topic', function(error, topics){ if(error){ throw error; } db.query(`SELECT * FROM topic WHERE id=?`,[queryData.id], function(error2, topic){ if(error2){ throw error2; } db.query('SELECT * FROM author', function(error2, authors){ var list = template.list(topics); var html = template.HTML(sanitizeHtml(topic[0].title), list, ` <form action="/update_process" method="post"> <input type="hidden" name="id" value="${topic[0].id}"> <p><input type="text" name="title" placeholder="title" value="${sanitizeHtml(topic[0].title)}"></p> <p> <textarea name="description" placeholder="description">${sanitizeHtml(topic[0].description)}</textarea> </p> <p> ${template.authorSelect(authors, topic[0].author_id)} </p> <p> <input type="submit"> </p> </form> `, `<a href="/create">create</a> <a href="/update?id=${topic[0].id}">update</a>` ); response.writeHead(200); response.end(html); }); }); }); } exports.update_process = function(request, response){ var body = ''; request.on('data', function(data){ body = body + data; }); request.on('end', function(){ var post = qs.parse(body); db.query('UPDATE topic SET title=?, description=?, author_id=? WHERE id=?', [post.title, post.description, post.author, post.id], function(error, result){ response.writeHead(302, {Location: `/?id=${post.id}`}); response.end(); }) }); } exports.delete_process = function(request, response){ var body = ''; request.on('data', function(data){ body = body + data; }); request.on('end', function(){ var post = qs.parse(body); db.query('DELETE FROM topic WHERE id = ?', [post.id], function(error, result){ if(error){ throw error; } response.writeHead(302, {Location: `/`}); response.end(); }); }); }