-
Notifications
You must be signed in to change notification settings - Fork 195
Multi threat libs #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Multi threat libs #263
Conversation
@@ -1405,5 +1405,5 @@ | |||
"name": "my test tm", | |||
"onDuplicates": "Action.NO_ACTION", | |||
"threatsExcluded": [], | |||
"threatsFile": "pytm/threatlib/threats.json" | |||
"threatsFile": "{'pytm/threatlib/threats.json'}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the output a string and not a list of strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's what varStrings with only one value put out...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the immortal words of just about everybody..."works for me"!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I had a quick look and the issue is that the default to_serializable
is used.
Lines 2015 to 2018 in 9dc0f1f
@singledispatch | |
def to_serializable(val): | |
"""Used by default.""" | |
return str(val) |
This is just the same as
json.dumps(str(set(["./a.json"])))
The issue is created by
Line 2023 in 9dc0f1f
return serialize(obj, nested=True) |
Because of this check for not nested
in serialize()
.
Line 2064 in 9dc0f1f
not nested |
Lines 2035 to 2070 in 9dc0f1f
def serialize(obj, nested=False): | |
"""Used if *obj* is an instance of TM, Element, Threat or Finding.""" | |
klass = obj.__class__ | |
result = {} | |
if isinstance(obj, (Actor, Asset)): | |
result["__class__"] = klass.__name__ | |
for i in dir(obj): | |
if ( | |
i.startswith("__") | |
or callable(getattr(klass, i, {})) | |
or ( | |
isinstance(obj, TM) | |
and i in ("_sf", "_duplicate_ignored_attrs", "_threats") | |
) | |
or (isinstance(obj, Element) and i in ("_is_drawn", "uuid")) | |
or (isinstance(obj, Finding) and i == "element") | |
): | |
continue | |
value = getattr(obj, i) | |
if isinstance(obj, TM) and i == "_elements": | |
value = [e for e in value if isinstance(e, (Actor, Asset))] | |
if value is not None: | |
if isinstance(value, (Element, Data)): | |
value = value.name | |
elif isinstance(obj, Threat) and i == "target": | |
value = [v.__name__ for v in value] | |
elif i in ("levels", "sourceFiles", "assumptions"): | |
value = list(value) | |
elif ( | |
not nested | |
and not isinstance(value, str) | |
and isinstance(value, Iterable) | |
): | |
value = [v.id if isinstance(v, Finding) else v.name for v in value] | |
result[i.lstrip("_")] = value | |
return result |
This whole serialize function is a bit overloaded with special handling of member variables and might require a rewrite.
All the checks seem to be for specific classes which are all handle in the same function.
Also why is there the check for nested
?
This is basically equivalent to checking if the class is TM
.
A quick fix would be something like if instance(obj, TM) and i == "threatsFile"
but oh boy that is not nice.
Should this be a new issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds like something to be addressed. @nineinchnick , you there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There appears to be only 1 check for nested. If nested is true, the behavior seems to be potentially undefined (if the code reaches the not nested
elif).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I started to rewrite the code see #268, but it is difficult to understand what the intention here was.
It seems that the default is result[i.lstrip("_")] = value
and the cases above only change the value of value
.
So setting nested = True
means that value will not be touched, except it is a Element
, Data
, or Threat
. Or the name of the member is in ("levels", "sourceFiles", "assumptions")
.
The last one seems to be a fix for a specific class.
[--exclude EXCLUDE] [--seq] [--list] [--describe DESCRIBE] | ||
[--list-elements] [--json JSON] [--levels LEVELS [LEVELS ...]] | ||
[--stale_days STALE_DAYS] | ||
usage: tm.py [-h] [--sqldump SQLDUMP] [--debug] [--dfd] [--report REPORT] [--exclude EXCLUDE] [--seq] [--list] [--colormap] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you adding --colormap
in this PR also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be there already, it is an old one. It appears on the currently checked code on the master branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh this is the readme! let me fix that
@@ -1129,6 +1129,21 @@ def _process(self): | |||
result = get_args() | |||
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") | |||
|
|||
# delaying loading of threats to accomodate multiple threat files in the | |||
# command line | |||
if result.threat_files: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do extra work on line 791 to load the default when you can just do it here? You will always reach this line at least once, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't remember the details but there is a timing issue there. Something around things being used before loaded.
@@ -1405,5 +1405,5 @@ | |||
"name": "my test tm", | |||
"onDuplicates": "Action.NO_ACTION", | |||
"threatsExcluded": [], | |||
"threatsFile": "pytm/threatlib/threats.json" | |||
"threatsFile": "{'pytm/threatlib/threats.json'}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There appears to be only 1 check for nested. If nested is true, the behavior seems to be potentially undefined (if the code reaches the not nested
elif).
foo_file = f"{dir_path}/1.json" | ||
bar_file = f"{dir_path}/2.json" | ||
threat_files = [os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) | ||
+ "/pytm/threatlib/threats.json", foo_file, bar_file] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a test for when the default is not included in threatsfile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. No. Gotta do one, you're right.
Adds capability to select which threat lib to use on the command line: