From ebce82b136005b03b5205453bb629f36876d73eb Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 14 Feb 2014 10:50:42 -0500 Subject: [PATCH] Eliminate json_to_dict and use check_dict instead. All usages of json_to_dict were replaced with the check_dict validator. The check_dict validations can eventually be extended to validate the keys and values of incoming data, but now we just use check_dict([]) in all the places where we had json_to_dict, which means we aren't checking for any specific keys; we are just making sure it's a dictionary. (imported from commit fc5add9a7ef149dfac2a9a6d9a153799c4c0c24d) --- zerver/decorator.py | 3 --- zerver/tornadoviews.py | 2 +- zerver/views/__init__.py | 4 ++-- zerver/views/webhooks.py | 13 +++++++------ zilencer/views.py | 7 ++++--- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/zerver/decorator.py b/zerver/decorator.py index bbea71ee13..1e4c586822 100644 --- a/zerver/decorator.py +++ b/zerver/decorator.py @@ -489,9 +489,6 @@ def json_to_foo(json, type): raise ValueError("argument is not a %s" % (type().__class__.__name__)) return data -def json_to_dict(json): - return json_to_foo(json, dict) - def json_to_list(json): return json_to_foo(json, list) diff --git a/zerver/tornadoviews.py b/zerver/tornadoviews.py index e0f89db957..2e3721099e 100644 --- a/zerver/tornadoviews.py +++ b/zerver/tornadoviews.py @@ -5,7 +5,7 @@ from zerver.models import get_client from zerver.decorator import asynchronous, \ authenticated_json_post_view, internal_notify_view, RespondAsynchronously, \ - has_request_variables, json_to_dict, REQ + has_request_variables, REQ from zerver.lib.response import json_success, json_error from zerver.lib.validator import check_bool, check_list, check_string diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index fd9ad2021a..39ae37fa4e 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -55,7 +55,7 @@ from zerver.lib.validator import check_string, check_list, check_dict, check_int from zerver.decorator import require_post, \ authenticated_api_view, authenticated_json_post_view, \ has_request_variables, authenticated_json_view, \ - to_non_negative_int, json_to_dict, json_to_bool, \ + to_non_negative_int, json_to_bool, \ JsonableError, get_user_profile_by_email, REQ, require_realm_admin, \ RequestVariableConversionError from zerver.lib.avatar import avatar_url, get_avatar_url @@ -1726,7 +1726,7 @@ def json_report_unnarrow_time(request, user_profile, def json_report_error(request, user_profile, message=REQ, stacktrace=REQ, ui_message=REQ(validator=check_bool), user_agent=REQ, href=REQ, log=REQ, - more_info=REQ(converter=json_to_dict, default=None)): + more_info=REQ(validator=check_dict([]), default=None)): if not settings.ERROR_REPORTING: return json_success() diff --git a/zerver/views/webhooks.py b/zerver/views/webhooks.py index 09009d8df4..b93a5f414c 100644 --- a/zerver/views/webhooks.py +++ b/zerver/views/webhooks.py @@ -7,8 +7,9 @@ from zerver.models import UserProfile, get_client, get_user_profile_by_email from zerver.lib.actions import check_send_message from zerver.lib.notifications import convert_html_to_markdown from zerver.lib.response import json_success, json_error +from zerver.lib.validator import check_dict from zerver.decorator import authenticated_api_view, REQ, \ - has_request_variables, json_to_dict, authenticated_rest_api_view, \ + has_request_variables, authenticated_rest_api_view, \ api_key_only_webhook_view, to_non_negative_int, ruby_boolean from zerver.views.messages import send_message_backend from django.db.models import Q @@ -133,7 +134,7 @@ def api_github_v2(user_profile, event, payload, branches, default_stream, commit @authenticated_api_view @has_request_variables def api_github_landing(request, user_profile, event=REQ, - payload=REQ(converter=json_to_dict), + payload=REQ(validator=check_dict([])), branches=REQ(default=''), stream=REQ(default=''), version=REQ(converter=to_non_negative_int, default=1), @@ -609,7 +610,7 @@ def beanstalk_decoder(view_func): @authenticated_rest_api_view @has_request_variables def api_beanstalk_webhook(request, user_profile, - payload=REQ(converter=json_to_dict)): + payload=REQ(validator=check_dict([]))): # Beanstalk supports both SVN and git repositories # We distinguish between the two by checking for a # 'uri' key that is only present for git repos @@ -650,8 +651,8 @@ def api_deskdotcom_webhook(request, user_profile, data=REQ(), @api_key_only_webhook_view @has_request_variables -def api_newrelic_webhook(request, user_profile, alert=REQ(converter=json_to_dict, default=None), - deployment=REQ(converter=json_to_dict, default=None)): +def api_newrelic_webhook(request, user_profile, alert=REQ(validator=check_dict([]), default=None), + deployment=REQ(validator=check_dict([]), default=None)): try: stream = request.GET['stream'] except (AttributeError, KeyError): @@ -678,7 +679,7 @@ def api_newrelic_webhook(request, user_profile, alert=REQ(converter=json_to_dict @authenticated_rest_api_view @has_request_variables -def api_bitbucket_webhook(request, user_profile, payload=REQ(converter=json_to_dict), +def api_bitbucket_webhook(request, user_profile, payload=REQ(validator=check_dict([])), stream=REQ(default='commits')): repository = payload['repository'] commits = [{'id': commit['raw_node'], 'message': commit['message'], diff --git a/zilencer/views.py b/zilencer/views.py index 323a76cd97..23bb9fe894 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -5,11 +5,12 @@ from django.http import HttpResponseRedirect from django.shortcuts import render_to_response, redirect from django.template import RequestContext, loader -from zerver.decorator import has_request_variables, REQ, json_to_dict +from zerver.decorator import has_request_variables, REQ from zerver.lib.actions import internal_send_message from zerver.lib.redis_utils import get_redis_client from zerver.lib.response import json_success, json_error, json_response, json_method_not_allowed from zerver.lib.rest import rest_dispatch as _rest_dispatch +from zerver.lib.validator import check_dict from zerver.models import get_realm, get_user_profile_by_email, resolve_email_to_domain, \ UserProfile from zilencer.forms import EnterpriseToSForm @@ -43,7 +44,7 @@ def get_ticket_number(): return ticket_number @has_request_variables -def submit_feedback(request, deployment, message=REQ(converter=json_to_dict)): +def submit_feedback(request, deployment, message=REQ(validator=check_dict([]))): domainish = message["sender_domain"] if get_realm("zulip.com") not in deployment.realms.all(): domainish += " via " + deployment.name @@ -78,7 +79,7 @@ def submit_feedback(request, deployment, message=REQ(converter=json_to_dict)): return HttpResponse(message['sender_email']) @has_request_variables -def report_error(request, deployment, type=REQ, report=REQ(converter=json_to_dict)): +def report_error(request, deployment, type=REQ, report=REQ(validator=check_dict([]))): report['deployment'] = deployment.name if type == 'browser': notify_browser_error(report)