user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Cc: Eric Wong <e@80x24.org>
Subject: [PATCH 3/3] http: reject excessively large HTTP request bodies
Date: Sun,  6 Mar 2016 02:09:22 +0000	[thread overview]
Message-ID: <1457230162-10960-4-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1457230162-10960-1-git-send-email-e@80x24.org>

We cannot risk using all of a users' disk space buffering
gigantic requests.  Use the defaults git gives us since
we primarily host git repositories.
---
 lib/PublicInbox/HTTP.pm | 13 +++++++++++++
 t/httpd-corner.t        | 21 +++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 15db139..0675f6a 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -24,6 +24,12 @@ use constant {
 	CHUNK_MAX_HDR => 256,
 };
 
+# Use the same configuration parameter as git since this is primarily
+# a slow-client sponge for git-http-backend
+# TODO: support per-respository http.maxRequestBuffer somehow...
+our $MAX_REQUEST_BUFFER = $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} ||
+			(10 * 1024 * 1024);
+
 my $null_io = IO::File->new('/dev/null', '<');
 my $http_date;
 my $prev = 0;
@@ -232,6 +238,10 @@ sub input_prepare {
 	my $input = $null_io;
 	my $len = $env->{CONTENT_LENGTH};
 	if ($len) {
+		if ($len > $MAX_REQUEST_BUFFER) {
+			quit($self, 413);
+			return;
+		}
 		$input = IO::File->new_tmpfile;
 	} elsif (env_chunked($env)) {
 		$len = CHUNK_START;
@@ -306,6 +316,9 @@ sub event_read_input_chunked { # unlikely...
 		if ($len == CHUNK_START) {
 			if ($$rbuf =~ s/\A([a-f0-9]+).*?\r\n//i) {
 				$len = hex $1;
+				if (($len + -s $input) > $MAX_REQUEST_BUFFER) {
+					return quit($self, 413);
+				}
 			} elsif (length($$rbuf) > CHUNK_MAX_HDR) {
 				return quit($self, 400);
 			}
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 8670846..59f37aa 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -97,6 +97,27 @@ my $spawn_httpd = sub {
 	like($head, qr/\b400\b/, 'got 400 response');
 }
 
+{
+	my $conn = conn_for($sock, 'excessive body Content-Length');
+	$SIG{PIPE} = 'IGNORE';
+	my $n = (10 * 1024 * 1024) + 1;
+	$conn->write("PUT /sha1 HTTP/1.0\r\nContent-Length: $n\r\n\r\n");
+	ok($conn->read(my $buf, 8192), 'read response');
+	my ($head, $body) = split(/\r\n\r\n/, $buf);
+	like($head, qr/\b413\b/, 'got 413 response');
+}
+
+{
+	my $conn = conn_for($sock, 'excessive body chunked');
+	$SIG{PIPE} = 'IGNORE';
+	my $n = (10 * 1024 * 1024) + 1;
+	$conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: chunked\r\n");
+	$conn->write("\r\n".sprintf("%x\r\n", $n));
+	ok($conn->read(my $buf, 8192), 'read response');
+	my ($head, $body) = split(/\r\n\r\n/, $buf);
+	like($head, qr/\b413\b/, 'got 413 response');
+}
+
 # Unix domain sockets
 {
 	my $u = IO::Socket::UNIX->new(Type => SOCK_STREAM, Peer => $upath);
-- 
EW


      parent reply	other threads:[~2016-03-06  2:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-06  2:09 [PATCH 0/3] http: some DoS prevention Eric Wong
2016-03-06  2:09 ` [PATCH 1/3] http: reject excessive headers Eric Wong
2016-03-06  2:09 ` [PATCH 2/3] http: ensure errors are printable before PSGI env Eric Wong
2016-03-06  2:09 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1457230162-10960-4-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).