크로스 사이트 스크립팅(XSS) - jquery DOM
Cross-site Scripting (XSS)
Last updated
Cross-site Scripting (XSS)
Last updated
// 취약: 사용자 입력값이 DOM 조작 함수에 직접 전달
function vulnerable(userInput) {
$("#container").html(userInput); // XSS 취약점
}
// 또 다른 예시
const hash = window.location.hash;
$("div").append(hash); // XSS 가능// 안전 적용: 텍스트 출력에는 text() 사용
function safe(userInput) {
$("#container").text(userInput); // 안전
}
// 또는 HTML 이스케이프 처리 후 사용
function escapeHTML(str) {
return String(str).replace(/[&<>'"]/g, function (c) {
return {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
}[c];
});
}
function safeWithEscape(userInput) {
$("#container").html(escapeHTML(userInput)); // 이스케이프 처리 후 사용
}