izar / pytm

A Pythonic framework for threat modeling
Other
861 stars 161 forks source link

Error with data field in input JSON #204

Open jmrenshaw opened 1 year ago

jmrenshaw commented 1 year ago

When using the JSON model format as input to create a report I am getting an error "expecting a list of pytm.Data, item number 0 is a <class 'str'>" (line 213 in the code snippet). https://github.com/izar/pytm/blob/679ea0df19b7b92e7d8359891d53f7ed794d54a3/pytm/pytm.py#L194-L218 My input JSON for data and flows looks like this: "flows": [ { "name": "Actor 1 to Actor 2", "source": "Actor 1", "sink": "Actor 2", "order": 1, "data": [ "Data" ] },{ "name": "Actor 2 to Actor 3", "source": "Actor 2", "sink": "Actor 3", "description": "Another data flow", "data": [ ] } ], "data": [ { "name": "Data", "format": "Text", "isPII": true } ]

Which I believe matches the JSON format when using the JSON output of the CLI tool. Below is the function which leads to calling varData when it creates the Dataflow object. It seems like the varData function doesn't deal with a list of data name strings. input.json in the tests folder doesn't have a data field in it. Since data objects are not in either the boundaries, elements or flows section of the JSON should they be dealt with using their own function e.g. decode_data? https://github.com/izar/pytm/blob/679ea0df19b7b92e7d8359891d53f7ed794d54a3/pytm/json.py#L92-L107

izar commented 1 year ago

What does your command line look like?

jmrenshaw commented 1 year ago

Hi, I am not using the command line, I am using the load function imported to a python file to create a model object.

izar commented 1 year ago

I'll have to go back to that part of the code. Did you try to save a model as JSON and then load it back again?

jmrenshaw commented 1 year ago

Hi @izar , apologies for such a belated response on this! One thing I have tried is running your unit tests but renaming output.json to input.json as this is a much more complete model in terms of different functions. The test now fails with the following:

====================================================================== ERROR: test_json_loads (tests.test_pytmfunc.TestTM)

Traceback (most recent call last): File "/home/jon/pytm_clone/tests/test_pytmfunc.py", line 343, in test_json_loads tm = loads(contents) File "/home/jon/pytm_clone/pytm/json.py", line 23, in loads result = json.loads(s, object_hook=decode) File "/usr/lib/python3.10/json/init.py", line 359, in loads return cls(kw).decode(s) File "/usr/lib/python3.10/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) File "/home/jon/pytm_clone/pytm/json.py", line 41, in decode boundaries = decode_boundaries(data.pop("boundaries", [])) File "/home/jon/pytm_clone/pytm/json.py", line 61, in decode_boundaries e = Boundary(name, e) File "/home/jon/pytm_clone/pytm/pytm.py", line 1769, in init super().init(name, **kwargs) File "/home/jon/pytm_clone/pytm/pytm.py", line 1301, in init setattr(self, key, value) File "/home/jon/pytm_clone/pytm/pytm.py", line 248, in set raise ValueError( ValueError: expecting an Controls value, got a <class 'dict'>


izar commented 1 year ago

thanks for the report! I'll take a look at it as soon as time permits.