-
-
Notifications
You must be signed in to change notification settings - Fork 527
Django 6.0: Add stubs for built-in CSP support #2931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -541,3 +541,9 @@ SECURE_REDIRECT_EXEMPT: list[str] | |||||||||
| SECURE_REFERRER_POLICY: str | ||||||||||
| SECURE_SSL_HOST: str | None | ||||||||||
| SECURE_SSL_REDIRECT: bool | ||||||||||
|
|
||||||||||
| ################## | ||||||||||
| # CSP MIDDLEWARE # | ||||||||||
| ################## | ||||||||||
| SECURE_CSP: dict[str, Any] = {} | ||||||||||
| SECURE_CSP_REPORT_ONLY: dict[str, Any] = {} | ||||||||||
|
||||||||||
| SECURE_CSP: dict[str, Any] = {} | |
| SECURE_CSP_REPORT_ONLY: dict[str, Any] = {} | |
| SECURE_CSP: dict[str, Any] | |
| SECURE_CSP_REPORT_ONLY: dict[str, Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might also type it as Sequence[str] instead of Any maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently SECURE_CSP_REPORT_ONLY = {"report-uri": "/path/to/reports-endpoint/"} is a valid value, so it's not always Sequence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would Sequence[str] | str cover all use cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. Check the docs and if that is inconclusive, maybe check source too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the docs:
The setting must be a mapping (typically a dictionary) of directive names to their values. Each key should be a valid CSP directive such as default-src or script-src. The corresponding value can be a list, tuple, or set of source expressions or URLs to allow for that directive. If a set is used, it will be automatically sorted to ensure consistent output in the generated headers.
Based on this, I updated the type for the policies to be Mapping[str, Collection[str] | str]. Let me know if that works.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from django.http import HttpRequest, HttpResponse | ||
| from django.utils.csp import CSP as CSP | ||
| from django.utils.deprecation import MiddlewareMixin | ||
|
|
||
| class ContentSecurityPolicyMiddleware(MiddlewareMixin): | ||
| def process_request(self, request: HttpRequest) -> None: ... | ||
| def process_response(self, request: HttpRequest, response: HttpResponse) -> HttpResponse: ... |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import sys | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| from enum import StrEnum | ||
| else: | ||
| from enum import Enum | ||
|
|
||
| class ReprEnum(Enum): ... # type: ignore[misc] | ||
| class StrEnum(str, ReprEnum): ... # type: ignore[misc] | ||
|
|
||
| class CSP(StrEnum): | ||
intgr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| HEADER_ENFORCE = "Content-Security-Policy" | ||
| HEADER_REPORT_ONLY = "Content-Security-Policy-Report-Only" | ||
|
|
||
| NONE = "'none'" | ||
| REPORT_SAMPLE = "'report-sample'" | ||
| SELF = "'self'" | ||
| STRICT_DYNAMIC = "'strict-dynamic'" | ||
| UNSAFE_EVAL = "'unsafe-eval'" | ||
| UNSAFE_HASHES = "'unsafe-hashes'" | ||
| UNSAFE_INLINE = "'unsafe-inline'" | ||
| WASM_UNSAFE_EVAL = "'wasm-unsafe-eval'" | ||
|
|
||
| NONCE = "<CSP_NONCE_SENTINEL>" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from collections.abc import Callable | ||
| from typing import Any, TypeVar | ||
|
|
||
| _F = TypeVar("_F", bound=Callable[..., Any]) | ||
|
|
||
| def csp_override(config: dict[str, Any]) -> Callable[[_F], _F]: ... | ||
| def csp_report_only_override(config: dict[str, Any]) -> Callable[[_F], _F]: ... | ||
intgr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.