summaryrefslogtreecommitdiff
path: root/tests/annotations/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/annotations/tests.py')
-rw-r--r--tests/annotations/tests.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index 021f59d2d7..27cd7ebfb8 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -598,3 +598,37 @@ class NonAggregateAnnotationTestCase(TestCase):
total_books=Subquery(long_books_qs, output_field=IntegerField()),
).values('name')
self.assertCountEqual(publisher_books_qs, [{'name': 'Sams'}, {'name': 'Morgan Kaufmann'}])
+
+ def test_alias_sql_injection(self):
+ crafted_alias = """injected_name" from "annotations_book"; --"""
+ msg = (
+ "Column aliases cannot contain whitespace characters, quotation marks, "
+ "semicolons, or SQL comments."
+ )
+ with self.assertRaisesMessage(ValueError, msg):
+ Book.objects.annotate(**{crafted_alias: Value(1)})
+
+ def test_alias_forbidden_chars(self):
+ tests = [
+ 'al"ias',
+ "a'lias",
+ "ali`as",
+ "alia s",
+ "alias\t",
+ "ali\nas",
+ "alias--",
+ "ali/*as",
+ "alias*/",
+ "alias;",
+ # [] are used by MSSQL.
+ "alias[",
+ "alias]",
+ ]
+ msg = (
+ "Column aliases cannot contain whitespace characters, quotation marks, "
+ "semicolons, or SQL comments."
+ )
+ for crafted_alias in tests:
+ with self.subTest(crafted_alias):
+ with self.assertRaisesMessage(ValueError, msg):
+ Book.objects.annotate(**{crafted_alias: Value(1)})