1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
commit faebad54455c2c2919e234202362570925fb99d1
Author: Theo van Hoesel <tvanhoesel@perceptyx.com>
Date: Tue Jun 21 20:30:36 2022 +0000
Add new test for Content-Length issues
prove we fixed CVE-2022-31081
diff --git a/t/content_length.t b/t/content_length.t
new file mode 100644
index 0000000..1751845
--- /dev/null
+++ b/t/content_length.t
@@ -0,0 +1,278 @@
+use strict;
+use warnings;
+
+use Test::More 0.98;
+
+use Config;
+
+use HTTP::Daemon;
+use HTTP::Response;
+use HTTP::Status;
+use HTTP::Tiny 0.042;
+
+patch_http_tiny(); # do not fix Content-Length, we want to forge something bad
+
+plan skip_all => "This system cannot fork" unless can_fork();
+
+my $BASE_URL;
+my @TESTS = get_tests();
+
+for my $test (@TESTS) {
+
+ my $http_daemon = HTTP::Daemon->new() or die "HTTP::Daemon->new: $!";
+ $BASE_URL = $http_daemon->url;
+
+ my $pid = fork;
+ die "fork: $!" if !defined $pid;
+ if ($pid == 0) {
+ accept_requests($http_daemon);
+ }
+
+ my $resp = http_test_request($test);
+
+ ok $resp, $test->{title};
+
+ is $resp->{status}, $test->{status},
+ "... and has expected status";
+
+ like $resp->{content}, $test->{like},
+ "... and body does match"
+ if $test->{like};
+
+}
+
+done_testing;
+
+
+
+sub get_tests{
+ {
+ title => "Hello World Request ... it works as expected",
+ path => "hello-world",
+ status => 200,
+ like => qr/^Hello World$/,
+ },
+ {
+ title => "Positive Content Length",
+ method => "POST",
+ headers => {
+ 'Content-Length' => '+1', # quotes are needed to retain plus-sign
+ },
+ status => 400,
+ like => qr/value must be an unsigned integer/,
+ },
+ {
+ title => "Negative Content Length",
+ method => "POST",
+ headers => {
+ 'Content-Length' => '-1',
+ },
+ status => 400,
+ like => qr/value must be an unsigned integer/,
+ },
+ {
+ title => "Non Integer Content Length",
+ method => "POST",
+ headers => {
+ 'Content-Length' => '3.14',
+ },
+ status => 400,
+ like => qr/value must be an unsigned integer/,
+ },
+ {
+ title => "Explicit Content Length ... with exact length",
+ method => "POST",
+ headers => {
+ 'Content-Length' => '8',
+ },
+ body => "ABCDEFGH",
+ status => 200,
+ like => qr/^ABCDEFGH$/,
+ },
+ {
+ title => "Implicit Content Length ... will always pass",
+ method => "POST",
+ body => "ABCDEFGH",
+ status => 200,
+ like => qr/^ABCDEFGH$/,
+ },
+ {
+ title => "Shorter Content Length ... gets truncated",
+ method => "POST",
+ headers => {
+ 'Content-Length' => '4',
+ },
+ body => "ABCDEFGH",
+ status => 200,
+ like => qr/^ABCD$/,
+ },
+ {
+ title => "Different Content Length ... must fail",
+ method => "POST",
+ headers => {
+ 'Content-Length' => ['8', '4'],
+ },
+ body => "ABCDEFGH",
+ status => 400,
+ like => qr/values are not the same/,
+ },
+ {
+ title => "Underscore Content Length ... must match",
+ method => "POST",
+ headers => {
+ 'Content_Length' => '4',
+ },
+ body => "ABCDEFGH",
+ status => 400,
+ like => qr/values are not the same/,
+ },
+ {
+ title => "Longer Content Length ... gets timeout",
+ method => "POST",
+ headers => {
+ 'Content-Length' => '9',
+ },
+ body => "ABCDEFGH",
+ status => 599, # silly code !!!
+ like => qr/^Timeout/,
+ },
+
+}
+
+
+
+sub router_table {
+ {
+ '/hello-world' => {
+ 'GET' => sub {
+ my $resp = HTTP::Response->new(200);
+ $resp->content('Hello World');
+ return $resp;
+ },
+ },
+
+ '/' => {
+ 'POST' => sub {
+ my $rqst = shift;
+
+ my $body = $rqst->content();
+
+ my $resp = HTTP::Response->new(200);
+ $resp->content($body);
+
+ return $resp
+ },
+ },
+ }
+}
+
+
+
+sub can_fork {
+ $Config{d_fork} || (($^O eq 'MSWin32' || $^O eq 'NetWare')
+ and $Config{useithreads}
+ and $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/);
+}
+
+
+
+# run the mini HTTP dispatcher that can handle various routes / methods
+sub accept_requests{
+ my $http_daemon = shift;
+ while (my $conn = $http_daemon->accept) {
+ while (my $rqst = $conn->get_request) {
+ if (my $resp = dispatch_request($rqst)) {
+ $conn->send_response($resp);
+ }
+ }
+ $conn->close;
+ undef($conn);
+ $http_daemon->close;
+ exit 1;
+ }
+}
+
+
+
+sub dispatch_request{
+ my $rqst = shift
+ or return;
+ my $path = $rqst->uri->path
+ or return;
+ my $meth = $rqst->method
+ or return;
+ my $code = router_table()->{$path}{$meth}
+ or return HTTP::Response->new(RC_NOT_FOUND);
+ my $resp = $code->($rqst);
+ return $resp;
+}
+
+
+
+sub http_test_request {
+ my $test = shift;
+ my $http_client = HTTP::Tiny->new(
+ timeout => 5,
+ proxy => undef,
+ http_proxy => undef,
+ https_proxy => undef,
+ );
+ my $resp;
+ eval {
+ local $SIG{ALRM} = sub { die "Timeout\n" };
+ alarm 2;
+ $resp = $http_client->request(
+ $test->{method} || "GET",
+ $BASE_URL . ($test->{path} || ""),
+ {
+ headers => $test->{headers},
+ content => $test->{body}
+ },
+ );
+ };
+ my $err = $@;
+ alarm 0;
+ diag $err if $err;
+
+ return $resp
+}
+
+
+
+sub patch_http_tiny {
+
+ # we need to patch write_content_body
+ # this is part of HTTP::Tiny internal module HTTP::Tiny::Handle
+ #
+ # the below code is from the original HTTP::Tiny module, where just two lines
+ # have been commented out
+
+ no strict 'refs';
+
+ *HTTP::Tiny::Handle::write_content_body = sub {
+ @_ == 2 || die(q/Usage: $handle->write_content_body(request)/ . "\n");
+ my ($self, $request) = @_;
+
+ my ($len, $content_length) = (0, $request->{headers}{'content-length'});
+ while () {
+ my $data = $request->{cb}->();
+
+ defined $data && length $data
+ or last;
+
+ if ( $] ge '5.008' ) {
+ utf8::downgrade($data, 1)
+ or die(qq/Wide character in write_content()\n/);
+ }
+
+ $len += $self->write($data);
+ }
+
+# this should not be checked during our tests, we want to forge bad requests
+#
+# $len == $content_length
+# or die(qq/Content-Length mismatch (got: $len expected: $content_length)\n/);
+
+ return $len;
+ };
+}
|