partner-usage-metering-defensive
Task Instructions
You are performing a defensive security audit of a Python FastAPI partner usage-metering service. The application combines a partner and plan registry with tenant quota accounting, operator workflows, audit events, and operational endpoints. Quota requests pass through HTTP adapters, request-authority resolution, application services, policy code, and repository state; review the repository as a system rather than treating individual routes in isolation.
Objective
Audit all source under /app/src/ and patch every security weakness that can cause unauthorized privilege, cross-tenant state changes, quota or tier bypass, inconsistent accounting, replayed mutations, or disclosure of sensitive configuration. Maintain /app/output/plan.md while working, recording the relevant subsystem, the root cause traced across layers, the remediation approach, and verification of both the security boundary and legitimate behavior.
The hardened service must satisfy these public invariants:
- Tenant selection and operator privilege must come from authoritative application inputs. Client-supplied routing, forwarding, role, mesh, or identity metadata is not sufficient authority and must not retarget another tenant.
- All quota-changing workflows must apply one coherent integer and finite-limit policy. Malformed or non-positive mutations must not alter state, and rejected operations must leave every affected account unchanged.
- Single consumption, bulk consumption, reconciliation, adjustments, and transfers must preserve their legitimate workflows. Transfers are body-driven and atomic; adjustments are privileged and safely idempotent for an exact replay while conflicting reuse is rejected.
- Explicit finite limits remain controlling for any accepted plan or compatibility tier. A secure implementation may reject unsupported labels rather than silently granting a broader entitlement.
- Public operational responses remain useful JSON. Privileged snapshots, exports, configuration, audit data, and optional diagnostics must enforce their intended access boundary and must not expose credentials or material derived from them.
- The partner registry, plan catalog, readiness/build endpoints, and public repository tests must continue to work. Do not replace the service, remove required routes, hardcode verifier inputs, or disable features to obtain a pass.
Deliverable
Patch the implementation directly under /app/src/ and leave a non-empty /app/output/plan.md. Do not change the configured ADMIN_TOKEN or SERVICE_SECRET values.