fix(security): close backend merge blockers

This commit is contained in:
2026-08-18 17:51:29 +08:00
parent 2581631b51
commit 8853877fcd
15 changed files with 216 additions and 50 deletions
+14
View File
@@ -0,0 +1,14 @@
from __future__ import annotations
from collections.abc import Iterable
from typing import Generic, TypeVar
T = TypeVar("T")
class PaginatedList(list[T], Generic[T]):
"""A page of items carrying the total count from its data source."""
def __init__(self, items: Iterable[T], *, total: int) -> None:
super().__init__(items)
self.total = total
+19 -2
View File
@@ -10,6 +10,7 @@ from app.auth.metadata_dependencies import (
get_current_metadata_admin,
get_current_metadata_user,
)
from app.api.pagination import PaginatedList
from app.core.audit import AuditAction, log_audit_event
from app.domain.schemas.audit import AuditLogResponse
from app.infra.db.metadb.database import get_metadata_session
@@ -46,7 +47,7 @@ async def get_audit_logs(
_current_user=Depends(get_current_metadata_admin),
audit_repo: AuditRepository = Depends(get_audit_repository),
) -> list[AuditLogResponse]:
return await audit_repo.get_logs(
items = await audit_repo.get_logs(
user_id=user_id,
project_id=project_id,
action=action,
@@ -56,6 +57,15 @@ async def get_audit_logs(
skip=skip,
limit=limit,
)
total = await audit_repo.get_log_count(
user_id=user_id,
project_id=project_id,
action=action,
resource_type=resource_type,
start_time=start_time,
end_time=end_time,
)
return PaginatedList(items, total=total)
@router.get(
@@ -119,7 +129,7 @@ async def get_my_audit_logs(
current_user=Depends(get_current_metadata_user),
audit_repo: AuditRepository = Depends(get_audit_repository),
) -> list[AuditLogResponse]:
return await audit_repo.get_logs(
items = await audit_repo.get_logs(
user_id=current_user.id,
action=action,
start_time=start_time,
@@ -127,3 +137,10 @@ async def get_my_audit_logs(
skip=skip,
limit=limit,
)
total = await audit_repo.get_log_count(
user_id=current_user.id,
action=action,
start_time=start_time,
end_time=end_time,
)
return PaginatedList(items, total=total)
+9 -3
View File
@@ -14,6 +14,7 @@ from pydantic import BaseModel, JsonValue, create_model
from starlette.responses import Response
from app.api.problem_details import ProblemDetails
from app.api.pagination import PaginatedList
from app.api.v1.router import api_router as handler_api_router
from app.auth.metadata_dependencies import get_current_metadata_user
from app.auth.project_dependencies import ProjectContext, get_project_context
@@ -41,8 +42,8 @@ _PUBLIC_PARAMETER_RENAMES = {
"burst_ID": "burst_id",
"drainage_node_ID": "drainage_node_id",
}
_MODEL_NAME_IS_NETWORK = {"RunSimulationManuallyByDate"}
_MODEL_USERNAME_FROM_AUTH: set[str] = set()
_MODEL_NAME_IS_NETWORK = {"RunSimulationManuallyByDate", "PressureSensorPlacement"}
_MODEL_USERNAME_FROM_AUTH = {"PressureSensorPlacement"}
def _clean_name(name: str) -> str:
@@ -230,9 +231,14 @@ def _with_pagination(endpoint):
if not isinstance(result, list):
return result
if handler_handles_pagination:
if not isinstance(result, PaginatedList):
raise RuntimeError(
f"Paginated handler {endpoint.__name__!r} must return "
"PaginatedList with the real total"
)
return Page(
items=result,
total=offset + len(result),
total=result.total,
limit=limit or len(result),
offset=offset,
)
+3 -1
View File
@@ -53,6 +53,7 @@ from app.api.v1.endpoints.timeseries import (
)
from app.auth.permissions import (
BURST_RUN,
ENVIRONMENT_MANAGE,
OPTIMIZATION_RUN,
RISK_RUN,
SCADA_CLEAN,
@@ -88,6 +89,7 @@ simulation_access = Depends(
webgis_view_access = Depends(require_permission(WEBGIS_VIEW))
simulation_run_access = Depends(require_permission(SIMULATION_RUN))
environment_manage_access = Depends(require_permission(ENVIRONMENT_MANAGE))
burst_run_access = Depends(require_permission(BURST_RUN))
risk_run_access = Depends(require_permission(RISK_RUN))
optimization_run_access = Depends(require_permission(OPTIMIZATION_RUN))
@@ -169,7 +171,7 @@ api_router.include_router(
api_router.include_router(
cache.router,
tags=["Cache"],
dependencies=[simulation_run_access],
dependencies=[environment_manage_access],
)
api_router.include_router(
web_search.router,