Files
2nd/10_Wiki/Topics/Coding/Security_Output_Encoding_XSS.md
T
2026-05-09 21:08:02 +09:00

4.2 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
security-output-encoding-xss Output Encoding & XSS 방지 Coding draft B conceptual 2026-05-09 2026-05-09
security
xss
encoding
csp
vibe-coding
language applicable_to
TypeScript / React / Express
Web
HTML escape
dangerouslySetInnerHTML
CSP
sanitize

Output Encoding & XSS

XSS 의 답은 어떤 컨텍스트(HTML body / attribute / JS / URL / CSS)에 출력하느냐에 따라 인코딩이 달라진다. React 가 디폴트로 안전하지만 dangerouslySetInnerHTML / href 등에서 구멍.

📖 핵심 개념

3 종류 XSS:

  • Stored: 서버 DB 에 저장된 악성 스크립트 → 다른 사용자 화면에 실행.
  • Reflected: URL / 입력 즉시 페이지에 echo.
  • DOM-based: 클라이언트 JS 가 사용자 입력을 안전하지 않게 DOM 에 삽입.

💻 코드 패턴

React 디폴트는 안전

const name = userInput; // <script>alert(1)</script>
return <div>{name}</div>;
// 자동 escape. <script> 가 텍스트로 표시됨.

dangerouslySetInnerHTML — DOMPurify

import DOMPurify from 'dompurify';

function RichContent({ html }: { html: string }) {
  const safe = DOMPurify.sanitize(html, {
    ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'p', 'br', 'ul', 'ol', 'li'],
    ALLOWED_ATTR: ['href'],
  });
  return <div dangerouslySetInnerHTML={{ __html: safe }} />;
}

URL — 스킴 검증

function safeHref(url: string): string {
  try {
    const u = new URL(url, location.origin);
    if (!['http:', 'https:', 'mailto:'].includes(u.protocol)) return '#';
    return u.toString();
  } catch { return '#'; }
}

<a href={safeHref(userUrl)}>...</a>
// javascript: 같은 스킴 차단

Server-side template — 한국어 emoji 등 escape 정확히

// 서버에서 HTML 만들 때
import escapeHtml from 'escape-html';
const html = `<h1>${escapeHtml(name)}</h1>`;
// <, >, ", ', & 만 escape

CSP (Content Security Policy)

app.use((req, res, next) => {
  res.setHeader('Content-Security-Policy',
    "default-src 'self'; " +
    "script-src 'self' 'nonce-" + nonce + "'; " +
    "style-src 'self' 'unsafe-inline'; " +
    "img-src 'self' data: https:; " +
    "connect-src 'self' https://api.example.com; " +
    "frame-ancestors 'none'; " +
    "base-uri 'self'; " +
    "form-action 'self'"
  );
  next();
});

JSON in HTML (SSR)

// ❌ XSS 가능
<script>const data = ${JSON.stringify(data)};</script>
// data 안에 </script> 있으면 탈출

// ✅ 별도 element
<script type="application/json" id="boot-data">{JSON.stringify(data)}</script>
// Client 에서: JSON.parse(document.getElementById('boot-data').textContent)

🤔 의사결정 기준

컨텍스트 인코딩
HTML body 텍스트 escape <>&"'
HTML attribute (title="...") escape + 따옴표로 감싸기
URL attribute (href, src) URL encode + 스킴 검증
JS string literal JSON.stringify + closing tag escape
CSS value CSS escape
Markdown / Rich text 받을 때 sanitizer (DOMPurify, sanitize-html)

안티패턴

  • dangerouslySetInnerHTML 없이 sanitize: 검증 안 된 HTML 직접 주입.
  • 사용자 입력 URL 을 href 그대로: javascript:alert(1) 가능.
  • eval / Function / new Function: 사용자 입력이 들어가면 RCE.
  • innerHTML = userInput: 클래식 XSS.
  • CSP 없이 inline script + nonce 안 씀: 모든 inline 통과.
  • CSP 의 unsafe-inline / unsafe-eval: CSP 의미 절반 잃음. 마이그레이션 가이드 따라 nonce / hash 로.
  • 서버 검증만 + 클라이언트 출력 인코딩 누락: client-side rendered 변수가 raw HTML. React 디폴트 safe 라 보통 OK 지만 React 외 환경 주의.
  • 이메일/SMS 본문에 사용자 입력 그대로: 다른 종류 인젝션 (SMTP header, link rewriting).

🤖 LLM 활용 힌트

  • React 외 / SSR template / dangerouslySetInnerHTML / URL 컨텍스트에서 sanitizer 명시.
  • CSP 헤더는 helmet 라이브러리 권장.

🔗 관련 문서