Example #1
0
    def test_encode_pretty_printed(self) -> None:
        self.assertEqual(encode_pretty_printed_json({}), b"{}")
        self.assertEqual(list(iterencode_pretty_printed_json({})), [b"{}"])

        # non-ascii should come out utf8-encoded.
        self.assertEqual(
            encode_pretty_printed_json({u"la merde amusée": u"💩"}),
            b'{\n    "la merde amus\xc3\xa9e": "\xF0\x9F\x92\xA9"\n}',
        )
Example #2
0
    def test_unknown_type(self) -> None:
        class Unknown(object):
            pass

        unknown_object = Unknown()
        with self.assertRaises(Exception):
            encode_canonical_json(unknown_object)

        with self.assertRaises(Exception):
            encode_pretty_printed_json(unknown_object)
  def test_verify_json(self):
    # Convert the dictionary to json and pass to verify_json.
    # Test verify_json returns true on data passed.
    json_return_data_string = json.encode_pretty_printed_json(return_data["cdata"])
    return_verify = signing.verify_json(json_return_data_string)
    self.assertTrue(return_verify, "Verify json test did not return true when valid data was passed in.")

    # Test the verify_json function to make sure it is correctly 
    # returning valid results when passing in bad data.
    xdata = return_data["cdata"].copy()
    xdata["FakeName"] = 'FakeHost'
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    return_verify = signing.verify_json(json_xdata_string)
    # Add a Name field.  This should return false.
    self.assertFalse(return_verify, "Verify json test did not return false on invalid data.")

    # Delete the name field to revert to normal.  This should return true.
    xdata = return_data["cdata"].copy()
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    return_verify = signing.verify_json(json_xdata_string)
    self.assertTrue(return_verify, "JSON reverted back, should return true.")

    # Add invalid public key.  This should raise an exception.
    xdata = return_data["cdata"].copy()
    xdata["signed"]["keyval"]["public"] = "000234234243adsfadfasd"
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    self.assertRaises(tuf.CryptoError, signing.verify_json, json_xdata_string)

    # Add invalid private key.  This should raise an exception.
    xdata = return_data["cdata"].copy()
    xdata["signed"]["keyval"]["private"] = "000234234243adsfadfasd"
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    self.assertRaises(tuf.CryptoError, signing.verify_json, json_xdata_string)

    # Add invalid signature sig key.  This should raise an exception.
    xdata = return_data["cdata"].copy()
    xdata["signatures"]["sig"] = "000234234243adfadfadbcs"
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    # Raise a format error for the field.  This field should be
    # formatted as:  a-f, A-F, 0-9.
    self.assertRaises(tuf.FormatError, signing.verify_json, json_xdata_string)
    # Raise an error for an invalid sig field.
    xdata["signatures"]["sig"] = "000234234243adfadfadbc"
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    self.assertRaises(tuf.CryptoError, signing.verify_json, json_xdata_string)

    # Add invalid signature keyid key.  This should raise an exception.
    xdata = return_data["cdata"].copy()
    xdata["signatures"]["keyid"] = "000234234243adfadfadbcs"
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    # Raise a format error for the field.  This field should be
    # formatted as:  a-f, A-F, 0-9.
    self.assertRaises(tuf.FormatError, signing.verify_json, json_xdata_string)
    # Raise an error for an invalid keyid field.
    xdata["signatures"]["keyid"] = "000234234243adfadfadbc"
    json_xdata_string = json.encode_pretty_printed_json(xdata)
    self.assertRaises(tuf.CryptoError, signing.verify_json, json_xdata_string)
Example #4
0
def respond_with_json(
    request,
    code,
    json_object,
    send_cors=False,
    response_code_message=None,
    pretty_print=False,
    canonical_json=True,
):
    # could alternatively use request.notifyFinish() and flip a flag when
    # the Deferred fires, but since the flag is RIGHT THERE it seems like
    # a waste.
    if request._disconnected:
        logger.warning(
            "Not sending response to request %s, already disconnected.", request
        )
        return

    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + b"\n"
    else:
        if canonical_json or synapse.events.USE_FROZEN_DICTS:
            # canonicaljson already encodes to bytes
            json_bytes = encode_canonical_json(json_object)
        else:
            json_bytes = json.dumps(json_object).encode("utf-8")

    return respond_with_json_bytes(
        request,
        code,
        json_bytes,
        send_cors=send_cors,
        response_code_message=response_code_message,
    )
