How to use the sdv.validators.stix.best_practice.BestPracticeWarning function in sdv

To help you get started, we’ve selected a few sdv examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
idref       = attrib('idref')
            timestamp   = attrib('timestamp')

            if timestamp:
                tz_set = utils.has_tzinfo(timestamp)

                if not tz_set:
                    warning = BestPracticeWarning(
                        node = node,
                        message="Timestamp without timezone information."
                    )
                    warning['timestamp'] = timestamp
                    results.append(warning)

            if id_ and not timestamp:
                warning = BestPracticeWarning(
                    node=node,
                    message="ID present but missing timestamp"
                )
            elif idref and not timestamp:
                warning = BestPracticeWarning(
                    node=node,
                    message="IDREF present but missing timestamp"
                )
            elif idref and timestamp:
                resolves = common.idref_timestamp_resolves(
                    root=root,
                    idref=idref,
                    timestamp=timestamp,
                    namespaces=namespaces
                )
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def _get_campaign_related_indicators(self, root, namespaces):
        xpath = ".//{0}:Related_Indicators".format(common.PREFIX_STIX_CAMPAIGN)
        nodes = root.xpath(xpath, namespaces=namespaces)
        msg = "Related_Indicators has been deprecated in Campaign."
        return [BestPracticeWarning(node=n, message=msg) for n in nodes]
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def _check_titles(self, root, namespaces, selectors):
        """Checks that each node in `nodes` has a ``Title`` element unless
        there is an ``@idref`` attribute set.

        """
        results = BestPracticeWarningCollection("Missing Titles")
        xpath = " | ".join("//%s" % x for x in selectors)
        nodes = root.xpath(xpath, namespaces=namespaces)

        for node in nodes:
            if 'idref' in node.attrib:
                continue

            if not any(utils.localname(x) == 'Title' for x in utils.iterchildren(node)):
                warning = BestPracticeWarning(node=node)
                results.append(warning)

        return results
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def _check_idref_with_content(self, root, namespaces, version):  # noqa
        """Checks that constructs with idref set do not contain content.

        Note:
            Some STIX/CybOX constructs (e.g., ``Related_Object`` instances) are
            exceptions to this rule.

        """
        def is_invalid(node):
            if common.is_idref_content_exception(node):
                return False

            return utils.has_content(node)

        nodes = root.xpath("//*[@idref]")
        warnings = (BestPracticeWarning(x) for x in nodes if is_invalid(x))

        results = BestPracticeWarningCollection("IDREF with Content")
        results.extend(warnings)

        return results
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
if not tz_set:
                    warning = BestPracticeWarning(
                        node = node,
                        message="Timestamp without timezone information."
                    )
                    warning['timestamp'] = timestamp
                    results.append(warning)

            if id_ and not timestamp:
                warning = BestPracticeWarning(
                    node=node,
                    message="ID present but missing timestamp"
                )
            elif idref and not timestamp:
                warning = BestPracticeWarning(
                    node=node,
                    message="IDREF present but missing timestamp"
                )
            elif idref and timestamp:
                resolves = common.idref_timestamp_resolves(
                    root=root,
                    idref=idref,
                    timestamp=timestamp,
                    namespaces=namespaces
                )

                if resolves:
                    continue

                warning = BestPracticeWarning(
                    node=node,
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def _check_1_0_duplicate_ids(self, root, namespaces, version):  # noqa
        """Checks for duplicate ids in the document.

        """
        id_nodes = collections.defaultdict(list)

        for node in root.xpath("//*[@id]"):
            id_nodes[node.attrib['id']].append(node)

        results = BestPracticeWarningCollection('Duplicate IDs')
        for nodes in itervalues(id_nodes):
            if len(nodes) > 1:
                results.extend(BestPracticeWarning(node=x) for x in nodes)

        return results
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
"""
        to_check = itertools.chain(
            common.STIX_CORE_COMPONENTS,
            common.CYBOX_CORE_COMPONENTS
        )

        results = BestPracticeWarningCollection('Missing IDs')
        xpath = " | ".join("//%s" % x for x in to_check)
        nodes = root.xpath(xpath, namespaces=namespaces)

        for node in nodes:
            if any(x in node.attrib for x in ('id', 'idref')):
                continue

            warning = BestPracticeWarning(node=node)
            results.append(warning)

        return results
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
to_check = itertools.chain(
            common.STIX_CORE_COMPONENTS,
            common.CYBOX_CORE_COMPONENTS
        )

        results = BestPracticeWarningCollection('ID Format')
        msg = "ID should be formatted as [ns prefix]:[construct type]-[GUID]"
        xpath = " | ".join("//%s[@id]" % x for x in to_check)

        for node in root.xpath(xpath, namespaces=namespaces):
            id_ = node.attrib['id']

            if ID_PATTERN.match(id_):
                continue

            result = BestPracticeWarning(node=node, message=msg)
            results.append(result)

        return results
github STIXProject / stix-validator / sdv / validators / stix / best_practice.py View on Github external
def insert(self, idx, value):
        """Inserts `value` at `idx` into this
        :class:`BestPracticeWarningCollection` instance.

        Note:
            Values that evaluate to ``False`` will not be inserted.

        """
        if not value:
            return

        if isinstance(value, etree._Element):  # noqa
            value = BestPracticeWarning(node=value)

        self._warnings.insert(idx, value)