Improper handling of permissions

Improper handling of insufficient permissions or privileges

Description

A user can reach restricted data or functions if an application checks only that they are signed in or uses a caller-supplied value as proof of permission.

Potential impact

  • Privilege escalation: Unauthorized users may gain administrative capabilities.
  • Data exposure: Sensitive data may be disclosed to users who lack access.
  • Service disruption: Unauthorized operations on system resources may interrupt the service.

Remediation

  • For each protected operation or resource, check the signed-in user’s permissions on the server. Use authentication and authorization features suited to Django, Flask, or FastAPI.
  • Stop before performing an operation the user lacks permission for. For an API, return the appropriate HTTP error. Changing the error text alone cannot enforce access control.

Examples

The excerpts omit authentication setup, session management, and user lookup. Flask-Login needs a verified user loader and login configuration. Load is_admin from permission data the server trusts.

Django

Before

python
# Unsafe Django code
from django.http import HttpResponse

def view_sensitive_data(request):
    if request.user.username != 'admin':
        return HttpResponse("Not authorized")
    # Process sensitive data
    return HttpResponse("Sensitive data")

After

python
# Safe Django code
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, user_passes_test

@login_required
@user_passes_test(lambda u: u.is_superuser)
def view_sensitive_data(request):
    # Process sensitive data
    return HttpResponse("Sensitive data")

A username chosen during registration or changed later cannot establish administrative authority on its own. The revised view uses login_required and checks is_superuser with user_passes_test. A failed check redirects the user to the login URL.

Flask

Before

python
# Unsafe Flask code
from flask import Flask, request

app = Flask(__name__)

@app.route('/sensitive')
def view_sensitive_data():
    if request.args.get('user') != 'admin':
        return "Not authorized"
    # Process sensitive data
    return "Sensitive data"

After

python
# Safe Flask code
from flask import Flask, request, abort
from flask_login import login_required, current_user

app = Flask(__name__)

@app.route('/sensitive')
@login_required
def view_sensitive_data():
    if not current_user.is_admin:
        return abort(403)
    # Process sensitive data
    return "Sensitive data"

The caller supplies the URL’s user value. The revised handler checks is_admin on Flask-Login’s authenticated current_user and returns 403 if permission is missing.

FastAPI

Before

python
# Unsafe FastAPI code
from fastapi import FastAPI, Request, HTTPException

app = FastAPI()

@app.get("/sensitive")
async def view_sensitive_data(request: Request):
    if request.query_params.get('user') != 'admin':
        return {"detail": "Not authorized"}
    # Process sensitive data
    return {"detail": "Sensitive data"}

After

python
# Safe FastAPI code
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

class User(BaseModel):
    username: str
    is_admin: bool

def get_current_user(token: str = Depends(oauth2_scheme)):
    # Reject access until real token verification and user lookup are implemented.
    raise HTTPException(status_code=401, detail="Token verification is not configured")

@app.get("/sensitive")
async def view_sensitive_data(current_user: User = Depends(get_current_user)):
    if not current_user.is_admin:
        raise HTTPException(status_code=403, detail="Not authorized")
    # Process sensitive data
    return {"detail": "Sensitive data"}

OAuth2PasswordBearer extracts the token. The example’s get_current_user denies access until verification is implemented. Return a User only after verifying the token and reading permissions from a trusted user store.

References