def _check_register_page(self): """ Load the login form and check that it contains a TestShib button """ response = self.client.get(self.register_page_url) self.assertEqual(response.status_code, 200) self.assertIn("TestShib", response.content) self.assertIn(escape_json_dumps(TPA_TESTSHIB_REGISTER_URL), response.content) return response
def inner(request, *args, **kwargs): if request.is_ajax(): content = escape_json_dumps({"error": message}) return HttpResponse(content, content_type="application/json", status=status) else: return func(request, *args, **kwargs)
def test_escape_json_dumps_escapes_unsafe_html(self): """ Test escape_json_dumps properly escapes &, <, and >. """ malicious_json = {"</script><script>alert('hello, ');</script>": "</script><script>alert('&world!');</script>"} expected_encoded_json = ( r'''{"\u003c/script\u003e\u003cscript\u003ealert('hello, ');\u003c/script\u003e": ''' r'''"\u003c/script\u003e\u003cscript\u003ealert('\u0026world!');\u003c/script\u003e"}''' ) encoded_json = escape_json_dumps(malicious_json) self.assertEquals(expected_encoded_json, encoded_json)
def test_escape_json_dumps_escapes_unsafe_html(self): """ Test escape_json_dumps properly escapes &, <, and >. """ malicious_json = { "</script><script>alert('hello, ');</script>": "</script><script>alert('&world!');</script>" } expected_encoded_json = ( r'''{"\u003c/script\u003e\u003cscript\u003ealert('hello, ');\u003c/script\u003e": ''' r'''"\u003c/script\u003e\u003cscript\u003ealert('\u0026world!');\u003c/script\u003e"}''' ) encoded_json = escape_json_dumps(malicious_json) self.assertEquals(expected_encoded_json, encoded_json)
def test_escape_json_dumps_with_custom_encoder_escapes_unsafe_html(self): """ Test escape_json_dumps first encodes with custom JSNOEncoder before escaping &, <, and > The test encoder class should first perform the replacement of "<script>" with "sample-encoder-was-here", and then should escape the remaining &, <, and >. """ malicious_json = { "</script><script>alert('hello, ');</script>": self.NoDefaultEncoding("</script><script>alert('&world!');</script>") } expected_custom_encoded_json = ( r'''{"\u003c/script\u003e\u003cscript\u003ealert('hello, ');\u003c/script\u003e": ''' r'''"\u003c/script\u003esample-encoder-was-herealert('\u0026world!');\u003c/script\u003e"}''' ) encoded_json = escape_json_dumps(malicious_json, cls=self.SampleJSONEncoder) self.assertEquals(expected_custom_encoded_json, encoded_json)
def _get_entrance_exam(request, course_key): # pylint: disable=W0613 """ Internal workflow operation to retrieve an entrance exam """ course = modulestore().get_course(course_key) if course is None: return HttpResponse(status=400) if not getattr(course, "entrance_exam_id"): return HttpResponse(status=404) try: exam_key = UsageKey.from_string(course.entrance_exam_id) except InvalidKeyError: return HttpResponse(status=404) try: exam_descriptor = modulestore().get_item(exam_key) return HttpResponse( escape_json_dumps({"locator": unicode(exam_descriptor.location)}), status=200, mimetype="application/json" ) except ItemNotFoundError: return HttpResponse(status=404)
def _assert_third_party_auth_data(self, response, current_backend, current_provider, providers): """Verify that third party auth info is rendered correctly in a DOM data attribute. """ finish_auth_url = None if current_backend: finish_auth_url = reverse("social:complete", kwargs={"backend": current_backend}) + "?" auth_info = { "currentProvider": current_provider, "providers": providers, "secondaryProviders": [], "finishAuthUrl": finish_auth_url, "errorMessage": None, } auth_info = escape_json_dumps(auth_info) expected_data = '"third_party_auth": {auth_info}'.format( auth_info=auth_info ) self.assertContains(response, expected_data)
def _assert_third_party_auth_data(self, response, current_backend, current_provider, providers): """Verify that third party auth info is rendered correctly in a DOM data attribute. """ finish_auth_url = None if current_backend: finish_auth_url = reverse( "social:complete", kwargs={"backend": current_backend}) + "?" auth_info = { "currentProvider": current_provider, "providers": providers, "secondaryProviders": [], "finishAuthUrl": finish_auth_url, "errorMessage": None, } auth_info = escape_json_dumps(auth_info) expected_data = '"third_party_auth": {auth_info}'.format( auth_info=auth_info) self.assertContains(response, expected_data)
def _get_entrance_exam(request, course_key): # pylint: disable=W0613 """ Internal workflow operation to retrieve an entrance exam """ course = modulestore().get_course(course_key) if course is None: return HttpResponse(status=400) if not course.entrance_exam_id: return HttpResponse(status=404) try: exam_key = UsageKey.from_string(course.entrance_exam_id) except InvalidKeyError: return HttpResponse(status=404) try: exam_descriptor = modulestore().get_item(exam_key) return HttpResponse(escape_json_dumps( {'locator': unicode(exam_descriptor.location)}), status=200, content_type='application/json') except ItemNotFoundError: return HttpResponse(status=404)
def test_escape_json_dumps_with_custom_encoder_escapes_unsafe_html(self): """ Test escape_json_dumps first encodes with custom JSNOEncoder before escaping &, <, and > The test encoder class should first perform the replacement of "<script>" with "sample-encoder-was-here", and then should escape the remaining &, <, and >. """ malicious_json = { "</script><script>alert('hello, ');</script>": self.NoDefaultEncoding( "</script><script>alert('&world!');</script>") } expected_custom_encoded_json = ( r'''{"\u003c/script\u003e\u003cscript\u003ealert('hello, ');\u003c/script\u003e": ''' r'''"\u003c/script\u003esample-encoder-was-herealert('\u0026world!');\u003c/script\u003e"}''' ) encoded_json = escape_json_dumps(malicious_json, cls=self.SampleJSONEncoder) self.assertEquals(expected_custom_encoded_json, encoded_json)
def get(self, request, *args, **kwargs): """Returns organization list as json.""" organizations = get_organizations() org_names_list = [(org["short_name"]) for org in organizations] return HttpResponse(escape_json_dumps(org_names_list), content_type='application/json; charset=utf-8')