Server information exposure

Unnecessary internal server information is exposed to clients.

Description

Server versions, software components, and debug messages in responses can reveal application internals to clients.

Potential impact

  • Attack preparation: Disclosed details can help an attacker locate weaknesses.
  • Exploitation: Software-version information may help identify known vulnerabilities.
  • Further compromise: When exploitable weaknesses also exist, the information may assist attacks against systems or data.

Remediation

  • Disable application debug mode in production and check error responses for internal details.
  • Check the response headers added by the web server and proxy, and remove unnecessary server or version information.

Examples

Django

Before

python
# Django with DEBUG enabled
from django.http import HttpResponse

def unsafe_view(request):
    return HttpResponse("Hello, world!")

# settings.py
DEBUG = True

After

python
# Django with DEBUG disabled
from django.http import HttpResponse

def safe_view(request):
    return HttpResponse("Hello, world!")

# settings.py
DEBUG = False
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_HSTS_SECONDS = 3600
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

Django sets DEBUG=False. The HTTPS and HSTS settings shown require an HTTPS deployment and a configured SecurityMiddleware. The session and CSRF cookies are also restricted to HTTPS.

Flask

Before

python
# Flask with DEBUG enabled
from flask import Flask

app = Flask(__name__)
app.config['DEBUG'] = True

@app.route('/')
def unsafe():
    return "Hello, world!"

After

python
# Flask with DEBUG disabled
from flask import Flask

app = Flask(__name__)
app.config['DEBUG'] = False
app.config['SESSION_COOKIE_SECURE'] = True
app.config['REMEMBER_COOKIE_SECURE'] = True

@app.route('/')
def safe():
    return "Hello, world!"

Flask turns off DEBUG and restricts the session cookie to HTTPS. REMEMBER_COOKIE_SECURE applies to Flask-Login’s remember cookie.

FastAPI

Before

python
# FastAPI with debug mode enabled
from fastapi import FastAPI

app = FastAPI(debug=True)

@app.get("/")
async def unsafe():
    return {"message": "Hello, world!"}

After

python
# FastAPI with debug mode disabled
from fastapi import FastAPI

app = FastAPI(debug=False)

@app.get("/")
async def safe():
    return {"message": "Hello, world!"}

In FastAPI, set debug=False when creating the application.

References