Example #1
0
 def _validate_icon(self, document):
     mime_type = None
     encoding = None
     data = None
     xml = StringIO(document)
     try:
         tree = etree.parse(xml)
         root = tree.getroot()
         ns = root.nsmap[None]
         for element in root:
             if element.tag == "{%s}mime-type" % ns:
                 mime_type = element.text.lower()
             if element.tag == "{%s}encoding" % ns:
                 encoding = element.text.lower()
             if element.tag == "{%s}data" % ns:
                 data = element.text
     except etree.ParseError:
         raise errors.NotWellFormedError()
     else:
         if mime_type not in self.icon_mime_types:
             raise errors.ConstraintFailureError(
                 phrase="Unsupported MIME type. Allowed MIME types: %s" %
                 ','.join(self.icon_mime_types))
         if encoding != self.icon_encoding:
             raise errors.ConstraintFailureError(
                 phrase="Unsupported encoding. Allowed enconding: %s" %
                 self.icon_encoding)
         if data is None:
             raise errors.ConstraintFailureError(
                 phrase="No icon data was provided")
         if len(data) > self.icon_max_size:
             raise errors.ConstraintFailureError(
                 phrase=
                 "Size limit exceeded, maximum allowed size is %d bytes" %
                 self.icon_max_size)
Example #2
0
 def validate_document(self, xcap_doc):
     """Check if a document is valid for this application."""
     try:
         xml_doc = etree.parse(StringIO(xcap_doc))
         # XXX do not use TreeBuilder here
     except etree.XMLSyntaxError, ex:
         ex.http_error = errors.NotWellFormedError(comment=str(ex))
         raise
Example #3
0
    def _validate_rules(self, document, node_uri):
        common_policy_namespace = 'urn:ietf:params:xml:ns:common-policy'
        oma_namespace = 'urn:oma:xml:xdm:common-policy'

        actions_tag = '{%s}actions' % common_policy_namespace
        conditions_tag = '{%s}conditions' % common_policy_namespace
        identity_tag = '{%s}identity' % common_policy_namespace
        rule_tag = '{%s}rule' % common_policy_namespace
        transformations_tag = '{%s}transformations' % common_policy_namespace

        sub_handling_tag = '{%s}sub-handling' % self.default_ns

        oma_anonymous_request_tag = '{%s}anonymous-request' % oma_namespace
        oma_entry_tag = '{%s}entry' % oma_namespace
        oma_external_list_tag = '{%s}external-list' % oma_namespace
        oma_other_identity_tag = '{%s}other-identity' % oma_namespace

        try:
            xml = StringIO(document)
            tree = etree.parse(xml)
            root = tree.getroot()

            if oma_namespace in root.nsmap.values():
                # Condition constraints
                for element in root.iter(conditions_tag):
                    if any([
                            len(element.findall(item)) > 1
                            for item in (identity_tag, oma_external_list_tag,
                                         oma_other_identity_tag,
                                         oma_anonymous_request_tag)
                    ]):
                        raise errors.ConstraintFailureError(
                            phrase="Complex rules are not allowed")
                # Transformations constraints
                for rule in root.iter(rule_tag):
                    actions = rule.find(actions_tag)
                    if actions is not None:
                        sub_handling = actions.find(sub_handling_tag)
                        transformations = rule.find(transformations_tag)
                        if sub_handling is not None and sub_handling.text != 'allow' and transformations is not None and transformations.getchildren(
                        ):
                            raise errors.ConstraintFailureError(
                                phrase="transformations element not allowed")
                # External list constraints
                if not ServerConfig.allow_external_references:
                    for element in root.iter(oma_external_list_tag):
                        for entry in element.iter(oma_entry_tag):
                            self._check_external_list(
                                entry.attrib.get('anc', None), node_uri)
        except etree.ParseError:
            raise errors.NotWellFormedError()
Example #4
0
class ApplicationUsage(object):
    """Base class defining an XCAP application"""
    id = None                ## the Application Unique ID (AUID)
    default_ns = None        ## the default XML namespace
    mime_type = None         ## the MIME type
    schema_file = None       ## filename of the schema for the application

    def __init__(self, storage):
        ## the XML schema that defines valid documents for this application
        if self.schema_file:
            xml_schema_doc = etree.parse(open(os.path.join(os.path.dirname(__file__), 'xml-schemas', self.schema_file), 'r'))
            self.xml_schema = etree.XMLSchema(xml_schema_doc)
        else:
            class EverythingIsValid(object):
                def __call__(self, *args, **kw):
                    return True
                def validate(self, *args, **kw):
                    return True
            self.xml_schema = EverythingIsValid()
        if storage is not None:
            self.storage = storage

    ## Validation

    def _check_UTF8_encoding(self, xml_doc):
        """Check if the document is UTF8 encoded. Raise an NotUTF8Error if it's not."""
        if xml_doc.docinfo.encoding.lower() != 'utf-8':
            raise errors.NotUTF8Error(comment='document encoding is %s' % xml_doc.docinfo.encoding)

    def _check_schema_validation(self, xml_doc):
        """Check if the given XCAP document validates against the application's schema"""
        if not self.xml_schema(xml_doc):
            raise errors.SchemaValidationError(comment=self.xml_schema.error_log)

    def _check_additional_constraints(self, xml_doc):
        """Check additional validations constraints for this XCAP document. Should be
           overriden in subclasses if specified by the application usage, and raise
           a ConstraintFailureError if needed."""

    def validate_document(self, xcap_doc):
        """Check if a document is valid for this application."""
        try:
            xml_doc = etree.parse(StringIO(xcap_doc))
            # XXX do not use TreeBuilder here
        except etree.XMLSyntaxError, ex:
            ex.http_error = errors.NotWellFormedError(comment=str(ex))
            raise
        except Exception, ex:
            ex.http_error = errors.NotWellFormedError()
            raise