The lead still recommended secure, which the flask-cors addition moved to a challenger slot, and said nothing about CORS. The lead now names nh3 and the CORS picks, and the closing paragraph names the sanitizer and the CSP instead of "both". Co-Authored-By: Claude <noreply@anthropic.com>
4 KiB
A Python web security library like nh3 sanitizes HTML your users write. Flask-CORS and django-cors-headers add CORS headers to Flask and Django apps.
How to choose:
- User-written HTML, like comments and posts, or code moving off bleach: nh3
- HTML rendered from user Markdown: nh3, run on the rendered output
- Letting a frontend on another origin call your API: Flask-CORS on Flask, django-cors-headers on Django
- Security headers like CSP and HSTS, one policy across ASGI and WSGI apps: secure
nh3 is a Python binding to ammonia, an allowlist-based HTML sanitizer built to stop cross-site scripting from untrusted user HTML. It parses fragments the same way browsers do. Call nh3.clean() on each fragment, and pass tags and attributes to set what it allows, like tags={"b"}. When you use the same options everywhere, create an nh3.Cleaner once and reuse it. ammonia won't linkify bare URLs or add line breaks, so its docs say to run a markup processor before the sanitizer: render Markdown first, then clean the HTML it outputs.
Flask-CORS is a Flask extension built on one idea: when you enable CORS, you enable it for every use case on a domain. CORS(app) turns it on for all routes and all origins. To scope it to part of the app, pass resources to map path patterns like r"/api/*" to their own options, such as origins. To cover a single route, decorate it with @cross_origin(). Cross-origin cookies stay off until you set supports_credentials=True, and its docs ask you to add CSRF protection before you do.
django-cors-headers is a Django app that adds CORS headers to responses, so browsers let pages on other origins call your site. Add corsheaders to INSTALLED_APPS, and put CorsMiddleware in MIDDLEWARE as high as possible, above any middleware that can return a response, like Django's CommonMiddleware. Otherwise those responses go out without CORS headers. Then list the sites you trust in CORS_ALLOWED_ORIGINS. Its docs warn that CORS_ALLOW_ALL_ORIGINS = True can be dangerous, since any website could then make cross-origin requests to yours.
secure keeps all your security headers in one policy object, instead of header strings copied across handlers and hooks. Start from Secure.with_default_headers(), which its docs call the recommended starting point, configure it once, and reuse it everywhere. Prefer its ASGI or WSGI middleware to cover the whole app, and set headers per response when you're inside an existing hook or view. The defaults are only a starting point: adjust the Content Security Policy for the scripts, styles, and third-party services your app really uses. Then test a stricter policy against the real app before rollout.
Against cross-site scripting, use both a sanitizer and a CSP, with sanitizing as the main defense. OWASP says to sanitize HTML when users author it, and to treat CSP as an additional layer of defense, never the primary one.