apifrom.security.hstsο
HTTP Strict Transport Security (HSTS) implementation for APIFromAnything.
This module provides utilities for implementing HSTS preloading to ensure that browsers always use HTTPS for your API.
Overviewο
Classes
BaseMiddlewareHSTSMiddleware
Classesο
- BaseMiddleware(**options):bases: abc.ABC
Base middleware class for APIFromAnything.
- HSTSMiddleware(max_age = 31536000, include_subdomains = True, preload = False, force_https_redirect = True, exempt_paths = None):bases: apifrom.middleware.base.BaseMiddleware
Middleware for implementing HTTP Strict Transport Security (HSTS).
This middleware adds the Strict-Transport-Security header to responses to instruct browsers to only use HTTPS for your API.
Initialize the HSTS middleware.
- param max_age:
The time, in seconds, that the browser should remember that a site is only to be accessed using HTTPS
- param include_subdomains:
Whether the HSTS policy applies to all subdomains
- param preload:
Whether to include the site in the HSTS preload list
- param force_https_redirect:
Whether to redirect HTTP requests to HTTPS
- param exempt_paths:
Paths exempt from HSTS
- apifrom.security.hsts._add_hsts_header(response)ο
Add the Strict-Transport-Security header to a response.
- param response:
The response to add the header to
- apifrom.security.hsts._build_hsts_header()ο
Build the Strict-Transport-Security header value.
- returns:
The header value
- apifrom.security.hsts._get_https_redirect_url(request)ο
Get the HTTPS redirect URL for a request.
- param request:
The request to redirect
- returns:
The HTTPS redirect URL
- apifrom.security.hsts._is_exempt(request)ο
Check if a request is exempt from HSTS.
- param request:
The request to check
- returns:
True if the request is exempt, False otherwise
- apifrom.security.hsts._is_https(request)ο
Check if a request is using HTTPS.
- param request:
The request to check
- returns:
True if the request is using HTTPS, False otherwise
- apifrom.security.hsts.process_request(request)ο
- :async:
Process a request through the HSTS middleware.
- param request:
The request to process
- returns:
The processed request
- apifrom.security.hsts.process_response(response)ο
- :async:
Process a response through the HSTS middleware.
- param response:
The response to process
- returns:
The processed response
Utility for checking if a domain is eligible for HSTS preloading.
This class provides methods to check if a domain meets the requirements for inclusion in the HSTS preload list.
- apifrom.security.hsts.check_eligibility(domain, hsts_header, has_valid_certificate=True, all_subdomains_https=False, redirect_to_https=True)ο
- :staticmethod:
Check if a domain is eligible for HSTS preloading.
- param domain:
The domain to check
- param hsts_header:
The Strict-Transport-Security header value
- param has_valid_certificate:
Whether the domain has a valid SSL/TLS certificate
- param all_subdomains_https:
Whether all subdomains support HTTPS
- param redirect_to_https:
Whether the domain redirects HTTP to HTTPS
- returns:
A dictionary with the eligibility status and any issues
- apifrom.security.hsts.get_submission_instructions(domain)ο
- :staticmethod:
Get instructions for submitting a domain to the HSTS preload list.
- param domain:
The domain to submit
- returns:
Instructions for submitting the domain
- class apifrom.security.hsts.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]ο
Request class for APIFromAnything.
This class wraps a Starlette request and provides methods for accessing request data in a convenient way.
- apifrom.security.hsts._requestο
The underlying Starlette request.
- apifrom.security.hsts.path_paramsο
Path parameters extracted from the URL.
- apifrom.security.hsts.query_paramsο
Query parameters extracted from the URL.
- apifrom.security.hsts.headersο
HTTP headers.
- apifrom.security.hsts.methodο
HTTP method.
- apifrom.security.hsts.pathο
Request path.
- apifrom.security.hsts._bodyο
Cached request body.
Initialize a new Request instance.
- param request:
The underlying Starlette request.
- param path_params:
Path parameters extracted from the URL.
- param method:
The HTTP method.
- param path:
The request path.
- param query_params:
Query parameters.
- param headers:
HTTP headers.
- param body:
Request body.
- param client_ip:
Client IP address.
- class apifrom.security.hsts.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]ο
Response class for APIFromAnything.
This class represents an HTTP response and provides methods for setting response data, status code, and headers.
- apifrom.security.hsts.contentο
The response content.
- apifrom.security.hsts.status_codeο
The HTTP status code.
- apifrom.security.hsts.headersο
HTTP headers.
- apifrom.security.hsts.content_typeο
The content type of the response.
Initialize a new Response instance.
- param content:
The response content.
- param status_code:
The HTTP status code.
- param headers:
HTTP headers.
- param content_type:
The content type of the response.