Files
romm/backend/endpoints/stats.py
zurdi 4e686e1d74 fix(permissions): address PR review (Copilot + adversarial) findings
Resolves the CI blocker and a cluster of opt-out visibility "fail-open"
gaps surfaced in review of the granular permission system.

Security / correctness:
- admin oauth_scopes projection keeps canonical FULL_SCOPES order
  (order_scopes) instead of sorting alphabetically, fixing the red
  test_user.py::test_admin on MariaDB + Postgres.
- default-group hides no longer fail open: the resolver resolves the
  effective (own-or-default) group before the hidden-entity lookup.
- /roms/by-hash and /roms/by-metadata-provider now 404-mask hidden roms.
- USERS-entity grant no longer enables admin creation: add_user and
  invite-link require a real admin to mint admin accounts.

Visibility leaks closed on secondary read paths:
- feeds, sibling roms (list query + single-rom schemas), /stats counts
  and per-platform breakdowns, collection rom_ids/rom_count, search_rom.

Hardening / cleanups:
- firmware/platform PUT 404-mask hidden entities; group rename conflict
  returns 400 not 500; guard against removing the last default group;
  kiosk read-only enforced at the fine layer; add_hidden_entity rejects
  non-cascading entity types.

Frontend:
- permissionGroups.ensureLoaded coalesces concurrent callers on one
  in-flight request; permissions.setGrants resets isAdmin/hidden;
  CreateUserDialog no longer orphans a user when group assignment fails;
  HiddenGamesPicker search rows are native buttons (keyboard/gamepad);
  invite-role labels and group swatch aria-label use i18n; drop dead code
  (originalRole, unused permissionsApi export).

AI assistance: changes authored with Claude Code (Claude Opus), driven by
the Copilot review and a multi-agent adversarial review, then verified
(backend pytest, frontend typecheck/vitest, i18n parity, trunk).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 12:37:58 +00:00

54 lines
1.7 KiB
Python

from fastapi import Request
from endpoints.responses.stats import StatsReturn
from handler.auth.dependencies import get_permissions
from handler.database import db_stats_handler
from utils.router import APIRouter
router = APIRouter(
prefix="/stats",
tags=["stats"],
)
@router.get("")
def stats(request: Request, include_platform_stats: bool = False) -> StatsReturn:
"""Endpoint to return the current RomM stats
Returns:
dict: Dictionary with all the stats
"""
# Exclude platforms/roms hidden from the caller (admins/anon: no filtering).
hidden_platform_ids: list[int] = []
hidden_rom_ids: list[int] = []
if request.user.is_authenticated:
perms = get_permissions(request)
hidden_platform_ids = list(perms.hidden_platform_ids)
hidden_rom_ids = list(perms.hidden_rom_ids)
result: StatsReturn = {
"PLATFORMS": db_stats_handler.get_platforms_count(
hidden_platform_ids, hidden_rom_ids
),
"ROMS": db_stats_handler.get_roms_count(hidden_platform_ids, hidden_rom_ids),
"SAVES": db_stats_handler.get_saves_count(),
"STATES": db_stats_handler.get_states_count(),
"SCREENSHOTS": db_stats_handler.get_screenshots_count(),
"TOTAL_FILESIZE_BYTES": db_stats_handler.get_total_filesize(
hidden_platform_ids, hidden_rom_ids
),
}
if include_platform_stats:
result["METADATA_COVERAGE"] = (
db_stats_handler.get_metadata_coverage_by_platform(
hidden_platform_ids, hidden_rom_ids
)
)
result["REGION_BREAKDOWN"] = db_stats_handler.get_region_breakdown_by_platform(
hidden_platform_ids, hidden_rom_ids
)
return result