Skip to content

fix: initialize private attributes in MatrixEntry and Tactic to prevent AttributeError#243

Open
Alisha-chaudhary wants to merge 1 commit into
mitre-attack:mainfrom
Alisha-chaudhary:fix/initialize-private-attrs-matrix-entry-tactic
Open

fix: initialize private attributes in MatrixEntry and Tactic to prevent AttributeError#243
Alisha-chaudhary wants to merge 1 commit into
mitre-attack:mainfrom
Alisha-chaudhary:fix/initialize-private-attrs-matrix-entry-tactic

Conversation

@Alisha-chaudhary

Copy link
Copy Markdown

While reviewing the navlayers exporter code, I noticed that MatrixEntry and Tactic only initialize some private attributes when the corresponding constructor arguments are not None.

As a result, creating these objects with omitted or default arguments can leave attributes undefined. Accessing the related properties later raises an AttributeError instead of returning None.

Reproduction

from mitreattack.navlayers.exporters.matrix_gen import MatrixEntry

entry = MatrixEntry()
print(entry.id)


Current behavior:

AttributeError: 'MatrixEntry' object has no attribute '_MatrixEntry__id'

The same issue can occur in Tactic for its private attributes when constructor arguments are omitted.

Cause

Several private attributes are only assigned inside conditional blocks that check whether the provided argument is not None. If an argument is omitted, the attribute is never created, causing property getters to fail when accessed.

Fix

Initialize the affected private attributes to None at the start of __init__ before the existing conditional setter logic runs.

MatrixEntry

self.__id = None
self.__name = None


#### Tactic
self.__tactic = None
self.__techniques = None
self.__subtechniques = None

This preserves the existing behavior for valid inputs while ensuring the attributes always exist and can be accessed safely.

Tests

Added tests in tests/test_matrix_gen.py to verify that:

  • MatrixEntry can be instantiated with default arguments without raising AttributeError
  • Tactic can be instantiated with default arguments without raising AttributeError
  • Property access behaves correctly when optional constructor arguments are omitted

@sonarqubecloud

Copy link
Copy Markdown

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.

1 participant