Skip to content

Commit 4da9d1f

Browse files
sgkasselauclaudep
authored andcommitted
Fixed an issue with handling bad comment content_types seen in the wild.
(The old behaviour raised a LookupError, and caused a 500 status code instead of the intended 400.)
1 parent 69b166a commit 4da9d1f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

django_comments/views/comments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def post_comment(request, next=None, using=None):
5252
try:
5353
model = apps.get_model(*ctype.split(".", 1))
5454
target = model._default_manager.using(using).get(pk=object_pk)
55-
except TypeError:
55+
except (LookupError, TypeError):
5656
return CommentPostBadRequest(
5757
"Invalid content_type value: %r" % escape(ctype))
5858
except AttributeError:

tests/testapp/tests/test_comment_views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ def testPostCommentBadCtype(self):
3232
response = self.client.post("/post/", data)
3333
self.assertEqual(response.status_code, 400)
3434

35+
def testPostCommentBadCtypeInvalidModelName(self):
36+
a = Article.objects.get(pk=1)
37+
data = self.getValidData(a)
38+
data["content_type"] = str(Article._meta) + "_91232"
39+
response = self.client.post("/post/", data)
40+
self.assertEqual(response.status_code, 400)
41+
42+
def testPostCommentBadCtypeInjectionAttempt(self):
43+
a = Article.objects.get(pk=1)
44+
data = self.getValidData(a)
45+
data["content_type"] = str(Article._meta) + "'\"()&%<acx><ScRiPt >prompt(998230)</ScRiPt>"
46+
response = self.client.post("/post/", data)
47+
self.assertEqual(response.status_code, 400)
48+
3549
def testPostCommentMissingObjectPK(self):
3650
a = Article.objects.get(pk=1)
3751
data = self.getValidData(a)

0 commit comments

Comments
 (0)