Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions django-stubs/conf/global_settings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SECURE_CSP: dict[str, Any] = {}
SECURE_CSP_REPORT_ONLY: dict[str, Any] = {}
SECURE_CSP: dict[str, Any]
SECURE_CSP_REPORT_ONLY: dict[str, Any]

Copy link
Member

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

7 changes: 7 additions & 0 deletions django-stubs/middleware/csp.pyi
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: ...
1 change: 1 addition & 0 deletions django-stubs/template/context_processors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from django.utils.functional import SimpleLazyObject

_R = TypeVar("_R", bound=HttpRequest)

def csp(request: HttpRequest) -> dict[str, SimpleLazyObject | None]: ...
def csrf(request: HttpRequest) -> dict[str, SimpleLazyObject]: ...
def debug(request: HttpRequest) -> dict[str, Callable | bool]: ...
def i18n(request: HttpRequest) -> dict[str, list[tuple[str, str]] | bool | str]: ...
Expand Down
24 changes: 24 additions & 0 deletions django-stubs/utils/csp.pyi
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):
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>"
7 changes: 7 additions & 0 deletions django-stubs/views/decorators/csp.pyi
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]: ...
6 changes: 0 additions & 6 deletions scripts/stubtest/allowlist_todo_django60.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
django.conf.FORMS_URLFIELD_ASSUME_HTTPS_DEPRECATED_MSG
django.conf.global_settings.FORMS_URLFIELD_ASSUME_HTTPS
django.conf.global_settings.SECURE_CSP
django.conf.global_settings.SECURE_CSP_REPORT_ONLY
django.conf.global_settings.TASKS
django.conf.global_settings.URLIZE_ASSUME_HTTPS
django.contrib.admin.AdminSite.password_change_form
Expand Down Expand Up @@ -212,7 +210,6 @@ django.forms.ClearableFileInput.use_fieldset
django.forms.models.BaseModelForm.validate_constraints
django.forms.renderers.Jinja2DivFormRenderer
django.forms.widgets.ClearableFileInput.use_fieldset
django.middleware.csp
django.tasks
django.tasks.backends
django.tasks.backends.base
Expand All @@ -223,7 +220,6 @@ django.tasks.checks
django.tasks.exceptions
django.tasks.signals
django.template.base.PartialTemplate
django.template.context_processors.csp
django.template.defaulttags.PartialDefNode
django.template.defaulttags.PartialNode
django.template.defaulttags.partial_func
Expand All @@ -232,7 +228,6 @@ django.test.runner.QueryFormatter
django.test.selenium.SeleniumTestCase.get_browser_logs
django.test.testcases._AssertTemplateUsedContext.rendered_template_names
django.utils.copy
django.utils.csp
django.utils.datastructures.DeferredSubDict
django.utils.deprecation.RemovedInDjango60Warning
django.utils.deprecation.RemovedInDjango70Warning
Expand All @@ -245,4 +240,3 @@ django.utils.itercompat
django.utils.json
django.utils.log.log_message
django.utils.text.acompress_sequence
django.views.decorators.csp
Loading