Debian Codegraph @

t/integration/serializer/yaml.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Plack::Test;
6 use HTTP::Request;
7 use Scalar::Util 'blessed';
8
9 use Dancer2::Serializer::YAML;
10
11 # YAML tags can ask the loader to build things that are not data. The two that
12 # matter for a serializer fed untrusted request bodies are:
13 #
14 # !!perl/hash:Some::Class instantiate an arbitrary blessed object, which is
15 # the entry point for DESTROY/AUTOLOAD gadget chains
16 # !!perl/code string-eval a sub body
17 #
18 # Dancer2::Serializer::YAML::deserialize refuses both by setting
19 # $YAML::LoadBlessed, $YAML::LoadCode and $YAML::UseCode to 0 itself, rather
20 # than relying on YAML.pm's defaults. That distinction is the point of these
21 # tests: the variables are localised inside deserialize, so the guarantee has
22 # to hold even when the surrounding process has set them to something hostile.
23 # Each test below therefore sets the ambient values to 1 first -- which is what
24 # YAML.pm older than 1.30 does by default, and what Dancer2::Session::YAML
25 # sets for its own load -- and asserts the serializer still refuses.
26 #
27 # UseCode has to be guarded too: YAML::Loader::Base decides on code loading
28 # with a plain OR -- load_code($YAML::LoadCode || $YAML::UseCode) -- so an
29 # ambient $YAML::UseCode = 1 defeats LoadCode = 0 on its own, and the
30 # !!perl/code string eval is not gated on LoadBlessed at all. Hence the
31 # code-eval subtest below, with a side-effecting payload as its control.
32 #
33 # The assertions deliberately test only the security property -- "no
34 # Attacker::Gadget object comes back" -- and not the exact shape of what does.
35 # That shape is YAML-version-dependent: with LoadBlessed off, YAML 1.30 and
36 # earlier silently strip the tag and return a plain hash, while newer YAML
37 # rejects the document outright (which deserialize turns into undef for an
38 # engine instance). Both satisfy the guarantee; pinning either one made the
39 # test pass on the author's YAML and fail on CI's.
40 #
41 # For the same reason every deserialize below is called on an *instance*, which
42 # is how the framework itself always calls it. A class-method call routes a
43 # rejection through Dancer2::Core::Role::Serializer's error handler, which is
44 # not class-safe (see GH #1837) and dies with an unrelated internal error --
45 # noise that has nothing to do with what these tests are about.
46
47 my $blessed_payload = qq{--- !!perl/hash:Attacker::Gadget\nfoo: bar\n};
48
49 my $serializer = Dancer2::Serializer::YAML->new( log_cb => sub {} );
50
51 subtest 'a blessing tag never yields the attacker-named object' => sub {
52 no warnings 'once';
53 local $YAML::LoadBlessed = 1; # hostile ambient values
54 local $YAML::LoadCode = 1;
55 local $YAML::UseCode = 1;
56
57 my $data = eval { $serializer->deserialize($blessed_payload) };
58
59 ok !( blessed($data) && $data->isa('Attacker::Gadget') ),
60 'deserialize did not return an Attacker::Gadget object'
61 or diag "got: " . ( blessed($data) || ref($data) || 'undef' );
62
63 # Sanity check on the payload itself: with the guard bypassed it really
64 # does bless. Without this, the assertion above would also pass on a YAML
65 # that could not bless at all, and would prove nothing.
66 my $direct = eval { YAML::Load($blessed_payload) };
67 ok( blessed($direct) && $direct->isa('Attacker::Gadget'),
68 'control: the payload blesses when passed straight to YAML::Load' )
69 or diag "control got: " . ( blessed($direct) || ref($direct) || 'undef' );
70 };
71
72 subtest 'the localisation does not leak' => sub {
73 no warnings 'once';
74 local $YAML::LoadBlessed = 1;
75 local $YAML::LoadCode = 1;
76 local $YAML::UseCode = 1;
77 eval { $serializer->deserialize($blessed_payload) };
78 is $YAML::LoadBlessed, 1,
79 'an ambient LoadBlessed is restored after deserialize returns';
80 is $YAML::LoadCode, 1,
81 'an ambient LoadCode is restored after deserialize returns';
82 is $YAML::UseCode, 1,
83 'an ambient UseCode is restored after deserialize returns';
84 };
85
86 subtest 'ordinary YAML still round-trips' => sub {
87 my $data = { name => 'dancer', list => [ 1, 2, 3 ], nested => { a => 'b' } };
88 my $out = $serializer->deserialize( $serializer->serialize($data) );
89
90 is_deeply $out, $data, 'a normal structure survives a round trip unchanged';
91
92 is_deeply(
93 $serializer->deserialize("---\n- one\n- two\n"),
94 [ 'one', 'two' ],
95 'a top-level sequence still loads',
96 );
97 };
98
99 {
100 my $code_payload = qq{--- !!perl/code \x27evil { BEGIN { \$::yaml_code_ran = 1 } }\x27\n};
101
102 subtest 'a code tag never evaluates the payload' => sub {
103 # YAML::Loader::Base decides on code loading with load_code($LoadCode
104 # || $UseCode), so with both ambient values hostile -- and even
105 # LoadBlessed hostile, which would be irrelevant here anyway -- a
106 # direct YAML::Load string-evals the payload. The serializer must not.
107 no warnings 'once';
108 local $YAML::LoadBlessed = 1;
109 local $YAML::LoadCode = 1;
110 local $YAML::UseCode = 1;
111
112 undef $::yaml_code_ran;
113 {
114 local $SIG{__WARN__} = sub {};
115 eval { YAML::Load($code_payload) };
116 }
117 ok $::yaml_code_ran,
118 'control: the payload is evaluated when passed straight to YAML::Load'
119 or diag 'attacker code was not evaluated, cannot prove the guard matters';
120
121 undef $::yaml_code_ran;
122 eval { $serializer->deserialize($code_payload) };
123 ok !$::yaml_code_ran,
124 'deserialize did not evaluate the supplied code'
125 or diag 'attacker code ran during deserialize';
126 };
127 }
128
129 {
130 package YAMLApp;
131 use Dancer2;
132 set logger => 'null';
133 set serializer => 'YAML';
134
135 post '/echo' => sub {
136 my $body = request->data;
137 return {
138 # fully qualified: Scalar::Util is not imported into this package
139 blessed_into => Scalar::Util::blessed($body) || '',
140 ref => ref($body) || 'none',
141 };
142 };
143 }
144
145 subtest 'a blessing payload sent as a request body never blesses' => sub {
146 my $app = YAMLApp->to_app;
147
148 # As above, the ambient values are made hostile first; Plack::Test runs the
149 # app in this process, so this localisation is what the serializer sees.
150 no warnings 'once';
151 local $YAML::LoadBlessed = 1;
152 local $YAML::LoadCode = 1;
153 local $YAML::UseCode = 1;
154
155 test_psgi $app, sub {
156 my $cb = shift;
157
158 my $req = HTTP::Request->new(
159 POST => '/echo',
160 [ 'Content-Type' => 'text/x-yaml' ],
161 $blessed_payload,
162 );
163
164 my $res = $cb->($req);
165
166 # 200 (older YAML strips the tag, route sees a plain hash) or a clean
167 # 4xx/5xx (newer YAML rejects the document) are both acceptable. What
168 # must never happen is an Attacker::Gadget reaching the route or the
169 # class name surfacing in the response.
170 unlike $res->content, qr/Attacker::Gadget/,
171 'no blessed gadget leaked into the response';
172
173 if ( $res->code == 200 ) {
174 my $out = $serializer->deserialize( $res->content );
175 is $out->{blessed_into}, '',
176 'the route saw an unblessed value';
177 }
178 else {
179 ok $res->code >= 400,
180 'a rejected body produced an error status, not a gadget';
181 }
182 };
183 };
184
185 done_testing;