From c2eb7c765d847e3029ff58f37f9e5b6cca46ce08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Fri, 24 Oct 2025 20:13:54 +0200 Subject: [PATCH] fix latin-1 errors --- app/api.py | 18 ++++++++++++++---- app/main.py | 14 +++----------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/api.py b/app/api.py index f97e22d..930878f 100644 --- a/app/api.py +++ b/app/api.py @@ -3,10 +3,13 @@ from fastapi.security import HTTPBasic, HTTPBasicCredentials from .deps import get_geo from .config import settings from .geo import reload_provider +from urllib.parse import quote import secrets import ipaddress import re import json +import unicodedata + router = APIRouter() security = HTTPBasic() @@ -37,6 +40,14 @@ def _check_admin(creds: HTTPBasicCredentials): return True +def _safe_hdr(v: str) -> str: + try: + v.encode("latin-1") + return v + except UnicodeEncodeError: + return unicodedata.normalize("NFKD", v).encode("ascii", "ignore").decode("ascii") or "?" + + def _normalize_ip_str(ip_raw: str) -> str | None: """Usuń port, whitespace i ewentualne cudzysłowy""" if not ip_raw: @@ -82,13 +93,12 @@ def geo_headers(data: dict) -> dict: city = data.get("city") ip_val = data.get("ip") if ip_val and country: - h["X-IP-ADDRESS"] = ip_val - h["X-COUNTRY"] = country + h["X-IP-ADDRESS"] = _safe_hdr(str(ip_val)) + h["X-COUNTRY"] = _safe_hdr(str(country)) if city: - h["X-CITY"] = city + h["X-CITY"] = _safe_hdr(str(city)) return h - def get_client_ip(request: Request) -> str: """ Zwraca IP klienta biorąc pod uwagę: diff --git a/app/main.py b/app/main.py index 3ce5ff8..7ac1f7b 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ from fastapi import FastAPI, Request, Response from fastapi.responses import JSONResponse, PlainTextResponse from starlette.middleware.base import BaseHTTPMiddleware from .deps import get_geo -from .api import get_client_ip, router +from .api import get_client_ip, router, geo_headers from .config import settings import uvicorn @@ -17,16 +17,8 @@ async def add_geo_headers(request, call_next): response: Response = await call_next(request) - country = data.get("country", {}).get("name") if data.get("country") else None - city = data.get("city") - ip_val = data.get("ip") - - if ip_val and country: - response.headers["X-IP-ADDRESS"] = ip_val - response.headers["X-COUNTRY"] = country - if city: - response.headers["X-CITY"] = city - + for k, v in geo_headers(data).items(): + response.headers[k] = v return response