Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 2.0.0 instead.

Secweb logo

Secweb helps in setting security headers for FastApi and Starlette



Secweb is the pack of middlewares for setting security headers for fastapi and can also be used for any framework created on starlette it has 16 middlewares for setting headers of your website and also for your api(s)

Now all the middlewares are pure ASGI implemented middlewares and from now on all the release will have sigstore signatures you can get them from the github release for both dev and production Secweb.

The PermissionsPolicy middleware lies in development branch here

The list of middleware is as follows:

  1. Content Security Policy (CSP)

  1. ExpectCT (deprecated) ⚠️

  1. Origin Agent Cluster

  1. Referrer Policy

  1. HTTP Strict Transport Security(HSTS)

  1. X-Content-Type-Options

  1. X-DNS-Prefetch-Control

  1. X-Download-Options

  1. X-Frame

  1. X-Permitted-Cross-Domain-Policies

  1. X-XSS-Protection

  1. Cross-Origin-Embedder-Policy

  1. Cross-Origin-Opener-Policy

  1. Cross-Origin-Resource-Policy

  1. Clear-Site-Data

  1. Cache-Control

Requirements

Installation

pip install Secweb

Usage

The package Secweb can be used in two different ways

  1. To use SecWeb class it includes all the 16 classes together

  1. To use the 16 middleware classes separately

SecWeb class

from Secweb import SecWeb

SecWeb(app=app)  # The app is the ASGIapp required by the starlette to give access to the different methods to the class

The above example uses all the default headers value that are are preset you can change the values by creating the option dict you can also set flags for nonce generation for csp header using the script_nonce=True and style_nonce=True flags. For Clear-Site-Data header Routes=[] array has been added. It is empty by default.

from Secweb import SecWeb

SecWeb(app=app, Option={'referrer': {'Referrer-Policy': 'no-referrer'}}, Routes=[], script_nonce=False, style_nonce=False)

