mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[4.1.x] Fixed CVE-2022-34265 -- Protected Trunc(kind)/Extract(lookup_name) against SQL injection.
Thanks Takuto Yoshikai (Aeye Security Lab) for the report.
This commit is contained in:
		| @@ -9,6 +9,7 @@ from django.db import NotSupportedError, transaction | |||||||
| from django.db.backends import utils | from django.db.backends import utils | ||||||
| from django.utils import timezone | from django.utils import timezone | ||||||
| from django.utils.encoding import force_str | from django.utils.encoding import force_str | ||||||
|  | from django.utils.regex_helper import _lazy_re_compile | ||||||
|  |  | ||||||
|  |  | ||||||
| class BaseDatabaseOperations: | class BaseDatabaseOperations: | ||||||
| @@ -54,6 +55,8 @@ class BaseDatabaseOperations: | |||||||
|     # Prefix for EXPLAIN queries, or None EXPLAIN isn't supported. |     # Prefix for EXPLAIN queries, or None EXPLAIN isn't supported. | ||||||
|     explain_prefix = None |     explain_prefix = None | ||||||
|  |  | ||||||
|  |     extract_trunc_lookup_pattern = _lazy_re_compile(r"[\w\-_()]+") | ||||||
|  |  | ||||||
|     def __init__(self, connection): |     def __init__(self, connection): | ||||||
|         self.connection = connection |         self.connection = connection | ||||||
|         self._cache = None |         self._cache = None | ||||||
|   | |||||||
| @@ -51,6 +51,8 @@ class Extract(TimezoneMixin, Transform): | |||||||
|         super().__init__(expression, **extra) |         super().__init__(expression, **extra) | ||||||
|  |  | ||||||
|     def as_sql(self, compiler, connection): |     def as_sql(self, compiler, connection): | ||||||
|  |         if not connection.ops.extract_trunc_lookup_pattern.fullmatch(self.lookup_name): | ||||||
|  |             raise ValueError("Invalid lookup_name: %s" % self.lookup_name) | ||||||
|         sql, params = compiler.compile(self.lhs) |         sql, params = compiler.compile(self.lhs) | ||||||
|         lhs_output_field = self.lhs.output_field |         lhs_output_field = self.lhs.output_field | ||||||
|         if isinstance(lhs_output_field, DateTimeField): |         if isinstance(lhs_output_field, DateTimeField): | ||||||
| @@ -235,6 +237,8 @@ class TruncBase(TimezoneMixin, Transform): | |||||||
|         super().__init__(expression, output_field=output_field, **extra) |         super().__init__(expression, output_field=output_field, **extra) | ||||||
|  |  | ||||||
|     def as_sql(self, compiler, connection): |     def as_sql(self, compiler, connection): | ||||||
|  |         if not connection.ops.extract_trunc_lookup_pattern.fullmatch(self.kind): | ||||||
|  |             raise ValueError("Invalid kind: %s" % self.kind) | ||||||
|         inner_sql, inner_params = compiler.compile(self.lhs) |         inner_sql, inner_params = compiler.compile(self.lhs) | ||||||
|         tzname = None |         tzname = None | ||||||
|         if isinstance(self.lhs.output_field, DateTimeField): |         if isinstance(self.lhs.output_field, DateTimeField): | ||||||
|   | |||||||
| @@ -5,3 +5,14 @@ Django 3.2.14 release notes | |||||||
| *July 4, 2022* | *July 4, 2022* | ||||||
|  |  | ||||||
| Django 3.2.14 fixes a security issue with severity "high" in 3.2.13. | Django 3.2.14 fixes a security issue with severity "high" in 3.2.13. | ||||||
|  |  | ||||||
|  | CVE-2022-34265: Potential SQL injection via ``Trunc(kind)`` and ``Extract(lookup_name)`` arguments | ||||||
|  | ================================================================================================== | ||||||
|  |  | ||||||
|  | :class:`Trunc() <django.db.models.functions.Trunc>` and | ||||||
|  | :class:`Extract() <django.db.models.functions.Extract>` database functions were | ||||||
|  | subject to SQL injection if untrusted data was used as a | ||||||
|  | ``kind``/``lookup_name`` value. | ||||||
|  |  | ||||||
|  | Applications that constrain the lookup name and kind choice to a known safe | ||||||
|  | list are unaffected. | ||||||
|   | |||||||
| @@ -6,7 +6,13 @@ Django 4.0.6 release notes | |||||||
|  |  | ||||||
| Django 4.0.6 fixes a security issue with severity "high" in 4.0.5. | Django 4.0.6 fixes a security issue with severity "high" in 4.0.5. | ||||||
|  |  | ||||||
| Bugfixes | CVE-2022-34265: Potential SQL injection via ``Trunc(kind)`` and ``Extract(lookup_name)`` arguments | ||||||
| ======== | ================================================================================================== | ||||||
|  |  | ||||||
| * ... | :class:`Trunc() <django.db.models.functions.Trunc>` and | ||||||
|  | :class:`Extract() <django.db.models.functions.Extract>` database functions were | ||||||
|  | subject to SQL injection if untrusted data was used as a | ||||||
|  | ``kind``/``lookup_name`` value. | ||||||
|  |  | ||||||
|  | Applications that constrain the lookup name and kind choice to a known safe | ||||||
|  | list are unaffected. | ||||||
|   | |||||||
| @@ -235,6 +235,23 @@ class DateFunctionTests(TestCase): | |||||||
|                 self.assertEqual(qs.count(), 1) |                 self.assertEqual(qs.count(), 1) | ||||||
|                 self.assertGreaterEqual(str(qs.query).lower().count("extract"), 2) |                 self.assertGreaterEqual(str(qs.query).lower().count("extract"), 2) | ||||||
|  |  | ||||||
|  |     def test_extract_lookup_name_sql_injection(self): | ||||||
|  |         start_datetime = datetime(2015, 6, 15, 14, 30, 50, 321) | ||||||
|  |         end_datetime = datetime(2016, 6, 15, 14, 10, 50, 123) | ||||||
|  |         if settings.USE_TZ: | ||||||
|  |             start_datetime = timezone.make_aware(start_datetime) | ||||||
|  |             end_datetime = timezone.make_aware(end_datetime) | ||||||
|  |         self.create_model(start_datetime, end_datetime) | ||||||
|  |         self.create_model(end_datetime, start_datetime) | ||||||
|  |  | ||||||
|  |         msg = "Invalid lookup_name: " | ||||||
|  |         with self.assertRaisesMessage(ValueError, msg): | ||||||
|  |             DTModel.objects.filter( | ||||||
|  |                 start_datetime__year=Extract( | ||||||
|  |                     "start_datetime", "day' FROM start_datetime)) OR 1=1;--" | ||||||
|  |                 ) | ||||||
|  |             ).exists() | ||||||
|  |  | ||||||
|     def test_extract_func(self): |     def test_extract_func(self): | ||||||
|         start_datetime = datetime(2015, 6, 15, 14, 30, 50, 321) |         start_datetime = datetime(2015, 6, 15, 14, 30, 50, 321) | ||||||
|         end_datetime = datetime(2016, 6, 15, 14, 10, 50, 123) |         end_datetime = datetime(2016, 6, 15, 14, 10, 50, 123) | ||||||
| @@ -915,6 +932,23 @@ class DateFunctionTests(TestCase): | |||||||
|             [obj], |             [obj], | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  |     def test_trunc_lookup_name_sql_injection(self): | ||||||
|  |         start_datetime = datetime(2015, 6, 15, 14, 30, 50, 321) | ||||||
|  |         end_datetime = datetime(2016, 6, 15, 14, 10, 50, 123) | ||||||
|  |         if settings.USE_TZ: | ||||||
|  |             start_datetime = timezone.make_aware(start_datetime) | ||||||
|  |             end_datetime = timezone.make_aware(end_datetime) | ||||||
|  |         self.create_model(start_datetime, end_datetime) | ||||||
|  |         self.create_model(end_datetime, start_datetime) | ||||||
|  |         msg = "Invalid kind: " | ||||||
|  |         with self.assertRaisesMessage(ValueError, msg): | ||||||
|  |             DTModel.objects.filter( | ||||||
|  |                 start_datetime__date=Trunc( | ||||||
|  |                     "start_datetime", | ||||||
|  |                     "year', start_datetime)) OR 1=1;--", | ||||||
|  |                 ) | ||||||
|  |             ).exists() | ||||||
|  |  | ||||||
|     def test_trunc_func(self): |     def test_trunc_func(self): | ||||||
|         start_datetime = datetime(999, 6, 15, 14, 30, 50, 321) |         start_datetime = datetime(999, 6, 15, 14, 30, 50, 321) | ||||||
|         end_datetime = datetime(2016, 6, 15, 14, 10, 50, 123) |         end_datetime = datetime(2016, 6, 15, 14, 10, 50, 123) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user