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

filter: Statementstmt 100% 49 / 49 Branchbran 83.3% 20 / 24 Conditioncond 50.0% 10 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 78.3% 87 / 111 CC 25 SCAR 24.8
CC
25
Cov
85%
_count_nodes
15.1
check_query_nodes
13.0
BEGIN
1.0
CRAP 12.0
« blib/lib/GraphQL/Houtou/Type/Scalar.pm ↑ Summary blib/lib/GraphQL/Houtou/Runtime/VMProgram.pm »
1 package GraphQL::Houtou::Validation::NodeLimit;
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_nodes);
9  
10 # The depth limit bounds nesting but not breadth: an alias-flooded query
11 # like { a:f b:f c:f ... } (optionally repeated under a shallow nesting)
12 # stays within the depth limit while forcing a huge amount of resolution
13 # and a huge response. This validator caps the total number of field
14 # selections an operation resolves, counting fragment spreads by
15 # expansion so { ...F ...F ...F } cannot multiply cheaply. It is a coarse
16 # DoS bound, not a cost model. The separate XS weighted-cost walk accounts
17 # for field cost and estimated list fan-out; both limits are enforced.
18 45 ▶use constant DEFAULT_MAX_NODES => 10_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 447 ▶sub check_query_nodes {
Subroutine check_query_nodes: called CC=12 SCAR=25.6
Pod documented
21 447 my ($ast, %opts) = @_;
22 447 ▶ my $max_nodes = exists $opts{max_nodes} ? $opts{max_nodes} : DEFAULT_MAX_NODES;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'max_nodes'} ? : 447 0 447
23 447 ▶ return () unless defined $max_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_nodes 1 446 447
24  
25 13 my %fragments = map { $_->{name} => $_ }
26 446 ▶ grep { ($_->{kind} // '') eq 'fragment' } @{ $ast || [] };
Condition50%2/4
$_->{"kind"} // ""
l!l
4600
$ast || []
l!l
4460
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
27  
28 446 my @errors;
29 446 ▶ for my $node (@{ $ast || [] }) {
Condition50%1/2
$ast || []
l!l
4460
MC/DC0%0/1
$ast || []
$ast
Truth table50%1/2
A
A= $ast
Aresult
11
00
30 460 ▶ next unless ($node->{kind} // '') eq 'operation';
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->{"kind"} // "") eq "operation" 13 447 460
Condition50%1/2
$node->{"kind"} // ""
l!l
4600
MC/DC0%0/1
$node->{"kind"} // ""
$node->{"kind"}
Truth table50%1/2
A
A= $node->{"kind"}
Aresult
11
00
31 # Cap the walk itself at max_nodes+1 so a pathological document cannot
32 # make counting expensive: as soon as the budget is blown we stop.
33 447 ▶ my $count = _count_nodes($node->{selections} // [], \%fragments, {}, $max_nodes + 1);
Condition50%1/2
$node->{"selections"} // []
l!l
4470
MC/DC0%0/1
$node->{"selections"} // []
$node->{"selections"}
Truth table50%1/2
A
A= $node->{"selections"}
Aresult
11
00
34 447 ▶ if ($count > $max_nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $max_nodes) 3 444 447
35 3 my $name = $node->{name};
36 3 ▶ my $label = defined $name ? qq("$name") : 'anonymous operation';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 3 3
37 3 push @errors, {
38 message => "Query has too many field selections (exceeds maximum of $max_nodes) in $label",
39 };
40 }
41 }
42 446 return @errors;
43 }
44  
45 # Returns the number of field selections reachable from $selections,
46 # stopping early once $budget is exceeded (the returned value is then
47 # just "> max_nodes", which is all the caller needs).
48 1040 ▶sub _count_nodes {
Subroutine _count_nodes: called CC=14 SCAR=27.1
49 1040 my ($selections, $fragments, $visited, $budget) = @_;
50 1040 my $count = 0;
51 1040 ▶ for my $sel (@{ $selections || [] }) {
Condition50%1/2
$selections || []
l!l
10400
MC/DC0%0/1
$selections || []
$selections
Truth table50%1/2
A
A= $selections
Aresult
11
00
52 2035 ▶ my $kind = $sel->{kind} // '';
Condition50%1/2
$sel->{"kind"} // ""
l!l
20350
MC/DC0%0/1
$sel->{"kind"} // ""
$sel->{"kind"}
Truth table50%1/2
A
A= $sel->{"kind"}
Aresult
11
00
53 2035 ▶ if ($kind eq 'field') {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind eq 'field') { } 1970 65 2035
elsif ($kind eq 'inline_fragment') { } 27 38 65
elsif ($kind eq 'fragment_spread') { } 38 0 38
54 1970 $count++;
55 $count += _count_nodes($sel->{selections}, $fragments, $visited, $budget - $count)
56 1970 ▶ if $sel->{selections};
Branch: 1 branch
BranchTrueFalseTotal
if $sel->{"selections"} 529 1441 1970
57 } elsif ($kind eq 'inline_fragment') {
58 27 ▶ $count += _count_nodes($sel->{selections} // [], $fragments, $visited, $budget - $count);
Condition50%1/2
$sel->{"selections"} // []
l!l
270
MC/DC0%0/1
$sel->{"selections"} // []
$sel->{"selections"}
Truth table50%1/2
A
A= $sel->{"selections"}
Aresult
11
00
59 } elsif ($kind eq 'fragment_spread') {
60 38 ▶ my $name = $sel->{name} // '';
Condition50%1/2
$sel->{"name"} // ""
l!l
380
MC/DC0%0/1
$sel->{"name"} // ""
$sel->{"name"}
Truth table50%1/2
A
A= $sel->{"name"}
Aresult
11
00
61 38 ▶ next if $visited->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$name} 1 37 38
62 37 ▶ my $frag = $fragments->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $frag = $fragments->{$name} or next 37 0 37
63 37 local $visited->{$name} = 1;
64 37 ▶ $count += _count_nodes($frag->{selections} // [], $fragments, $visited, $budget - $count);
Condition50%1/2
$frag->{"selections"} // []
l!l
370
MC/DC0%0/1
$frag->{"selections"} // []
$frag->{"selections"}
Truth table50%1/2
A
A= $frag->{"selections"}
Aresult
11
00
65 }
66 2034 ▶ return $count if $count > $budget;
Branch: 1 branch
BranchTrueFalseTotal
if $count > $budget 4 2030 2034
67 }
68 1036 return $count;
69 }
70  
71 45 1;
72  
73 __END__
« blib/lib/GraphQL/Houtou/Type/Scalar.pm ↑ Summary blib/lib/GraphQL/Houtou/Runtime/VMProgram.pm »