fix(web): remove password from public applications
This commit is contained in:
1 parent
0c59d8cb15
commit
e5bd31b428
9 files changed
+9
-91
No files matched your search
+1
-35
@@ -24,7 +24,6 @@ WEB_ROOT = ROOT / "web"
|
||||
_HISTORY_STORE: CalculationStore | None = None
|
||||
_IDENTITY_DIR: IdentityDirectory | None = None
|
||||
_APPLICATION_STORE: ApplicationStore | None = None
|
||||
_WEB_SESSIONS: set[str] = set()
|
||||
_ADMIN_SESSIONS: set[str] = set()
|
||||
|
||||
|
||||
@@ -109,18 +108,6 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return False
|
||||
return True
|
||||
|
||||
def _web_authorized(self) -> bool:
|
||||
expected = os.getenv("BELKI_WEB_PASSWORD", "")
|
||||
if not expected:
|
||||
self._json(503, {"error": "Веб-доступ не настроен: задайте BELKI_WEB_PASSWORD"})
|
||||
return False
|
||||
cookies = self.headers.get("Cookie", "")
|
||||
token = next((part.strip().split("=", 1)[1] for part in cookies.split(";") if part.strip().startswith("belki_session=")), "")
|
||||
if token not in _WEB_SESSIONS:
|
||||
self._json(401, {"error": "Требуется авторизация"})
|
||||
return False
|
||||
return True
|
||||
|
||||
def _json_with_cookie(self, status: int, body: dict, cookie: str) -> None:
|
||||
encoded = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||
self.send_response(status)
|
||||
@@ -198,12 +185,6 @@ class Handler(BaseHTTPRequestHandler):
|
||||
status = query.get("status", [None])[0] or None
|
||||
self._json(200, {"items": application_store().list(status=status)})
|
||||
return
|
||||
if path == "/api/web/session":
|
||||
expected = os.getenv("BELKI_WEB_PASSWORD", "")
|
||||
cookies = self.headers.get("Cookie", "")
|
||||
token = next((part.strip().split("=", 1)[1] for part in cookies.split(";") if part.strip().startswith("belki_session=")), "")
|
||||
self._json(200, {"authenticated": bool(expected and token in _WEB_SESSIONS)})
|
||||
return
|
||||
requested = unquote(path).lstrip("/")
|
||||
candidate = (WEB_ROOT / requested).resolve()
|
||||
if candidate.is_file() and WEB_ROOT.resolve() in candidate.parents:
|
||||
@@ -220,7 +201,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.wfile.write(content)
|
||||
|
||||
def do_POST(self) -> None: # noqa: N802
|
||||
if self.path not in ("/api/admin/auth", "/api/admin/logout", "/api/web/auth", "/api/web/logout", "/api/web/upload", "/api/calculate", "/api/inspect-bulk", "/api/resolve-identity",
|
||||
if self.path not in ("/api/admin/auth", "/api/admin/logout", "/api/web/upload", "/api/calculate", "/api/inspect-bulk", "/api/resolve-identity",
|
||||
"/api/save-profile", "/api/get-profile", "/api/applications"):
|
||||
self._json(404, {"error": "Не найдено"})
|
||||
return
|
||||
@@ -238,21 +219,6 @@ class Handler(BaseHTTPRequestHandler):
|
||||
elif self.path == "/api/admin/logout":
|
||||
_ADMIN_SESSIONS.discard(self._session_token("belki_admin_session"))
|
||||
self._json_with_cookie(200, {"authenticated": False}, "belki_admin_session=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax")
|
||||
elif self.path == "/api/web/auth":
|
||||
expected = os.getenv("BELKI_WEB_PASSWORD", "")
|
||||
if not expected or not secrets.compare_digest(str(payload.get("password", "")), expected):
|
||||
self._json(401, {"error": "Неверный пароль"})
|
||||
return
|
||||
token = secrets.token_urlsafe(32)
|
||||
_WEB_SESSIONS.add(token)
|
||||
self._json_with_cookie(200, {"authenticated": True}, f"belki_session={token}; Path=/; HttpOnly; SameSite=Lax")
|
||||
elif self.path == "/api/web/logout":
|
||||
cookies = self.headers.get("Cookie", "")
|
||||
token = next((part.strip().split("=", 1)[1] for part in cookies.split(";") if part.strip().startswith("belki_session=")), "")
|
||||
_WEB_SESSIONS.discard(token)
|
||||
self._json_with_cookie(200, {"authenticated": False}, "belki_session=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax")
|
||||
elif self.path == "/api/applications" and not self._web_authorized():
|
||||
return
|
||||
elif self.path == "/api/web/upload":
|
||||
raw = str(payload.get("data", ""))
|
||||
if "," in raw:
|
||||
|
||||
Reference in new issue
Block a user