blib/lib/Kossy/Connection.pm

filter: Statementstmt 40.6% 24 / 59 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 58.3% 7 / 12 Podpod 0.0% 0 / 7 totaltotal 28.4% 31 / 109 CC 11 SCAR 36.6
CC
11
Cov
29%
render_json
90.0
render
12.0
escape
2.0
CRAP 39.0
↑ Summary blib/lib/Kossy.pm »
1 package Kossy::Connection;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use Class::Accessor::Lite (
6 6 new => 1,
7 rw => [qw/req res stash args tx debug/]
8 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use JSON qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Kossy::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 our $VERSION = '0.38';
13 6 my $_JSON = JSON->new()->allow_blessed(1)->convert_blessed(1)->ascii(1);
14  
15 # for IE7 JSON venularity.
16 # see http://www.atmarkit.co.jp/fcoding/articles/webapp/05/webapp05a.html
17 # Copy from Amon2::Plugin::Web::JSON => Fixed to escape only string parts
18 6 my %_ESCAPE = (
19 '+' => '\\u002b', # do not eval as UTF-7
20 '<' => '\\u003c', # do not eval as HTML
21 '>' => '\\u003e', # ditto.
22 );
23 sub escape {
24 0 ▶ my $self = shift;
Subroutine escape: not called CC=1 SCAR=6.9
Pod undocumented
25 0 my $body = shift;
26 0 $body =~ s!([+<>])!$_ESCAPE{$1}!g;
27 0 return qq("$body");
28 }
29 sub escape_json {
30 0 ▶ my $self = shift;
Subroutine escape_json: not called CC=1 SCAR=6.9
Pod undocumented
31 0 my $body = shift;
32 # escape only string parts
33 0 $body =~ s/"((?:\\"|[^"])*)"/$self->escape($1)/eg;
34 0 return $body;
35 }
36  
37 6 *request = \&req;
38 6 *response = \&res;
39  
40 sub env {
41 1 ▶ $_[0]->{req}->env;
Subroutine env: called CC=1 SCAR=0.0
Pod undocumented
42 }
43  
44 sub halt {
45 4 ▶ my $self = shift;
Subroutine halt: called CC=1 SCAR=0.0
Pod undocumented
46 4 die Kossy::Exception->new(@_);
47 }
48  
49 sub redirect {
50 0 ▶ my $self = shift;
Subroutine redirect: not called CC=1 SCAR=6.9
Pod undocumented
51 0 $self->res->redirect(@_);
52 0 $self->res;
53 }
54  
55 sub render {
56 0 ▶ my $self = shift;
Subroutine render: not called CC=3 SCAR=24.8
Pod undocumented
57 0 my $file = shift;
58 0 ▶ my %args = ( @_ && ref $_[0] ) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 0 0
Condition0%0/3
@_ and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table0%0/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
59 0 my %vars = (
60 c => $self,
61 stash => $self->stash,
62 %args,
63 );
64  
65 0 my $body = $self->tx->render($file, \%vars);
66 0 $self->res->status( 200 );
67 0 $self->res->content_type('text/html; charset=UTF-8');
68 0 $self->res->body( $body );
69 0 $self->res;
70 }
71  
72 sub render_json {
73 0 ▶ my $self = shift;
Subroutine render_json: not called CC=9 SCAR=45.0
Pod undocumented
74 0 my $obj = shift;
75  
76 # defense from JSON hijacking
77 # Copy from Amon2::Plugin::Web::JSON
78 0 ▶ if ( !$self->req->header('X-Requested-With') &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->req->header("X-Requested-With") and ($self->req->user_agent || "") =~ /android/i and defined $self->req->header("Cookie") and ($self->req->method || "GET") eq "GET") 0 0 0
Condition0%0/13
not $self->req->header("X-Requested-With") and ($self->req->user_agent || "") =~ /android/i and defined $self->req->header("Cookie") and ($self->req->method || "GET") eq "GET"
!ll&&!rl&&r
000
not $self->req->header("X-Requested-With") and ($self->req->user_agent || "") =~ /android/i and defined $self->req->header("Cookie")
!ll&&!rl&&r
000
not $self->req->header("X-Requested-With") and ($self->req->user_agent || "") =~ /android/i
!ll&&!rl&&r
000
$self->req->user_agent || ''
l!l
00
$self->req->method || 'GET'
l!l
00
MC/DC0%0/4
not $self->req->header("X-Requested-With") and ($self->req->user_agent || "") =~ /android/i and defined $self->req->header("Cookie") and ($self->req->method || "GET") eq "GET"
not $self->req->header("X-Requested-With") $self->req->user_agent defined $self->req->header("Cookie") $self->req->method
Truth table0%0/5
not(A) and B and C and D
A= not $self->req->header("X-Requested-With")B= $self->req->user_agentC= defined $self->req->header("Cookie")D= $self->req->method
ABCDresult
0XXX0
10XX0
110X0
11100
11111
79 ($self->req->user_agent||'') =~ /android/i &&
80 defined $self->req->header('Cookie') &&
81 ($self->req->method||'GET') eq 'GET'
82 ) {
83 0 $self->halt(403,"Your request is maybe JSON hijacking.\nIf you are not a attacker, please add 'X-Requested-With' header to each request.");
84 }
85  
86 0 my $body = $_JSON->encode($obj);
87 0 $body = $self->escape_json($body);
88  
89 0 ▶ if ( ( $self->req->user_agent || '' ) =~ m/Safari/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->req->user_agent || "") =~ /Safari/) 0 0 0
Condition0%0/2
$self->req->user_agent || ''
l!l
00
MC/DC0%0/1
$self->req->user_agent || ''
$self->req->user_agent
Truth table0%0/2
A
A= $self->req->user_agent
Aresult
11
00
90 0 $body = "\xEF\xBB\xBF" . $body;
91 }
92  
93 0 $self->res->status( 200 );
94 0 $self->res->content_type('application/json; charset=UTF-8');
95 0 $self->res->header( 'X-Content-Type-Options' => 'nosniff' ); # defense from XSS
96 0 $self->res->body( $body );
97 0 $self->res;
98 }
99  
100  
101  
102 6 1;
103  
↑ Summary blib/lib/Kossy.pm »