Skip to content

fix: let custom ATT&CK objects serialize through stix2 - #256

Open
frostyhand wants to merge 1 commit into
mitre-attack:mainfrom
frostyhand:fix/custom-object-serialize
Open

frostyhand wants to merge 1 commit into
mitre-attack:mainfrom
frostyhand:fix/custom-object-serialize

Conversation

@frostyhand

Copy link
Copy Markdown

Fixes #207

print(), str(), .serialize() and MitreAttackData.print_stix_object() fail for every ATT&CK custom object (matrix, tactic, data source, data component, asset, analytic, detection strategy) with:

NotImplementedError: serialize() should be provided by the STIX2 base class.

Cause

CustomStixObject.serialize() was added as a placeholder with the type hints in 5d4b16c. The @CustomObject decorator builds each class as _CustomObject(DataComponent, _DomainObject), so the method resolution order is DataComponent -> CustomStixObject -> _DomainObject -> _STIXBase20. The placeholder sits ahead of stix2's real serialize() and hides it. _STIXBase.__str__ calls serialize(), which is why a plain print(obj) fails too.

On the current Enterprise ATT&CK bundle, none of these objects can be printed on main: 0 of 109 data components, 15 tactics, 1 matrix, 38 data sources, 1,773 analytics and 708 detection strategies.

Fix

Keep the method so type hints still see serialize() on CustomStixObject, but pass the call on to stix2 with super().serialize(*args, **kwargs). The arguments are the same as stix2's, so serialize(pretty=True), the positional serialize(True) used by print_stix_object, and sort_keys and friends all work.

Tests

  • New parametrized test_custom_objects_serialize covers all seven custom classes: compact and pretty output, the positional form, and str(). All 7 cases fail on main.
  • Full suite: 304 passed, 4 skipped. ruff check and ruff format --check are clean.
  • With the fix, every object above serializes on the real Enterprise bundle, and print_stix_object prints a tactic as pretty JSON.

CustomStixObject.serialize() was a placeholder that raised
NotImplementedError. The @CustomObject decorator puts CustomStixObject
ahead of the stix2 base class in the MRO, so the placeholder hid the
real serialize(). str(), print() and MitreAttackData.print_stix_object()
failed for every matrix, tactic, data source, data component, asset,
analytic and detection strategy.

Keep the method for type hints but pass the call on to stix2.

Fixes mitre-attack#207
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Error while trying to serialize stix object

1 participant