Harden Telegram login, callback spinner and Mini App escaping
checks / pytest (push) Has been cancelled
checks / moderation (push) Has been cancelled
checks / node (push) Has been cancelled

- skin: tg_login rejects cross-site Origin; hash compared as bytes; tg_* cookies Secure
- bot: answer callback on fast handler failure; cap MENU_OWNER; image retries stop on
  terminal errors; upstream error texts sent without HTML parse mode
- Mini App: escape ids in action buttons and task rows; refresh in Neurolab empty state
- tests for spinner and tg login hardening
This commit is contained in:
2026-10-31 02:16:16 +03:00
parent af292ff15d
commit a05a36aa7b
5 changed files with 122 additions and 15 deletions
+46
View File
@@ -0,0 +1,46 @@
"""Spinner middleware must answer the callback when the handler fails fast."""
import ast
import asyncio
from pathlib import Path
import pytest
SRC = Path(__file__).resolve().parents[1] / "bot" / "grok_bot.py"
def _load_mw():
tree = ast.parse(SRC.read_text(encoding="utf-8"))
fn = next(n for n in tree.body if isinstance(n, ast.AsyncFunctionDef) and n.name == "_cb_spinner_mw")
fn.decorator_list = []
ns = {"asyncio": asyncio}
exec(compile(ast.Module(body=[fn], type_ignores=[]), str(SRC), "exec"), ns)
return ns["_cb_spinner_mw"]
class _Ev:
def __init__(self):
self.answered = 0
async def answer(self):
self.answered += 1
def test_fast_failure_answers_and_reraises():
mw, ev = _load_mw(), _Ev()
async def handler(e, d):
raise ValueError("boom")
with pytest.raises(ValueError):
asyncio.run(mw(handler, ev, {}))
assert ev.answered == 1
def test_success_does_not_double_answer():
mw, ev = _load_mw(), _Ev()
async def handler(e, d):
return "ok"
assert asyncio.run(mw(handler, ev, {})) == "ok"
assert ev.answered == 0
+39
View File
@@ -0,0 +1,39 @@
"""Skin Telegram login: non-ASCII hash is a mismatch (not a 500), cross-site Origin is refused."""
import ast
import asyncio
import hashlib
import hmac
import json
import time
from pathlib import Path
from aiohttp import web
from aiohttp.test_utils import make_mocked_request
SRC = Path(__file__).resolve().parents[1] / "gateway" / "grok-web-skin.py"
def _load(*names, **extra):
tree = ast.parse(SRC.read_text(encoding="utf-8"))
nodes = [n for n in tree.body
if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) and n.name in names]
assert {n.name for n in nodes} == set(names)
ns = {"time": time, "hashlib": hashlib, "hmac": hmac, "json": json, "web": web,
"TG_AUTH_MAX_AGE": 86400, "_tg_bot_token": lambda: "123:abc", **extra}
exec(compile(ast.Module(body=nodes, type_ignores=[]), str(SRC), "exec"), ns)
return ns
def test_non_ascii_hash_is_rejected_not_500():
ns = _load("_tg_check_hash")
data = {"id": "1", "auth_date": str(int(time.time())), "hash": "ф" * 64}
assert ns["_tg_check_hash"](data) is False
def test_tg_login_refuses_cross_site_origin():
ns = _load("tg_login")
req = make_mocked_request("POST", "/api/tg_widget_login",
headers={"Origin": "https://evil.example"})
resp = asyncio.run(ns["tg_login"](req))
assert resp.status == 403
assert json.loads(resp.body)["error"] == "bad_origin"