Ejemplo n.º 1
0
    def test_scrub_locals(self, send_payload):
        invalid_b64 = b'CuX2JKuXuLVtJ6l1s7DeeQ=='
        invalid = base64.b64decode(invalid_b64)

        def _raise():
            # Make sure that the _invalid local variable makes its
            # way into the payload even if its value cannot be serialized
            # properly.
            _invalid = invalid

            # Make sure the Password field gets scrubbed even though its
            # original value could not be serialized properly.
            Password = invalid

            password = '******'
            raise Exception((_invalid, Password, password))

        try:
            _raise()
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals']['password'], '\*+')
        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals']['Password'], '\*+')
        self.assertIn('_invalid', payload['data']['body']['trace']['frames'][-1]['locals'])

        binary_type_name = 'str' if python_major_version() < 3 else 'bytes'
        undecodable_message = '<Undecodable type:(%s) base64:(%s)>' % (binary_type_name, base64.b64encode(invalid).decode('ascii'))
        self.assertEqual(undecodable_message, payload['data']['body']['trace']['frames'][-1]['locals']['_invalid'])
Ejemplo n.º 2
0
    def test_scrub_locals(self, send_payload):
        invalid_b64 = b'CuX2JKuXuLVtJ6l1s7DeeQ=='
        invalid = base64.b64decode(invalid_b64)

        def _raise():
            # Make sure that the _invalid local variable makes its
            # way into the payload even if its value cannot be serialized
            # properly.
            _invalid = invalid

            # Make sure the Password field gets scrubbed even though its
            # original value could not be serialized properly.
            Password = invalid

            password = '******'
            raise Exception((_invalid, Password, password))

        try:
            _raise()
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = send_payload.call_args[0][0]

        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals']['password'], '\*+')
        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals']['Password'], '\*+')
        self.assertIn('_invalid', payload['data']['body']['trace']['frames'][-1]['locals'])

        binary_type_name = 'str' if python_major_version() < 3 else 'bytes'
        undecodable_message = '<Undecodable type:(%s) base64:(%s)>' % (binary_type_name, base64.b64encode(invalid).decode('ascii'))
        self.assertEqual(undecodable_message, payload['data']['body']['trace']['frames'][-1]['locals']['_invalid'])
Ejemplo n.º 3
0
def transform(obj, transform, key=None):
    key = key or ()

    def do_transform(type_name, val, key=None, **kw):
        fn = getattr(transform, 'transform_%s' % type_name,
                     transform.transform_custom)
        val = fn(val, key=key, **kw)

        return val

    if python_major_version() < 3:

        def string_handler(s, key=None):
            if isinstance(s, str):
                return do_transform('py2_str', s, key=key)
            elif isinstance(s, unicode):
                return do_transform('unicode', s, key=key)
    else:

        def string_handler(s, key=None):
            if isinstance(s, bytes):
                return do_transform('py3_bytes', s, key=key)
            elif isinstance(s, str):
                return do_transform('unicode', s, key=key)

    def default_handler(o, key=None):
        if isinstance(o, integer_types + (float, )):
            return do_transform('number', o, key=key)

        if isinstance(o, bool):
            return do_transform('boolean', o, key=key)

        return do_transform('custom', o, key=key)

    handlers = {
        'string_handler':
        string_handler,
        'tuple_handler':
        lambda o, key=None: do_transform('tuple', o, key=key),
        'namedtuple_handler':
        lambda o, key=None: do_transform('namedtuple', o, key=key),
        'list_handler':
        lambda o, key=None: do_transform('list', o, key=key),
        'set_handler':
        lambda o, key=None: do_transform('set', o, key=key),
        'mapping_handler':
        lambda o, key=None: do_transform('dict', o, key=key),
        'circular_reference_handler':
        lambda o, key=None, ref_key=None: do_transform(
            'circular_reference', o, key=key, ref_key=ref_key),
        'default_handler':
        default_handler,
        'allowed_circular_reference_types':
        _ALLOWED_CIRCULAR_REFERENCE_TYPES
    }

    return traverse.traverse(obj, key=key, **handlers)
    def test_encode_namedtuple(self):
        MyType = collections.namedtuple('MyType', ('field_1', 'field_2'))
        nt = MyType(field_1='this is field 1', field_2=invalid)

        start = nt
        if python_major_version() < 3:
            expected = "<MyType(field_1='this is field 1', field_2=u'%s')>" % undecodable_repr
        else:
            expected = "<MyType(field_1='this is field 1', field_2='%s')>" % undecodable_repr

        self._assertSerialized(start, expected)
    def test_encode_namedtuple(self):
        MyType = collections.namedtuple('MyType', ('field_1', 'field_2'))
        nt = MyType(field_1='this is field 1', field_2=invalid)

        start = nt
        if python_major_version() < 3:
            expected = "<MyType(field_1='this is field 1', field_2=u'%s')>" % undecodable_repr
        else:
            expected = "<MyType(field_1='this is field 1', field_2='%s')>" % undecodable_repr

        self._assertSerialized(start, expected)
