[verified] Security hardening + UX/UI polish
Security (requesting-code-review pipeline + independent reviewer): - Fix path traversal on file upload (basename sanitize + resolve-containment) - Fix IDOR: org + owner scoping on all group/chat routes (_authorize_group/_get_owned_group), hide other users' personal groups in listings - Remove XSS via v-html in Chat task (text interpolation) - Add test_security.py (traversal + cross-user denial) — all pass UX/UI (ui-ux-pro-max + frontend-dev-verification): - Global: focus rings, 44px touch targets, hover/press transitions, input focus glow, prefers-reduced-motion, skeleton loaders, empty states, back links, spinner - Login: password toggle, autocomplete, spinner, disabled-when-empty - Cards lift on hover; dashboard skeleton + empty state; analyze button spinner All backend tests pass (m0/m1/routes/security/e2e); frontend builds; served SPA verified via curl.
This commit is contained in:
@@ -29,6 +29,30 @@ def _stores():
|
||||
}
|
||||
|
||||
|
||||
def _authorize_group(group: dict) -> None:
|
||||
"""Enforce org-scoped access (IDOR defense). super_admin may access any org.
|
||||
|
||||
Private/personal groups (owner_user_id set) are only accessible by their owner
|
||||
(or super_admin), even within the same org.
|
||||
"""
|
||||
actor = current_user()
|
||||
if actor.get("role") == "super_admin":
|
||||
return
|
||||
owner = group.get("owner_user_id")
|
||||
if owner and owner != actor["id"]:
|
||||
raise ApiError("permission denied", 403)
|
||||
if group.get("org_id") != actor.get("org_id"):
|
||||
raise ApiError("permission denied", 403)
|
||||
|
||||
|
||||
def _get_owned_group(s, gid: str) -> dict:
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group:
|
||||
raise ApiError("group not found", 404)
|
||||
_authorize_group(group)
|
||||
return group
|
||||
|
||||
|
||||
def _upload_dir():
|
||||
d = Config.DATA_DIR / "uploads"
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
@@ -46,10 +70,21 @@ def create_group():
|
||||
|
||||
if request.files:
|
||||
for file in request.files.getlist("files"):
|
||||
ext = (file.filename or "").rsplit(".", 1)[-1].lower()
|
||||
raw_name = file.filename or ""
|
||||
# Path traversal defense: take only the basename, drop any directory
|
||||
# segments and reject empty/unsafe names. Never trust client filename as a path.
|
||||
safe_name = Path(raw_name).name
|
||||
if not safe_name or safe_name in (".", "..", "/", "\\") or "/" in raw_name or "\\" in raw_name:
|
||||
raise ApiError("invalid file name")
|
||||
ext = safe_name.rsplit(".", 1)[-1].lower()
|
||||
if ext not in Config.ALLOWED_UPLOAD_EXTS:
|
||||
raise ApiError(f"unsupported file type: {ext}")
|
||||
dest = _upload_dir() / f"{current_user()['id'].replace('@','_')}__{file.filename}"
|
||||
dest = _upload_dir() / f"{current_user()['id'].replace('@','_')}__{safe_name}"
|
||||
# Ensure resolved path stays inside the upload dir (defense in depth).
|
||||
try:
|
||||
dest.resolve(strict=False).relative_to(_upload_dir().resolve(strict=True))
|
||||
except ValueError:
|
||||
raise ApiError("invalid file path")
|
||||
file.save(dest)
|
||||
saved_files.append(dest.name)
|
||||
|
||||
@@ -95,6 +130,13 @@ def list_groups():
|
||||
visible = s["groups"].list_visible_to(
|
||||
role=actor.get("role"), org_id=actor.get("org_id")
|
||||
)
|
||||
# Expose personal/private groups only to their owner (IDOR defense in listing).
|
||||
if actor.get("role") != "super_admin":
|
||||
visible = [
|
||||
g
|
||||
for g in visible
|
||||
if not g.get("owner_user_id") or g.get("owner_user_id") == actor["id"]
|
||||
]
|
||||
return jsonify({"groups": visible})
|
||||
|
||||
|
||||
@@ -104,11 +146,7 @@ def list_groups():
|
||||
def analyze_group(gid: str):
|
||||
"""Run analysis: sales kit + 15 personas. Synchronous for v1 (replaces gen)."""
|
||||
s = _stores()
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group:
|
||||
raise ApiError("group not found", 404)
|
||||
if group.get("org_id") != (current_user().get("org_id") or "org-default"):
|
||||
raise ApiError("permission denied", 403)
|
||||
group = _get_owned_group(s, gid)
|
||||
|
||||
inp = group.get("input", {})
|
||||
if not s["llm"]:
|
||||
@@ -152,12 +190,8 @@ def analyze_group(gid: str):
|
||||
@require_auth
|
||||
def get_group(gid: str):
|
||||
s = _stores()
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group:
|
||||
raise ApiError("group not found", 404)
|
||||
group = _get_owned_group(s, gid)
|
||||
actor = current_user()
|
||||
if actor.get("role") != "super_admin" and group.get("org_id") != actor.get("org_id"):
|
||||
raise ApiError("permission denied", 403)
|
||||
|
||||
view = dict(group)
|
||||
if actor.get("role") == "user":
|
||||
@@ -172,9 +206,7 @@ def get_group(gid: str):
|
||||
@require_auth
|
||||
def list_personas(gid: str):
|
||||
s = _stores()
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group:
|
||||
raise ApiError("group not found", 404)
|
||||
group = _get_owned_group(s, gid)
|
||||
actor = current_user()
|
||||
if actor.get("role") == "user":
|
||||
if group.get("status") != "ready":
|
||||
@@ -197,9 +229,7 @@ def list_personas(gid: str):
|
||||
@require_auth
|
||||
def get_persona(gid: str, pid: str):
|
||||
s = _stores()
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group:
|
||||
raise ApiError("group not found", 404)
|
||||
group = _get_owned_group(s, gid)
|
||||
p = s["groups"].get_persona(gid, pid)
|
||||
if not p:
|
||||
raise ApiError("persona not found", 404)
|
||||
@@ -215,9 +245,7 @@ def get_persona(gid: str, pid: str):
|
||||
@require_roles("admin")
|
||||
def update_persona(gid: str, pid: str):
|
||||
s = _stores()
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group:
|
||||
raise ApiError("group not found", 404)
|
||||
_get_owned_group(s, gid)
|
||||
data = request.get_json(silent=True) or {}
|
||||
try:
|
||||
updated = s["groups"].update_persona(gid, pid, data)
|
||||
|
||||
Reference in New Issue
Block a user