The Option uses 13 keys for calling middleware classes to set the user-defined policies. 3 middleware classes doesn`t take any values.

The values are as follows:

  1. 'csp' for calling ContentSecurityPolicy class to set the user-defined values

  1. 'expectCt' for calling ExpectCt class to set the user-defined values ⚠️

  1. 'referrer' for calling ReferrerPolicy class to set the user-defined values

  1. 'xdns' for calling XDNSPrefetchControl class to set the user-defined values

  1. 'xcdp' for calling XPermittedCrossDomainPolicies class to set the user-defined values

  1. 'hsts' for calling HSTS class to set the user-defined values

  1. 'xss' for calling xXSSProtection class to set the user-defined values

  1. 'xframe' for calling XFrame class to set the user-defined values

  1. 'coep' for calling CrossOriginEmbedderPolicy class to set the user-defined values

  1. 'coop' for calling CrossOriginOpenerPolicy class to set the user-defined values

  1. 'corp' for calling CrossOriginResourcePolicy class to set the user-defined values

  1. 'clearSiteData' for calling ClearSiteData class to set the user-defined values

  1. 'cacheControl' for calling CacheControl class to set the user-defined values
# Example of the values
SecWeb(app=app, Option={'csp': {'default-src': ["'self'"]}, 'xframe': {'X-Frame-Options': 'SAMEORIGIN'}, 'xss': {'X-XSS-Protection': '1; mode=block'}, 'hsts': {'max-age': 4, 'preload': True}, 'xcdp': {'X-Permitted-Cross-Domain-Policies': 'all'}, 'xdns': {'X-DNS-Prefetch-Control': 'on'}, 'referrer': {'Referrer-Policy': 'no-referrer'}, 'expectCt': {'max-age': 128, 'enforce': True, 'report-uri': "https://example.com/example"}, 'coep': {'Cross-Origin-Embedder-Policy': 'require-corp'}, 'coop': {'Cross-Origin-Opener-Policy': 'same-origin-allow-popups'}, 'corp': {'Cross-Origin-Resource-Policy': 'same-site'}, 'clearSiteData': {'cache': True, 'storage': True}, 'cacheControl': {'public': True, 's-maxage': 600}}, Routes=['/login/{id}', '/logout/{id:uuid}/username/{username:string}'])

Middleware Classes

Content Security Policy (CSP)

ContentSecurityPolicy class sets the csp header

The Nonce_Processor module generates nonce for csp header

Nonce Processor

    # Some Code
    nonce = Nonce_Processor(DEFAULT_ENTROPY=20)  # inject the nonce variable into the jinja or html
    # Some Code

DEFAULT_ENTROPY is used to set the nonce length. The nonce processor needs to be called on the route the following example is of FastApi calling the nonce processor on the route

from fastapi import FastAPI
from Secweb.ContentSecurityPolicy import Nonce_Processor

app = FastAPI()


@app.get("/")
async def root():
    # some code
    nonce = Nonce_Processor(DEFAULT_ENTROPY=20)  # inject the nonce variable into the jinja or html
    # some more code

ContentSecurityPolicy

This is for the FastApi

from fastapi import FastAPI
from Secweb.ContentSecurityPolicy import Nonce_Processor

app = FastAPI()

app.add_middleware(ContentSecurityPolicy, Option={'default-src': ["'self'"], 'base-uri': ["'self'"], 'block-all-mixed-content': []}, script_nonce=False, style_nonce=False)

This is for the Starlette

from starlette.applications import Starlette
from Secweb.ContentSecurityPolicy import Nonce_Processor

app = Starlette()

app.add_middleware(ContentSecurityPolicy, Option={'default-src': ["'self'"], 'base-uri': ["'self'"], 'block-all-mixed-content': []}, script_nonce=False, style_nonce=False)

script_nonce=False This is the nonce flag for inline Js

style_nonce=False This is the nonce flag for inline css

For more detail on CSP header go to this MDN Docs

ExpectCT ⚠️

ExpectCt class sets the ExpectCt header. The default value will not work for ExpectCt class you need to explicitly set the header.

from fastapi import FastAPI
from Secweb.ExpectCt import ExpectCt

app = FastAPI()

app.add_middleware(ExpectCt, Option={'max-age': 128, 'enforce': True, 'report-uri': "https://example.com/example"})

# OR
from starlette.applications import Starlette
from Secweb.ExpectCt import ExpectCt

app = Starlette()

app.add_middleware(ExpectCt, Option={'max-age': 128, 'enforce': True, 'report-uri': "https://example.com/example"})

For more detail on ExpectCt header go to this MDN Docs

Origin Agent Cluster

OriginAgentCluster class sets the Origin-Agent-Cluster header the class takes no parameters

from fastapi import FastAPI
from Secweb.OriginAgentCluster import OriginAgentCluster

app = FastAPI()

app.add_middleware(OriginAgentCluster)

# OR

from starlette.applications import Starlette
from Secweb.OriginAgentCluster import OriginAgentCluster

app = Starlette()

app.add_middleware(OriginAgentCluster)

For more detail on Origin-Agent-Cluster header go to this WHATWG Site

Referrer Policy

ReferrerPolicy class sets the Referrer-Policy header

from fastapi import FastAPI
from Secweb.ReferrerPolicy import ReferrerPolicy

app = FastAPI()

app.add_middleware(ReferrerPolicy, Option={'Referrer-Policy': 'strict-origin-when-cross-origin'})

# OR

from starlette.applications import Starlette
from Secweb.ReferrerPolicy import ReferrerPolicy

app = Starlette()

app.add_middleware(ReferrerPolicy, Option={'Referrer-Policy': 'strict-origin-when-cross-origin'})

For more detail on Referrer-Policy header go to this MDN Docs

HTTP Strict Transport Security (HSTS)

HSTS class sets the Strict-Transport-Security header

from fastapi import FastAPI
from Secweb.StrictTransportSecurity import HSTS

app = FastAPI()

app.add_middleware(HSTS, Option={'max-age': 4, 'preload': True})

# OR

from starlette.applications import Starlette
from Secweb.StrictTransportSecurity import HSTS

app = Starlette()

app.add_middleware(HSTS, Option={'max-age': 4, 'preload': True})

For more detail on Strict-Transport-Security header go to this MDN Docs

X-Content-Type-Options

XContentTypeOptions class sets the X-Content-Type-Options header the class takes no parameters

from fastapi import FastAPI
from Secweb.XContentTypeOptions import XContentTypeOptions

app = FastAPI()

app.add_middleware(XContentTypeOptions)

# OR

from starlette.applications import Starlette
from Secweb.XContentTypeOptions import XContentTypeOptions

app = Starlette()

app.add_middleware(XContentTypeOptions)

For more detail on X-Content-Type-Options header go to this MDN Docs

X-DNS-Prefetch-Control

XDNSPrefetchControl class sets the X-DNS-Prefetch-Control header

from fastapi import FastAPI
from Secweb.XDNSPrefetchControl import XDNSPrefetchControl

app = FastAPI()

app.add_middleware(XDNSPrefetchControl, Option={'X-DNS-Prefetch-Control': 'on'})

# OR

from starlette.applications import Starlette
from Secweb.XDNSPrefetchControl import XDNSPrefetchControl

app = Starlette()

app.add_middleware(XDNSPrefetchControl, Option={'X-DNS-Prefetch-Control': 'off'})

For more detail on X-DNS-Prefetch-Control header go to this MDN Docs

X-Download-Options

XDownloadOptions class sets the X-Download-Options header the class takes no parameter

from fastapi import FastAPI
from Secweb.XDownloadOptions import XDownloadOptions

app = FastAPI()

app.add_middleware(XDownloadOptions)

# OR

from starlette.applications import Starlette
from Secweb.XDownloadOptions import XDownloadOptions

app = Starlette()

app.add_middleware(XDownloadOptions)

X-Frame

XFrame class sets the X-Frame-Options header

from fastapi import FastAPI
from Secweb.XFrameOptions import XFrame

app = FastAPI()

app.add_middleware(XFrame, Option={'X-Frame-Options': 'DENY'})

# OR

from starlette.applications import Starlette
from Secweb.XFrameOptions import XFrame

app = Starlette()

app.add_middleware(XFrame, Option={'X-Frame-Options': 'DENY'})

For more detail on X-Frame-Options header go to this MDN Docs

X-Permitted-Cross-Domain-Policies

XPermittedCrossDomainPolicies class sets the X-Permitted-Cross-Domain-Policies header

from fastapi import FastAPI
from Secweb.XPermittedCrossDomainPolicies import XPermittedCrossDomainPolicies

app = FastAPI()

app.add_middleware(XPermittedCrossDomainPolicies, Option={'X-Permitted-Cross-Domain-Policies': 'none'})

# OR

from starlette.applications import Starlette
from Secweb.XPermittedCrossDomainPolicies import XPermittedCrossDomainPolicies

app = Starlette()

app.add_middleware(XPermittedCrossDomainPolicies, Option={'X-Permitted-Cross-Domain-Policies': 'none'})

For more detail on X-Permitted-Cross-Domain-Policies header go to this OWASP Site

X-XSS-Protection

xXSSProtection class sets the X-XSS-Protection header

from fastapi import FastAPI
from Secweb.xXSSProtection import xXSSProtection

app = FastAPI()

app.add_middleware(xXSSProtection, Option={'X-XSS-Protection': '0'})

# OR

from starlette.applications import Starlette
from Secweb.xXSSProtection import xXSSProtection

app = Starlette()

app.add_middleware(xXSSProtection, Option={'X-XSS-Protection': '0'})

For more detail on X-XSS-Protection header go to this MDN Docs

Cross Origin Embedder Policy

CrossOriginEmbedderPolicy class sets the Cross Origin Embedder Policy header

from fastapi import FastAPI
from Secweb.CrossOriginEmbedderPolicy import CrossOriginEmbedderPolicy

app = FastAPI()

app.add_middleware(CrossOriginEmbedderPolicy, Option={'Cross-Origin-Embedder-Policy': 'unsafe-none'})

# OR

from starlette.applications import Starlette
from Secweb.CrossOriginEmbedderPolicy import CrossOriginEmbedderPolicy

app = Starlette()

app.add_middleware(CrossOriginEmbedderPolicy, Option={'Cross-Origin-Embedder-Policy': 'unsafe-none'})

For more detail on Cross Origin Embedder Policy header go to this MDN Docs

Cross Origin Opener Policy

CrossOriginOpenerPolicy class sets the Cross Origin Opener Policy header

from fastapi import FastAPI
from Secweb.CrossOriginOpenerPolicy import CrossOriginOpenerPolicy

app = FastAPI()

app.add_middleware(CrossOriginOpenerPolicy, Option={'Cross-Origin-Opener-Policy': 'unsafe-none'})

# OR

from starlette.applications import Starlette
from Secweb.CrossOriginOpenerPolicy import CrossOriginOpenerPolicy

app = Starlette()

app.add_middleware(CrossOriginOpenerPolicy, Option={'Cross-Origin-Opener-Policy': 'unsafe-none'})

For more detail on Cross Origin Opener Policy header go to this MDN Docs

Cross Origin Resource Policy

CrossOriginResourcePolicy class sets the Cross Origin Resource Policy header. You have to call the CrossOriginResourcePolicy class explicitly by providing the 'corp' key in the Option dictionary.

from fastapi import FastAPI
from Secweb.CrossOriginResourcePolicy import CrossOriginResourcePolicy

app = FastAPI()

app.add_middleware(CrossOriginResourcePolicy, Option={'Cross-Origin-Resource-Policy': 'same-site'})

# OR

from starlette.applications import Starlette
from Secweb.CrossOriginResourcePolicy import CrossOriginResourcePolicy

app = Starlette()

app.add_middleware(CrossOriginResourcePolicy, Option={'Cross-Origin-Resource-Policy': 'same-site'})

For more detail on Cross Origin Resource Policy header go to this MDN Docs

Clear Site Data

ClearSiteData class sets the Clear-Site-Data header. In this class the routes array is compulsory so that the header can only be applied to the specified route as it clears every data on the users browser you can add static, dynamic routes like shown below.

from fastapi import FastAPI
from Secweb.ClearSiteData import ClearSiteData

app = FastAPI()

app.add_middleware(ClearSiteData, Option={'cookies': True}, Routes=['/login', '/logout/{id}'])

# OR

from starlette.applications import Starlette
from Secweb.ClearSiteData import ClearSiteData

app = Starlette()

app.add_middleware(ClearSiteData, Option={'cookies': True}, Routes=['/login', '/logout/{id}'])

For more detail on Clear Site Data Header go to this MDN Docs

Cache Control

CacheControl class sets the Cache-Control header. This is useful for controlling cached data on user`s browser