Ejemplo n.º 6
0
def transform(obj, transform, key=None):
    key = key or ()

    def do_transform(type_name, val, key=None, **kw):
        fn = getattr(transform, 'transform_%s' % type_name, transform.transform_custom)
        val = fn(val, key=key, **kw)

        return val

    if python_major_version() < 3:
        def string_handler(s, key=None):
            if isinstance(s, str):
                return do_transform('py2_str', s, key=key)
            elif isinstance(s, unicode):
                return do_transform('unicode', s, key=key)
    else:
        def string_handler(s, key=None):
            if isinstance(s, bytes):
                return do_transform('py3_bytes', s, key=key)
            elif isinstance(s, str):
                return do_transform('unicode', s, key=key)

    def default_handler(o, key=None):
        if isinstance(o, bool):
            return do_transform('boolean', o, key=key)

        # There is a quirk in the current version (1.1.6) of the enum
        # backport enum34 which causes it to not have the same
        # behavior as Python 3.4+. One way to identify IntEnums is that
        # they are instances of numbers but not number types.
        if isinstance(o, number_types):
            if type(o) not in number_types:
                return do_transform('custom', o, key=key)
            else:
                return do_transform('number', o, key=key)

        return do_transform('custom', o, key=key)

    handlers = {
        'string_handler': string_handler,
        'tuple_handler': lambda o, key=None: do_transform('tuple', o, key=key),
        'namedtuple_handler': lambda o, key=None: do_transform('namedtuple', o, key=key),
        'list_handler': lambda o, key=None: do_transform('list', o, key=key),
        'set_handler': lambda o, key=None: do_transform('set', o, key=key),
        'mapping_handler': lambda o, key=None: do_transform('dict', o, key=key),
        'circular_reference_handler': lambda o, key=None, ref_key=None:
            do_transform('circular_reference', o, key=key, ref_key=ref_key),
        'default_handler': default_handler,
        'allowed_circular_reference_types': _ALLOWED_CIRCULAR_REFERENCE_TYPES
    }

    return traverse.traverse(obj, key=key, **handlers)
    def test_encode_with_custom_repr_returns_bytes(self):
        class CustomRepr(object):
            def __repr__(self):
                return b'hello'

        start = {'hello': 'world', 'custom': CustomRepr()}

        serializable = SerializableTransform(whitelist_types=[CustomRepr])
        result = transforms.transform(start, serializable)

        if python_major_version() < 3:
            self.assertEqual(result['custom'], b'hello')
        else:
            self.assertRegex(result['custom'], "<class '.*CustomRepr'>")
    def test_encode_with_custom_repr_returns_bytes(self):
        class CustomRepr(object):
            def __repr__(self):
                return b'hello'

        start = {'hello': 'world', 'custom': CustomRepr()}

        serializable = SerializableTransform(whitelist_types=[CustomRepr])
        result = transforms.transform(start, serializable)

        if python_major_version() < 3:
            self.assertEqual(result['custom'], b'hello')
        else:
            self.assertRegex(result['custom'], "<class '.*CustomRepr'>")
