0
0
FastAPIframework~20 mins

Trusted host middleware in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trusted Host Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing an allowed host?

Consider this FastAPI app with TrustedHostMiddleware configured to allow only 'example.com' and 'localhost'. What will be the HTTP status code when a request comes from 'example.com'?

FastAPI
from fastapi import FastAPI
from starlette.middleware.trustedhost import TrustedHostMiddleware

app = FastAPI()
app.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com', 'localhost'])

@app.get('/')
async def root():
    return {'message': 'Hello World'}
A200
B400
C404
D500
Attempts:
2 left
💡 Hint

TrustedHostMiddleware blocks requests from hosts not in the allowed list.

component_behavior
intermediate
2:00remaining
What happens when a disallowed host makes a request?

Given the same FastAPI app with TrustedHostMiddleware allowing only 'example.com' and 'localhost', what is the response status code when a request comes from 'malicious.com'?

FastAPI
from fastapi import FastAPI
from starlette.middleware.trustedhost import TrustedHostMiddleware

app = FastAPI()
app.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com', 'localhost'])

@app.get('/')
async def root():
    return {'message': 'Hello World'}
A200
B400
C404
D403
Attempts:
2 left
💡 Hint

TrustedHostMiddleware returns a 400 Bad Request for disallowed hosts.

📝 Syntax
advanced
2:00remaining
Which option correctly configures TrustedHostMiddleware to allow all subdomains of example.com?

How do you configure TrustedHostMiddleware to allow 'example.com' and any subdomain like 'api.example.com'?

Aapp.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com', 'api.*.com'])
Bapp.add_middleware(TrustedHostMiddleware, allowed_hosts=['*.example.com', 'example.com'])
Capp.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com', '*example.com'])
Dapp.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com', '*.example.com'])
Attempts:
2 left
💡 Hint

Wildcard subdomains must start with '*.' exactly.

🔧 Debug
advanced
2:00remaining
Why does this TrustedHostMiddleware configuration cause all requests to fail?

Given this code, why do all requests return 400 even from 'localhost'?

FastAPI
app.add_middleware(TrustedHostMiddleware, allowed_hosts=['localhost:8000'])
ABecause allowed_hosts should not include port numbers, only hostnames
BBecause 'localhost:8000' is not a valid hostname format
CBecause TrustedHostMiddleware requires IP addresses, not hostnames
DBecause the middleware must be added after all routes
Attempts:
2 left
💡 Hint

Check if ports are allowed in hostnames for TrustedHostMiddleware.

🧠 Conceptual
expert
2:00remaining
What is the main security benefit of using TrustedHostMiddleware in FastAPI?

Why should you use TrustedHostMiddleware in your FastAPI app?

AIt encrypts all incoming requests to secure data
BIt automatically blocks IP addresses with too many requests
CIt prevents HTTP Host header attacks by allowing only specified hosts
DIt validates user authentication tokens in headers
Attempts:
2 left
💡 Hint

Think about what the Host header in HTTP requests can be used for in attacks.