나는 아약스 (JSON) 분석과 데이터를 가져 오는거야 그 다음과 같은 템플릿에 : 다음div에 데이터를로드 할 때 XSS를 방지하는 방법은 무엇입니까?
function noteTemplate(obj) {
var date = obj.created_at.split(/[- :]/),
dateTime = date[2] + '.' + date[1] + '.' + date[0];
dateTime += ' @ ' + date[3] + ':' + date[4] + ':' + date[5];
var note = '<div class="note-container" target="' + obj.id + '">';
note += '<div class="note-options"><div class="note-remove"></div></div>';
note += '<div class="note-name">' + obj.name + '</div>';
note += '<div class="note-date">' + dateTime + '</div>';
note += '<div class="note-content">' + obj.content + '</div>';
note += '</div>';
return note;
}
그리고 사업부에 jQuery를 사용하는 것을 추가. 이제 obj.name
또는 obj.content
에 JS가 들어 있으면 실행됩니다.
이를 방지하는 가장 적절한 방법은 무엇입니까? 감사합니다
업데이트 :
나는 < 탈출>이 일을하는 것처럼 개체로 대체하여 솔루션 및 기본 방법을 발견했다.
obj.content.replace(/</g, "<").replace(/>/g, ">")
jQuery를 :
var $note = $(note);
$note.find('.note-name').text(obj.name);
$note.find('.note-content').text(obj.content);
return $note;
는 .text()는 당신을 위해 그것을 sanitazes.
@Rory McCrossan에게 감사드립니다!
나는 해결책을 찾았을 : obj.content.replace (/ g, "<") .replace (/>/g, ">") –