git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Denton Liu <liu.denton@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Cc: Brandon Williams <bmwill@google.com>, Jeff King <peff@peff.net>,
	Jonathan Tan <jonathantanmy@google.com>
Subject: [RFC PATCH] http-backend: write error packet if backend command fails
Date: Fri, 27 Mar 2020 22:50:32 -0400	[thread overview]
Message-ID: <b5f8b81498e1d152014acab92fa1b6e9701b3a0e.1585363771.git.liu.denton@gmail.com> (raw)

If one tries to fetch packs with an incorrectly formatted parameter
while using the smart HTTP protocol v2, the client will block forever.
This is seen with the following command which does not terminate:

	$ git -c protocol.version=2 clone https://github.com/git/git.git --shallow-since=20151012
	Cloning into 'git'...

Meanwhile, if one uses v1, the command will terminate as expected:

	$ git -c protocol.version=1 clone https://github.com/git/git.git --shallow-since=20151012
	Cloning into 'git'...
	fatal: the remote end hung up unexpectedly

This happens because when upload-pack detects invalid parameters, it
will die(). When http-backend calls finish_command() and detects a
non-zero exit code, it will simply call exit(1). Since there is no way
in a CGI script to force a connection to terminate, the client keeps
blocking on the read(), expecting more output.

Write an error packet if the backend command fails to start or finishes
with an error so that the client can terminate the connection.

Note that if the included test case is run without the remainder of the
patch, it will run forever and fail to terminate.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---

Notes:
    So this is the first time I've touched the networking code of Git. There
    are a few problems with this patch that I'd appreciate some help
    addressing.
    
    First of all, is this even the right approach? I tried to find some
    other way to force a CGI script to terminate a connection but I don't
    think that it's possible so I had to get the client to do it instead.
    
    Next, with this patch applied it seems like t5539-fetch-http-shallow
    fails on 'no shallow lines after receiving ACK ready'. I've been trying
    to dig into why this happens but I can't quite figure it out.
    
    Finally, I'd like the test case I added in t5702 to be guarded by some
    timeout in case we regress instead of blocking the test suite forever
    but I'm not sure if that's even available functionality. It seems like
    we don't use the `timeout` program yet so I'm not sure if it's a good
    idea to introduce it here.
    
    Thanks,
    
    Denton

 http-backend.c         | 10 +++++++---
 t/t5702-protocol-v2.sh | 17 +++++++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/http-backend.c b/http-backend.c
index ec3144b444..7ea3a688fd 100644
--- a/http-backend.c
+++ b/http-backend.c
@@ -488,10 +488,11 @@ static void run_service(const char **argv, int buffer_input)
 	cld.git_cmd = 1;
 	cld.clean_on_exit = 1;
 	cld.wait_after_clean = 1;
-	if (start_command(&cld))
+	if (start_command(&cld)) {
+		packet_write_fmt(1, "ERR %s failed to start\n", argv[0]);
 		exit(1);
+	}
 
-	close(1);
 	if (gzipped_request)
 		inflate_request(argv[0], cld.in, buffer_input, req_len);
 	else if (buffer_input)
@@ -501,8 +502,11 @@ static void run_service(const char **argv, int buffer_input)
 	else
 		close(0);
 
-	if (finish_command(&cld))
+	if (finish_command(&cld)) {
+		packet_write_fmt(1, "ERR %s failed\n", argv[0]);
 		exit(1);
+	}
+	close(1);
 }
 
 static int show_text_ref(const char *name, const struct object_id *oid,
diff --git a/t/t5702-protocol-v2.sh b/t/t5702-protocol-v2.sh
index 5039e66dc4..69a920a34b 100755
--- a/t/t5702-protocol-v2.sh
+++ b/t/t5702-protocol-v2.sh
@@ -586,6 +586,23 @@ test_expect_success 'clone with http:// using protocol v2' '
 	! grep "Send header: Transfer-Encoding: chunked" log
 '
 
+test_expect_success 'clone with http:// using protocol v2 and invalid parameters' '
+	test_when_finished "rm -f log" &&
+
+	test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" \
+		git -c protocol.version=2 \
+		clone --shallow-since=20151012 "$HTTPD_URL/smart/http_parent" http_child_invalid &&
+
+	# Client requested to use protocol v2
+	grep "Git-Protocol: version=2" log &&
+	# Server responded using protocol v2
+	grep "git< version 2" log &&
+	# Verify that we sucessfully errored out
+	grep -F "ERR upload-pack failed" log &&
+	# Verify that the chunked encoding sending codepath is NOT exercised
+	! grep "Send header: Transfer-Encoding: chunked" log
+'
+
 test_expect_success 'clone big repository with http:// using protocol v2' '
 	test_when_finished "rm -f log" &&
 
-- 
2.26.0.159.g23e2136ad0


             reply	other threads:[~2020-03-28  2:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-28  2:50 Denton Liu [this message]
2020-03-28 14:57 ` [RFC PATCH] http-backend: write error packet if backend command fails Jeff King
2020-03-28 15:13   ` Jeff King
2020-03-28 15:49     ` Jeff King
2020-03-29  5:34       ` Denton Liu

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: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=b5f8b81498e1d152014acab92fa1b6e9701b3a0e.1585363771.git.liu.denton@gmail.com \
    --to=liu.denton@gmail.com \
    --cc=bmwill@google.com \
    --cc=git@vger.kernel.org \
    --cc=jonathantanmy@google.com \
    --cc=peff@peff.net \
    /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/mirrors/git.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).