Ejemplo n.º 9
0
def transform(obj, transforms, key=None):
    key = key or ()

    def do_transforms(type_name, val, key=None, **kw):
        for transform in transforms:
            fn = getattr(transform, 'transform_%s' % type_name, transform.transform_custom)
            val = fn(val, key=key, **kw)

        return val

    if python_major_version() < 3:
        def string_handler(s, key=None):
            if isinstance(s, str):
                return do_transforms('py2_str', s, key=key)
            elif isinstance(s, unicode):
                return do_transforms('unicode', s, key=key)
    else:
        def string_handler(s, key=None):
            if isinstance(s, bytes):
                return do_transforms('py3_bytes', s, key=key)
            elif isinstance(s, str):
                return do_transforms('unicode', s, key=key)

    def default_handler(o, key=None):
        if isinstance(o, integer_types + (float,)):
            return do_transforms('number', o, key=key)

        if isinstance(o, bool):
            return do_transforms('boolean', o, key=key)

        return do_transforms('custom', o, key=key)

    handlers = {
        'string_handler': string_handler,
        'tuple_handler': lambda o, key=None: do_transforms('tuple', o, key=key),
        'namedtuple_handler': lambda o, key=None: do_transforms('namedtuple', o, key=key),
        'list_handler': lambda o, key=None: do_transforms('list', o, key=key),
        'set_handler': lambda o, key=None: do_transforms('set', o, key=key),
        'mapping_handler': lambda o, key=None: do_transforms('dict', o, key=key),
        'circular_reference_handler': lambda o, key=None, ref_key=None: \
            do_transforms('circular_reference', o, key=key, ref_key=ref_key),
        'default_handler': default_handler,
        'allowed_circular_reference_types': _ALLOWED_CIRCULAR_REFERENCE_TYPES
    }

    return traverse.traverse(obj, key=key, **handlers)
Ejemplo n.º 10
0
    def transform_dict(self, o, key=None):
        ret = {}
        for k, v in iteritems(o):
            if isinstance(k, string_types) or isinstance(k, binary_type):
                if python_major_version() < 3:
                    if isinstance(k, unicode):
                        new_k = self.transform_unicode(k)
                    else:
                        new_k = self.transform_py2_str(k)
                else:
                    if isinstance(k, bytes):
                        new_k = self.transform_py3_bytes(k)
                    else:
                        new_k = self.transform_unicode(k)
            else:
                new_k = text(k)

            ret[new_k] = v

        return super(SerializableTransform, self).transform_dict(ret, key=key)
Ejemplo n.º 11
0
    def transform_dict(self, o, key=None):
        ret = {}
        for k, v in iteritems(o):
            if isinstance(k, string_types) or isinstance(k, binary_type):
                if python_major_version() < 3:
                    if isinstance(k, unicode):
                        new_k = self.transform_unicode(k)
                    else:
                        new_k = self.transform_py2_str(k)
                else:
                    if isinstance(k, bytes):
                        new_k = self.transform_py3_bytes(k)
                    else:
                        new_k = self.transform_unicode(k)
            else:
                new_k = text(k)

            ret[new_k] = v

        return super(SerializableTransform, self).transform_dict(ret, key=key)
import copy
import enum

try:
    # Python 3
    from collections.abc import Mapping
except ImportError:
    # Python 2.7
    from collections import Mapping

from rollbar.lib import transforms, python_major_version
from rollbar.lib.transforms.serializable import SerializableTransform

from rollbar.test import BaseTest, SNOWMAN, SNOWMAN_UNICODE

if python_major_version() >= 3:
    SNOWMAN = SNOWMAN_UNICODE

SNOWMAN_LEN = len(SNOWMAN)


# This base64 encoded string contains bytes that do not
# convert to utf-8 data
invalid_b64 = b'CuX2JKuXuLVtJ6l1s7DeeQ=='

invalid = base64.b64decode(invalid_b64)
binary_type_name = 'str' if python_major_version() < 3 else 'bytes'
undecodable_repr = '<Undecodable type:(%s) base64:(%s)>' % (binary_type_name, invalid_b64.decode('ascii'))


class SerializableTransformTest(BaseTest):
import copy

from rollbar.lib import map, transforms, string_types, urlparse, parse_qs, python_major_version
from rollbar.lib.transforms.scruburl import ScrubUrlTransform, _starts_with_auth_re

from rollbar.test import BaseTest, SNOWMAN, SNOWMAN_UNICODE

if python_major_version() >= 3:
    SNOWMAN = SNOWMAN_UNICODE

SNOWMAN_LEN = len(SNOWMAN)


class ScrubUrlTransformTest(BaseTest):
    def _assertScrubbed(self,
                        params_to_scrub,
                        start,
                        expected,
                        scrub_username=False,
                        scrub_password=True,
                        redact_char='-',
                        skip_id_check=False):
        scrubber = ScrubUrlTransform(suffixes=[],
                                     params_to_scrub=params_to_scrub,
                                     scrub_username=scrub_username,
                                     scrub_password=scrub_password,
                                     redact_char=redact_char,
                                     randomize_len=False)
        result = transforms.transform(start, scrubber)

        if not skip_id_check:
