Skip to content

fix(metrics): prevent 500 on Critical Asset Metrics page when no critical products exist#15057

Open
valentijnscholten wants to merge 2 commits into
DefectDojo:bugfixfrom
valentijnscholten:fix-critical-asset-metrics-500
Open

fix(metrics): prevent 500 on Critical Asset Metrics page when no critical products exist#15057
valentijnscholten wants to merge 2 commits into
DefectDojo:bugfixfrom
valentijnscholten:fix-critical-asset-metrics-500

Conversation

@valentijnscholten

Copy link
Copy Markdown
Member

Fixes #15051

/critical_asset_metrics returned a 500 when no product types are marked critical.

Root cause

critical_product_metrics renders dojo/metrics.html passing only name, critical_prods, and url_prefix. When there are no critical product types, critical_prods is an empty queryset — which is falsy. The template's general-dashboard section is gated only on {% if not critical_prods %}, so the empty queryset makes it render the full metrics dashboard, which references context the view never supplies (max_findings_details, findings, …). That raises:

django.template.base.VariableDoesNotExist: Failed lookup for key [max_findings_details] in
  [..., {'name': 'Critical Asset Metrics', 'critical_prods': <BaseQuerySet []>, 'url_prefix': ''}]

The header section already distinguishes the critical page correctly via {% elif name == labels.ASSET_METRICS_CRITICAL_LABEL %}; the dashboard gates just didn't.

Fix

Gate the dashboard branches on name != labels.ASSET_METRICS_CRITICAL_LABEL as well, in both the V3 (dojo/templates/dojo/metrics.html) and classic (dojo/templates_classic/dojo/metrics.html) templates. On the critical asset page with no critical products, the existing "No Critical Assets registered" message is shown instead of the (broken) general dashboard.

Tests

Adds unittests/test_metrics_critical_asset.py:

  • test_no_critical_product_types — reproduces Impossible to access /critical_asset_metrics #15051 (fails with the exact VariableDoesNotExist before this change, passes after).
  • test_with_critical_product_type — page renders with a critical product type present.

🤖 Generated with Claude Code

@valentijnscholten valentijnscholten force-pushed the fix-critical-asset-metrics-500 branch from 9ca1a17 to 0d90e36 Compare June 22, 2026 17:35
@valentijnscholten valentijnscholten changed the base branch from dev to bugfix June 22, 2026 17:37
@valentijnscholten valentijnscholten force-pushed the fix-critical-asset-metrics-500 branch from 0d90e36 to 01e8e14 Compare June 22, 2026 17:43
…ical products exist (DefectDojo#15051)

When no product types are marked critical, the empty critical_prods queryset
is falsy, so the metrics template fell through its '{% if not critical_prods %}'
general-dashboard branch, which references context (max_findings_details,
findings, ...) that the critical_product_metrics view does not supply, raising
VariableDoesNotExist for 'max_findings_details'.

Gate the dashboard branches on 'name != labels.ASSET_METRICS_CRITICAL_LABEL'
too, mirroring the existing header branch, so the critical asset page shows the
'no critical assets' message instead of the general dashboard. Applied to both
the V3 and classic metrics templates.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…#15051)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@mtesauro mtesauro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants