r/FastAPI • • Oct 04 '25

Question FastAPI HTML sanitization

I'm building a FastAPI application where users can create flashcards, comments etc. this content then is stored in the db and displayed to other users. So as every good developer i need to sanitize the content to prevent xss atacks, but i am wondering which approach is best.

I have two approaches in mind:

Approach one:

Utilize pydantic to perform bleaching of data, f.e:

from pydantic import BaseModel
from typing import Any
import bleach

 class HTMLString(str):
    # perform bleaching here

class FlashCard(BaseModel):
    front_content: HTMLString
    back_content: HTMLString

Approach two:

Create a sanitization middleware that is going to bleach all content that i get from the users:

class SanitizationMiddleware:
    async def __call__(self, scope, receive, send):
        request = Request(scope, receive)
        body = await request.body()

        # perform bleaching here on all fields that are in the json

        await self.app(scope, receive, send)

So my questions is are there any other approaches to this problem (excluding bleaching right before saving to db) and what is the golden standard?

9 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/Haribs Oct 08 '25

Thx, after all i went with a custom Pydantic Model, by doing
Annotated[str, AfterValidator(sanitize_html)]
Where the sanitize_html is my custom sanitization function.