| 1 | package CSAF::Renderer::HTML; | ||
| 2 | |||
| 3 | use 5.010001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use CSAF::Util qw(tt_templates_path); | ||
| 9 | use Template; | ||
| 10 | |||
| 11 | use Moo; | ||
| 12 | extends 'CSAF::Renderer::Base'; | ||
| 13 | with 'CSAF::Util::Log'; | ||
| 14 | |||
| 15 | sub render { | ||
| 16 | |||
| 17 | my ($self, %options) = @_; | ||
| 18 | |||
| 19 | my $products = $CSAF::CACHE->{products} || {}; | ||
| 20 | |||
| 21 | my $max_base_score = 0; | ||
| 22 | |||
| 23 | $self->csaf->build; | ||
| 24 | |||
| 25 | foreach my $vuln ($self->csaf->vulnerabilities->each) { | ||
| 26 | foreach my $score ($vuln->scores->each) { | ||
| 27 | if ($score->cvss_v3 && $score->cvss_v3->baseScore && $max_base_score < $score->cvss_v3->baseScore) { | ||
| 28 | $max_base_score = $score->cvss_v3->baseScore; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | my %tt_options = ( | ||
| 34 | PRE_CHOMP => 1, | ||
| 35 | TRIM => 1, | ||
| 36 | ENCODING => 'UTF-8', | ||
| 37 | VARIABLES => { | ||
| 38 | document => $self->csaf->document, | ||
| 39 | product_tree => $self->csaf->product_tree, | ||
| 40 | vulnerabilities => $self->csaf->vulnerabilities, | ||
| 41 | max_base_score => $max_base_score, | ||
| 42 | }, | ||
| 43 | FILTERS => { | ||
| 44 | product_name => sub { | ||
| 45 | my ($product_id) = @_; | ||
| 46 | return $products->{$product_id} || $product_id; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | ); | ||
| 50 | |||
| 51 | my $template = $options{template} || 'default'; | ||
| 52 | my $vars = $options{vars} || {}; | ||
| 53 | my $output = undef; | ||
| 54 | |||
| 55 | $template .= '.tt2' unless $template =~ /\.tt2$/; | ||
| 56 | |||
| 57 | unless (-e $template) { | ||
| 58 | $tt_options{INCLUDE_PATH} = tt_templates_path; | ||
| 59 | } | ||
| 60 | |||
| 61 | my $tt = Template->new(%tt_options) or Carp::croak $Template::ERROR; | ||
| 62 | |||
| 63 | $self->log->debug("Render CSAF document using $template template"); | ||
| 64 | |||
| 65 | $tt->process($template, $vars, \$output) or Carp::croak $tt->error; | ||
| 66 | |||
| 67 | return $output; | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | |||
| 73 | __END__ |