Skip to content
Draft
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
3 changes: 2 additions & 1 deletion vulnerabilities/templates/advisory_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% load static %}
{% load show_cvss %}
{% load url_filters %}
{% load ssvc_filters %}

{% block title %}
VulnerableCode Advisory Details - {{ advisory.advisory_id }}
Expand Down Expand Up @@ -605,7 +606,7 @@
<summary class="is-size-7 has-text-link" style="cursor: pointer;">
View SSVC decision tree
</summary>
<pre>{{ ssvc.options|pprint }}</pre>
<pre>{{ ssvc.options|to_yaml }}</pre>
</details>
</div>
</div>
Expand Down
28 changes: 28 additions & 0 deletions vulnerabilities/templatetags/ssvc_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

import saneyaml
from django import template

register = template.Library()


@register.filter(name="to_yaml")
def to_yaml(value):
"""
Convert a Python object (typically SSVC options) to a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this generic? if yes, remove references to SSVC.
Also why not indent? And we have similar code in SCIO https://github.com/aboutcode-org/scancode.io/blob/d6b14acbb94ecfef52b3dbe34c1cd7f5f112e4bf/scanpipe/views.py#L206C5-L206C19 with a different approach. What about using that approach instead? @tdruez FYI

human-readable YAML string.
"""
if not value:
return ""
try:
return saneyaml.dump(value).strip()
except Exception:
return str(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keeping the pprint behavior there? And why would there be a case that raises an exception? Is the exception really needed?


38 changes: 38 additions & 0 deletions vulnerabilities/tests/test_ssvc_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

from vulnerabilities.templatetags.ssvc_filters import to_yaml


def test_to_yaml_with_ssvc_options():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are essentially testing the saneyaml library that is already tested. Can you test the tag instead? (I though I question using a filter is what we want here.)

options = [
{"Exploitation": "active"},
{"Automatable": "yes"},
{"Technical Impact": "total"},
{"Mission Prevalence": "essential"},
{"Public Well-being Impact": "irreversible"},
{"Mission & Well-being": "high"},
]
result = to_yaml(options)
assert "Exploitation: active" in result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this series of asserts has a lot of value. Why not assert at once the whole "result" string?

assert "Technical Impact: total" in result
assert "Mission Prevalence: essential" in result
assert "Public Well-being Impact: irreversible" in result


def test_to_yaml_with_empty_value():
assert to_yaml(None) == ""
assert to_yaml([]) == ""
assert to_yaml("") == ""


def test_to_yaml_with_non_serializable_value():
result = to_yaml("plain string")
assert isinstance(result, str)