Be more picky about the type of get_old_messages's narrow POST parameter.

This addresses 500s observed on prod due to bad narrow values.

(imported from commit 5a865ce41e8a90d3990332d906cba4336eb53ada)
This commit is contained in:
Jessica McKellar 2012-12-08 12:31:41 -05:00
parent cba3ad8315
commit 435c98b35c
1 changed files with 7 additions and 1 deletions

View File

@ -51,6 +51,12 @@ def to_non_negative_int(x):
raise ValueError("argument is negative") raise ValueError("argument is negative")
return x return x
def from_json_to_dict(json):
data = simplejson.loads(json)
if not isinstance(data, dict):
raise ValueError("argument is not a dictionary")
return data
def get_stream(stream_name, realm): def get_stream(stream_name, realm):
try: try:
return Stream.objects.get(name__iexact=stream_name, realm=realm) return Stream.objects.get(name__iexact=stream_name, realm=realm)
@ -254,7 +260,7 @@ def api_get_old_messages(request, user_profile,
def get_old_messages_backend(request, anchor = POST(converter=to_non_negative_int), def get_old_messages_backend(request, anchor = POST(converter=to_non_negative_int),
num_before = POST(converter=to_non_negative_int), num_before = POST(converter=to_non_negative_int),
num_after = POST(converter=to_non_negative_int), num_after = POST(converter=to_non_negative_int),
narrow = POST('narrow', converter=simplejson.loads), narrow = POST('narrow', converter=from_json_to_dict),
user_profile=None, apply_markdown=True): user_profile=None, apply_markdown=True):
query = Message.objects.select_related().filter(usermessage__user_profile = user_profile).order_by('id') query = Message.objects.select_related().filter(usermessage__user_profile = user_profile).order_by('id')