def gen_json(metadata_dict, metadata_name):
  """
  <Purpose>
    Generates the a metadata file in JSON format corresponding to the given
    dictionary. We use pretty-printing to make it human-readable.

  <Arguments>
    metadata_dict:
      The dictionary to be written to a JSON file.

    metadata_name:
      The name to be used for the metadata file (e.g. "metadata"), which will be
      appended with ".json"

  <Exceptions>
    TBD.

  <Return>
    None.
  """

  filename = metadata_name + '.json'
  fileobj = open(filename, 'w')
  fileobj.write(canonicaljson.encode_pretty_printed_json(metadata_dict))
  fileobj.close()
def gen_json(metadata_dict, metadata_name):
    """
  <Purpose>
    Generates the a metadata file in JSON format corresponding to the given
    dictionary. We use pretty-printing to make it human-readable.

  <Arguments>
    metadata_dict:
      The dictionary to be written to a JSON file.

    metadata_name:
      The name to be used for the metadata file (e.g. "metadata"), which will be
      appended with ".json"

  <Exceptions>
    TBD.

  <Return>
    None.
  """

    filename = metadata_name + '.json'
    fileobj = open(filename, 'w')
    fileobj.write(canonicaljson.encode_pretty_printed_json(metadata_dict))
    fileobj.close()
 def test_frozen_dict(self):
     self.assertEquals(
         encode_canonical_json(frozendict({"a": 1})),
         b'{"a":1}',
     )
     self.assertEquals(encode_pretty_printed_json(frozendict({"a": 1})),
                       b'{\n    "a": 1\n}')
Example #8
0
def respond_with_json(request, code, json_object, send_cors=False,
                      response_code_message=None, pretty_print=False,
                      canonical_json=True):
    # could alternatively use request.notifyFinish() and flip a flag when
    # the Deferred fires, but since the flag is RIGHT THERE it seems like
    # a waste.
    if request._disconnected:
        logger.warn(
            "Not sending response to request %s, already disconnected.",
            request)
        return

    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + b"\n"
    else:
        if canonical_json or synapse.events.USE_FROZEN_DICTS:
            # canonicaljson already encodes to bytes
            json_bytes = encode_canonical_json(json_object)
        else:
            json_bytes = json.dumps(json_object).encode("utf-8")

    return respond_with_json_bytes(
        request, code, json_bytes,
        send_cors=send_cors,
        response_code_message=response_code_message,
    )
Example #9
0
 def test_frozen_dict(self) -> None:
     # For mypy's benefit:
     assert frozendict_type is not None
     self.assertEqual(
         encode_canonical_json(frozendict_type({"a": 1})),
         b'{"a":1}',
     )
     self.assertEqual(
         encode_pretty_printed_json(frozendict_type({"a": 1})),
         b'{\n    "a": 1\n}',
     )
Example #10
0
def respond_with_json(
    request: Request,
    code: int,
    json_object: Any,
    send_cors: bool = False,
    pretty_print: bool = False,
    canonical_json: bool = True,
):
    """Sends encoded JSON in response to the given request.

    Args:
        request: The http request to respond to.
        code: The HTTP response code.
        json_object: The object to serialize to JSON.
        send_cors: Whether to send Cross-Origin Resource Sharing headers
            https://fetch.spec.whatwg.org/#http-cors-protocol
        pretty_print: Whether to include indentation and line-breaks in the
            resulting JSON bytes.
        canonical_json: Whether to use the canonicaljson algorithm when encoding
            the JSON bytes.

    Returns:
        twisted.web.server.NOT_DONE_YET if the request is still active.
    """
    # could alternatively use request.notifyFinish() and flip a flag when
    # the Deferred fires, but since the flag is RIGHT THERE it seems like
    # a waste.
    if request._disconnected:
        logger.warning(
            "Not sending response to request %s, already disconnected.",
            request)
        return None

    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + b"\n"
    else:
        if canonical_json or synapse.events.USE_FROZEN_DICTS:
            # canonicaljson already encodes to bytes
            json_bytes = encode_canonical_json(json_object)
        else:
            json_bytes = json.dumps(json_object).encode("utf-8")

    return respond_with_json_bytes(request,
                                   code,
                                   json_bytes,
                                   send_cors=send_cors)
