You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

bacinet

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bacinet

Setting up security-relevant response headers in FastAPI

0.0.6
pipPyPI
Maintainers
1

bacinet

Helmet-like http response Headers for FastAPI.

This is alpha software lacking testing and proper documentation. Use at your own risk!

Basic usage

Install package via pip install bacinet.

In your code, import apply from the package and add it as a middleware to your app:

from bacinet import apply
from fastapi import FastAPI, Request

app = FastAPI()


@app.middleware("http")
async def add_bacinet(request: Request, call_next):
    response = await call_next(request)
    apply(response.headers)
    return response

Alternatively, you can use an ASGI-conformant Middlware:

from bacinet import BacinetMiddleware


app = FastAPI()


app.add_middleware(BacinetMiddleware)

To change the default options, import and change the options dictionary from bacinet:

from bacinet import options

options["X-DNS-Prefetch-Control"] = "on"

You can also import individual header functions (like x_download_options) and apply those on your own applicator function that can be provided to BacinetMiddleware's constructor.

Headers

In it's standard form, the following headers will be set or removed by the middleware:

HeaderDefaultDescription
Origin-Agent-Cluster?1https://github.com/WICG/origin-agent-cluster
X-Content-Type-OptionsnosniffPrevents MIME type sniffing attacks
X-XSS-Protection1Enables XSS filtering and page sanitation in the browser
X-Powered-ByremovedHeader gets removed to hinder server identification
X-Download-OptionsnoopenPrevent automated opening of downloads in legacy browsers
X-DNS-Prefetch-ControloffPrevents general DNS prefetching for links
Cross-Origin-Resource-Policysame-origindisallow cross-origin no-cors requests
X-Frame-OptionsDENYDeny embedding in other sited
Cross-Origin-Opener-Policysame-originDeny sharing browsing context with cross-origin documents
X-Permitted-Cross-Domain-PoliciesnoneDisallow document embedding of the resource
Cross-Origin-Embedder-Policyrequire-corphttps://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy
Referrer-Policyno-referrerDo not send referrer headers
Content-Security-Policysee belowRestrict resource access
Strict-Transport-SecuritymaxAge=15552000;includeSubDomainsMake sure that the site is only accessed via https

Default content-security policies

The following content-security policies are applied in the default setting:

  • default-src 'self';base-uri 'self'
  • font-src 'self' https: data:
  • form-action 'self'
  • frame-ancestors 'self'
  • img-src 'self' data:
  • object-src 'none'
  • script-src 'self'
  • script-src-attr 'none'
  • style-src 'self' https: 'unsafe-inline'
  • upgrade-insecure-requests

Keywords

FastAPI headers http

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.