apifrom.security.cspο
Content Security Policy (CSP) middleware for APIFromAnything.
This module provides middleware for adding Content Security Policy headers to API responses.
Overviewο
Classes
BaseMiddlewareCSPMiddleware
Classesο
- BaseMiddleware(**options):bases: abc.ABC
Base middleware class for APIFromAnything.
Helper class for building CSP policies.
- apifrom.security.csp.create_api_policy()ο
- :staticmethod:
Create a CSP policy suitable for APIs.
- returns:
A CSP policy for APIs
- apifrom.security.csp.create_report_only_policy(report_uri)ο
- :staticmethod:
Create a report-only CSP policy.
- param report_uri:
The URI to send violation reports to
- returns:
A report-only CSP policy
- apifrom.security.csp.create_strict_policy()ο
- :staticmethod:
Create a strict CSP policy.
- returns:
A strict CSP policy
- apifrom.security.csp.create_web_policy()ο
- :staticmethod:
Create a CSP policy suitable for web applications.
- returns:
A CSP policy for web applications
Content Security Policy directive constants.
- CSPMiddleware(policy = None, exempt_paths = None, exempt_path_regex = None):bases: apifrom.middleware.base.BaseMiddleware
Middleware for adding Content Security Policy headers to responses.
Initialize the CSP middleware.
- param policy:
The CSP policy to apply
- param exempt_paths:
List of paths to exempt from CSP
- param exempt_path_regex:
Regex pattern for paths to exempt from CSP
- apifrom.security.csp._create_default_policy()ο
Create a default CSP policy.
- returns:
A default CSP policy
- apifrom.security.csp._is_path_exempt(path)ο
Check if a path is exempt from CSP.
- param path:
The request path
- returns:
True if the path is exempt, False otherwise
- apifrom.security.csp.process_request(request)ο
- :async:
Process the request.
- param request:
The request object
- returns:
The processed request
- apifrom.security.csp.process_response(response)ο
- :async:
Process the response and add CSP headers.
- param response:
The response object
- returns:
The response with CSP headers
Content Security Policy nonce generator.
- apifrom.security.csp.generate()ο
- :staticmethod:
Generate a random nonce for CSP.
- returns:
A random nonce string
Content Security Policy builder.
Initialize the CSP policy.
- param report_only:
Whether to use the report-only mode
- apifrom.security.csp.add_base_uri(source)ο
Add base-uri directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_block_all_mixed_content()ο
Add block-all-mixed-content directive.
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_connect_src(source)ο
Add connect-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_default_src(source)ο
Add default-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_directive(directive, source)ο
Add a directive to the policy.
- param directive:
The directive name
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_font_src(source)ο
Add font-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_form_action(source)ο
Add form-action directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_frame_ancestors(source)ο
Add frame-ancestors directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_frame_src(source)ο
Add frame-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_img_src(source)ο
Add img-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_manifest_src(source)ο
Add manifest-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_media_src(source)ο
Add media-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
Add navigate-to directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_object_src(source)ο
Add object-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_prefetch_src(source)ο
Add prefetch-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_require_trusted_types_for(value='script')ο
Add require-trusted-types-for directive.
- param value:
The value for the directive
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_sandbox(value)ο
Add sandbox directive.
- param value:
The sandbox value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_script_src(source)ο
Add script-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_style_src(source)ο
Add style-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_trusted_types(value)ο
Add trusted-types directive.
- param value:
The trusted types value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_upgrade_insecure_requests()ο
Add upgrade-insecure-requests directive.
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.add_worker_src(source)ο
Add worker-src directive.
- param source:
The source value(s)
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.get_header_name()ο
Get the appropriate header name based on the policy mode.
- returns:
The CSP header name
- apifrom.security.csp.set_reporter(reporter)ο
Set the violation reporter.
- param reporter:
The violation reporter
- returns:
The CSP policy instance for chaining
- apifrom.security.csp.to_header()ο
Convert the policy to a header value.
- returns:
The CSP header value
- apifrom.security.csp.to_header_value()ο
Convert the policy to a header value.
This is an alias for to_header() for backward compatibility.
- returns:
The CSP header value
Content Security Policy source constants.
- class apifrom.security.csp.CSPViolationReporter(report_uri=None, report_to=None, callback=None)[source]ο
Content Security Policy violation reporter.
Initialize the CSP violation reporter.
- param report_uri:
The URI to send violation reports to
- param report_to:
The reporting group to send violation reports to
- param callback:
A callback function to handle violation reports
- apifrom.security.csp.get_directives()ο
Get the reporting directives.
- returns:
A dictionary of reporting directives
- apifrom.security.csp.handle_report(report)ο
- :async:
Handle a CSP violation report.
- param report:
The violation report
- class apifrom.security.csp.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.csp._requestο
The underlying Starlette request.
- apifrom.security.csp.path_paramsο
Path parameters extracted from the URL.
- apifrom.security.csp.query_paramsο
Query parameters extracted from the URL.
- apifrom.security.csp.headersο
HTTP headers.
- apifrom.security.csp.methodο
HTTP method.
- apifrom.security.csp.pathο
Request path.
- apifrom.security.csp._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.csp.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.csp.contentο
The response content.
- apifrom.security.csp.status_codeο
The HTTP status code.
- apifrom.security.csp.headersο
HTTP headers.
- apifrom.security.csp.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.