예제 #1
0
    def testComplexOrderedDict(self):
        data = collections.OrderedDict()
        data["foo.bar"] = collections.OrderedDict()
        data["foo.bar"]["quux"] = [4, 8, 15, 16, 23, 42]
        data["foo.bar"]["thud"] = ["blargh", "norf"]
        data["foo.baz"] = [3.14, 1.62]
        dumped = json.Dump(data)

        expected = """{
  "foo.bar": {
    "quux": [
      4,
      8,
      15,
      16,
      23,
      42
    ],
    "thud": [
      "blargh",
      "norf"
    ]
  },
  "foo.baz": [
    3.14,
    1.62
  ]
}"""

        self.assertEqual(dumped, expected)
예제 #2
0
    def testPOSTRequestWithoutCSRFTokenFails(self):
        data = {"client_ids": ["C.0000000000000000"], "labels": ["foo", "bar"]}

        response = requests.post(self.base_url + "/api/clients/labels/add",
                                 data=json.Dump(data))

        self.assertEqual(response.status_code, 403)
        self.assertIn("CSRF", response.text)
예제 #3
0
  def _WriteJSONValue(self, output_file, value, delimiter=None):
    # We write newline-separated dicts of JSON values, so each JSON value is not
    # allowed to contain any newline characters.
    dumped_json = json.Dump(self._GetNestedDict(value)).replace("\n", "")

    if delimiter:
      output_file.write(delimiter.encode("utf-8"))

    output_file.write(dumped_json.encode("utf-8"))
예제 #4
0
    def testSimpleList(self):
        dumped = json.Dump([4, 8, 15, 16, 23, 42])
        expected = """[
  4,
  8,
  15,
  16,
  23,
  42
]"""
        self.assertEqual(dumped, expected)
예제 #5
0
    def testSimpleDict(self):
        data = collections.OrderedDict()
        data["foo"] = "bar"
        data["quux"] = 42
        dumped = json.Dump(data)

        expected = """{
  "foo": "bar",
  "quux": 42
}"""

        self.assertEqual(dumped, expected)
예제 #6
0
    def testPOSTRequestFailsIfCSRFTokenIsExpired(self):
        with test_lib.FakeTime(rdfvalue.RDFDatetime.FromSecondsSinceEpoch(42)):
            index_response = requests.get(self.base_url)
            csrf_token = index_response.cookies.get("csrftoken")

            headers = {"x-csrftoken": csrf_token}
            data = {
                "client_ids": ["C.0000000000000000"],
                "labels": ["foo", "bar"]
            }
            cookies = {"csrftoken": csrf_token}

            response = requests.post(self.base_url + "/api/clients/labels/add",
                                     headers=headers,
                                     data=json.Dump(data),
                                     cookies=cookies)
            self.assertEqual(response.status_code, 200)

        # This should still succeed as we use strict check in wsgiapp.py:
        # current_time - token_time > CSRF_TOKEN_DURATION.microseconds
        with test_lib.FakeTime(
                rdfvalue.RDFDatetime.FromSecondsSinceEpoch(42) +
                wsgiapp.CSRF_TOKEN_DURATION.seconds):
            response = requests.post(self.base_url + "/api/clients/labels/add",
                                     headers=headers,
                                     data=json.Dump(data),
                                     cookies=cookies)
            self.assertEqual(response.status_code, 200)

        with test_lib.FakeTime(
                rdfvalue.RDFDatetime.FromSecondsSinceEpoch(42) +
                wsgiapp.CSRF_TOKEN_DURATION.seconds + 1):
            response = requests.post(self.base_url + "/api/clients/labels/add",
                                     headers=headers,
                                     data=json.Dump(data),
                                     cookies=cookies)
            self.assertEqual(response.status_code, 403)
            self.assertIn("Expired CSRF token", response.text)