from fastapi import FastAPI
from Secweb.CacheControl import CacheControl

app = FastAPI()

app.add_middleware(CacheControl, Option={'s-maxage': 600, 'public': True})

# OR

from starlette.applications import Starlette
from Secweb.CacheControl import CacheControl

app = Starlette()

app.add_middleware(CacheControl, Option={'s-maxage': 600, 'public': True})

For more detail on Cache Control Header go to this MDN Docs

Contributing

Pull requests and Issues are welcome. For major changes, please open an issue first to discuss what you would like to change.


Github

License

MLP 2.0

Secweb Icon

Secweb Icon © 2021 - 2023 by Motagamwala Taha Arif Ali is licensed under Attribution-NonCommercial-NoDerivatives 4.0 International

Metadata

Release files for Secweb 1.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Secweb 1.7.1
File Size Uploaded Provenance Actions
Secweb-1.7.1.tar.gz 23.3 kB Without provenance Details for Secweb-1.7.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for Secweb 1.7.1
File Interpreter ABI Platform Actions
Secweb-1.7.1-py3-none-any.whl Python 3 none any Details for Secweb-1.7.1-py3-none-any.whl

Total release size: 56.9 kB

Release files / Secweb-1.7.1.tar.gz

Download URL Secweb-1.7.1.tar.gz
Size 23.3 kB
Tags Source
SHA-256 checksum
How to use checksums
458d35e447dec2f93ff79ce1a435790ef38844f7a0260c5f89936a2fc1638b92
BLAKE2b-256 checksum
How to use checksums
a43b13cebc6e40eda6ced020a2d8d821d0f3974418d84249b0deb16470d39fb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / Secweb-1.7.1-py3-none-any.whl

Download URL Secweb-1.7.1-py3-none-any.whl
Size 33.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fdf2d68cb6bb1c61976e0ad9ea7c5a8b6342b5afed8fd5fd835e07313240b47
BLAKE2b-256 checksum
How to use checksums
acf3a39e803c4dc1957205fd2ca8935742e0a8ec1a45a6913abcca1f2085ec87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for Secweb 1.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page