CSAF/Parser.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« CSAF.pm ↑ Summary CSAF/Type/Remediation.pm »
1 package CSAF::Parser;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶use CSAF;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use CSAF::Util qw(file_read);
10 use CSAF::Schema;
11 use List::Util qw(first);
12 use Cpanel::JSON::XS;
13  
14 use Moo;
15  
16 has file => (is => 'ro');
17 has content => (is => 'ro');
18 has data => (is => 'rw');
19  
20 sub parse {
21  
22 my $self = shift;
23  
24 if ($self->content || $self->file) {
25  
26 my $content = $self->content;
27  
28 if ($self->file) {
29 Carp::croak sprintf('File "%s" not found', $self->file) unless (-e $self->file);
30 $content = file_read($self->file);
31 }
32  
33 Carp::croak "Empty 'content'" unless $content;
34  
35 my $json = eval { Cpanel::JSON::XS->new->decode($content) };
36  
37 Carp::croak "Failed to parse the CSAF document: $@" if ($@);
38  
39 $self->data($json);
40  
41 }
42  
43 my $data = $self->data;
44  
45 Carp::croak 'Invalid CSAF document' unless (exists $data->{document});
46  
47 my $csaf = CSAF->new;
48  
49 if (my $document = $data->{document}) {
50  
51 $csaf->document->title($document->{title});
52 $csaf->document->category($document->{category});
53 $csaf->document->csaf_version($document->{csaf_version});
54  
55 $csaf->document->lang($document->{lang}) if ($document->{lang});
56 $csaf->document->source_lang($document->{source_lang}) if ($document->{source_lang});
57  
58 if (my $aggregate_severity = $document->{aggregate_severity}) {
59 $csaf->document->aggregate_severity(%{$aggregate_severity});
60 }
61  
62 if (my $distribution = $document->{distribution}) {
63  
64 $csaf->document->distribution(%{$distribution});
65  
66 if (my $tlp = $distribution->{tlp}) {
67 $csaf->document->distribution->tlp(%{$tlp});
68 }
69  
70 }
71  
72 $csaf->document->publisher(%{$document->{publisher}});
73  
74 if (my $notes = $document->{notes}) {
75 $csaf->document->notes->item(%{$_}) for (@{$notes});
76 }
77  
78 if (my $references = $document->{references}) {
79 $csaf->document->references->item(%{$_}) for (@{$references});
80 }
81  
82 if (my $tracking = $document->{tracking}) {
83 $csaf->document->tracking(%{$tracking});
84 $csaf->document->tracking->generator(%{$tracking->{generator}}) if ($tracking->{generator});
85 $csaf->document->tracking->generator->engine(%{$tracking->{generator}->{engine}})
86 if ($tracking->{generator}->{engine});
87 $csaf->document->tracking->revision_history->item(%{$_}) for (@{$tracking->{revision_history}});
88 }
89  
90 if (my $acknowledgments = $document->{acknowledgments}) {
91 $csaf->document->acknowledgments->item(%{$_}) for (@{$acknowledgments});
92 }
93  
94 }
95  
96 if (my $vulnerabilities = $data->{vulnerabilities}) {
97 foreach my $vulnerability (@{$vulnerabilities}) {
98  
99 my $vuln = $csaf->vulnerabilities->item(cve => $vulnerability->{cve});
100  
101 if (my $cwe = $vulnerability->{cwe}) {
102 $vuln->cwe(%{$cwe});
103 }
104  
105 if (my $notes = $vulnerability->{notes}) {
106 $vuln->notes->item(%{$_}) for (@{$notes});
107 }
108  
109 if (my $references = $vulnerability->{references}) {
110 $vuln->references->item(%{$_}) for (@{$references});
111 }
112  
113 if (my $product_status = $vulnerability->{product_status}) {
114 $vuln->product_status(%{$product_status});
115 }
116  
117 if (my $scores = $vulnerability->{scores}) {
118 $vuln->scores->item(%{$_}) for (@{$scores});
119 }
120  
121 if (my $acknowledgments = $vulnerability->{acknowledgments}) {
122 $vuln->acknowledgments->item(%{$_}) for (@{$acknowledgments});
123 }
124  
125 if (my $remediations = $vulnerability->{remediations}) {
126 $vuln->remediations->item(%{$_}) for (@{$remediations});
127 }
128  
129 if (my $threats = $vulnerability->{threats}) {
130 $vuln->threats->item(%{$_}) for (@{$threats});
131 }
132  
133 if (my $involvements = $vulnerability->{involvements}) {
134 $vuln->involvements->item(%{$_}) for (@{$involvements});
135 }
136  
137 if (my $flags = $vulnerability->{flags}) {
138 $vuln->flags->item(%{$_}) for (@{$flags});
139 }
140  
141 if (my $ids = $vulnerability->{ids}) {
142 $vuln->ids->item(%{$_}) for (@{$ids});
143 }
144  
145 }
146 }
147  
148  
149 if (my $product_tree = $data->{product_tree}) {
150  
151 my $csaf_product_tree = $csaf->product_tree;
152  
153 if (my $branches = $product_tree->{branches}) {
154 _branches_walk($branches, $csaf_product_tree);
155 }
156  
157 if (my $relationships = $product_tree->{relationships}) {
158 $csaf_product_tree->relationships->item(%{$_}) for (@{$relationships});
159 }
160  
161 if (my $product_groups = $product_tree->{product_groups}) {
162 $csaf_product_tree->product_groups->item(%{$_}) for (@{$product_groups});
163 }
164  
165 if (my $full_product_names = $product_tree->{full_product_names}) {
166 $csaf_product_tree->full_product_names->item(%{$_}) for (@{$full_product_names});
167 }
168  
169 }
170  
171 my $v = $csaf->validator;
172  
173 my $schema = CSAF::Schema->validator('strict-csaf-2.0');
174 my @errors = $schema->validate($data);
175  
176 foreach my $error (@errors) {
177 if (first { 'additionalProperties' eq $_ } @{$error->details}) {
178 $v->add_message(
179 type => 'warning',
180 category => 'optional',
181 path => $error->path,
182 code => '6.2.20',
183 message => $error->message
184 );
185 }
186 }
187  
188 return $csaf;
189  
190 }
191  
192 sub _branches_walk {
193  
194 my ($branches, $csaf) = @_;
195  
196 foreach my $branch (@{$branches}) {
197 if (defined $branch->{branches}) {
198 _branches_walk($branch->{branches}, $csaf->branches->item(%{$branch}));
199 }
200 else {
201 $csaf->branches->item(%{$branch});
202 }
203 }
204  
205 }
206  
207 1;
208  
209 __END__
« CSAF.pm ↑ Summary CSAF/Type/Remediation.pm »