예제 #7
0
    def testPOSTRequestWithCSRFTokenInHeadersAndCookiesSucceeds(self):
        # Fetch csrf token from the cookie set on the main page.
        index_response = requests.get(self.base_url)
        csrf_token = index_response.cookies.get("csrftoken")

        headers = {"x-csrftoken": csrf_token}
        data = {"client_ids": ["C.0000000000000000"], "labels": ["foo", "bar"]}
        cookies = {"csrftoken": csrf_token}

        response = requests.post(self.base_url + "/api/clients/labels/add",
                                 headers=headers,
                                 data=json.Dump(data),
                                 cookies=cookies)
        self.assertEqual(response.status_code, 200)
예제 #8
0
    def testPOSTRequestWithCSRFTokenInCookiesAndNotInHeadersFails(self):
        # Fetch csrf token from the cookie set on the main page.
        index_response = requests.get(self.base_url)
        csrf_token = index_response.cookies.get("csrftoken")

        data = {"client_ids": ["C.0000000000000000"], "labels": ["foo", "bar"]}
        cookies = {"csrftoken": csrf_token}

        response = requests.post(self.base_url + "/api/clients/labels/add",
                                 data=json.Dump(data),
                                 cookies=cookies)

        self.assertEqual(response.status_code, 403)
        self.assertIn("CSRF", response.text)
예제 #9
0
    def testPOSTRequestFailsIfCSRFTokenIsMalformed(self):
        index_response = requests.get(self.base_url)
        csrf_token = index_response.cookies.get("csrftoken")

        headers = {"x-csrftoken": csrf_token + "BLAH"}
        data = {"client_ids": ["C.0000000000000000"], "labels": ["foo", "bar"]}
        cookies = {"csrftoken": csrf_token}

        response = requests.post(self.base_url + "/api/clients/labels/add",
                                 headers=headers,
                                 data=json.Dump(data),
                                 cookies=cookies)
        self.assertEqual(response.status_code, 403)
        self.assertIn("Malformed", response.text)
예제 #10
0
    def testUnorderedDictWithSortKeys(self):
        data = {}
        data["foo.bar"] = collections.OrderedDict()
        data["foo.bar"]["quux"] = 1
        data["foo.bar"]["thud"] = 2
        data["foo.baz"] = 4
        dumped = json.Dump(data, sort_keys=True)

        expected = """{
  "foo.bar": {
    "quux": 1,
    "thud": 2
  },
  "foo.baz": 4
}"""
        self.assertEqual(dumped, expected)
예제 #11
0
    def testPOSTRequestFailsIfCSRFTokenDoesNotMatch(self):
        index_response = requests.get(self.base_url)
        csrf_token = index_response.cookies.get("csrftoken")

        headers = {"x-csrftoken": csrf_token}
        data = {"client_ids": ["C.0000000000000000"], "labels": ["foo", "bar"]}
        cookies = {"csrftoken": csrf_token}

        # This changes the default test username, meaning that encoded CSRF
        # token and the token corresponding to the next requests's user won't
        # match.
        webauth.WEBAUTH_MANAGER.SetUserName(u"someotheruser")
        response = requests.post(self.base_url + "/api/clients/labels/add",
                                 headers=headers,
                                 data=json.Dump(data),
                                 cookies=cookies)
        self.assertEqual(response.status_code, 403)
        self.assertIn("Non-matching", response.text)
예제 #12
0
    def _BuildResponse(self,
                       status,
                       rendered_data,
                       method_name=None,
                       headers=None,
                       content_length=None,
                       token=None,
                       no_audit_log=False):
        """Builds HTTPResponse object from rendered data and HTTP status."""

        # To avoid IE content sniffing problems, escape the tags. Otherwise somebody
        # may send a link with malicious payload that will be opened in IE (which
        # does content sniffing and doesn't respect Content-Disposition header) and
        # IE will treat the document as html and executre arbitrary JS that was
        # passed with the payload.
        str_data = json.Dump(rendered_data,
                             encoder=JSONEncoderWithRDFPrimitivesSupport)
        # XSSI protection and tags escaping
        rendered_data = ")]}'\n" + str_data.replace("<", r"\u003c").replace(
            ">", r"\u003e")

        response = werkzeug_wrappers.Response(
            rendered_data,
            status=status,
            content_type="application/json; charset=utf-8")
        response.headers[
            "Content-Disposition"] = "attachment; filename=response.json"
        response.headers["X-Content-Type-Options"] = "nosniff"

        if token and token.reason:
            response.headers["X-GRR-Reason"] = utils.SmartStr(token.reason)
        if method_name:
            response.headers["X-API-Method"] = method_name
        if no_audit_log:
            response.headers["X-No-Log"] = "True"

        for key, value in iteritems(headers or {}):
            response.headers[key] = value

        if content_length is not None:
            response.content_length = content_length

        return response
