From 176cb980a24ac65cf2efba19fb6607b7e37dcf4c Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Tue, 28 Jan 2020 18:13:36 -0500 Subject: [PATCH 1/3] Remove workaround for "values" being both a Mapping method name and sometimes a STIX property name. It didn't work (caused crashes under some circumstances). Now, attributes whose names conflict with Mapping methods will have the Mapping interpretation. Same-named STIX object properties will not be accessible as attributes. --- stix2/properties.py | 15 +-------------- stix2/test/v20/test_observed_data.py | 6 +++--- stix2/test/v21/test_observed_data.py | 6 +++--- stix2/v20/observables.py | 7 +------ stix2/v21/observables.py | 7 +------ 5 files changed, 9 insertions(+), 32 deletions(-) diff --git a/stix2/properties.py b/stix2/properties.py index dd6cd44..b013b7e 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -11,7 +11,7 @@ from six import string_types, text_type import stix2 -from .base import _Observable, _STIXBase +from .base import _STIXBase from .core import STIX2_OBJ_MAPS, parse, parse_observable from .exceptions import ( CustomContentError, DictionaryKeyError, MissingPropertiesError, @@ -219,19 +219,6 @@ class ListProperty(Property): return result -class CallableValues(list): - """Wrapper to allow `values()` method on WindowsRegistryKey objects. - Needed because `values` is also a property. - """ - - def __init__(self, parent_instance, *args, **kwargs): - self.parent_instance = parent_instance - super(CallableValues, self).__init__(*args, **kwargs) - - def __call__(self): - return _Observable.values(self.parent_instance) - - class StringProperty(Property): def __init__(self, **kwargs): diff --git a/stix2/test/v20/test_observed_data.py b/stix2/test/v20/test_observed_data.py index a822efb..bfe9c34 100644 --- a/stix2/test/v20/test_observed_data.py +++ b/stix2/test/v20/test_observed_data.py @@ -1301,9 +1301,9 @@ def test_windows_registry_key_example(): values=[v], ) assert w.key == "hkey_local_machine\\system\\bar\\foo" - assert w.values[0].name == "Foo" - assert w.values[0].data == "qwerty" - assert w.values[0].data_type == "REG_SZ" + assert w["values"][0].name == "Foo" + assert w["values"][0].data == "qwerty" + assert w["values"][0].data_type == "REG_SZ" # ensure no errors in serialization because of 'values' assert "Foo" in str(w) diff --git a/stix2/test/v21/test_observed_data.py b/stix2/test/v21/test_observed_data.py index c1f15cd..371018c 100644 --- a/stix2/test/v21/test_observed_data.py +++ b/stix2/test/v21/test_observed_data.py @@ -1382,9 +1382,9 @@ def test_windows_registry_key_example(): values=[v], ) assert w.key == "hkey_local_machine\\system\\bar\\foo" - assert w.values[0].name == "Foo" - assert w.values[0].data == "qwerty" - assert w.values[0].data_type == "REG_SZ" + assert w["values"][0].name == "Foo" + assert w["values"][0].data == "qwerty" + assert w["values"][0].data_type == "REG_SZ" # ensure no errors in serialization because of 'values' assert "Foo" in str(w) diff --git a/stix2/v20/observables.py b/stix2/v20/observables.py index dc2b4aa..81e2c48 100644 --- a/stix2/v20/observables.py +++ b/stix2/v20/observables.py @@ -12,7 +12,7 @@ from ..base import _Extension, _Observable, _STIXBase from ..custom import _custom_extension_builder, _custom_observable_builder from ..exceptions import AtLeastOnePropertyError, DependentPropertiesError from ..properties import ( - BinaryProperty, BooleanProperty, CallableValues, DictionaryProperty, + BinaryProperty, BooleanProperty, DictionaryProperty, EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty, HashesProperty, HexProperty, IntegerProperty, ListProperty, ObjectReferenceProperty, StringProperty, TimestampProperty, TypeProperty, @@ -723,11 +723,6 @@ class WindowsRegistryKey(_Observable): ('extensions', ExtensionsProperty(spec_version="2.0", enclosing_type=_type)), ]) - @property - def values(self): - # Needed because 'values' is a property on collections.Mapping objects - return CallableValues(self, self._inner['values']) - class X509V3ExtenstionsType(_STIXBase): """For more detailed information on this object's properties, see diff --git a/stix2/v21/observables.py b/stix2/v21/observables.py index 6f53255..1703263 100644 --- a/stix2/v21/observables.py +++ b/stix2/v21/observables.py @@ -15,7 +15,7 @@ from ..exceptions import ( AtLeastOnePropertyError, DependentPropertiesError, STIXDeprecationWarning, ) from ..properties import ( - BinaryProperty, BooleanProperty, CallableValues, DictionaryProperty, + BinaryProperty, BooleanProperty, DictionaryProperty, EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty, HashesProperty, HexProperty, IDProperty, IntegerProperty, ListProperty, ObjectReferenceProperty, ReferenceProperty, StringProperty, @@ -935,11 +935,6 @@ class WindowsRegistryKey(_Observable): ]) _id_contributing_properties = ["key", "values"] - @property - def values(self): - # Needed because 'values' is a property on collections.Mapping objects - return CallableValues(self, self._inner['values']) - class X509V3ExtenstionsType(_STIXBase): # TODO: Add link From 1cadeaa1c628fedee8461cf8d54efdf7d6386278 Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Wed, 29 Jan 2020 16:01:15 -0500 Subject: [PATCH 2/3] Added a warning to creating.ipynb about Mapping attributes: they can't be used to access STIX object properties. --- docs/guide/creating.ipynb | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/guide/creating.ipynb b/docs/guide/creating.ipynb index 92a3336..246f26d 100644 --- a/docs/guide/creating.ipynb +++ b/docs/guide/creating.ipynb @@ -330,6 +330,20 @@ "indicator.name" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "### Warning\n", + "Note that because these objects are designed to be usable as `Mapping`s, the attributes of that interface retain their mapping meanings and can't be used to access STIX object properties. This includes the attributes:\n", + "- keys\n", + "- values\n", + "- items\n", + "- get\n", + "-----" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1110,21 +1124,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 2", "language": "python", - "name": "python3" + "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.7" + "pygments_lexer": "ipython2", + "version": "2.7.15" } }, "nbformat": 4, From c6b2b6fbfa71ad2e10b0d2aaefb704b02ecbfc78 Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Mon, 3 Feb 2020 16:51:12 -0500 Subject: [PATCH 3/3] Change the warning in the jupyter notebook for creating content, regarding name collisions between method and property names, to not pick on the Mapping methods specifically. The problem is more general than that: stix objects have more methods than those. Instead of listing them all out, a more general statement is made, that accessing those attributes will result in a bound method, not a STIX property value. --- docs/guide/creating.ipynb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/docs/guide/creating.ipynb b/docs/guide/creating.ipynb index 246f26d..6544e89 100644 --- a/docs/guide/creating.ipynb +++ b/docs/guide/creating.ipynb @@ -334,14 +334,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "-----\n", - "### Warning\n", - "Note that because these objects are designed to be usable as `Mapping`s, the attributes of that interface retain their mapping meanings and can't be used to access STIX object properties. This includes the attributes:\n", - "- keys\n", - "- values\n", - "- items\n", - "- get\n", - "-----" + "
\n", + "\n", + "**Warning**\n", + "\n", + "Note that there are several attributes on these objects used for method names. Accessing those will return a bound method, not the attribute value.\n", + "\n", + "
\n" ] }, {