flawopen.com/크로스 사이트 스크립팅/Python

크로스 사이트 스크립팅 in Python

높음 CWE-79 Draft — pending review
Language: English Português (Brasil) Español Français Deutsch Русский 简体中文 日本語 हिन्दी 한국어 Bahasa Indonesia
쉬운 설명 (ELI5)

Imagine a guestbook where a visitor writes a note containing hidden script tags that execute in the browser of the next person who opens the page.

이 페이지의 주요 용어
output encoding
Converting special markup characters into safe entities so browsers display them as text rather than script.

발생 원인

크로스 사이트 스크립팅 in Python occurs when untrusted input is formatted directly into HTML strings instead of using template auto-escaping.

실제 피해 사례

In 2005, the Samy XSS worm infected over 1 million user profiles on MySpace in under 20 hours, forcing the platform offline.

Documented historical AppSec case study.

취약한 코드 vs 수정된 코드

VULNERABLE
# username inserted with no escaping
@app.route("/welcome")
def welcome():
  name = request.args.get("name")
  return f"
Welcome, {name}
"
FIXED
# Jinja2 template auto-escapes by default
@app.route("/welcome")
def welcome():
  name = request.args.get("name")
  return render_template("welcome.html", name=name)

수정 방법이 동작하는 이유

Template engines like Jinja2 and Django auto-escape HTML entities by default, rendering markup inert.

언어별 주요 주의사항

Django mark_safe() and Jinja2 | safe

Marking untrusted data as safe disables all escaping, reopening stored and reflected XSS.

f-strings bypass templates

Python f-strings execute before template rendering, bypassing auto-escaping entirely.

흔한 오해

"f-strings are safe inside templates"

f-strings evaluate before template escaping happens, leaving user input completely raw.

취약점 확인 방법

grep -rn "render_template_string(.*f\"" --include="*.py" . grep -rn "mark_safe(" --include="*.py" .
Run Bandit with rule B701 (jinja2_autoescape_false) in CI to catch unescaped templates automatically.

예방 체크리스트

자주 묻는 질문 (FAQ)

Does Flask escape by default?

Yes, for .html, .htm, and .xml template extensions rendered with render_template().

참고 자료

참고할 취약점: SQL Injection Command InjectionPath Traversal