Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
rdict = {'xml': None,
'best_practices': None,
#'profile': None,
}
xml = xml.encode('utf-8').strip()
f = BytesIO(xml)
try:
result = sdv.validate_xml(f, schemaloc=True)
rdict['xml'] = result.as_dict()
except sdv.errors.UnknownSTIXVersionError, e:
rdict['xml'] = "Could not determine @version attribute: %s" % str(e)
except sdv.errors.InvalidSTIXVersionError, e:
rdict['xml'] = "@version attribute is invalid: %s" % str(e)
except sdv.errors.ValidationError, e:
rdict['xml'] = "Schema directory not found or schemaloc is False: %s" % str(e)
except sdv.errors.XMLSchemaImportError, e:
rdict['xml'] = "Error while processing schemas for validation: %s" % str(e)
except sdv.errors.XMLSchemaIncludeError, e:
rdict['xml'] = "Error processing xs:include directives: %s" % str(e)
except IOError, e:
rdict['xml'] = "Not a valid XML document: %s" % str(e)
except Exception, e:
rdict['xml'] = str(e)
f.seek(0)
try:
result = sdv.validate_best_practices(f)
rdict['best_practices'] = result.as_dict()
except sdv.errors.UnknownSTIXVersionError, e:
rdict['best_practices'] = "Could not determine @version attribute: %s" % str(e)
# Copyright (c) 2014, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.
class ValidationError(Exception):
"""Base Exception for all validator-specific exceptions. This is used
directly by some modules as a generic Exception.
"""
pass
class UnknownNamespaceError(ValidationError):
"""Raised when an unknown namespace is encountered in a function.
"""
pass
class UnknownVocabularyError(ValidationError):
"""Raised when an unknown controlled vocabulary name is discovered
during best practice validation.
"""
pass
class IdrefLookupError(ValidationError):
"""Raised when an attempt to resolve an ID reference fails. This can
class ValidationError(Exception):
"""Base Exception for all validator-specific exceptions. This is used
directly by some modules as a generic Exception.
"""
pass
class UnknownNamespaceError(ValidationError):
"""Raised when an unknown namespace is encountered in a function.
"""
pass
class UnknownVocabularyError(ValidationError):
"""Raised when an unknown controlled vocabulary name is discovered
during best practice validation.
"""
pass
class IdrefLookupError(ValidationError):
"""Raised when an attempt to resolve an ID reference fails. This can
occur when the full STIX component definition resides outside of the
input document.
"""
def __init__(self, idref, message=None):
super(IdrefLookupError, self).__init__(message)
self.idref = idref
def check_root(doc):
if utils.is_stix(doc):
return
error = "Input document does not contain a valid STIX root element."
raise errors.ValidationError(error)
"""
def __init__(self, idref, message=None):
super(IdrefLookupError, self).__init__(message)
self.idref = idref
class XMLSchemaIncludeError(ValidationError):
"""Raised when errors occur during the processing of ``xs:include``
directives found within schema documents.
"""
pass
class XMLSchemaImportError(ValidationError):
"""Raised when errors occur when generating ``xs:import`` directives for
the "uber" schema, used to validate XML instance documents.
"""
pass
class UnknownSTIXVersionError(ValidationError):
"""Raised when no STIX version information can be found in an instance
document and no version information was provided to a method which
requires version information.
"""
pass
the "uber" schema, used to validate XML instance documents.
"""
pass
class UnknownSTIXVersionError(ValidationError):
"""Raised when no STIX version information can be found in an instance
document and no version information was provided to a method which
requires version information.
"""
pass
class InvalidSTIXVersionError(ValidationError):
"""Raised when an invalid version of STIX is discovered within an instance
document or is passed into a method which depends on STIX version
information.
Args:
message: The error message.
expected: A version or list of expected versions.
found: The STIX version that was declared for an instance document or
found within an instance document.
"""
def __init__(self, message, expected=None, found=None):
super(InvalidSTIXVersionError, self).__init__(message)
self.expected = expected
self.found = found
information.
Args:
message: The error message.
expected: A version or list of expected versions.
found: The CybOX version that was declared for an instance document or
found within an instance document.
"""
def __init__(self, message, expected=None, found=None):
super(InvalidCyboxVersionError, self).__init__(message)
self.expected = expected
self.found = found
class ProfileParseError(ValidationError):
"""Raised when an error occurs during the parse or initialization
of a STIX profile document.
"""
pass
"""
try:
if isinstance(doc, etree._Element): # noqa
root = doc
elif isinstance(doc, etree._ElementTree): # noqa
root = doc.getroot()
else:
parser = get_xml_parser()
if isinstance(doc, StringIO):
tree = etree.parse(BytesIO(doc.getvalue().encode()), parser=parser)
else:
tree = etree.parse(doc, parser=parser)
root = tree.getroot()
except Exception as ex:
raise errors.ValidationError(str(ex))
return root