summaryrefslogtreecommitdiff
path: root/django/utils
AgeCommit message (Collapse)Author
9 daysFixed #37482 -- Corrected parse_time() docs about UTC offsets.Ines Besrour
12 daysFixed CVE-2026-113593 -- Differentiated empty and missing headers in ↵Andrew James Gonzalez
response cache. When learning the cache key for a response, a missing header must be cached differently to an empty one, as per RFC 9111. Thanks to Andrew Gonzalez for the report. Co-authored-by: Jake Howard <git@theorangeone.net>
2026-10-26Fixed #37232 -- Added support for the HTTP QUERY method.Michiel W. Beijen
QUERY (RFC 10008) is a safe, idempotent method that carries the query in the request content rather than in the URI's query component. Treat it as safe throughout: exempt it from CSRF protection, evaluate request preconditions as for GET, accept it in require_safe(), and dispatch it from View. Query content is available as HttpRequest.body and is not parsed into HttpRequest.POST. Caching is left for a follow-up. Per RFC 10008 Section 2.7 the cache key for a QUERY request must incorporate the request content, which the cache middleware and cache_page() don't do, so they continue to ignore QUERY. Thanks Ben Atkinson, Jonathan Biemond, and Jacob Walls for reviews.
2026-10-23Fixed #30949 -- Deprecated django.utils.functional.cached_property.Jacob Rief
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
2026-10-23Refs #30949 -- Replaced django.utils.functional.cached_property with ↵Jacob Rief
functools.cached_property. Since Python 3.12, `functools.cached_property` no longer takes a lock (https://github.com/python/cpython/issues/87634), which was the main objection to using it instead of Django's own implementation. `Prefetch(to_attr=...)` and the admindocs model detail page now accept `functools.cached_property` as well as Django's own `cached_property`. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
2026-10-22Fixed #37395 -- Emitted 'none' for CSP directives left empty by an unused nonce.Sanskar Kharya
Before, if a directive's only source was a nonce, and that nonce was not rendered, the directive was omitted, when in reality we should consider the set of scripts matching the nonce to be empty, and instead fail closed. Thanks Rob Hudson for the implementation idea and review.
2026-10-22Fixed #37401 -- Aligned safe_makedirs() with os.makedirs() semantics.Natalia
Python 3.15 added the `parent_mode` argument to `os.makedirs()`, the upstream fix for the issue that led Django to vendor its own `makedirs()` in the fix for CVE-2026-25674. The vendored copy differed from CPython only by an extra `chmod()` call that ignored the process umask. Dropping that call allows using `os.makedirs()` directly on Python 3.15+, and removing the vendored copy once Python 3.14 support ends. As a trade-off, directories created by `FileSystemStorage` when `FILE_UPLOAD_DIRECTORY_PERMISSIONS` (or `directory_permissions_mode`) is set, and by `FileBasedCache`, now have the requested permissions mode combined with the process umask, and the set-group-ID bit and other bits beyond the last 9 are no longer guaranteed to be set. `FILE_UPLOAD_PERMISSIONS` is unaffected. Thanks Shai Berger for the proposal and review, Ken Whitesell and Carlton Gibson for the discussion, and Tim Schilling, Jacob Walls, and Lily for reviews. Refs #36664.
2026-10-22Refs #37433 -- Added django.utils.text.trim_value() helper.Natalia
The debug page and the raster lookup check each trimmed large values to 4096 characters with their own copy of the same code. The shared helper also corrects the suffix, which reported characters as bytes and showed the original length as if it were the amount trimmed. The trimmed value and the suffix are returned separately so callers can escape the value without escaping the suffix. Refs #34746.
2026-10-21Refs #37419 -- Moved CSP nonce detection helper to django.utils.csp for ↵Sarah Boyce
middleware reuse.
2026-10-14Fixed #37424 -- Fixed whitespace handling when parsing max-age directives.Jacob Walls
2026-10-06Fixed CVE-2026-84429 -- Simplified parse_header_parameters by leveraging ↵Natalia
stdlib's Message. The `parse_header_parameters` function historically used Python's `cgi` module (now deprecated). In 34e2148fc725e7200050f74130d7523e3cd8507a, the logic was inlined to work around this deprecation (#33173). Later, in d4d5427571b4bf3a21c902276c2a00215c2a37cc, the header parsing logic was further cleaned up to align with multipartparser.py (#33697). It was then replaced with Python's email.message.Message API in #35440, and reverted in #36520 over a performance concern on common headers. But recently, a security report showed that `parse_header_parameters()` parsed header values in time quadratic in the number of separators inside a quoted parameter. Because the per-call length limit does not bound the combined size of a repeated header, an unauthenticated request could consume excessive CPU, for example via the `Accept` header, which is parsed by `HttpRequest.accepted_types`. Therefore, this change restores the `email.message.Message` approach, which parses in linear time, while keeping the fast path for headers without parameters and continuing to reject invalid RFC 2231 encodings (#36991). Thanks to Raphael Gaschignard for testing, and to Jake Howard, Adam Johnson, and Shai Berger for reviews. Thanks to Jisung Chae and Peter Thomassen for the security report. Co-authored-by: Khudyakov Artem <khudyak.artem@gmail.com> Co-authored-by: Ben Cail <bcail@crossway.org>
2026-10-06Fixed CVE-2026-77050 -- Prevented memory exhaustion in ↵Sarah Boyce
get_supported_language_variant(). The fix for CVE-2024-39614 limited the length of language codes processed by get_supported_language_variant(), but the full language code was retained as an lru_cache key before the length check. Moved caching after language code truncation and validation to prevent excessively long language codes from being retained in memory. Thank you to Gleb Lizunov for the report and Jacob Walls for the review.
2026-09-27Fixed #37066 -- Wrapped async iteration sites in (maybe_)aclosing.Thomas Grainger
Replaced bare `async for` over a not-locally-defined async iterable with `async with (maybe_)aclosing(aiter(...)) as it: async for item in it:` at every consumption site, so the iterator's aclose() runs deterministically when the consumer exits — instead of being deferred to asyncio's asyncgen finalizer hook. Added django.utils.asyncio.maybe_aclosing as a small wrapper that uses contextlib.aclosing when the iterator has aclose() and contextlib.nullcontext otherwise (for caller-supplied iterators that may only implement __aiter__ and __anext__). Sites: - contrib/auth/backends.py:_aget_permissions (aclosing) - core/paginator.py:AsyncPage.__aiter__ (maybe_aclosing) - core/paginator.py:AsyncPage.aget_object_list (maybe_aclosing) - db/models/query.py:QuerySet.aiterator (aclosing, hoisted across both prefetch and no-prefetch branches) - http/response.py:StreamingHttpResponse.streaming_content awrapper (maybe_aclosing) - http/response.py:StreamingHttpResponse.__iter__ to_list (maybe_aclosing) - http/response.py:StreamingHttpResponse.__aiter__ (aclosing) - shortcuts.py:aget_list_or_404 (aclosing) - test/client.py:aclosing_iterator_wrapper (maybe_aclosing) - utils/text.py:acompress_sequence (maybe_aclosing)
2026-09-15Fixed false positive in func_supports_parameter() for non-keyword args.Mike Edmunds
Clarified that the internal utility `func_supports_parameter()` is meant to check whether `func` supports a particular named argument, and changed it to return False for "args" and "kwargs" on functions that accept variable `*args` and `**kwargs`, as well as for positional-only parameters, which likewise cannot be passed by name.
2026-09-04Fixed #36523 -- Added django.utils.module_loading.qualname() helper.VimalN2005
Thanks Jake Howard for the reivew.
2026-09-04Fixed #37271 -- Added calendar version support to django.utils.version.Natalia
From Django 2028, feature releases are calendar versioned as YYYY[.N], where N is the patch number, as per DEP 20. A version is now read as the numbers of the printed version followed by the status and its iteration, so calendar versions have four components, (year, patch, status, iteration), while the earlier scheme keeps its five. django.VERSION is now a VersionTuple, providing `.feature`, `.patch`, `.status`, and `.iteration` attributes which give the same answers under both schemes. Indexing it from its third component on, reading it in a way which depends on its length, and comparing it against three or more components are deprecated, as each breaks or silently changes meaning when the tuple loses a component. Thanks Carlton Gibson for reviews.
2026-09-04Refs #37178 -- Deferred django.middleware import in django.utils.deprecation.Natalia
django.utils.deprecation imported django.middleware.MiddlewareMixin at module level, solely to serve its deprecated __getattr__ import path. That gave the module a hard dependency on asgiref (via django.middleware) merely to be imported, which breaks in contexts where Django's own runtime dependencies aren't installed yet, such as a PEP 517 build backend resolving django.__version__ before installing install_requires.
2026-09-03Refs #37293 -- Updated deprecation docs for calendar-based warning names.Natalia
2026-09-03Fixed #37293 -- Renamed RemovedInDjango71Warning to RemovedInDjango2029Warning.Carlton Gibson
Per DEP 20, Django's version numbers are calendar-based from 2028, so deprecation warning classes are now named after the calendar version that removes the feature they mark, and the deprecation timeline is headed by the same calendar version. Updated references to RemovedInDjango71Warning to use the new warning, and adjusted references to "Django 7.1" to "Django 2029".
2026-09-03Refs #37293 -- Aliased RemovedInDjango70Warning to RemovedInDjango2028Warning.Carlton Gibson
Updated references to RemovedInDjango70Warning to use the new warning, and adjusted references to "Django 7.0" to "Django 2028". RemovedInDjango70Warning remains importable as an alias of the same class, so existing warning filters and except clauses in third-party code keep working across the rename.
2026-08-04Fixed CVE-2026-15337 -- Mitigated potential DoS in check_for_language().Natalia
Language codes longer than 500 characters are now rejected before the cached lookup, so they are no longer retained as cache keys consuming memory from each process. Thanks Jaeyoung Jang for the report, and Sarah Boyce for reviews.
2026-07-27Fixed #37198 -- Fixed content_disposition_header() quoting of filenames ↵Vishy
ending with newlines.
2026-07-24Fixed minor typos and grammatical errors in docs and docstrings.Jacob Walls
Thanks to these people for pointing these out: * Hojeong Park * Natalia Bidart * Sulav Raj Bista
2026-07-13Refs #37187 -- Implemented warn_about_implementation() deprecation utility.Mike Edmunds
Created warn_about_implementation() for issuing warnings about particular code that isn't currently on the call stack.
2026-07-13Fixed #37178 -- Moved MiddlewareMixin out of utils.deprecation.CharulL00
2026-07-10Fixed #36864 -- Supported modules and submodules in import_string().Leland
Before, only submodules worked (and only if already imported). Now, all modules work regardless of prior import state. Updated expected exception in auth tests where import_string() returns a non-callable.
2026-06-24Fixed #37183 -- Prevented writing control characters into XML attributes.Jacob Walls
`SimplerXMLGenerator` already rejected control characters (unsupported in XML 1.0) in element content, but attribute values were written unchecked, so unsanitized input could still produce an unparseable XML document. The existing check now runs over attribute values in `startElement()`. This covers both syndication feeds, where item data may flow into attributes such as `Atom` category terms, and the XML serializer, which wraps the error to identify the offending object.
2026-06-24Fixed #37101 -- Used netstring delimiter between vary on headers for cached ↵Jacob Walls
pages. This prevents collisions between header values that concatenate to the same thing.
2026-06-19Refs #36560, CVE-2026-35193 -- Recognized qualified cache-control directives.Natalia
The switch from substring matching to exact token membership in 142b881cecaddc334cabec139e701c0e4b9798da caused qualified directive forms permitted by RFC 9111 (e.g. `Cache-Control: private="Set-Cookie"`) to be missed, allowing such responses to be stored in a shared cache. This work added a new `split_directive_names()` helper that yields the lowercased directive name from each token, dropping any qualified value and stripping whitespace around "=", so qualified forms reduce to their directive name. `UpdateCacheMiddleware` now uses it so `private`, `no-cache`, and `no-store` (and the `public` exception for `Authorization`) match regardless of qualified form. Aligned `ConditionalGetMiddleware.needs_etag()` to use the same helper, since it relied on the same brittle exact-token check. Sharing one helper keeps the two directive lookups consistent and means malformed input (e.g. `no-store="x"`) now correctly suppresses the `ETag` instead of being silently ignored. Also stripped whitespace around `=` in `patch_cache_control`'s directive parsing so a qualified directive with stray whitespace is still recognized. Thanks to Jacob Walls for reviews.
2026-06-10Fixed #37102 -- Used **kwargs instead of *kwargs in CountsDict.__init__().muss-Snippy38-lang
2026-06-10Fixed #37142 -- Moved django_file_prefixes() to django.utils.warnings.zhengkangyang
2026-06-08Refs CVE-2026-48587 -- Added helper to properly split header values.Natalia
Extracted the repeated `split(",")` + per-token `.strip()` pattern into a `split_header_value()` generator in django/utils/http.py. The previous `cc_delim_re` regex only stripped whitespace adjacent to the comma delimiter, leaving leading or trailing whitespace on the first and last tokens. Now, `split_header_value()` strips every token fully, matching RFC 9110's optional-whitespace rules. Thanks to Shai Berger, Jacob Walls, and Sarah Boyce for reviews.
2026-06-03Fixed CVE-2026-48587 -- Ignored whitespace padding when checking Vary header ↵Jake Howard
values. Thanks to Navid Rezazadeh for the report and Jacob Walls for review.
2026-06-01Fixed #28800 -- Added a listurls management command.Chris Rose
Thanks JaeHyuck Sa, Jacob Walls, and Tim McCurrach for reviews. Co-authored-by: Ülgen Sarıkavak <ulgensrkvk@gmail.com>
2026-05-20Advanced deprecation warnings for Django 6.2.Sarah Boyce
2026-05-13Fixed #35514 -- Implemented dictionary-based MAILERS.Mike Edmunds
See DEP 0018. Added: * MAILERS setting. * django.core.mail.mailers dict-like EmailBackend factory. * `using` argument to mail sending APIs. * `sent_using` attribute to mail.outbox messages in locmem backend. * MAILERS in startproject settings template, set to console backend. * AdminLogHandler.using argument. * BrokenLinkEmailsMiddleware.send_mail() method. Updated: * BaseEmailBackend to track the MAILERS alias used to construct it, and to report errors for unknown kwargs (OPTIONS). * EmailBackend implementations to initialize from kwargs (OPTIONS) only when MAILERS is being used. * smtp.EmailBackend to require `host` option and to default `port` option based on SSL/TLS options. * SimpleTestCase setup to substitute the locmem backend for all defined MAILERS configurations. * Django's tests that send mail to define MAILERS. Deprecated: * EMAIL_BACKEND and other backend-related EMAIL_* settings. * mail.get_connection(). * The `connection`, `fail_silently`, `auth_user`, and `auth_password` arguments to mail functions. * The EmailMessage.connection attribute. * BaseEmailBackend support for `fail_silently`. Backends that support fail_silently (SMTP, console, file) now implement it directly. * AdminEmailHandler.email_backend argument. Removed undocumented features without deprecation: * EmailMessage.get_connection() method. (send() now raises an error if a subclass has attempted to override it.) * EmailMessage.send() no longer sets self.connection to the connection used for sending. (It still _uses_ a pre-existing self.connection.) * AdminEmailHandler.connection() method. (Init now raises an error if a subclass has attempted to override it.) Thanks to Natalia Bidart for shepherding DEP 0018 and for extensive reviews and suggestions on the implementation. Thanks to Jacob Rief for the initial implementation and multiple iterations while refining the design. Co-authored-by: Jacob Rief <jacob.rief@gmail.com>
2026-05-08Refs #36712, #36664 -- Used annotation_format parameter of getfullargspec() ↵Jacob Walls
on Python 3.15. https://github.com/python/cpython/pull/149457
2026-05-06Fixed #36784 -- Added csp_nonce_attr template tag for CSP nonce inclusion.Natalia
New default tag `{% csp_nonce_attr %}` was added for explicit CSP nonce inclusion into `<script>` and `<link>` elements. `{% csp_nonce_attr %}` renders `nonce="<value>"` when `csp_nonce` is present in the template context, and renders nothing otherwise. `{% csp_nonce_attr media %}` renders a `Media` object's assets with the nonce attr applied to each tag. Thanks Jacob Walls for the accurate and spot on review comments. Co-authored-by: Johannes Maron <johannes@maron.family>
2026-05-04Fixed #37078 -- Deprecated SHA-1 default for salted_hmac() and base64_hmac() ↵Denny Biasiolli
algorithm. Deprecated the default value of the algorithm argument in django.utils.crypto.salted_hmac() and django.core.signing.base64_hmac(), which will change from 'sha1' to 'sha256' in Django 7.0.
2026-04-29Fixed #37067 -- Added trailing slash in django_file_prefixes().Fashad Ahmed
Ensure skip_file_prefixes does not match sibling packages like django*. Bug in f42b89f1bf49a5b89ed852b60f79342320a81c5e and 34bd3ed944bf38792c631b55e581963d44d52284.
2026-04-27Refs #35514 -- Added warn_about_external_use() deprecation utility.Mike Edmunds
Implemented a new `warn_about_external_use()` helper to conditionally issue warnings depending on whether a deprecated feature is used from within Django. Fixed `LazySettings._show_deprecation_warning()` (Refs #26029) to work correctly when called from anywhere in `LazySettings`. Previously, it assumed a specific code path through `LazyObject.__getattribute__()` and an `@property` getter on `LazySettings`.
2026-04-22Fixed #36991 -- Raised BadRequest for invalid encodings in Content-Type headers.Dinesh
2026-04-08Refs #35440 -- Optimized parse_header_parameters() for the simplest case.Pravin Kamble
Added a fast-path to parse_header_parameters Benchmark results (50,000 iterations): - Simple headers: ~73% improvement Thanks Nick Pope (@ngnpope) for the review.
2026-04-08Removed PY38 and PY39 version constants.Jacob Walls
As the oldest supported version is Django 5.2, we only need constants for PY310+.
2026-03-16Fixed #36894 -- Added TypeError for conflicting arguments in mail APIs.Praful Gulani
A TypeError is now raised if fail_silently=True, auth_user, or auth_password are provided along a connection. Updated AdminEmailHandler in django.utils.log to remove redundant fail_silently=True. Thanks Mike Edmunds for the report and Jacob Tyler Walls for the review.
2026-03-10Fixed #36943 -- Preserved any exception from URLconf module in autoreloader.varunkasyap
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
2026-03-09Fixed #36293 -- Avoided buffering streaming responses in GZipMiddleware.farhan
This avoids latency and/or blocking. The example of streaming a CSV file was rewritten to employ batching for greater efficiency in all layers (db, HTTP, etc.). The improved performance from batching should outweigh the drag introduced by an additional byte for each flush. Co-authored-by: huoyinghui <huoyinghui@users.noreply.github.com>
2026-03-03Fixed CVE-2026-25674 -- Prevented potentially incorrect permissions on file ↵Natalia
system object creation. This fix introduces `safe_makedirs()` in the `os` utils as a safer alternative to `os.makedirs()` that avoids umask-related race conditions in multi-threaded environments. This is a workaround for https://github.com/python/cpython/issues/86533 and the solution is based on the fix being proposed for CPython. Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Zackery Spytz <zspytz@gmail.com> Refs CVE-2020-24583 and #31921. Thanks Tarek Nakkouch for the report, and Jake Howard, Jacob Walls, and Shai Berger for reviews.
2026-03-02Fixed #36961 -- Fixed TypeError in deprecation warnings if Django is ↵Jacob Walls
imported by namespace.
2026-02-25Fixed #36944 -- Removed MAX_LENGTH_HTML and related 5M chars limit ↵Natalia
references from HTML truncation docs.