The User Had Permission When They Opened the Page. Should They Still Have It When They Submit?
Imagine a user opening a production record in an application. They have permission to edit it, so the frontend displays the form and enables the Save button.
A few minutes later, an administrator revokes their editing permission.
The user hasn't refreshed the page. Their session is still active. Their JWT hasn't expired. They make changes and click Save.
If the backend accepts that request simply because the user was authenticated, something is wrong with the authorization design.
This is a problem I think backend developers should pay more attention to: authorization is not a decision that should be made only when a user logs in or loads a page. It must be enforced when the actual operation is performed.
1. Authentication Is Not Authorization
Authentication answers a relatively straightforward question:
Who is making this request?
Authorization answers a different question:
Is this user allowed to perform this particular action on this particular resource?
A valid access token may establish the identity of a user, but it doesn't automatically grant permission to update every record in the application.
Consider an application with the following permission structure:
Admin: Full access to permitted sites and modules.
Operator: Can view and update production records.
Viewer: Can view records but cannot modify them.
The application might also have site-specific restrictions, where an operator can edit records at one production site but only view records at another.
This means authorization cannot always be reduced to checking whether a user has an admin, operator, or viewer role.
The actual decision might depend on:
The authenticated user.
The requested operation.
The specific module.
The resource being accessed.
The site to which the resource belongs.
The user's current permission.
That is a considerably more meaningful authorization check than simply asking whether the user has a valid JWT.
2. Why Frontend Permission Checks Are Not Enough
Frontend permission checks are important. They make applications easier to use by hiding unavailable actions, disabling buttons and preventing users from attempting operations they cannot perform.
However, they are not a security boundary.
A user can interact with an API independently of the frontend interface using browser developer tools, an HTTP client or another application.
For example, the frontend might hide the Edit button for a viewer.
But the API endpoint could still accept the following request:
PATCH /api/batches/104
Authorization: Bearer <access_token>
Content-Type: application/json
{
"status": "completed"
}
If the backend validates only the token and never checks the user's permission to modify batch 104, the frontend restriction is meaningless from a security perspective.
The backend must enforce authorization independently of what the frontend displays.
Frontend checks improve the experience. Backend checks enforce the security policy.
3. The Stale Permission Problem
Now consider a more interesting scenario.
An operator has permission to edit production records. They open a record and begin working on it.
While the page remains open, an administrator changes their permission from Edit to View.
The frontend may still display the Save button because it loaded the permissions earlier.
The JWT may still contain the same identity and claims because the token hasn't expired.
The browser has no automatic knowledge that the administrator changed the permission.
This is where authorization architecture becomes important.
If permissions are embedded in a JWT and the backend relies exclusively on those claims for every authorization decision, permission changes may not become effective until the token expires, unless the application implements an additional revocation or permission-version mechanism.
That is a design trade-off.
JWTs are useful for authentication and can carry authorization-related claims. However, when an application requires permissions to take effect immediately or within a defined short window, relying exclusively on long-lived token claims may not be sufficient.
Depending on the requirements, a backend may use:
Current permission lookups from the database.
Short-lived authorization caches.
Permission versioning.
Token revocation mechanisms.
Centralized authorization services.
The goal is not to eliminate caching. The goal is to understand how caching affects permission revocation and to define the consistency guarantees the system actually provides.
4. Enforcing Authorization at the Backend
In a FastAPI application, authorization can be centralized rather than repeated as unrelated checks throughout every endpoint.
For example, consider a reusable authorization service:
async def require_module_access(
user_id: int,
module: str,
action: str,
site_id: int,
session: AsyncSession,
):
permission = await permission_service.get_current_permission(
session=session,
user_id=user_id,
module=module,
site_id=site_id,
)
if not permission:
raise HTTPException(
status_code=403,
detail="Access denied",
)
if not permission.allows(action):
raise HTTPException(
status_code=403,
detail="You do not have permission for this action",
)
return True
The important part isn't the exact function name or implementation.
The important part is that the backend retrieves or validates the current permission and evaluates the requested action against the appropriate resource and site.
Now let's apply this to an API endpoint.
@router.patch("/batches/{batch_id}")
async def update_batch(
batch_id: int,
payload: BatchUpdate,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
):
batch = await batch_service.get_by_id(
session, batch_id
)
if not batch:
raise HTTPException(
status_code=404,
detail="Batch not found",
)
await require_module_access(
user_id=current_user.id,
module="rearing",
action="edit",
site_id=batch.site_id,
session=session,
)
return await batch_service.update(
session, batch, payload
)
The backend first identifies the resource, determines which site it belongs to, and checks whether the authenticated user currently has permission to edit that resource within the relevant module and site.
Only after the authorization check succeeds should the protected operation proceed.
This pattern also makes permission enforcement easier to maintain because the authorization logic can be reused across multiple endpoints.
FastAPI supports reusable dependencies through Depends() and Security(), including OAuth2 scopes where appropriate. These mechanisms can help structure authentication and authorization checks, although application-specific site and resource permissions often require additional logic beyond standard OAuth2 scopes.
5. What Happens When Permissions Change During a Request?
Checking permissions before an operation is better than relying exclusively on frontend state or old token claims.
However, there is another subtle problem.
Consider this sequence:
The backend checks that a user has Edit permission.
An administrator revokes that permission.
The original request continues executing.
The backend writes the changes to the database.
This is a time-of-check-to-time-of-use (TOCTOU) problem.
The authorization decision and the protected operation did not happen as one coordinated process.
For many applications, checking current permissions immediately before the operation may provide an acceptable consistency guarantee.
But for systems requiring strict revocation guarantees, that may not be sufficient.
Depending on the database and application architecture, stronger guarantees may require:
Database transactions.
Appropriate row locking.
Permission-revocation operations participating in the same locking strategy.
Conditional writes.
Carefully designed transaction isolation.
Consistent authorization checks across all mutation paths.
These mechanisms are not universally necessary for every application. Their suitability depends on the security requirements, database behavior and concurrency model.
The important thing is to understand the guarantee your implementation actually provides instead of assuming that a permission check automatically eliminates every concurrent authorization race.
6. Authorization Should Be Part of the System Design
For me, authorization is not simply a collection of if statements added to API endpoints.
It is part of the application's architecture.
A well-designed authorization system should make it clear:
Who can perform an operation?
Which resources can they access?
Which sites or organizational boundaries apply?
Which actions are permitted?
How quickly do permission changes become effective?
How are sensitive operations recorded?
How are concurrent permission changes handled?
Audit logging also becomes valuable here.
For sensitive operations, the application should record meaningful information about successful actions and relevant denied attempts according to its audit policy. This can help administrators investigate unexpected activity and understand how permission changes affect application behavior.
Authorization, audit logging and data integrity are different responsibilities, but they complement each other when designing reliable backend systems.
Conclusion
A user having permission when they opened a page does not necessarily mean they should still have permission when they submit a request.
The frontend can display what the user is currently expected to be allowed to do. The JWT can establish the user's identity. But the backend must enforce the authorization policy for the actual operation.
And when permissions can change concurrently with data operations, the consistency of those decisions becomes part of the engineering problem.
Authentication tells us who the user is. Authorization determines what they can do. Reliable backend systems must enforce that distinction.
#BackendEngineering #Python #FastAPI #APISecurity #Authorization #SoftwareArchitecture #RBAC #WebDevelopment
Join the conversation! Your thoughts help the community grow.