apifrom.security.sriο
Subresource Integrity (SRI) implementation for APIFromAnything.
This module provides utilities for implementing Subresource Integrity (SRI) to ensure that resources loaded from external sources have not been tampered with.
Overviewο
Classes
BaseMiddlewareSRIHashAlgorithmSRIMiddleware
Classesο
- BaseMiddleware(**options):bases: abc.ABC
Base middleware class for APIFromAnything.
- class apifrom.security.sri.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.sri._requestο
The underlying Starlette request.
- apifrom.security.sri.path_paramsο
Path parameters extracted from the URL.
- apifrom.security.sri.query_paramsο
Query parameters extracted from the URL.
- apifrom.security.sri.headersο
HTTP headers.
- apifrom.security.sri.methodο
HTTP method.
- apifrom.security.sri.pathο
Request path.
- apifrom.security.sri._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.sri.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.sri.contentο
The response content.
- apifrom.security.sri.status_codeο
The HTTP status code.
- apifrom.security.sri.headersο
HTTP headers.
- apifrom.security.sri.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.
Helper class for building SRI policies.
- apifrom.security.sri.create_common_cdn_policy()ο
- :staticmethod:
Create an SRI policy for common CDN resources.
- returns:
An SRI policy for common CDN resources
- apifrom.security.sri.create_empty_policy()ο
- :staticmethod:
Create an empty SRI policy.
- returns:
An empty SRI policy
- apifrom.security.sri.create_policy_from_html(html_content)ο
- :staticmethod:
- :async:
Create an SRI policy from HTML content by extracting script and link tags.
- param html_content:
The HTML content to extract sources from
- returns:
An SRI policy with the extracted sources
Utility for generating Subresource Integrity hashes.
- apifrom.security.sri.generate_hash(content, algorithm=SRIHashAlgorithm.SHA384)ο
- :staticmethod:
Generate a Subresource Integrity hash for the given content.
- param content:
The content to hash (string or bytes)
- param algorithm:
The hash algorithm to use
- returns:
The SRI hash string in the format βalgorithm-base64hashβ
- apifrom.security.sri.generate_integrity_attribute(content, algorithms=None)ο
- :staticmethod:
Generate a complete integrity attribute for HTML elements.
- param content:
The content to hash
- param algorithms:
The hash algorithms to use (defaults to [SHA384])
- returns:
The integrity attribute value with multiple hashes if requested
- apifrom.security.sri.verify_integrity(content, integrity_value)ο
- :staticmethod:
Verify that content matches an integrity value.
- param content:
The content to verify
- param integrity_value:
The integrity value to check against
- returns:
True if the content matches any of the hashes in the integrity value
- SRIHashAlgorithm:bases: enum.Enum
Hash algorithms supported by Subresource Integrity.
- SRIMiddleware(script_sources = None, style_sources = None, verify_external_resources = False, algorithms = None, exempt_paths = None):bases: apifrom.middleware.base.BaseMiddleware
Middleware for adding Subresource Integrity headers to responses.
This middleware can modify HTML responses to add integrity attributes to script and link tags that load external resources.
- param script_sources:
Dictionary mapping script URLs to their integrity values
- param style_sources:
Dictionary mapping style URLs to their integrity values
- param verify_external_resources:
Whether to verify external resources
- param algorithms:
List of hash algorithms to use for verification
- param exempt_paths:
Paths exempt from SRI
- apifrom.security.sri._add_integrity_to_html(html_content)ο
Add integrity attributes to script and link tags in HTML content.
- param html_content:
The HTML content to modify
- returns:
The modified HTML content
- apifrom.security.sri._fetch_and_compute_integrity(url)ο
- :async:
Fetch a resource and compute its integrity value.
- param url:
The URL of the resource to fetch
- returns:
The integrity value, or None if the resource could not be fetched
- apifrom.security.sri._is_exempt(request)ο
Check if a request is exempt from SRI processing.
- param request:
The request to check
- returns:
True if the request is exempt, False otherwise
- apifrom.security.sri.process_request(request)ο
- :async:
Process a request through the SRI middleware.
- param request:
The request to process
- returns:
The processed request
- apifrom.security.sri.process_response(response)ο
- :async:
Process a response through the SRI middleware.
- param response:
The response to process
- returns:
The processed response
Policy for configuring Subresource Integrity.
Initialize the SRI policy.
- apifrom.security.sri._compute_integrity(url)ο
- :async:
Compute the integrity value for a URL.
- param url:
The URL to compute the integrity for
- returns:
The integrity value, or None if it could not be computed
- apifrom.security.sri.add_script_source(url, integrity=None)ο
Add a script source to the policy.
- param url:
The URL of the script
- param integrity:
The integrity value (will be computed if None)
- returns:
The SRI policy instance for chaining
- apifrom.security.sri.add_style_source(url, integrity=None)ο
Add a style source to the policy.
- param url:
The URL of the stylesheet
- param integrity:
The integrity value (will be computed if None)
- returns:
The SRI policy instance for chaining
- apifrom.security.sri.compute_missing_integrity_values()ο
- :async:
Compute integrity values for sources that donβt have them.
- returns:
The SRI policy instance for chaining
- apifrom.security.sri.enable_verification(enable=True)ο
Enable or disable verification of external resources.
- param enable:
Whether to enable verification
- returns:
The SRI policy instance for chaining
- apifrom.security.sri.set_algorithms(algorithms)ο
Set the hash algorithms to use.
- param algorithms:
The hash algorithms to use
- returns:
The SRI policy instance for chaining