blib/lib/Amon2/Plugin/Web/CpanelJSON.pm

filter: Statementstmt 100% 97 / 97 Branchbran 100% 30 / 30 Conditioncond 85.1% 23 / 27 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 1 totaltotal 95.0% 173 / 182 CC 30 SCAR 17.0
CC
30
Cov
97%
__ANON__
8.2
init
7.0
__ANON__
7.0
CRAP 5.5
↑ Summary
1 package Amon2::Plugin::Web::CpanelJSON;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 ▶use Amon2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use HTTP::SecureHeaders;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 our $VERSION = "0.01";
11  
12 13 my %DEFAULT_CONFIG = (
13 name => 'render_json',
14  
15 # for security
16 # refs https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html#security-headers
17 secure_headers => {
18 content_security_policy => "default-src 'none'",
19 strict_transport_security => 'max-age=631138519',
20 x_content_type_options => 'nosniff',
21 x_download_options => undef,
22 x_frame_options => 'DENY',
23 x_permitted_cross_domain_policies => 'none',
24 x_xss_protection => '1; mode=block',
25 referrer_policy => 'no-referrer',
26 },
27  
28 json_escape_filter => {
29 # Ref: https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html
30 # Ref: (Japanese) http://www.atmarkit.co.jp/fcoding/articles/webapp/05/webapp05a.html
31 '+' => '\\u002b', # do not eval as UTF-7
32 '<' => '\\u003c', # do not eval as HTML
33 '>' => '\\u003e', # ditto.
34 },
35  
36 # JSON config
37 json => {
38 ascii => !!1, # for security
39 },
40  
41 # for convenience
42 unbless_object => undef,
43 status_code_field => undef,
44  
45 # for compatibility options
46 defence_json_hijacking_for_legacy_browser => !!0,
47 );
48  
49  
50 22 ▶sub init {
Subroutine init: called CC=7 SCAR=19.5
Pod undocumented
51 22 my ($class, $c, $conf) = @_;
52  
53 22 $conf = do {
54 22 ▶ $conf ||= {};
Condition100%2/2
$conf ||= {}
l!l
139
MC/DC100%1/1
$conf ||= {}
$conf
Truth table100%2/2
A
A= $conf
Aresult
11
00
55  
56 22 for my $key (qw/secure_headers json_escape_filter json/) {
57 66 ▶ if (exists $conf->{$key} && !defined $conf->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf->{$key} and not defined $conf->{$key}) { } 3 63 66
Condition100%3/3
exists $conf->{$key} and not defined $conf->{$key}
!ll&&!rl&&r
5853
MC/DC100%2/2
exists $conf->{$key} and not defined $conf->{$key}
exists $conf->{$key} not defined $conf->{$key}
Truth table100%3/3
A and not(B)
A= exists $conf->{$key}B= not defined $conf->{$key}
ABresult
0X0
100
111
58 3 $conf->{$key} = undef;
59 }
60 else {
61 $conf->{$key} = {
62 63 %{ $DEFAULT_CONFIG{$key} },
63 63 ▶ %{ $conf->{$key} || {} },
Condition100%2/2
$conf->{$key} || {}
l!l
558
MC/DC100%1/1
$conf->{$key} || {}
$conf->{$key}
Truth table100%2/2
A
A= $conf->{$key}
Aresult
11
00
64 }
65 }
66 }
67  
68 22 +{ %DEFAULT_CONFIG, %{$conf} };
69 };
70  
71 22 my $name = $conf->{name};
72  
73 22 ▶ unless ($c->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->can($name)) 21 1 22
74 21 my $render_json = _generate_render_json($conf);
75 21 Amon2::Util::add_method($c, $name, $render_json)
76 }
77 }
78  
79 21 ▶sub _generate_render_json {
Subroutine _generate_render_json: called CC=2 SCAR=6.9
80 21 my $conf = shift;
81  
82 21 my $encoder = _generate_json_encoder($conf);
83 21 my $validator = _generate_req_validator($conf);
84  
85 21 my $secure_headers;
86 21 ▶ if ($conf->{secure_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"secure_headers"}) 20 1 21
87 20 $secure_headers = HTTP::SecureHeaders->new(%{ $conf->{secure_headers} });
88 }
89  
90 42 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
91 42 my ($c, $data, $spec, $status) = @_;
92 42 ▶ $status //= 200;
Condition100%2/2
$status //= 200
l!l
141
MC/DC100%1/1
$status //= 200
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
93  
94 42 ▶ if (my $error = $validator->($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &$validator($c)) 1 41 42
95 1 return $error;
96 }
97  
98 41 my $output = $encoder->($data, $spec);
99  
100 38 my $res = do {
101 38 my $res = $c->create_response($status);
102  
103 38 my $encoding = $c->encoding();
104 38 ▶ $encoding = lc($encoding->mime_name) if ref $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if ref $encoding 9 29 38
105  
106 38 $res->content_type("application/json; charset=$encoding");
107 38 $res->content_length(length($output));
108 38 $res->body($output);
109  
110 38 ▶ if ($secure_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secure_headers) 37 1 38
111 37 $secure_headers->apply($res->headers);
112 }
113  
114 # X-API-Status
115 # (Japanese) http://web.archive.org/web/20190503111531/http://blog.yappo.jp/yappo/archives/000829.html
116 38 ▶ if (my $status_code_field = $conf->{status_code_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status_code_field = $conf->{"status_code_field"}) 2 36 38
117 2 ▶ if (exists $data->{$status_code_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{$status_code_field}) 1 1 2
118 1 $res->header('X-API-Status' => $data->{$status_code_field})
119 }
120 }
121  
122 $res
123 38 };
124  
125 38 return $res;
126 }
127 21 }
128  
129 21 ▶sub _generate_json_encoder {
Subroutine _generate_json_encoder: called CC=6 SCAR=17.9
130 21 my $conf = shift;
131  
132 21 my $json = Cpanel::JSON::XS->new;
133 21 ▶ if (my $json_args = $conf->{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $json_args = $conf->{"json"}) 20 1 21
134 20 for my $key (keys %{$json_args}) {
135 23 $json->$key($json_args->{$key})
136 }
137 }
138  
139 21 ▶ my $escape_filter = $conf->{json_escape_filter} || {};
Condition100%2/2
$conf->{'json_escape_filter'} || {}
l!l
201
MC/DC100%1/1
$conf->{'json_escape_filter'} || {}
$conf->{'json_escape_filter'}
Truth table100%2/2
A
A= $conf->{'json_escape_filter'}
Aresult
11
00
140 21 my $escape_target = '';
141 21 for my $key (keys %{$escape_filter}) {
142 60 ▶ if ($escape_filter->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape_filter->{$key}) 59 1 60
143 59 $escape_target .= $key
144 }
145 }
146  
147 41 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
148 41 my ($data, $spec) = @_;
149  
150 41 ▶ if (my $unbless_object = $conf->{unbless_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $unbless_object = $conf->{"unbless_object"}) 2 39 41
151 2 ▶ if (blessed($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data)) 1 1 2
152 1 $data = $unbless_object->($data, $spec);
153 }
154 }
155  
156 41 my $output = $json->encode($data, $spec);
157  
158 38 ▶ if ($escape_target && $escape_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape_target and $escape_filter) 37 1 38
Condition66%2/3
$escape_target and $escape_filter
!ll&&!rl&&r
1037
MC/DC50%1/2
$escape_target and $escape_filter
$escape_target $escape_filter
Truth table66%2/3
A and B
A= $escape_targetB= $escape_filter
ABresult
0X0
100
111
159 37 $output =~ s!([$escape_target])!$escape_filter->{$1}!g;
160 }
161  
162 38 return $output;
163 }
164 21 }
165  
166 21 ▶sub _generate_req_validator {
Subroutine _generate_req_validator: called CC=1 SCAR=0.0
167 21 my $conf = shift;
168  
169 42 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=21.0
170 42 my ($c) = @_;
171  
172 # defense from JSON hijacking
173 42 ▶ if ($conf->{defence_json_hijacking_for_legacy_browser}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"defence_json_hijacking_for_legacy_browser"}) 3 39 42
174 3 ▶ my $user_agent = $c->req->user_agent || '';
Condition50%1/2
$c->req->user_agent || ''
l!l
30
MC/DC0%0/1
$c->req->user_agent || ''
$c->req->user_agent
Truth table50%1/2
A
A= $c->req->user_agent
Aresult
11
00
175  
176 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->req->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie") and ($c->req->method || "GET") eq "GET") 1 2 3
Condition81%9/11
not $c->req->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie") and ($c->req->method || "GET") eq "GET"
!ll&&!rl&&r
111
not $c->req->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie")
!ll&&!rl&&r
102
not $c->req->header("X-Requested-With") and $user_agent =~ /android/i
!ll&&!rl&&r
102
$c->req->method || 'GET'
l!l
11
MC/DC50%2/4
not $c->req->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie") and ($c->req->method || "GET") eq "GET"
not $c->req->header("X-Requested-With") $user_agent =~ /android/i defined $c->req->header("Cookie") $c->req->method
Truth table60%3/5
not(A) and B and C and D
A= not $c->req->header("X-Requested-With")B= $user_agent =~ /android/iC= defined $c->req->header("Cookie")D= $c->req->method
ABCDresult
0XXX0
10XX0
110X0
11100
11111
177 (!$c->req->header('X-Requested-With')) &&
178 $user_agent =~ /android/i &&
179 defined $c->req->header('Cookie') &&
180 ($c->req->method||'GET') eq 'GET'
181 ) {
182 1 return _error_response($c);
183 }
184 }
185 }
186 21 }
187  
188 1 ▶sub _error_response {
Subroutine _error_response: called CC=1 SCAR=0.0
189 1 my $c = shift;
190  
191 1 my $res = $c->create_response(403);
192 1 $res->content_type('text/plain');
193 1 $res->content("invalid JSON request");
194 1 $res->content_length(length $res->content);
195 1 return $res;
196 }
197  
198 13 1;
199 __END__
↑ Summary