Beispiel #1
0
def dict_v(_self, attr, value):
    """
    Validate dict content, every key, if unicode, have not to
    overcome the generic length limit.
    """
    if not value:
        return {}

    if not isinstance(value, dict):
        raise errors.InvalidModelInput("(%s) dict expected" % attr)

    for key, subvalue in value.iteritems():
        if isinstance(subvalue, str):
            subvalue = unicode(subvalue)

        if isinstance(subvalue, unicode):
            if len(subvalue) > GLSettings.memory_copy.maximum_textsize:
                raise errors.InvalidModelInput("In dict %s the key %s" \
                                                "overcome length limit of %d" % (attr, key,
                                                                                 GLSettings.memory_copy.maximum_textsize))

        if isinstance(subvalue, dict):
            dict_v(_self, attr, subvalue)

    return value
Beispiel #2
0
def dict_v(self, attr, value):
    if not value:
        return {}

    if not isinstance(value, dict):
        raise errors.InvalidModelInput("dict_v: expected dict (%s)" % attr)

    for key, subvalue in value.items():
        if isinstance(subvalue, str):
            subvalue = unicode(subvalue)

        if isinstance(subvalue, unicode):
            if Settings.enable_input_length_checks and len(
                    subvalue) > State.tenant_cache[1].maximum_textsize:
                raise errors.InvalidModelInput("dict_v: text for key %s in %s " \
                                                "overcomes length limit of %d" % (key, attr,
                                                                                  State.tenant_cache[1].maximum_textsize))

        if isinstance(subvalue, dict):
            dict_v(self, attr, subvalue)

    # If a language does not exist, it does not mean that a malicious input have been provided,
    # this condition in fact may happen when a language is removed from the package and
    # so the proper way to handle it so is simply to log the issue and discard the input.
    # https://github.com/globaleaks/GlobaLeaks/issues/879
    remove = [lang for lang in value if lang not in LANGUAGES_SUPPORTED_CODES]
    for k in remove:
        del value[k]

    return value
Beispiel #3
0
    def __call__(self, model_obj, attr, value):
        if not isinstance(value, int):
            raise errors.InvalidModelInput("range_v: expected int (%s)" % attr)
        if value < self.start or value > self.stop:
            m = "range_v(%d, %d): value outside of acceptable range (%d)" % (self.start, self.stop, value)
            raise errors.InvalidModelInput(m)

        return value
Beispiel #4
0
def shorttext_v(self, attr, value):
    if isinstance(value, str):
        value = unicode(value)

    if not isinstance(value, unicode):
        raise errors.InvalidModelInput("shorttext_v: expected unicode (%s:%s)" % (attr, value))

    if GLSettings.enable_input_length_checks and len(value) > GLSettings.memory_copy.maximum_namesize:
        raise errors.InvalidModelInput("shorttext_v: length need to be < of %d"
                                        % GLSettings.memory_copy.maximum_namesize)

    return value
Beispiel #5
0
def shorttext_v(_self, _attr, value):
    """
    Validator for 'name' element, receiver, context, node, and few others
        are here checked
    """
    if isinstance(value, str):
        value = unicode(value)

    if not isinstance(value, unicode):
        raise errors.InvalidModelInput("Name expected unicode (%s)" % value)

    if len(value) > GLSettings.memory_copy.maximum_namesize:
        raise errors.InvalidModelInput("Name length need to be < of %d" %
                                       GLSettings.memory_copy.maximum_namesize)

    return value
Beispiel #6
0
def natnum_v(self, attr, value):
    """
    Validates that the passed value is a natural number (in Z+)
    """
    if not isinstance(value, int) or value < 0:
        raise errors.InvalidModelInput("natnum_v: expected val to be in Z+ (%s:%d)" % (attr, value))
    return value
Beispiel #7
0
def longtext_v(self, attr, value):
    if not attr:
        return value

    if isinstance(value, str):
        value = unicode(value)

    if not isinstance(value, unicode):
        raise errors.InvalidModelInput("longtext_v: expected unicode (%s:%s)" %
                                       (attr, value))

    if GLSettings.enable_input_length_checks and len(value) > GLSettings.memory_copy.maximum_textsize:
        raise errors.InvalidModelInput("longtext_v: unicode text in %s " \
                                        "overcomes length " \
                                        "limit %d" % (attr, GLSettings.memory_copy.maximum_textsize))

    return value
Beispiel #8
0
def longtext_v(_self, attr, value):
    """
    Validator for every generic text element stored in the DB,
    in future may check for markdown
    """
    if not attr:
        return value

    if isinstance(value, str):
        value = unicode(value)

    if not isinstance(value, unicode):
        raise errors.InvalidModelInput("attr %s: Text expected unicode (%s)" %
                                       (attr, value))

    if len(value) > GLSettings.memory_copy.maximum_textsize:
        raise errors.InvalidModelInput("Text unicode in %s " \
                                        "overcomes length " \
                                        "limit %d" % (attr, GLSettings.memory_copy.maximum_textsize))

    return value
Beispiel #9
0
def dict_v(self, attr, value):
    if not value:
        return {}

    if not isinstance(value, dict):
        raise errors.InvalidModelInput("dict_v: expected dict (%s)" % attr)

    for key, subvalue in value.iteritems():
        if isinstance(subvalue, str):
            subvalue = unicode(subvalue)

        if isinstance(subvalue, unicode):
            if GLSettings.enable_input_length_checks and len(
                    subvalue) > GLSettings.memory_copy.maximum_textsize:
                raise errors.InvalidModelInput("dict_v: text for key %s in %s " \
                                                "overcomes length limit of %d" % (key, attr,
                                                                                  GLSettings.memory_copy.maximum_textsize))

        if isinstance(subvalue, dict):
            dict_v(self, attr, subvalue)

    return value
Beispiel #10
0
def longurl_v(_self, attr, value):
    if not re.match(r'^/[a-zA-Z0-9 _\-%?]{0,255}$', value):
        raise errors.InvalidModelInput("invalid longurl")

    return value
Beispiel #11
0
def shorturl_v(_self, attr, value):
    if not re.match(r'^/s/[a-zA-Z0-9_\-%?]{1,30}$', value):
        raise errors.InvalidModelInput("invalid shorturl")

    return value
Beispiel #12
0
def longurl_v(self, attr, value):
    if not re.match(r'^(/[a-z0-9#=_&?/-]{1,255})$', value):
        raise errors.InvalidModelInput("invalid longurl")

    return value
Beispiel #13
0
def shorturl_v(self, attr, value):
    if not re.match(r'^(/s/[a-z0-9]{1,30})$', value):
        raise errors.InvalidModelInput("invalid shorturl")

    return value