Skip to content
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

Fix cvss and response override #248

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pytm/pytm.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ def resolve(self):
elements = defaultdict(list)
for e in TM._elements:
if not e.inScope:
e.findings = findings
continue

override_ids = set(f.threat_id for f in e.overrides)
Expand Down Expand Up @@ -2089,6 +2090,8 @@ def encode_threat_data(obj):
"threat_id",
"references",
"condition",
"cvss",
"response",
]

if type(obj) is Finding or (len(obj) != 0 and type(obj[0]) is Finding):
Expand All @@ -2104,7 +2107,8 @@ def encode_threat_data(obj):
# ignore missing attributes, since this can be called
# on both a Finding and a Threat
continue
setattr(t, a, html.escape(v))
if v is not None:
setattr(t, a, html.escape(v))

encoded_threat_data.append(t)

Expand Down
40 changes: 40 additions & 0 deletions tests/test_private_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
Dataflow,
Datastore,
DatastoreType,
Finding,
Process,
Server,
Threat,
UIError,
encode_threat_data,
)


Expand Down Expand Up @@ -245,3 +247,41 @@ def test_defaults(self):
case["condition"],
),
)


class TestFunction(unittest.TestCase):
def test_encode_threat_data(self):
findings = [
Finding(
description="A test description",
severity="High",
id="1",
threat_id="INP01",
cvss="9.876",
response="A test response",
),
Finding(
description="An escape test <script>",
severity="Medium",
id="2",
threat_id="INP02",
cvss="1.234",
response="A test response",
assumption=Assumption("Test Assumption", exclude=["INP02"]),
)
]
encoded_findings = encode_threat_data(findings)

self.assertEqual(len(encoded_findings), 2)
self.assertEqual(encoded_findings[0].description, "A test description")
self.assertEqual(encoded_findings[0].severity, "High")
self.assertEqual(encoded_findings[0].id, "1")
self.assertEqual(encoded_findings[0].threat_id, "INP01")
self.assertEqual(encoded_findings[0].cvss, "9.876")
self.assertEqual(encoded_findings[0].response, "A test response")
self.assertEqual(encoded_findings[1].description, "An escape test &lt;script&gt;")
self.assertEqual(encoded_findings[1].severity, "Medium")
self.assertEqual(encoded_findings[1].id, "2")
self.assertEqual(encoded_findings[1].threat_id, "INP02")
self.assertEqual(encoded_findings[1].cvss, "1.234")
self.assertEqual(encoded_findings[1].response, "A test response")
18 changes: 14 additions & 4 deletions tests/test_pytmfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ def test_overrides(self):
web = Server(
"Web Server",
overrides=[
Finding(threat_id="Server", response="mitigated by adding TLS"),
Finding(
threat_id="Server",
response="mitigated by adding TLS",
cvss="1.234",
),
],
)
db = Datastore(
Expand All @@ -304,6 +308,7 @@ def test_overrides(self):
Finding(
threat_id="Datastore",
response="accepted since inside the trust boundary",
cvss="9.876",
),
],
)
Expand All @@ -327,10 +332,18 @@ def test_overrides(self):
self.assertEqual(
[f.response for f in web.findings], ["mitigated by adding TLS"]
)
self.assertEqual(
[f.cvss for f in web.findings],
["1.234"],
)
self.assertEqual(
[f.response for f in db.findings],
["accepted since inside the trust boundary"],
)
self.assertEqual(
[f.cvss for f in db.findings],
["9.876"],
)

def test_json_dumps(self):
random.seed(0)
Expand Down Expand Up @@ -435,9 +448,6 @@ def test_report(self):
self.assertTrue(tm.check())
output = tm.report("docs/basic_template.md")

with open(os.path.join(output_path, "output_current.md"), "w") as x:
x.write(output)

izar marked this conversation as resolved.
Show resolved Hide resolved
with open(os.path.join(output_path, "output_current.md"), "w") as x:
x.write(output)

Expand Down
Loading