oasis-open / cti-python-stix2

OASIS TC Open Repository: Python APIs for STIX 2
https://stix2.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
356 stars 113 forks source link

Fix detect_spec_version() with respect to 2.0 bundles #542

Closed chisholm closed 1 year ago

chisholm commented 2 years ago

Fixes #541 .

Fix detect_spec_version() such that it uses presence of the "spec_version" property in a bundle to infer spec version, not the property's value. Update unit tests accordingly.

Content was discovered which consisted of a bundle with spec_version="2.1". This is (perhaps confusingly) a STIX 2.0 bundle, but it was incorrectly being identified by the library as a STIX 2.1 bundle. This produced the following error when parsing it:

stix2.exceptions.ExtraPropertiesError: Unexpected properties for Bundle: (spec_version).

because STIX 2.1 bundles don't have a spec_version property. The object's spec_version property was used to determine that it should not have a spec_version property. Doesn't make much sense. With this PR, you get:

stix2.exceptions.InvalidValueError: Invalid value for Bundle 'spec_version': must equal '2.0'.

This is correct: the library defines the spec_version property of STIX 2.0 bundles to have a fixed value of "2.0", since content of other spec versions is not supported in a STIX 2.0 bundle by this library. This shows the spec version of the bundle was properly detected.

codecov-commenter commented 2 years ago

Codecov Report

Merging #542 (b5260c9) into master (17445a0) will increase coverage by 0.00%. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #542   +/-   ##
=======================================
  Coverage   87.29%   87.29%           
=======================================
  Files         153      153           
  Lines       18033    18035    +2     
=======================================
+ Hits        15742    15744    +2     
  Misses       2291     2291           
Impacted Files Coverage Δ
stix2/test/test_spec_version_detect.py 100.00% <ø> (ø)
stix2/test/v20/test_utils.py 100.00% <ø> (ø)
stix2/test/v21/test_utils.py 100.00% <ø> (ø)
stix2/utils.py 79.32% <100.00%> (+0.20%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 17445a0...b5260c9. Read the comment docs.