예제 #13
0
    def testEncoder(self):
        class Foo(object):
            def __init__(self, foo):
                self.foo = foo

        class FooEncoder(json.Encoder):
            def default(self, obj):
                if isinstance(obj, Foo):
                    return obj.foo
                else:
                    return super(FooEncoder, self).default(obj)

        data = [Foo("quux"), Foo("norf"), Foo("thud")]
        dumped = json.Dump(data, encoder=FooEncoder)
        self.assertEqual(dumped, """[
  "quux",
  "norf",
  "thud"
]""")
예제 #14
0
    def testUnicode(self):
        data = collections.OrderedDict()
        data["gęsi (🦆)"] = ["zbożowa", "krótkodzioba", "białoczelna"]
        data["grzebiące (🐔)"] = ["jarząbek", "głuszec", "bażant"]
        dumped = json.Dump(data)

        expected = """{
  "gęsi (🦆)": [
    "zbożowa",
    "krótkodzioba",
    "białoczelna"
  ],
  "grzebiące (🐔)": [
    "jarząbek",
    "głuszec",
    "bażant"
  ]
}"""

        self.assertEqual(dumped, expected)
예제 #15
0
    def _PrepareV1Request(self, method, args=None):
        """Prepares API v1 request for a given method and args."""

        args_proto = None
        if args:
            args_proto = args.AsPrimitiveProto()
        request = self.connector.BuildRequest(method, args_proto)
        request.url = request.url.replace("/api/v2/", "/api/")
        if args and request.data:
            body_proto = args.__class__().AsPrimitiveProto()
            json_format.Parse(request.data, body_proto)
            body_args = args.__class__()
            body_args.ParseFromString(body_proto.SerializeToString())
            request.data = json.Dump(
                api_value_renderers.StripTypeInfo(
                    api_value_renderers.RenderValue(body_args)),
                encoder=http_api.JSONEncoderWithRDFPrimitivesSupport)

        prepped_request = request.prepare()

        return request, prepped_request
예제 #16
0
  def Generate(self):
    """Prints generated 'golden' output to the stdout."""

    sample_data = {}

    tests = self._GroupRegressionTestsByHandler()
    for handler, test_classes in iteritems(tests):
      for test_class in sorted(test_classes, key=lambda cls: cls.__name__):
        if getattr(test_class, "connection_type", "") != self.connection_type:
          continue

        # If a test is meant to use other tests data, there's nothing to
        # generate.
        if getattr(test_class, "use_golden_files_of", ""):
          continue

        test_class.setUpClass()
        try:
          test_instance = test_class()
          test_instance.setUp()
          try:
            test_instance.Run()
            sample_data.setdefault(handler.__name__,
                                   []).extend(test_instance.checks)
          finally:
            try:
              test_instance.tearDown()
            except Exception as e:  # pylint: disable=broad-except
              logging.exception(e)
        finally:
          try:
            test_class.tearDownClass()
          except Exception as e:  # pylint: disable=broad-except
            logging.exception(e)

    json_sample_data = json.Dump(sample_data, sort_keys=True)
    if compatibility.PY2:
      print(json_sample_data.encode("utf-8"))
    else:
      print(json_sample_data)
예제 #17
0
파일: artifacts.py 프로젝트: x35029/grr
 def ToJson(self):
     artifact_dict = self.ToPrimitiveDict()
     return json.Dump(artifact_dict)