| 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
| |||||||||||||||||||||||
| 23 | 447 | ▶ | return () unless defined $max_nodes; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | 13 | my %fragments = map { $_->{name} => $_ } | |||||||||||||||||||||
| 26 | 446 | ▶ | 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
| |||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 446 | my @errors; | |||||||||||||||||||||
| 29 | 446 | ▶ | for my $node (@{ $ast || [] }) { | ||||||||||||||||||||
|
Condition50%1/2
$ast || []
MC/DC0%0/1
$ast || []
$ast
Truth table50%1/2
A
A= $ast
| |||||||||||||||||||||||
| 30 | 460 | ▶ | 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"}
| |||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
$node->{"selections"} // []
$node->{"selections"}
Truth table50%1/2
A
A= $node->{"selections"}
| |||||||||||||||||||||||
| 34 | 447 | ▶ | if ($count > $max_nodes) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 35 | 3 | my $name = $node->{name}; | |||||||||||||||||||||
| 36 | 3 | ▶ | my $label = defined $name ? qq("$name") : 'anonymous operation'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 || []
MC/DC0%0/1
$selections || []
$selections
Truth table50%1/2
A
A= $selections
| |||||||||||||||||||||||
| 52 | 2035 | ▶ | 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"}
| |||||||||||||||||||||||
| 53 | 2035 | ▶ | if ($kind eq 'field') { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 54 | 1970 | $count++; | |||||||||||||||||||||
| 55 | $count += _count_nodes($sel->{selections}, $fragments, $visited, $budget - $count) | ||||||||||||||||||||||
| 56 | 1970 | ▶ | if $sel->{selections}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 57 | } elsif ($kind eq 'inline_fragment') { | ||||||||||||||||||||||
| 58 | 27 | ▶ | $count += _count_nodes($sel->{selections} // [], $fragments, $visited, $budget - $count); | ||||||||||||||||||||
|
Condition50%1/2
$sel->{"selections"} // []
MC/DC0%0/1
$sel->{"selections"} // []
$sel->{"selections"}
Truth table50%1/2
A
A= $sel->{"selections"}
| |||||||||||||||||||||||
| 59 | } elsif ($kind eq 'fragment_spread') { | ||||||||||||||||||||||
| 60 | 38 | ▶ | 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"}
| |||||||||||||||||||||||
| 61 | 38 | ▶ | next if $visited->{$name}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 62 | 37 | ▶ | my $frag = $fragments->{$name} or next; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 63 | 37 | local $visited->{$name} = 1; | |||||||||||||||||||||
| 64 | 37 | ▶ | $count += _count_nodes($frag->{selections} // [], $fragments, $visited, $budget - $count); | ||||||||||||||||||||
|
Condition50%1/2
$frag->{"selections"} // []
MC/DC0%0/1
$frag->{"selections"} // []
$frag->{"selections"}
Truth table50%1/2
A
A= $frag->{"selections"}
| |||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | 2034 | ▶ | return $count if $count > $budget; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | 1036 | return $count; | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 45 | 1; | |||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||