| 1 | package Langertha::Manifest::Validation; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Internal validation rules shared by the provider manifest value objects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use B (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # A field whose name contains one of these words is rejected explicitly, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # before the unknown-field check, so the error says WHY: a manifest comes from | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # the network and never carries anything that could run a command, load code, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # point at a local secret or inject a prompt (langertha-raider ADR 0007). No v1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # field name contains any of these words. The closed field set is what actually | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # keeps such fields out; this list only sharpens the message. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | my %FORBIDDEN_WORD = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | command commands cmd exec shell script run install hook hooks | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | class module package code eval require plugin plugins perl | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | secret secrets password token credential credentials key apikey env path file | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | prompt mission mcp packs skills tools | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 19 | ▶ | sub _field_words { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_field_words: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 19 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 19 | ( my $split = $name ) =~ s/([a-z0-9])([A-Z])/$1_$2/g; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 19 | return grep { length } split /[_\-.\s]+/, lc $split; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 19 | ▶ | sub _is_forbidden_field { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_forbidden_field: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 19 | my ( $class, $name ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 19 | return scalar grep { $FORBIDDEN_WORD{$_} } _field_words($name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # Untrusted text ends up in error messages a client prints: show it escaped | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # and bounded, never raw (no terminal escapes, no bidi overrides). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 120 | ▶ | sub _display { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_display: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 120 | my ( $class, $text ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 120 | ▶ | $text = '' unless defined $text; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 120 | ▶ | $text = substr( $text, 0, 64 ) . '...' if length $text > 64; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 120 | $text =~ s/([^\x20-\x7e])/sprintf '\\x{%x}', ord $1/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 120 | return $text; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 78 | ▶ | sub _error { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 78 | my ( $class, $message ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 78 | die "Langertha::Manifest: $message\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Re-raise an error from a nested entry with its location in the document. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 33 | ▶ | sub _rethrow { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rethrow: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 33 | my ( $class, $path, $error ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 33 | ▶ | my $message = blessed($error) && $error->can('message') ? $error->message : "$error"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($error) and $error->can("message")
MC/DC0%0/2
builtin::blessed($error) and $error->can("message")
builtin::blessed($error)
$error->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->can("message")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 33 | $message =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 33 | $message =~ s/\ALangertha::Manifest: //; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 33 | die "Langertha::Manifest: $path: $message\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 316 | ▶ | sub _check_fields { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_fields: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 316 | my ( $class, $data, %spec ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 316 | ▶ | $class->_error('must be a JSON object') unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 315 | ▶ | my %allowed = map { $_ => 1 } @{ $spec{required} || [] }, @{ $spec{optional} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$spec{'required'} || []
$spec{'optional'} || []
MC/DC0%0/1
$spec{'required'} || []
$spec{'required'}
MC/DC100%1/1
$spec{'optional'} || []
$spec{'optional'}
Truth table50%1/2
A
A= $spec{'required'}
Truth table100%2/2
A
A= $spec{'optional'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 315 | for my $field ( sort keys %$data ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 1369 | ▶ | next if $allowed{$field}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 19 | my $shown = $class->_display($field); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 19 | ▶ | $class->_error( "forbidden field '$shown': a manifest never carries " | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | . 'commands, code, secrets or prompts' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | if $class->_is_forbidden_field($field); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | $class->_error("unknown field '$shown'"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 296 | ▶ | for my $field ( @{ $spec{required} || [] } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$spec{'required'} || []
MC/DC0%0/1
$spec{'required'} || []
$spec{'required'}
Truth table50%1/2
A
A= $spec{'required'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 918 | ▶ | $class->_error("field '$field' is required") unless defined $data->{$field}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 292 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # A schema string field: a JSON string (a JSON number is accepted and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # stringified, so "id": 42 serializes back as "42"), never an object/array. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 658 | ▶ | sub _string { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_string: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 658 | my ( $class, $what, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 658 | ▶ | return undef unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 658 | ▶ | $class->_error("$what must be a string") if ref $value; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 656 | return "$value"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | my $ID_RE = qr/\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 401 | ▶ | sub _check_id { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_id: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 401 | my ( $class, $what, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 401 | ▶ | $class->_error( "$what must match [A-Za-z0-9][A-Za-z0-9._-]* (max 64), got '" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $value and not ref $value and $value =~ /$ID_RE/
defined $value and not ref $value
MC/DC33%1/3
defined $value and not ref $value and $value =~ /$ID_RE/
defined $value
not ref $value
$value =~ /$ID_RE/
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /$ID_RE/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | . $class->_display($value) . q{'} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | unless defined $value && !ref $value && $value =~ $ID_RE; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 400 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 203 | ▶ | sub _check_token { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_token: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 203 | my ( $class, $what, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 203 | ▶ | $class->_error( "$what must match [a-z][a-z0-9_-]*, got '" . $class->_display($value) . q{'} ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $value and not ref $value and $value =~ /\A[a-z][a-z0-9_-]{0,63}\z/
defined $value and not ref $value
MC/DC33%1/3
defined $value and not ref $value and $value =~ /\A[a-z][a-z0-9_-]{0,63}\z/
defined $value
not ref $value
$value =~ /\A[a-z][a-z0-9_-]{0,63}\z/
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A[a-z][a-z0-9_-]{0,63}\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | unless defined $value && !ref $value && $value =~ /\A[a-z][a-z0-9_-]{0,63}\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 201 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # http/https, a host, printable ASCII only (IDN hosts go punycode), and no | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # userinfo, query or fragment -- the usual places a credential is smuggled | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # into a URL. This is best effort: a secret embedded in the PATH | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # (/key/SECRET/v1, ;key=SECRET) cannot be told apart from a real path. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 192 | ▶ | sub _check_url { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_url: called CC=13 SCAR=26.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 192 | my ( $class, $what, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 192 | ▶ | $class->_error("$what: must be an http or https URL") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $value and not ref $value and length $value
defined $value and not ref $value
MC/DC0%0/3
defined $value and not ref $value and length $value
defined $value
not ref $value
length $value
Truth table25%1/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | unless defined $value && !ref $value && length $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 192 | ▶ | $class->_error("$what: must be printable ASCII (no control, space or non-ASCII characters)") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | if $value =~ /[^\x21-\x7e]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 190 | my $uri = URI->new($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 190 | ▶ | my $scheme = $uri->scheme // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$uri->scheme // ""
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 190 | ▶ | $class->_error( "$what: must be an http or https URL, got '" . $class->_display($value) . q{'} ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%6/8
$scheme eq "http" || $scheme eq "https" and length($uri->host // "")
$scheme eq "http" or $scheme eq "https"
$uri->host // ""
MC/DC66%2/3
$scheme eq "http" || $scheme eq "https" and length($uri->host // "")
$scheme eq "http"
$scheme eq "https"
$uri->host
Truth table60%3/5
A or B and C
A= $scheme eq "http"B= $scheme eq "https"C= $uri->host
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | unless ( $scheme eq 'http' || $scheme eq 'https' ) && length( $uri->host // '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 188 | ▶ | $class->_error("$what: must not carry userinfo (credentials) in the URL") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | if defined $uri->userinfo; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 187 | ▶ | $class->_error("$what: must not carry a query string (secrets hide there)") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | if defined $uri->query; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 186 | ▶ | $class->_error("$what: must not carry a fragment") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | if defined $uri->fragment; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 185 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 737 | ▶ | sub _sv_flags { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sv_flags: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 737 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 737 | return B::svref_2object( \$value )->FLAGS; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # Is this a NUMBER, not a string? Backend-independent: JSON::PP and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # Cpanel::JSON::XS disagree on whether 1e0 decodes to an integer or a float, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # so the test is "has a numeric slot" (IOK or NOK), and -- when a string is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # cached on it too (a printed number, or a numified string) -- the string must | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # be a plain JSON number. A decoded JSON string ("1") has only a string slot | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # and fails; a numified Perl string like "1abc" fails on its text. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 8 | my $JSON_NUMBER_RE = qr/\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 739 | ▶ | sub _is_number { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_number: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 739 | my ( $class, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 739 | ▶ | return 0 if !defined $value || ref $value; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $value or ref $value
MC/DC50%1/2
not defined $value or ref $value
not defined $value
ref $value
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 737 | my $flags = _sv_flags($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 737 | ▶ | return 0 unless $flags & ( B::SVp_IOK | B::SVp_NOK ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 728 | ▶ | return 0 if ( $flags & B::SVp_POK ) && $value !~ $JSON_NUMBER_RE; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$flags & 16384 and not $value =~ /$JSON_NUMBER_RE/
MC/DC50%1/2
$flags & 16384 and not $value =~ /$JSON_NUMBER_RE/
$flags & 16384
not $value =~ /$JSON_NUMBER_RE/
Truth table66%2/3
A and not(B)
A= $flags & 16384B= not $value =~ /$JSON_NUMBER_RE/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 727 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # A number whose value is whole: 1, 1.0 and 1e0 alike, on every backend. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 104 | ▶ | sub _is_whole_number { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_whole_number: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 104 | my ( $class, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 104 | ▶ | return 0 unless $class->_is_number($value); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 96 | ▶ | return $value == int($value) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # A boolean is a JSON boolean, \1 / \0, or the NUMBERS 1 / 0. A JSON string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # ("1", "true", "yes") is not a boolean. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 701 | ▶ | sub _bool { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bool: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 701 | my ( $class, $what, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 701 | ▶ | return $value ? 1 : 0 if JSON::MaybeXS::is_bool($value); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 637 | ▶ | return 0 + $$value if ref $value eq 'SCALAR' && defined $$value && $$value =~ /\A[01]\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
ref $value eq "SCALAR" and defined $$value and $$value =~ /\A[01]\z/
ref $value eq "SCALAR" and defined $$value
MC/DC33%1/3
ref $value eq "SCALAR" and defined $$value and $$value =~ /\A[01]\z/
ref $value eq "SCALAR"
defined $$value
$$value =~ /\A[01]\z/
Truth table50%2/4
A and B and C
A= ref $value eq "SCALAR"B= defined $$valueC= $$value =~ /\A[01]\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 635 | ▶ | return 0 + $value | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$class->_is_number($value) and $value == 0 || $value == 1
$value == 0 or $value == 1
MC/DC100%3/3
$class->_is_number($value) and $value == 0 || $value == 1
$class->_is_number($value)
$value == 0
$value == 1
Truth table100%4/4
A and B or C
A= $class->_is_number($value)B= $value == 0C= $value == 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | if $class->_is_number($value) && ( $value == 0 || $value == 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | $class->_error("$what must be a boolean"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # Deep copy through JSON: proves the value is plain JSON data (no objects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # other than JSON booleans, no code refs) and detaches it from the caller. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 8 | my $CLONE_JSON = JSON::MaybeXS->new( utf8 => 1, canonical => 1, allow_nonref => 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 106 | ▶ | sub _json_clone { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_clone: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 106 | my ( $class, $what, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 106 | my $copy = eval { $CLONE_JSON->decode( $CLONE_JSON->encode($value) ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 106 | ▶ | $class->_error("$what must hold plain JSON data") if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 104 | return $copy; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||