diff --git a/CHANGES.md b/CHANGES.md index bc9f57e..e8ec0e2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ CHANGES ======= Unreleased -=========== +========== - Add type hints. ([#228](https://github.com/mozilla/django-csp/pull/228)) 4.0b1 diff --git a/csp/checks.py b/csp/checks.py index 9c15b0a..c7ca61a 100644 --- a/csp/checks.py +++ b/csp/checks.py @@ -1,6 +1,8 @@ from __future__ import annotations + import pprint -from typing import Dict, Tuple, Any, Optional, Sequence, TYPE_CHECKING, List +from collections.abc import Sequence +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple from django.conf import settings from django.core.checks import Error diff --git a/csp/context_processors.py b/csp/context_processors.py index 4c34e0a..13f3808 100644 --- a/csp/context_processors.py +++ b/csp/context_processors.py @@ -1,5 +1,6 @@ from __future__ import annotations -from typing import Dict, Literal, TYPE_CHECKING + +from typing import TYPE_CHECKING, Dict, Literal if TYPE_CHECKING: from django.http import HttpRequest diff --git a/csp/contrib/rate_limiting.py b/csp/contrib/rate_limiting.py index 3645a47..9633a87 100644 --- a/csp/contrib/rate_limiting.py +++ b/csp/contrib/rate_limiting.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING + import random +from typing import TYPE_CHECKING from django.conf import settings diff --git a/csp/extensions/__init__.py b/csp/extensions/__init__.py index 45c4de4..8ddc782 100644 --- a/csp/extensions/__init__.py +++ b/csp/extensions/__init__.py @@ -1,5 +1,6 @@ from __future__ import annotations -from typing import Callable, TYPE_CHECKING, Any + +from typing import TYPE_CHECKING, Any, Callable from jinja2 import nodes from jinja2.ext import Extension diff --git a/csp/middleware.py b/csp/middleware.py index 03cc745..ff58c61 100644 --- a/csp/middleware.py +++ b/csp/middleware.py @@ -1,4 +1,5 @@ from __future__ import annotations + import base64 import http.client as http_client import os diff --git a/csp/templatetags/csp.py b/csp/templatetags/csp.py index a28bfc1..8cdf83f 100644 --- a/csp/templatetags/csp.py +++ b/csp/templatetags/csp.py @@ -1,12 +1,14 @@ from __future__ import annotations + from typing import TYPE_CHECKING, Optional + from django import template from django.template.base import token_kwargs from csp.utils import build_script_tag if TYPE_CHECKING: - from django.template.base import NodeList, FilterExpression, Token, Parser + from django.template.base import FilterExpression, NodeList, Parser, Token from django.template.context import Context register = template.Library() @@ -18,7 +20,7 @@ def _unquote(s: str) -> str: @register.tag(name="script") -def script(parser: Parser, token: Token) -> "NonceScriptNode": +def script(parser: Parser, token: Token) -> NonceScriptNode: # Parse out any keyword args token_args = token.split_contents() kwargs = token_kwargs(token_args[1:], parser) diff --git a/csp/tests/environment.py b/csp/tests/environment.py index a5d188d..e53a562 100644 --- a/csp/tests/environment.py +++ b/csp/tests/environment.py @@ -1,6 +1,7 @@ -from jinja2 import Environment from typing import Any +from jinja2 import Environment + def environment(**options: Any) -> Environment: env = Environment(**options) diff --git a/csp/tests/settings.py b/csp/tests/settings.py index ffad00d..8a0ea7e 100644 --- a/csp/tests/settings.py +++ b/csp/tests/settings.py @@ -1,6 +1,5 @@ from csp.constants import NONCE, SELF - CONTENT_SECURITY_POLICY = { "DIRECTIVES": { "default-src": [SELF, NONCE], diff --git a/csp/tests/test_decorators.py b/csp/tests/test_decorators.py index 1a11dfd..7e29b69 100644 --- a/csp/tests/test_decorators.py +++ b/csp/tests/test_decorators.py @@ -1,11 +1,13 @@ from __future__ import annotations + from typing import TYPE_CHECKING -import pytest from django.http import HttpResponse from django.test import RequestFactory from django.test.utils import override_settings +import pytest + from csp.constants import HEADER, HEADER_REPORT_ONLY, NONCE from csp.decorators import csp, csp_exempt, csp_replace, csp_update from csp.middleware import CSPMiddleware diff --git a/csp/tests/test_utils.py b/csp/tests/test_utils.py index d76f49a..74d5c4f 100644 --- a/csp/tests/test_utils.py +++ b/csp/tests/test_utils.py @@ -2,7 +2,7 @@ from django.utils.functional import lazy from csp.constants import NONCE, NONE, SELF -from csp.utils import build_policy, default_config, DEFAULT_DIRECTIVES +from csp.utils import DEFAULT_DIRECTIVES, build_policy, default_config def policy_eq(a: str, b: str) -> None: diff --git a/csp/tests/utils.py b/csp/tests/utils.py index 36512ca..bcd6120 100644 --- a/csp/tests/utils.py +++ b/csp/tests/utils.py @@ -1,6 +1,7 @@ from __future__ import annotations + from abc import ABC, abstractmethod -from typing import Dict, Optional, TYPE_CHECKING, Callable, Any, Tuple +from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple from django.http import HttpResponse from django.template import Context, Template, engines diff --git a/csp/utils.py b/csp/utils.py index fe35633..723b967 100644 --- a/csp/utils.py +++ b/csp/utils.py @@ -2,7 +2,7 @@ import re from collections import OrderedDict from itertools import chain -from typing import Any, Dict, Optional, Union, Callable +from typing import Any, Callable, Dict, Optional, Union from django.conf import settings from django.utils.encoding import force_str diff --git a/docs/configuration.rst b/docs/configuration.rst index 2980339..c226591 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -77,7 +77,7 @@ a more slightly strict policy and is used to test the policy without breaking th signifies that you do not want any sources for this directive. The ``None`` value is a Python keyword that represents the absence of a value and when used as the value of a directive, it will remove the directive from the policy. - + This is useful when using the ``@csp_replace`` decorator to effectively clear a directive from the base configuration as defined in the settings. For example, if the Django settings the ``frame-ancestors`` directive is set to a list of sources and you want to remove the @@ -124,9 +124,9 @@ policy. The CSP keyword values of ``'self'``, ``'unsafe-inline'``, ``'strict-dynamic'``, etc. must be quoted! e.g.: ``"default-src": ["'self'"]``. Without quotes they will not work as intended. - + New in version 4.0 are CSP keyword constants. Use these to minimize quoting mistakes and typos. - + The following CSP keywords are available: * ``NONE`` = ``"'none'"`` @@ -140,9 +140,9 @@ policy. * ``WASM_UNSAFE_EVAL`` = ``"'wasm-unsafe-eval'"`` Example usage: - + .. code-block:: python - + from csp.constants import SELF, STRICT_DYNAMIC CONTENT_SECURITY_POLICY = { @@ -318,4 +318,4 @@ the :ref:`decorator documentation ` for more details. .. _block-all-mixed-content_mdn: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/block-all-mixed-content .. _plugin_types_mdn: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/plugin-types .. _prefetch_src_mdn: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/prefetch-src -.. _strict-csp: https://csp.withgoogle.com/docs/strict-csp.html \ No newline at end of file +.. _strict-csp: https://csp.withgoogle.com/docs/strict-csp.html diff --git a/docs/migration-guide.rst b/docs/migration-guide.rst index 5320c8f..4c78327 100644 --- a/docs/migration-guide.rst +++ b/docs/migration-guide.rst @@ -106,11 +106,11 @@ The new settings would be: .. note:: If you were using the ``CSP_INCLUDE_NONCE_IN`` setting, this has been removed in the new settings - format. - + format. + **Previously:** You could use the ``CSP_INCLUDE_NONCE_IN`` setting to specify which directives in your Content Security Policy (CSP) should include a nonce. - + **Now:** You can include a nonce in any directive by adding the ``NONCE`` constant from the ``csp.constants`` module to the list of sources for that directive.