fix: 링크 등록 SSRF 차단 — 공인 http(s) 주소만 허용, 리다이렉트도 매번 검사

2026-10-06 31.22.91.234가 /links로 169.254.169.254·127.0.0.1·file:// 등
내부 주소 12건을 시도한 기록 발견(서버 침해 사고 조사 중).
- is_public_http_url: 스킴 http/https, 해석된 모든 IP가 공인일 때만 허용
- fetch_metadata: allow_redirects=False로 리다이렉트마다 재검사(최대 5회)
- add_link: 비공인 주소는 400으로 거부
- app.run: debug=True·0.0.0.0 → debug=False·127.0.0.1 (운영은 gunicorn)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dsyoon
2026-11-06 08:42:37 +09:00
parent fe71ca516d
commit db04f25f4d

56
app.py
View File

@@ -1,5 +1,7 @@
import ipaddress
import os
import re
import socket
import time
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
@@ -68,6 +70,41 @@ def normalize_url(raw_url: str) -> str:
return f"https://{raw_url}"
def is_public_http_url(url: str) -> bool:
"""
서버가 대신 가져와도 되는 주소인지 검사한다(SSRF 방지, 2026-11-06).
http/https만 허용하고, 호스트가 가리키는 모든 IP가 공인 주소일 때만 True.
내부망·루프백·링크로컬(클라우드 메타데이터 169.254.x)·예약 대역은 거부한다.
Args:
url: 검사할 주소
Returns:
공인 http(s) 주소이면 True
"""
try:
parsed = urlparse(url)
except ValueError:
return False
if parsed.scheme not in ("http", "https") or not parsed.hostname:
return False
try:
port = parsed.port or (443 if parsed.scheme == "https" else 80)
infos = socket.getaddrinfo(parsed.hostname, port, proto=socket.IPPROTO_TCP)
except (OSError, ValueError):
return False
if not infos:
return False
for info in infos:
addr = ipaddress.ip_address(info[4][0].split("%", 1)[0])
if getattr(addr, "ipv4_mapped", None):
addr = addr.ipv4_mapped
if not addr.is_global:
return False
return True
def extract_meta(soup: BeautifulSoup, property_name: str, name: str):
tag = soup.find("meta", property=property_name)
if tag and tag.get("content"):
@@ -97,8 +134,14 @@ def fetch_metadata(url: str):
if cached and cached["expires_at"] > now:
return cached["data"]
try:
# 리다이렉트도 한 번씩 직접 따라가며 매 주소를 공인 주소인지 검사한다(SSRF 방지).
current = url
response = None
for _ in range(5):
if not is_public_http_url(current):
return fallback
response = requests.get(
url,
current,
headers={
"User-Agent": (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
@@ -107,7 +150,14 @@ def fetch_metadata(url: str):
)
},
timeout=6,
allow_redirects=False,
)
if response.is_redirect and response.headers.get("Location"):
current = requests.compat.urljoin(current, response.headers["Location"])
continue
break
else:
return fallback
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
resolved_url = response.url or url
@@ -362,6 +412,8 @@ def add_link():
return jsonify({"error": "URL을 입력해주세요."}), 400
url = normalize_url(raw_url)
if not is_public_http_url(url):
return jsonify({"error": "공개된 http/https 주소만 등록할 수 있습니다."}), 400
table = os.getenv("TABLE", "news_link")
schema = os.getenv("DB_SCHEMA", DEFAULT_SCHEMA)
table_ref = _table_ref(schema, table)
@@ -464,4 +516,4 @@ def add_link():
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8021, debug=True)
app.run(host="127.0.0.1", port=8021, debug=False)