blib/lib/Dancer2/Serializer/YAML.pm

filter: Statementstmt 100% 36 / 36 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 66.6% 2 / 3 totaltotal 98.0% 51 / 52 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Dancer2/Logger/Capture.pm ↑ Summary blib/lib/Dancer2/Core/Role/Engine.pm »
1 package Dancer2::Serializer::YAML;
2 # ABSTRACT: Serializer for handling YAML data
3 1 $Dancer2::Serializer::YAML::VERSION = '2.2.1';
4 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Sub::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 with 'Dancer2::Core::Role::Serializer';
11  
12 4 ▶has '+content_type' => ( default => sub {'text/x-yaml'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 # deferred helpers. These are called as class methods, but need to
15 # ensure YAML is loaded.
16  
17 my $_from_yaml = defer_sub 'Dancer2::Serializer::YAML::from_yaml' => sub {
18 4 ▶ use_module('YAML');
Subroutine __ANON__: called CC=1 SCAR=0.0
19 4 ▶ sub { __PACKAGE__->deserialize(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 };
21  
22 my $_to_yaml = defer_sub 'Dancer2::Serializer::YAML::to_yaml' => sub {
23 4 ▶ use_module('YAML');
Subroutine __ANON__: called CC=1 SCAR=0.0
24 4 ▶ sub { __PACKAGE__->serialize(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 };
26  
27 # class definition
28 4 ▶sub BUILD { use_module('YAML') }
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
29  
30 sub serialize {
31 27 ▶ my ( $self, $entity ) = @_;
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
32 27 encode('UTF-8', YAML::Dump($entity));
33 }
34  
35 sub deserialize {
36 18 ▶ my ( $self, $content ) = @_;
Subroutine deserialize: called CC=1 SCAR=0.0
Pod documented
37  
38 # Content reaching here is untrusted -- for an app with 'serializer: YAML'
39 # (or Serializer::Mutable, which maps both text/x-yaml and text/html to
40 # this class) it is the raw request body.
41 #
42 # YAML tags can ask the loader to build things that are not data.
43 # !!perl/hash:Some::Class instantiates an arbitrary blessed object, which
44 # is the entry point for DESTROY/AUTOLOAD gadget chains, and !!perl/code
45 # asks for a string eval. Both are refused here.
46 #
47 # These are set explicitly rather than left to YAML.pm's defaults so the
48 # behaviour does not depend on which YAML.pm the user resolved: LoadBlessed
49 # only defaults to 0 from YAML 1.30, and the variable itself only exists
50 # from 1.25 (which is why cpanfile floors YAML -- see the note there).
51 #
52 # UseCode must be zeroed as well: YAML::Loader::Base decides on code
53 # loading with a plain OR -- load_code($YAML::LoadCode || $YAML::UseCode) --
54 # so LoadCode = 0 alone is defeated by an ambient $YAML::UseCode = 1, and
55 # the !!perl/code string eval is not gated on LoadBlessed at all.
56 18 local $YAML::LoadBlessed = 0;
57 18 local $YAML::LoadCode = 0;
58 18 local $YAML::UseCode = 0;
59  
60 18 YAML::Load(decode('UTF-8', $content));
61 }
62  
63 1 1;
64  
65 __END__
« blib/lib/Dancer2/Logger/Capture.pm ↑ Summary blib/lib/Dancer2/Core/Role/Engine.pm »