Exploit database

GH-1d259c534d58554626862e1f9e4ef85c

bhanunamikaze/badhost-cve-2026-48710-exploit

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-48710

4 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files8

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

badhost_vuln_lab/app_vulnerable.py

Python · 6,249 bytes
"""
Intentionally vulnerable FastAPI/Starlette app for CVE-2026-48710 / BadHost testing.

The vulnerability condition demonstrated here is NOT FastAPI's normal Depends() auth.
It is a deliberately bad custom middleware pattern:

    request.url.path   # attacker-influenced through malformed Host on vulnerable Starlette

The router still uses the real ASGI scope path, so the middleware can think the request
is for /health while the app actually routes to /admin.
"""

from __future__ import annotations

from typing import Any

from fastapi import FastAPI, Request
from pydantic import BaseModel
from starlette.responses import JSONResponse

app = FastAPI(
    title="BadHost Vulnerable FastAPI Lab",
    version="0.1.0-vulnerable",
    description=(
        "Local intentionally vulnerable lab for CVE-2026-48710 / BadHost. "
        "Do not expose this service to untrusted networks."
    ),
)

PUBLIC_PATH_PREFIXES = (
    "/health",
    "/public",
    "/openapi.json",
    "/docs",
    "/redoc",
)

API_KEY = "letmein"


@app.middleware("http")
async def intentionally_vulnerable_path_auth(request: Request, call_next):
    """
    Bad on purpose: security decision uses request.url.path.

    On affected Starlette versions, request.url is reconstructed from the raw Host
    header. A Host value such as:

        127.0.0.1:8000/health?x=

    can make request.url.path appear to be /health even when the real request-line
    path is /admin.
    """
    middleware_path_seen = request.url.path  # vulnerable sink
    real_scope_path = request.scope.get("path")

    # Public paths are allowed without auth. This allowlist is intentionally vulnerable
    # because it trusts middleware_path_seen instead of real_scope_path.
    if middleware_path_seen.startswith(PUBLIC_PATH_PREFIXES):
        response = await call_next(request)
        response.headers["X-Lab-Auth-Decision"] = "allowed-by-vulnerable-request-url-path"
        response.headers["X-Lab-Middleware-Path-Seen"] = middleware_path_seen
        response.headers["X-Lab-Real-Scope-Path"] = str(real_scope_path)
        return response

    if request.headers.get("x-api-key") != API_KEY:
        return JSONResponse(
            {
                "detail": "Forbidden: missing or invalid X-API-Key",
                "hint": "Use X-API-Key: letmein for normal authenticated access.",
                "middleware_path_seen": middleware_path_seen,
                "real_scope_path": real_scope_path,
            },
            status_code=403,
            headers={
                "X-Lab-Auth-Decision": "blocked-by-vulnerable-request-url-path",
                "X-Lab-Middleware-Path-Seen": middleware_path_seen,
                "X-Lab-Real-Scope-Path": str(real_scope_path),
            },
        )

    response = await call_next(request)
    response.headers["X-Lab-Auth-Decision"] = "allowed-by-api-key"
    response.headers["X-Lab-Middleware-Path-Seen"] = middleware_path_seen
    response.headers["X-Lab-Real-Scope-Path"] = str(real_scope_path)
    return response


@app.get("/health", tags=["public"])
async def health(request: Request) -> dict[str, Any]:
    return {
        "status": "ok",
        "public": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }


@app.get("/public/info", tags=["public"])
async def public_info(request: Request) -> dict[str, Any]:
    return {
        "service": "badhost-vuln-lab",
        "public": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }


@app.get("/admin", tags=["protected"])
async def admin_dashboard(request: Request) -> dict[str, Any]:
    return {
        "secret": "admin-dashboard-data",
        "protected_endpoint_reached": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }


@app.get("/admin/users/{user_id}", tags=["protected"])
async def admin_user(user_id: int, request: Request) -> dict[str, Any]:
    return {
        "secret": f"user-{user_id}-private-data",
        "user_id": user_id,
        "protected_endpoint_reached": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }


@app.get("/v1/models", tags=["protected", "ai"])
async def list_models(request: Request) -> dict[str, Any]:
    return {
        "object": "list",
        "data": [
            {"id": "internal-llm-prod", "object": "model", "owned_by": "internal"},
            {"id": "security-agent", "object": "model", "owned_by": "internal"},
        ],
        "protected_endpoint_reached": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }


@app.get("/model/info", tags=["protected", "ai"])
async def model_info(request: Request) -> dict[str, Any]:
    return {
        "model": "internal-llm-prod",
        "provider": "lab",
        "protected_endpoint_reached": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }


class ChatRequest(BaseModel):
    model: str
    messages: list[dict[str, str]]


@app.post("/v1/chat/completions", tags=["protected", "ai"])
async def chat_completions(body: ChatRequest, request: Request) -> dict[str, Any]:
    return {
        "id": "chatcmpl-lab",
        "object": "chat.completion",
        "model": body.model,
        "choices": [
            {"index": 0, "message": {"role": "assistant", "content": "lab response"}}
        ],
        "protected_endpoint_reached": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }


@app.post("/mcp", tags=["protected", "mcp"])
async def mcp_endpoint(payload: dict[str, Any], request: Request) -> dict[str, Any]:
    return {
        "jsonrpc": payload.get("jsonrpc", "2.0"),
        "id": payload.get("id", 1),
        "result": {
            "name": "badhost-vuln-lab-mcp",
            "tools": ["read_internal_notes", "list_models"],
        },
        "protected_endpoint_reached": True,
        "middleware_path_seen": request.url.path,
        "real_scope_path": request.scope.get("path"),
    }