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

filter: Statementstmt 100% 38 / 38 Branchbran 91.6% 11 / 12 Conditioncond 92.3% 12 / 13 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 94.6% 71 / 75 CC 12 SCAR 20.3
CC
12
Cov
97%
__ANON__
11.0
init
2.0
BEGIN
1.0
CRAP 7.6
« blib/lib/auto/share/dist/Amon2/flavor/Basic/lib/__PATH__/Web/Plugin/Session.pm ↑ Summary blib/script/amon2-setup.pl »
1 package Amon2::Plugin::Web::JSON;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use JSON 2 qw/encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Amon2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 my $_JSON = JSON->new()->ascii(1);
8  
9 5 my %_ESCAPE = (
10 '+' => '\\u002b', # do not eval as UTF-7
11 '<' => '\\u003c', # do not eval as HTML
12 '>' => '\\u003e', # ditto.
13 );
14  
15 9 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
16 9 my ($class, $c, $conf) = @_;
17 9 ▶ unless ($c->can('render_json')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->can("render_json")) 9 0 9
18 19 ▶ Amon2::Util::add_method($c, 'render_json', sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
19 19 my ($c, $stuff) = @_;
20  
21 # for IE7 JSON venularity.
22 # see http://www.atmarkit.co.jp/fcoding/articles/webapp/05/webapp05a.html
23 19 ▶ my $output = $_JSON->canonical( $conf->{canonical} ? 1 : 0 )->encode($stuff);
Branch: 1 branch
BranchTrueFalseTotal
$conf->{'canonical'} ? : 1 18 19
24 19 $output =~ s!([+<>])!$_ESCAPE{$1}!g;
25  
26 19 ▶ my $user_agent = $c->req->user_agent || '';
Condition100%2/2
$c->req->user_agent || ''
l!l
712
MC/DC100%1/1
$c->req->user_agent || ''
$c->req->user_agent
Truth table100%2/2
A
A= $c->req->user_agent
Aresult
11
00
27  
28 # defense from JSON hijacking
29 19 ▶ if ((!$c->request->header('X-Requested-With')) && $user_agent =~ /android/i && defined $c->req->header('Cookie') && ($c->req->method||'GET') eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->request->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie") and ($c->req->method || "GET") eq "GET") 1 18 19
Condition90%10/11
not $c->request->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
1711
not $c->request->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie")
!ll&&!rl&&r
1702
not $c->request->header("X-Requested-With") and $user_agent =~ /android/i
!ll&&!rl&&r
2152
$c->req->method || 'GET'
l!l
11
MC/DC75%3/4
not $c->request->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie") and ($c->req->method || "GET") eq "GET"
not $c->request->header("X-Requested-With") $user_agent =~ /android/i defined $c->req->header("Cookie") $c->req->method
Truth table80%4/5
not(A) and B and C and D
A= not $c->request->header("X-Requested-With")B= $user_agent =~ /android/iC= defined $c->req->header("Cookie")D= $c->req->method
ABCDresult
0XXX0
10XX0
110X0
11100
11111
30 1 my $res = $c->create_response(403);
31 1 $res->content_type('text/html; charset=utf-8');
32 1 $res->content("Your request may be JSON hijacking.\nIf you are not an attacker, please add 'X-Requested-With' header to each request.");
33 1 $res->content_length(length $res->content);
34 1 return $res;
35 }
36  
37 18 my $res = $c->create_response(200);
38  
39 18 my $encoding = $c->encoding();
40 18 ▶ $encoding = lc($encoding->mime_name) if ref $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if ref $encoding 16 2 18
41 18 $res->content_type("application/json; charset=$encoding");
42 18 $res->header( 'X-Content-Type-Options' => 'nosniff' ); # defense from XSS
43 18 $res->content_length(length($output));
44 18 $res->body($output);
45  
46 18 ▶ if (defined (my $status_code_field = $conf->{status_code_field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $status_code_field = $conf->{"status_code_field"})) 6 12 18
47 6 ▶ $res->header( 'X-API-Status' => $stuff->{$status_code_field} ) if exists $stuff->{$status_code_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stuff->{$status_code_field} 3 3 6
48 }
49  
50 18 return $res;
51 9 });
52 }
53 }
54  
55 5 1;
56 __END__
« blib/lib/auto/share/dist/Amon2/flavor/Basic/lib/__PATH__/Web/Plugin/Session.pm ↑ Summary blib/script/amon2-setup.pl »