|
1 | 1 | from django.db.models import F, Sum |
2 | | -from django.test import TestCase |
| 2 | +from django.test import TestCase, skipUnlessDBFeature |
3 | 3 |
|
4 | | -from .models import Company, Employee |
| 4 | +from .models import Company, Employee, JSONFieldModel |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class ValuesExpressionsTests(TestCase): |
@@ -43,6 +43,19 @@ def test_values_expression_alias_sql_injection(self): |
43 | 43 | with self.assertRaisesMessage(ValueError, msg): |
44 | 44 | Company.objects.values(**{crafted_alias: F("ceo__salary")}) |
45 | 45 |
|
| 46 | + @skipUnlessDBFeature("supports_json_field") |
| 47 | + def test_values_expression_alias_sql_injection_json_field(self): |
| 48 | + crafted_alias = """injected_name" from "expressions_company"; --""" |
| 49 | + msg = ( |
| 50 | + "Column aliases cannot contain whitespace characters, quotation marks, " |
| 51 | + "semicolons, or SQL comments." |
| 52 | + ) |
| 53 | + with self.assertRaisesMessage(ValueError, msg): |
| 54 | + JSONFieldModel.objects.values(f"data__{crafted_alias}") |
| 55 | + |
| 56 | + with self.assertRaisesMessage(ValueError, msg): |
| 57 | + JSONFieldModel.objects.values_list(f"data__{crafted_alias}") |
| 58 | + |
46 | 59 | def test_values_expression_group_by(self): |
47 | 60 | # values() applies annotate() first, so values selected are grouped by |
48 | 61 | # id, not firstname. |
|
0 commit comments