« 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 19 | $output =~ s!([+<>])!$_ESCAPE{$1}!g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 19 | ▶ | my $user_agent = $c->req->user_agent || ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$c->req->user_agent || ''
MC/DC100%1/1
$c->req->user_agent || ''
$c->req->user_agent
Truth table100%2/2
A
A= $c->req->user_agent
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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"
not $c->request->header("X-Requested-With") and $user_agent =~ /android/i and defined $c->req->header("Cookie")
not $c->request->header("X-Requested-With") and $user_agent =~ /android/i
$c->req->method || 'GET'
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 6 | ▶ | $res->header( 'X-API-Status' => $stuff->{$status_code_field} ) if exists $stuff->{$status_code_field}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 18 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||