Example #11
0
def respond_with_json(request, code, json_object, send_cors=False,
                      response_code_message=None, pretty_print=False,
                      version_string="", canonical_json=True):
    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + "\n"
    else:
        if canonical_json or synapse.events.USE_FROZEN_DICTS:
            json_bytes = encode_canonical_json(json_object)
        else:
            # ujson doesn't like frozen_dicts.
            json_bytes = ujson.dumps(json_object, ensure_ascii=False)

    return respond_with_json_bytes(
        request, code, json_bytes,
        send_cors=send_cors,
        response_code_message=response_code_message,
        version_string=version_string
    )
Example #12
0
File: server.py Project: Xe/synapse
def respond_with_json(request, code, json_object, send_cors=False,
                      response_code_message=None, pretty_print=False,
                      version_string="", canonical_json=True):
    if pretty_print:
        json_bytes = encode_pretty_printed_json(json_object) + "\n"
    else:
        if canonical_json or synapse.events.USE_FROZEN_DICTS:
            json_bytes = encode_canonical_json(json_object)
        else:
            # ujson doesn't like frozen_dicts.
            json_bytes = ujson.dumps(json_object, ensure_ascii=False)

    return respond_with_json_bytes(
        request, code, json_bytes,
        send_cors=send_cors,
        response_code_message=response_code_message,
        version_string=version_string
    )
Example #13
0
    def handle(self, *args, **options):
        include_re = re.compile(options["data_include_actions"])
        exclude_re = (re.compile(options["data_exclude_actions"])
                      if options["data_exclude_actions"] else None)

        outstream = sys.stdout

        outstream.write("[")
        is_first = True
        last = None
        current = LogEntry.objects.get_chain_end()

        while current and current != last:
            data = {
                "hash": current.auth_hash,
                "entry": current.get_authenticated_dict(),
            }

            if include_re.search(current.action_type) and (
                    exclude_re is None
                    or not exclude_re.search(current.action_type)):
                data["data"] = current.data

            data_pretty = canonicaljson.encode_pretty_printed_json(
                data).decode("utf-8")

            if not is_first:
                outstream.write(",\n    ")
            else:
                outstream.write("\n    ")

            outstream.write("\n    ".join(data_pretty.split("\n")))

            is_first = False
            last = current
            current = current.auth_prev

        outstream.write("\n]\n")
Example #14
0
    def handle(self, *args, **options):
        include_re = re.compile(options['data_include_actions'])
        exclude_re = re.compile(options['data_exclude_actions']
                                ) if options['data_exclude_actions'] else None

        outstream = sys.stdout

        outstream.write('[')
        is_first = True
        last = None
        current = LogEntry.objects.get_chain_end()

        while current and current != last:
            data = {
                'hash': current.auth_hash,
                'entry': current.get_authenticated_dict(),
            }

            if include_re.search(current.action_type) and (
                    exclude_re is None
                    or not exclude_re.search(current.action_type)):
                data['data'] = current.data

            data_pretty = canonicaljson.encode_pretty_printed_json(
                data).decode('utf-8')

            if not is_first:
                outstream.write(',\n    ')
            else:
                outstream.write('\n    ')

            outstream.write('\n    '.join(data_pretty.split('\n')))

            is_first = False
            last = current
            current = current.auth_prev

        outstream.write('\n]\n')
Example #15
0
    def test_invalid_float_values(self) -> None:
        """Infinity/-Infinity/NaN are not allowed in canonicaljson."""

        with self.assertRaises(ValueError):
            encode_canonical_json(inf)

        with self.assertRaises(ValueError):
            encode_pretty_printed_json(inf)

        with self.assertRaises(ValueError):
            encode_canonical_json(-inf)

        with self.assertRaises(ValueError):
            encode_pretty_printed_json(-inf)

        with self.assertRaises(ValueError):
            encode_canonical_json(nan)

        with self.assertRaises(ValueError):
            encode_pretty_printed_json(nan)
 def test_encode_pretty_printed(self):
     self.assertEquals(encode_pretty_printed_json({}), b'{}')
 def test_encode_pretty_printed(self):
     self.assertEquals(encode_pretty_printed_json({}), b'{}')
