fix(security): блок ① residuals по адверсариал-ревью Fable
- Смонтированные роутеры /api/gen|ref|wizard/* были полностью анонимны (нет глобального auth-middleware) → аноним жёг LLM/RAG-бюджет, грузил файлы, ловил сырые ошибки. Гейт на include_router через Depends(_auth_dep) — без правки чужого пакета smeta_railway. Живой чат их не использует (только депрекированные страницы). - safe_work_dir: username вида 'zzz/../victim@x.com' резолвился в чужую папку (parent==root проходил) → добавлена проверка user_dir.name==username (один компонент). - null-byte в tab_id из JSON-body давал ValueError→500 → теперь UnsafePath→чистый 400. - /api/gen/calculate был аноним (безопасен лишь из-за раннего return) → добавлен _get_user. Тесты: +collision, +null-byte, +anon-гейт всех смонтированных маршрутов. 300 passed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -75,6 +75,24 @@ def test_check_yaml_requires_auth(env):
|
||||
assert r.status_code == 401
|
||||
|
||||
|
||||
def test_mounted_routers_and_gen_reject_anon(env):
|
||||
"""Fable verify #1/#4: /api/gen|ref|wizard/* были полностью анонимны (жгли LLM/RAG,
|
||||
грузили файлы). Ни один маршрут под этими префиксами не должен пускать без токена."""
|
||||
import re as _re
|
||||
_auth, main, client, _tmp = env
|
||||
targets = []
|
||||
for r in main.app.routes:
|
||||
p = getattr(r, "path", "")
|
||||
if p.startswith(("/api/ref/", "/api/wizard/", "/api/gen/")): # с '/' — не ловить /api/refresh
|
||||
methods = getattr(r, "methods", None) or {"GET"}
|
||||
m = "POST" if "POST" in methods else ("GET" if "GET" in methods else next(iter(methods)))
|
||||
targets.append((m, _re.sub(r"\{[^}]+\}", "x", p)))
|
||||
assert targets, "нет смонтированных маршрутов — тест бесполезен"
|
||||
for m, url in targets:
|
||||
resp = client.request(m, url) # без Authorization
|
||||
assert resp.status_code == 401, f"{m} {url} → {resp.status_code}, ждали 401"
|
||||
|
||||
|
||||
# ---- владелец job'а ----
|
||||
|
||||
def test_require_job_owner_helper(env):
|
||||
|
||||
@@ -59,6 +59,22 @@ def test_traversal_username_rejected(_root):
|
||||
ps.safe_work_dir("../../../etc", "passwd")
|
||||
|
||||
|
||||
def test_username_dotdot_collision_rejected(_root):
|
||||
"""Fable: 'zzz/../victim@x.com' резолвится в чужую папку victim@x.com
|
||||
(parent тоже == root) — должно отвергаться (username = один компонент)."""
|
||||
ps, _ = _root
|
||||
with pytest.raises(ps.UnsafePath):
|
||||
ps.safe_work_dir("zzz/../victim@x.com", "tab1")
|
||||
|
||||
|
||||
def test_null_byte_gives_unsafepath_not_500(_root):
|
||||
"""Fable: null-byte в tab_id из JSON-body → .resolve() бросал ValueError → 500.
|
||||
Теперь ловится как UnsafePath (обработчик → чистый 400)."""
|
||||
ps, _ = _root
|
||||
with pytest.raises(ps.UnsafePath):
|
||||
ps.safe_work_dir("user@x.com", "tab\x00evil")
|
||||
|
||||
|
||||
def test_nested_tab_stays_inside_user(_root):
|
||||
"""tab_id с одиночным '/' (без '..') остаётся внутри папки юзера — не reject."""
|
||||
ps, root = _root
|
||||
|
||||
Reference in New Issue
Block a user