blib/lib/GraphQL/Houtou/Validation/DepthLimit.pm

filter: Statementstmt 100% 49 / 49 Branchbran 79.1% 19 / 24 Conditioncond 52.1% 12 / 23 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 76.7% 89 / 116 CC 26 SCAR 25.7
CC
26
Cov
83%
_compute_max_depth
17.1
check_query_depth
13.0
BEGIN
1.0
CRAP 13.0
« blib/lib/GraphQL/Houtou/PSGI.pm ↑ Summary blib/lib/GraphQL/Houtou/Type/Scalar.pm »
1 package GraphQL::Houtou::Validation::DepthLimit;
2  
3 45 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 our @EXPORT_OK = qw(check_query_depth);
9  
10 45 ▶use constant DEFAULT_MAX_DEPTH => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 451 ▶sub check_query_depth {
Subroutine check_query_depth: called CC=12 SCAR=25.6
Pod documented
13 451 my ($ast, %opts) = @_;
14 451 ▶ my $max_depth = exists $opts{max_depth} ? $opts{max_depth} : DEFAULT_MAX_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'max_depth'} ? : 451 0 451
15 451 ▶ return () unless defined $max_depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_depth 1 450 451
16  
17 12 my %fragments = map { $_->{name} => $_ }
18 450 ▶ grep { ($_->{kind} // '') eq 'fragment' } @{ $ast || [] };
Condition50%2/4
$_->{"kind"} // ""
l!l
4630
$ast || []
l!l
4500
MC/DC0%0/1
$_->{"kind"} // ""
$_->{"kind"}
MC/DC0%0/1
$ast || []
$ast
Truth table50%1/2
A
A= $_->{"kind"}
Aresult
11
00
Truth table50%1/2
A
A= $ast
Aresult
11
00
19  
20 450 my @errors;
21 450 ▶ for my $node (@{ $ast || [] }) {
Condition50%1/2
$ast || []
l!l
4500
MC/DC0%0/1
$ast || []
$ast
Truth table50%1/2
A
A= $ast
Aresult
11
00
22 463 ▶ next unless ($node->{kind} // '') eq 'operation';
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->{"kind"} // "") eq "operation" 12 451 463
Condition50%1/2
$node->{"kind"} // ""
l!l
4630
MC/DC0%0/1
$node->{"kind"} // ""
$node->{"kind"}
Truth table50%1/2
A
A= $node->{"kind"}
Aresult
11
00
23 451 ▶ my $depth = _compute_max_depth($node->{selections} // [], \%fragments, {});
Condition50%1/2
$node->{"selections"} // []
l!l
4510
MC/DC0%0/1
$node->{"selections"} // []
$node->{"selections"}
Truth table50%1/2
A
A= $node->{"selections"}
Aresult
11
00
24 451 ▶ if ($depth > $max_depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > $max_depth) 4 447 451
25 4 my $name = $node->{name};
26 4 ▶ my $label = defined $name ? qq("$name") : 'anonymous operation';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 4 4
27 4 push @errors, {
28 message => "Query depth $depth exceeds maximum allowed depth of $max_depth in $label",
29 };
30 }
31 }
32 450 return @errors;
33 }
34  
35 1046 ▶sub _compute_max_depth {
Subroutine _compute_max_depth: called CC=15 SCAR=28.4
36 1046 my ($selections, $fragments, $visited) = @_;
37 1046 my $max = 0;
38 1046 ▶ for my $sel (@{ $selections || [] }) {
Condition50%1/2
$selections || []
l!l
10460
MC/DC0%0/1
$selections || []
$selections
Truth table50%1/2
A
A= $selections
Aresult
11
00
39 2086 ▶ my $kind = $sel->{kind} // '';
Condition50%1/2
$sel->{"kind"} // ""
l!l
20860
MC/DC0%0/1
$sel->{"kind"} // ""
$sel->{"kind"}
Truth table50%1/2
A
A= $sel->{"kind"}
Aresult
11
00
40 2086 my $depth;
41 2086 ▶ if ($kind eq 'field') {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind eq 'field') { } 2032 54 2086
elsif ($kind eq 'inline_fragment') { } 28 26 54
elsif ($kind eq 'fragment_spread') { } 26 0 26
42 $depth = $sel->{selections}
43 2032 ▶ ? 1 + _compute_max_depth($sel->{selections}, $fragments, $visited)
Branch: 1 branch
BranchTrueFalseTotal
$sel->{'selections'} ? : 541 1491 2032
44 : 1;
45 } elsif ($kind eq 'inline_fragment') {
46 28 ▶ $depth = _compute_max_depth($sel->{selections} // [], $fragments, $visited);
Condition50%1/2
$sel->{"selections"} // []
l!l
280
MC/DC0%0/1
$sel->{"selections"} // []
$sel->{"selections"}
Truth table50%1/2
A
A= $sel->{"selections"}
Aresult
11
00
47 } elsif ($kind eq 'fragment_spread') {
48 26 ▶ my $name = $sel->{name} // '';
Condition50%1/2
$sel->{"name"} // ""
l!l
260
MC/DC0%0/1
$sel->{"name"} // ""
$sel->{"name"}
Truth table50%1/2
A
A= $sel->{"name"}
Aresult
11
00
49 26 ▶ next if $visited->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$name} 0 26 26
50 26 ▶ my $frag = $fragments->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $frag = $fragments->{$name} or next 26 0 26
51 26 local $visited->{$name} = 1;
52 26 ▶ $depth = _compute_max_depth($frag->{selections} // [], $fragments, $visited);
Condition50%1/2
$frag->{"selections"} // []
l!l
260
MC/DC0%0/1
$frag->{"selections"} // []
$frag->{"selections"}
Truth table50%1/2
A
A= $frag->{"selections"}
Aresult
11
00
53 }
54 2086 ▶ $max = $depth if defined $depth && $depth > $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $depth and $depth > $max 1220 866 2086
Condition66%2/3
defined $depth and $depth > $max
!ll&&!rl&&r
08661220
MC/DC50%1/2
defined $depth and $depth > $max
defined $depth $depth > $max
Truth table66%2/3
A and B
A= defined $depthB= $depth > $max
ABresult
0X0
100
111
55 }
56 1046 return $max;
57 }
58  
59 45 1;
60  
61 __END__
« blib/lib/GraphQL/Houtou/PSGI.pm ↑ Summary blib/lib/GraphQL/Houtou/Type/Scalar.pm »