Example #18
0
 def payload(self):
     payload = attr.asdict(self)
     payload.pop("signatures")
     return canonicaljson.encode_pretty_printed_json(payload)
Example #19
0
 def __repr__(self):
     return canonicaljson.encode_pretty_printed_json(attr.asdict(self))
Example #20
0
 def payload(self):
   payload = attr.asdict(self)
   payload.pop("signatures")
   return canonicaljson.encode_pretty_printed_json(payload)
Example #21
0
 def __repr__(self):
   return canonicaljson.encode_pretty_printed_json(attr.asdict(self))
Example #22
0
 def __repr__(self):
     # Note: The string returned from this function is used to generate
     # and verify signatures (c.f. `metadata.Metablock`). Changes to this
     # function might break backwards compatibility with existing metadata.
     return canonicaljson.encode_pretty_printed_json(attr.asdict(self))
    def test_verify_json(self):
        # Convert the dictionary to json and pass to verify_json.
        # Test verify_json returns true on data passed.
        json_return_data_string = json.encode_pretty_printed_json(
            return_data["cdata"])
        return_verify = signing.verify_json(json_return_data_string)
        self.assertTrue(
            return_verify,
            "Verify json test did not return true when valid data was passed in."
        )

        # Test the verify_json function to make sure it is correctly
        # returning valid results when passing in bad data.
        xdata = return_data["cdata"].copy()
        xdata["FakeName"] = 'FakeHost'
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        return_verify = signing.verify_json(json_xdata_string)
        # Add a Name field.  This should return false.
        self.assertFalse(
            return_verify,
            "Verify json test did not return false on invalid data.")

        # Delete the name field to revert to normal.  This should return true.
        xdata = return_data["cdata"].copy()
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        return_verify = signing.verify_json(json_xdata_string)
        self.assertTrue(return_verify,
                        "JSON reverted back, should return true.")

        # Add invalid public key.  This should raise an exception.
        xdata = return_data["cdata"].copy()
        xdata["signed"]["keyval"]["public"] = "000234234243adsfadfasd"
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        self.assertRaises(tuf.CryptoError, signing.verify_json,
                          json_xdata_string)

        # Add invalid private key.  This should raise an exception.
        xdata = return_data["cdata"].copy()
        xdata["signed"]["keyval"]["private"] = "000234234243adsfadfasd"
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        self.assertRaises(tuf.CryptoError, signing.verify_json,
                          json_xdata_string)

        # Add invalid signature sig key.  This should raise an exception.
        xdata = return_data["cdata"].copy()
        xdata["signatures"]["sig"] = "000234234243adfadfadbcs"
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        # Raise a format error for the field.  This field should be
        # formatted as:  a-f, A-F, 0-9.
        self.assertRaises(tuf.FormatError, signing.verify_json,
                          json_xdata_string)
        # Raise an error for an invalid sig field.
        xdata["signatures"]["sig"] = "000234234243adfadfadbc"
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        self.assertRaises(tuf.CryptoError, signing.verify_json,
                          json_xdata_string)

        # Add invalid signature keyid key.  This should raise an exception.
        xdata = return_data["cdata"].copy()
        xdata["signatures"]["keyid"] = "000234234243adfadfadbcs"
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        # Raise a format error for the field.  This field should be
        # formatted as:  a-f, A-F, 0-9.
        self.assertRaises(tuf.FormatError, signing.verify_json,
                          json_xdata_string)
        # Raise an error for an invalid keyid field.
        xdata["signatures"]["keyid"] = "000234234243adfadfadbc"
        json_xdata_string = json.encode_pretty_printed_json(xdata)
        self.assertRaises(tuf.CryptoError, signing.verify_json,
                          json_xdata_string)
 def test_frozen_dict(self):
     self.assertEquals(encode_canonical_json(frozendict({})), b'{}')
     self.assertEquals(encode_pretty_printed_json(frozendict({})), b'{}')
Example #25
0
 def test_frozen_dict(self):
     self.assertEquals(encode_canonical_json(frozendict({})), b'{}')
     self.assertEquals(encode_pretty_printed_json(frozendict({})), b'{}')