| 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
| |||||||||||||||||||||||||||||
| 15 | 451 | ▶ | return () unless defined $max_depth; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 12 | my %fragments = map { $_->{name} => $_ } | |||||||||||||||||||||||||||
| 18 | 450 | ▶ | grep { ($_->{kind} // '') eq 'fragment' } @{ $ast || [] }; | ||||||||||||||||||||||||||
|
Condition50%2/4
$_->{"kind"} // ""
$ast || []
MC/DC0%0/1
$_->{"kind"} // ""
$_->{"kind"}
MC/DC0%0/1
$ast || []
$ast
Truth table50%1/2
A
A= $_->{"kind"}
Truth table50%1/2
A
A= $ast
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 450 | my @errors; | |||||||||||||||||||||||||||
| 21 | 450 | ▶ | for my $node (@{ $ast || [] }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$ast || []
MC/DC0%0/1
$ast || []
$ast
Truth table50%1/2
A
A= $ast
| |||||||||||||||||||||||||||||
| 22 | 463 | ▶ | next unless ($node->{kind} // '') eq 'operation'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$node->{"kind"} // ""
MC/DC0%0/1
$node->{"kind"} // ""
$node->{"kind"}
Truth table50%1/2
A
A= $node->{"kind"}
| |||||||||||||||||||||||||||||
| 23 | 451 | ▶ | my $depth = _compute_max_depth($node->{selections} // [], \%fragments, {}); | ||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"selections"} // []
MC/DC0%0/1
$node->{"selections"} // []
$node->{"selections"}
Truth table50%1/2
A
A= $node->{"selections"}
| |||||||||||||||||||||||||||||
| 24 | 451 | ▶ | if ($depth > $max_depth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 4 | my $name = $node->{name}; | |||||||||||||||||||||||||||
| 26 | 4 | ▶ | my $label = defined $name ? qq("$name") : 'anonymous operation'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 || []
MC/DC0%0/1
$selections || []
$selections
Truth table50%1/2
A
A= $selections
| |||||||||||||||||||||||||||||
| 39 | 2086 | ▶ | my $kind = $sel->{kind} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$sel->{"kind"} // ""
MC/DC0%0/1
$sel->{"kind"} // ""
$sel->{"kind"}
Truth table50%1/2
A
A= $sel->{"kind"}
| |||||||||||||||||||||||||||||
| 40 | 2086 | my $depth; | |||||||||||||||||||||||||||
| 41 | 2086 | ▶ | if ($kind eq 'field') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 42 | $depth = $sel->{selections} | ||||||||||||||||||||||||||||
| 43 | 2032 | ▶ | ? 1 + _compute_max_depth($sel->{selections}, $fragments, $visited) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | : 1; | ||||||||||||||||||||||||||||
| 45 | } elsif ($kind eq 'inline_fragment') { | ||||||||||||||||||||||||||||
| 46 | 28 | ▶ | $depth = _compute_max_depth($sel->{selections} // [], $fragments, $visited); | ||||||||||||||||||||||||||
|
Condition50%1/2
$sel->{"selections"} // []
MC/DC0%0/1
$sel->{"selections"} // []
$sel->{"selections"}
Truth table50%1/2
A
A= $sel->{"selections"}
| |||||||||||||||||||||||||||||
| 47 | } elsif ($kind eq 'fragment_spread') { | ||||||||||||||||||||||||||||
| 48 | 26 | ▶ | my $name = $sel->{name} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$sel->{"name"} // ""
MC/DC0%0/1
$sel->{"name"} // ""
$sel->{"name"}
Truth table50%1/2
A
A= $sel->{"name"}
| |||||||||||||||||||||||||||||
| 49 | 26 | ▶ | next if $visited->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 26 | ▶ | my $frag = $fragments->{$name} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 26 | local $visited->{$name} = 1; | |||||||||||||||||||||||||||
| 52 | 26 | ▶ | $depth = _compute_max_depth($frag->{selections} // [], $fragments, $visited); | ||||||||||||||||||||||||||
|
Condition50%1/2
$frag->{"selections"} // []
MC/DC0%0/1
$frag->{"selections"} // []
$frag->{"selections"}
Truth table50%1/2
A
A= $frag->{"selections"}
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 2086 | ▶ | $max = $depth if defined $depth && $depth > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $depth and $depth > $max
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
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 1046 | return $max; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 45 | 1; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | __END__ | ||||||||||||||||||||||||||||