Skip to content

Commit 295a04f

Browse files
committed
Add type stubs for built-in CSP support in Django 6.0
1 parent 9c2e6ad commit 295a04f

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

django-stubs/conf/global_settings.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,9 @@ SECURE_REDIRECT_EXEMPT: list[str]
541541
SECURE_REFERRER_POLICY: str
542542
SECURE_SSL_HOST: str | None
543543
SECURE_SSL_REDIRECT: bool
544+
545+
##################
546+
# CSP MIDDLEWARE #
547+
##################
548+
SECURE_CSP: dict[str, Any] = {}
549+
SECURE_CSP_REPORT_ONLY: dict[str, Any] = {}

django-stubs/middleware/csp.pyi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import TYPE_CHECKING
2+
3+
from django.utils.csp import CSP as CSP
4+
5+
if TYPE_CHECKING:
6+
from django.http import HttpRequest, HttpResponse
7+
from django.utils.deprecation import MiddlewareMixin
8+
9+
class CSPMiddleware(MiddlewareMixin):
10+
def process_request(self, request: HttpRequest) -> None: ...
11+
def process_response(self, request: HttpRequest, response: HttpResponse) -> HttpResponse: ...

django-stubs/utils/csp.pyi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
3+
if sys.version_info >= (3, 11):
4+
from enum import StrEnum
5+
else:
6+
from enum import Enum
7+
8+
class ReprEnum(Enum): ... # type: ignore[misc]
9+
class StrEnum(str, ReprEnum): ... # type: ignore[misc]
10+
11+
class CSP(StrEnum):
12+
HEADER_ENFORCE = "Content-Security-Policy"
13+
HEADER_REPORT_ONLY = "Content-Security-Policy-Report-Only"
14+
15+
NONE = "'none'"
16+
REPORT_SAMPLE = "'report-sample'"
17+
SELF = "'self'"
18+
STRICT_DYNAMIC = "'strict-dynamic'"
19+
UNSAFE_EVAL = "'unsafe-eval'"
20+
UNSAFE_HASHES = "'unsafe-hashes'"
21+
UNSAFE_INLINE = "'unsafe-inline'"
22+
WASM_UNSAFE_EVAL = "'wasm-unsafe-eval'"
23+
24+
NONCE = "<CSP_NONCE_SENTINEL>"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from collections.abc import Callable
2+
from typing import Any, TypeVar
3+
4+
_F = TypeVar("_F", bound=Callable[..., Any])
5+
6+
def csp_override(config: dict[str, Any]) -> Callable[[_F], _F]: ...
7+
def csp_report_only_override(config: dict[str, Any]) -> Callable[[_F], _F]: ...

0 commit comments

Comments
 (0)