import collections
import base64
import copy
import enum

from rollbar.lib import transforms, python_major_version
from rollbar.lib.transforms.serializable import SerializableTransform

from rollbar.test import BaseTest, SNOWMAN, SNOWMAN_UNICODE

if python_major_version() >= 3:
    SNOWMAN = SNOWMAN_UNICODE

SNOWMAN_LEN = len(SNOWMAN)


# This base64 encoded string contains bytes that do not
# convert to utf-8 data
invalid_b64 = b'CuX2JKuXuLVtJ6l1s7DeeQ=='

invalid = base64.b64decode(invalid_b64)
binary_type_name = 'str' if python_major_version() < 3 else 'bytes'
undecodable_repr = '<Undecodable type:(%s) base64:(%s)>' % (binary_type_name, invalid_b64.decode('ascii'))


class SerializableTransformTest(BaseTest):
    def _assertSerialized(self, start, expected, safe_repr=True, whitelist=None, skip_id_check=False):
        serializable = SerializableTransform(safe_repr=safe_repr, whitelist_types=whitelist)
        result = transforms.transform(start, serializable)

        """
Ejemplo n.º 15
0
import copy

from rollbar.lib import map, transforms, string_types, urlparse, parse_qs, python_major_version
from rollbar.lib.transforms.scruburl import ScrubUrlTransform, _starts_with_auth_re

from rollbar.test import BaseTest, SNOWMAN, SNOWMAN_UNICODE

if python_major_version() >= 3:
    SNOWMAN = SNOWMAN_UNICODE

SNOWMAN_LEN = len(SNOWMAN)


class ScrubUrlTransformTest(BaseTest):
    def _assertScrubbed(self,
                        params_to_scrub,
                        start,
                        expected,
                        scrub_username=False,
                        scrub_password=True,
                        redact_char='-',
                        skip_id_check=False):
        scrubber = ScrubUrlTransform(suffixes=[],
                                     params_to_scrub=params_to_scrub,
                                     scrub_username=scrub_username,
                                     scrub_password=scrub_password,
                                     redact_char=redact_char,
                                     randomize_len=False)
        result = transforms.transform(start, [scrubber])

        """
Ejemplo n.º 16
0
def transform(obj, transform, key=None):
    key = key or ()

    def do_transform(type_name, val, key=None, **kw):
        fn = getattr(transform, 'transform_%s' % type_name,
                     transform.transform_custom)
        val = fn(val, key=key, **kw)

        return val

    if python_major_version() < 3:

        def string_handler(s, key=None):
            if isinstance(s, str):
                return do_transform('py2_str', s, key=key)
            elif isinstance(s, unicode):
                return do_transform('unicode', s, key=key)
    else:

        def string_handler(s, key=None):
            if isinstance(s, bytes):
                return do_transform('py3_bytes', s, key=key)
            elif isinstance(s, str):
                return do_transform('unicode', s, key=key)

    def default_handler(o, key=None):
        if isinstance(o, bool):
            return do_transform('boolean', o, key=key)

        # There is a quirk in the current version (1.1.6) of the enum
        # backport enum34 which causes it to not have the same
        # behavior as Python 3.4+. One way to identify IntEnums is that
        # they are instances of numbers but not number types.
        if isinstance(o, number_types):
            if type(o) not in number_types:
                return do_transform('custom', o, key=key)
            else:
                return do_transform('number', o, key=key)

        return do_transform('custom', o, key=key)

    handlers = {
        'string_handler':
        string_handler,
        'tuple_handler':
        lambda o, key=None: do_transform('tuple', o, key=key),
        'namedtuple_handler':
        lambda o, key=None: do_transform('namedtuple', o, key=key),
        'list_handler':
        lambda o, key=None: do_transform('list', o, key=key),
        'set_handler':
        lambda o, key=None: do_transform('set', o, key=key),
        'mapping_handler':
        lambda o, key=None: do_transform('dict', o, key=key),
        'circular_reference_handler':
        lambda o, key=None, ref_key=None: do_transform(
            'circular_reference', o, key=key, ref_key=ref_key),
        'default_handler':
        default_handler,
        'allowed_circular_reference_types':
        _ALLOWED_CIRCULAR_REFERENCE_TYPES
    }

    return traverse.traverse(obj, key=key, **handlers)