본문 바로가기
WEB/Nodejs

[Nodejs] 보안 - path.parse(), sanitize-html

by Ellen571 2020. 8. 5.

[생활코딩] App - 입력 정보에 대한 보안

[생활코딩] App - 출력 정보에 대한 보안

 

 

파일 탐색으로 인한 정보 유출

[예]

- 개인정보를 저장하는 파일 password.js

- 사용자가 http://abc.com/?id=../password.js 접속한다면

- fs.readFile('data/${queryData.id}', 'utf8', function(){}); 함수로 인해

- data/../password.js를 탐색하게됨

- ../는 상위 디렉토리를 가리키게 되고

- 상위 디렉토리인 data폴더와 같은 위치에 있는 password.js이 탐색되면서 개인정보가 유출됨

- 파일탐색 정보유출 막기 위한 경로 세탁 필요

 

경로 분석하기 path.parse(path)

 

var path = require('path');
var pathParse = path.parse('../password.js');
console.log(pathParse);

 

[결과]

{
  root : '',
  dir : '..', // 디렉토리
  base : 'password.js', // 파일명.확장자
  ext : '.js', // 확장자
  name : 'password' // 파일명
}

 

경로 세탁하기

 

var path = require('path');

var filteredId = path.parse(queryData.id).base;
fs.readFile('data/${filteredId}', 'utf8', function(){
	// 내용
}); 

 

- http://abc.com/?id=../password.js 으로 접속하면

- base로 필터링하여 password.js만 남고 data/password.js를 탐색하게 됨

 

 

 

오염된 정보로 인한 위험 노출

- 글쓰기로 <script>오염된 정보</script> 등록하면 해당 링크로 접속한 사용자에게 오염된 정보가 노출됨

[예]

- <script> location.href='http://aaa.com'; </script> 를 등록해 페이지 접속시 사용자를 다른 사이트로 이동하게 함

 

코드 소독하기 sanitize-html

sanitize-html 설치

- npm init

- 설치가 되면 프로젝트 정보가 담긴 package.json파일이 생성됨

- npm install -S sanitize-html

- sanitize-html 디렉토리 생성됨  -  node_module/sanitize-html

- pachage.json에 의존하고 있는 모듈로 등록됨 dependencies : { "sanitize-html": "1.00.00"}

 

sanitize-html 사용하기

 

var sanitizeHtml = require('sanitize-html');

fs.readFile('data/${filteredId}', 'utf8', function(err, description){
// 오염될 수 있는 정보는 입력된 내용인 title과 description

  var title = queryData.id;
  
  var sanitizedTitile = sanitizeHtml(title);
  var sanitizedDesc = sanitizeHtml(description);
});

 

- 등록 시 script태그를 삭제함

 

특정 태그는 허용하기 allowedTags

 

var sanitizedDesc = sanitizeHtml(description, allowedTags : {'h1', 'b', 'em'});
반응형

'WEB > Nodejs' 카테고리의 다른 글

[Nodejs] API  (0) 2020.08.05
[Nodejs] Module  (0) 2020.08.04
[Nodejs] 객체지향 프로그래밍  (0) 2020.08.04
[Nodejs] Function  (0) 2020.08.04
[Nodejs] Array/Object  (0) 2020.08.04