HIGH excessive data exposurefastapibasic auth

Excessive Data Exposure in Fastapi with Basic Auth

Excessive Data Exposure in Fastapi with Basic Auth — how this specific combination creates or exposes the vulnerability

Excessive Data Exposure occurs when an API returns more information than necessary for a given operation, such as full database rows, internal identifiers, or sensitive metadata. In Fastapi, using HTTP Basic Authentication without additional safeguards can amplify this risk. Basic Auth transmits a username and password in an Authorization header encoded with Base64, which is not encryption. If the API authenticates with Basic Auth but then returns complete user objects, internal IDs, or sensitive fields like email, password hashes, or role flags, the response may disclose data that should remain restricted.

For example, an endpoint that retrieves a user profile might return the full ORM model including fields like is_admin, password_hash, or reset_token. Because Basic Auth does not inherently limit what data the server sends back, developers must explicitly control the response shape. Without strict field filtering or serialization discipline, an unauthenticated or low-privilege actor who triggers an endpoint may see data they should not, especially if error messages or logs also expose stack traces or object graphs. The combination of weak transport security (Base64-only credentials) and verbose data returns increases the likelihood of sensitive information leakage.

OpenAPI specifications can unintentionally contribute to this problem. If the spec defines a response schema with many properties but the runtime implementation does not enforce the same constraints, the unauthenticated attack surface may reveal definitions or examples that hint at internal structures. middleBrick’s OpenAPI/Swagger spec analysis, with full $ref resolution, can highlight mismatches between declared schemas and actual responses, helping teams identify where responses include unexpected fields. In parallel, one of the 12 security checks runs LLM/AI Security probes, including unauthenticated LLM endpoint detection and output scanning for PII or API keys, which can catch leakage of sensitive data in automated responses or error payloads.

Common real-world triggers include missing serialization exclusions, overly broad SELECT queries, or returning entire nested objects when only a subset is needed. Attack patterns such as IDOR can intersect with Excessive Data Exposure when endpoints reveal internal IDs alongside sensitive fields, enabling enumeration. middleBrick’s checks for BOLA/IDOR and Property Authorization are designed to surface these overlaps by correlating authentication state with the breadth of returned data. By scanning the unauthenticated attack surface in 5–15 seconds, the tool surfaces prioritized findings with severity and remediation guidance, focusing teams on tightening response schemas and minimizing exposed fields.

Basic Auth-Specific Remediation in Fastapi — concrete code fixes

To mitigate Excessive Data Exposure with Basic Auth in Fastapi, you should combine secure credential handling, strict response serialization, and explicit field selection. Avoid returning raw database models directly; instead, use Pydantic models to define exactly which fields are exposed. Ensure credentials are transmitted over HTTPS to protect the Base64-encoded token in transit, and validate and scope permissions before constructing any response.

Below are concrete, working Fastapi examples that demonstrate secure patterns.

Secure Basic Auth setup with HTTPS enforcement and scoped responses

from fastapi import Fastapi, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from pydantic import BaseModel
from typing import Optional
import secrets

app = Fastapi()
security = HTTPBasic()

# Example user store; in production use a secure hashed store and HTTPS
USERS = {
    "alice": {"password": "hashed_secret", "role": "user", "email": "alice@example.com", "is_admin": False},
    "admin": {"password": "hashed_admin", "role": "admin", "email": "admin@example.com", "is_admin": True},
}

class UserPublic(BaseModel):
    username: str
    role: str
    email: str

class UserPrivate(UserPublic):
    is_admin: bool

def verify_basic_auth(credentials: HTTPBasicCredentials = Depends(security)):
    username = credentials.username
    provided = credentials.password
    if username not in USERS or not secrets.compare_digest(USERS[username]["password"], provided):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid credentials",
            headers={"WWW-Authenticate": "Basic"},
        )
    return USERS[username]

@app.get("/me/public", response_model=UserPublic)
def me_public(user: dict = Depends(verify_basic_auth)):
    # Return only safe fields
    return UserPublic(username=user["username"], role=user["role"], email=user["email"])

@app.get("/me/private", response_model=UserPrivate)
def me_private(user: dict = Depends(verify_basic_auth)):
    # Include sensitive fields only for authenticated context; still explicit
    return UserPrivate(
        username=user["username"],
        role=user["role"],
        email=user["email"],
        is_admin=user["is_admin"],
    )

This approach avoids returning raw ORM instances and limits exposure by using distinct Pydantic models. The verify function uses constant-time comparison to mitigate timing attacks and rejects unknown users without revealing which field was incorrect.

Complementary measures to reduce Excessive Data Exposure

  • Use selective serialization: define multiple response models with only necessary fields and choose them based on scope or role.
  • Ensure HTTPS is mandatory in production; Basic Auth over HTTP exposes credentials and makes leakage interception trivial.
  • Audit responses with automated scanning. middleBrick’s CLI tool can be run as middlebrick scan <url> to detect endpoints that return excessive fields, and the GitHub Action can enforce a maximum risk score before merging changes.
  • Correlate findings with compliance frameworks such as OWASP API Top 10 and SOC2; the Pro plan’s continuous monitoring can schedule regular scans to catch regressions in response design.

Related CWEs: propertyAuthorization

CWE IDNameSeverity
CWE-915Mass Assignment HIGH

Frequently Asked Questions

Can middleBrick detect Excessive Data Exposure in my Fastapi endpoints using Basic Auth?
Yes. middleBrick scans the unauthenticated attack surface and checks for excessive field exposure, returning a security risk score and prioritized findings with remediation guidance. It also cross-references your OpenAPI spec definitions with runtime behavior.
How can I integrate middleBrick into my Fastapi development workflow to prevent data exposure regressions?
Use the CLI with middlebrick scan <url> locally, add the GitHub Action to fail builds if the risk score drops below your threshold, or use the MCP Server to scan APIs directly from your IDE while you develop.