Skip to contentSkip to conversation

bundle-uri: do not die on unresolvable relative URIs

PATCH Superseded Aniket Gupta 4 participants 20 messages Nov 5 – Nov 23

lore
Share & tools
lore
#0 lore raw

v3 cover bundle-uri: do not die on unresolvable relative URIs

A bundle list can contain a relative URI that cannot be resolved, and today one such entry makes the whole clone die. This series lets the bundle code report it and skip that bundle instead.

Changes since v2:
- split into three patches
- reworded the commit message to describe relative_url() as a thin wrapper around relative_url_gently()
- the credentials test now compares the full error message
- the error names the URL that was passed in, not what is left of it after some components were stripped off

Aniket Gupta (3):
remote: add relative_url_gently()
remote: do not show credentials when relative_url() fails
bundle-uri: do not die on unresolvable relative URIs

 bundle-uri.c                | 10 +++++++++-
 remote.c                    | 37 +++++++++++++++++++++++++++++++------
 remote.h                    |  8 ++++++++
 t/t0060-path-utils.sh       |  9 +++++++++
 t/t5750-bundle-uri-parse.sh | 15 +++++++--------
 5 files changed, 64 insertions(+), 15 deletions(-)

base-commit: ebdc087dc99d68bb340c282b2ba5f722def28d19

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 remote: add relative_url_gently()

relative_url() calls die() when there are more "../" components in the URL than the base URL can absorb. A caller that would rather carry on has no way to do so.

Add relative_url_gently(), which returns NULL and adds the reason to a strbuf instead of dying. relative_url() is now a thin wrapper around it that continues to die, so existing callers behave the same.

---
 remote.c | 34 ++++++++++++++++++++++++++++------
 remote.h |  8 ++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)
remote.c +28 -6
index 49ee2ea318..899d805b76 100644
@@ -3005,7 +3005,8 @@ void remote_state_clear(struct remote_state *remote_state)
 }
 
 /*
- * Returns 1 if it was the last chop before ':'.
+ * Returns 1 if it was the last chop before ':', or -1 if there is
+ * nothing left to chop.
  */
 static int chop_last_dir(char **remoteurl, int is_relative)
 {
@@ -3022,16 +3023,15 @@ static int chop_last_dir(char **remoteurl, int is_relative)
 	}
 
 	if (is_relative || !strcmp(".", *remoteurl))
-		die(_("cannot strip one component off url '%s'"),
-			*remoteurl);
+		return -1;
 
 	free(*remoteurl);
 	*remoteurl = xstrdup(".");
 	return 0;
 }
 
-char *relative_url(const char *remote_url, const char *url,
-		   const char *up_path)
+char *relative_url_gently(const char *remote_url, const char *url,
+			  const char *up_path, struct strbuf *err)
 {
 	int is_relative = 0;
 	int colonsep = 0;
@@ -3073,8 +3073,19 @@ char *relative_url(const char *remote_url, const char *url,
 	 */
 	while (*url) {
 		if (starts_with_dot_dot_slash_native(url)) {
+			int chopped;
+
 			url += 3;
-			colonsep |= chop_last_dir(&remoteurl, is_relative);
+			chopped = chop_last_dir(&remoteurl, is_relative);
+			if (chopped < 0) {
+				strbuf_addf(err,
+					    _("cannot strip one component off url '%s'"),
+					    remoteurl);
+				free(remoteurl);
+				strbuf_release(&sb);
+				return NULL;
+			}
+			colonsep |= chopped;
 		} else if (starts_with_dot_slash_native(url))
 			url += 2;
 		else
@@ -3102,6 +3113,17 @@ char *relative_url(const char *remote_url, const char *url,
 	return strbuf_detach(&sb, NULL);
 }
 
+char *relative_url(const char *remote_url, const char *url,
+		   const char *up_path)
+{
+	struct strbuf err = STRBUF_INIT;
+	char *out = relative_url_gently(remote_url, url, up_path, &err);
+
+	if (!out)
+		die("%s", err.buf);
+	return out;
+}
+
 int valid_remote_name(const char *name)
 {
 	int result;
remote.h +8 -0
index cca02033b9..940474fa71 100644
@@ -478,6 +478,14 @@ void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
 char *relative_url(const char *remote_url, const char *url,
 		   const char *up_path);
 
+/*
+ * Like relative_url(), but if `url` cannot be resolved against
+ * `remote_url`, return NULL and add the reason to `err` instead of
+ * dying. `err` must not be NULL.
+ */
+char *relative_url_gently(const char *remote_url, const char *url,
+			  const char *up_path, struct strbuf *err);
+
 int valid_remote_name(const char *name);
 
 #endif
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/3] remote: add relative_url_gently()

Quoted 3 lines · Aniket Gupta

relative_url() calls die() when there are more "../" components in the URL than the base URL can absorb. A caller that would rather carry on has no way to do so.

I agree that we should have a mechanism to allow users to know about a broken bundle URI and to respond somehow.

Quoted 3 lines

Add relative_url_gently(), which returns NULL and adds the reason to a strbuf instead of dying. relative_url() is now a thin wrapper around it that continues to die, so existing callers behave the same.

This method is good to have, in general, and is written well.

The only question here is if we should be changing the behavior for bundle-URI clones from halting on bad data to skipping bundles and doing the full operation from the remote.

(But there's more about this in patch 3, so I'll defer until then.)

Thanks,
-Stolee

#3 lore raw

PATCH v3 2/3 remote: do not show credentials when relative_url() fails

In reply to Aniket Gupta

When relative_url() cannot resolve a URL, it names the URL in its error message. If the URL contains a user name and password, both end up in the message, and from there in terminal output and logs.

Pass the URL through transport_anonymize_url() first. Also name the URL that was passed in rather than what is left of it after some components have already been stripped off, which is what a user would recognize.

---
 remote.c              | 5 ++++-
 t/t0060-path-utils.sh | 9 +++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
remote.c +4 -1
index 899d805b76..36834b129c 100644
@@ -3078,9 +3078,12 @@ char *relative_url_gently(const char *remote_url, const char *url,
 			url += 3;
 			chopped = chop_last_dir(&remoteurl, is_relative);
 			if (chopped < 0) {
+				char *anon = transport_anonymize_url(remote_url);
+
 				strbuf_addf(err,
 					    _("cannot strip one component off url '%s'"),
-					    remoteurl);
+					    anon);
+				free(anon);
 				free(remoteurl);
 				strbuf_release(&sb);
 				return NULL;
t/t0060-path-utils.sh +9 -0
index 56faf5fe73..cea0d13429 100755
@@ -394,6 +394,15 @@ test_submodule_relative_url "../" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
 test_submodule_relative_url "../" "foo/bar" "../submodule" "../foo/submodule"
 test_submodule_relative_url "../" "foo" "../submodule" "../submodule"
 
+test_expect_success 'relative_url() does not show credentials when it fails' '
+	cat >expect <<-\EOF &&
+	fatal: cannot strip one component off url '\''https://host.example/a'\''
+	EOF
+	test_must_fail test-tool submodule resolve-relative-url "" \
+		"https://user:[email protected]/a" ../../../../../../x 2>actual &&
+	test_cmp expect actual
+'
+
 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c" "../foo/sub/a/b/c"
 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c/" "../foo/sub/a/b/c"
 test_submodule_relative_url "(null)" "../foo/bar/" "../sub/a/b/c" "../foo/sub/a/b/c"
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v3 2/3] remote: do not show credentials when relative_url() fails

Quoted 8 lines · Aniket Gupta

When relative_url() cannot resolve a URL, it names the URL in its error message. If the URL contains a user name and password, both end up in the message, and from there in terminal output and logs.

Pass the URL through transport_anonymize_url() first. Also name the URL that was passed in rather than what is left of it after some components have already been stripped off, which is what a user would recognize.

I'm less convinced on this factor. I understand anonymizing the URL if we are sending it to a location that is perhaps "less trusted" (like trace logs) but the user asked us to operate on a given URL that caused problems, so they should know the string that caused problems.

In environments that store stdout/stderr logs, those systems already need to be careful about such strings in the commands themselves, let alone the output strings.

And back to the bundle-URI application: if the bundle list uses such URIs for its child bundles, then that's not a safe mechanism for communicating those credentials. This isn't going to protect secrets like that from being exposed.

This is just my opinion on the necessity of this change. If we _do_ want this change, then I continued my review: the patch accomplishes its goal.

remote.c char *relative_url_gently(const char *remote_url, const char *url, · line 3087

Quoted 12 lines
remote.c +4 -1
@@ -3078,9 +3078,12 @@ char *relative_url_gently(const char *remote_url, const char *url,
 			url += 3;
 			chopped = chop_last_dir(&remoteurl, is_relative);
 			if (chopped < 0) {
+				char *anon = transport_anonymize_url(remote_url);
+
 				strbuf_addf(err,
 					    _("cannot strip one component off url '%s'"),
-					    remoteurl);
+					    anon);
+				free(anon);
 				free(remoteurl);

This code looks good, assuming taht we want to make this change.

t/t0060-path-utils.sh test_submodule_relative_url "../" "$(pwd)/addtest" "../repo" "$(pwd)/repo" · line 404

Quoted 8 lines
t/t0060-path-utils.sh +8 -0
+test_expect_success 'relative_url() does not show credentials when it fails' '
+	cat >expect <<-\EOF &&
+	fatal: cannot strip one component off url '\''https://host.example/a'\''
+	EOF
+	test_must_fail test-tool submodule resolve-relative-url "" \
+		"https://user:[email protected]/a" ../../../../../../x 2>actual &&
+	test_cmp expect actual
+'

Thank you for including a test that demonstrates this change.

Thanks,
-Stolee

#5 lore raw

Re: [PATCH v3 2/3] remote: do not show credentials when relative_url() fails

Quoted 3 lines · Derrick Stolee

In environments that store stdout/stderr logs, those systems already need to be careful about such strings in the commands themselves, let alone the output strings.

Hmph, is this true? How can these systems be _careful_ without knowing what these strings are? By guessing that https:// is a common enough thing that shoud raise red flag if there is some:thing@ that follows it?

Quoted 4 lines

And back to the bundle-URI application: if the bundle list uses such URIs for its child bundles, then that's not a safe mechanism for communicating those credentials. This isn't going to protect secrets like that from being exposed.

This is very true. So the patch may be protecting us against an issue that does not exist in practice?

Where do we use anonymize_url() in existing code paths?

- "git clone" leaving "clone: from X" in the reflog
- "git fetch" showing "From X" to show ref updates
- "git push" shwoing "Pushing to X" in verbose output
- remote-curl talking about "url" in die() messages
- transport showing "To X" to show push_report.

These are all talking about what is shown on the screen except for the reflog entry left by "git clone", I think.

Thanks.

#6 lore raw

Re: [PATCH v3 2/3] remote: do not show credentials when relative_url() fails

In reply to Derrick Stolee
Quoted 4 lines

I'm less convinced on this factor. I understand anonymizing the URL if we are sending it to a location that is perhaps "less trusted" (like trace logs) but the user asked us to operate on a given URL that caused problems, so they should know the string that caused problems.

I answered this on the cover letter by mistake. Here it is again. Apologies for the spam.

Fair point. I tried it with a password in the remote URL but the "failed to download bundle from URI" warning prints it as is. I think this patch will only cover this one message. The reason I did it is that git already hides the password when it shows a remote URL. For eg. in the "From" line of fetch. It is a separate patch so it is easy to drop if you and Patrick think it is not worth it.

#7 lore raw

PATCH v3 3/3 bundle-uri: do not die on unresolvable relative URIs

In reply to Aniket Gupta

A bundle list, either advertised by a server or given to "git clone --bundle-uri", may contain relative URIs. They are resolved with relative_url(), which calls die() when there are more "../" components than the base can absorb, so one bad entry aborts the whole clone.

Resolve them with relative_url_gently() instead. When that fails, bundle_list_update() reports the problem as an error and leaves the bundle without a URI. The download code already skips such a bundle, so the clone carries on with the others.

This resolves the TODO in t5750, so update the test to match.

---
 bundle-uri.c                | 10 +++++++++-
 t/t5750-bundle-uri-parse.sh | 15 +++++++--------
 2 files changed, 16 insertions(+), 9 deletions(-)
bundle-uri.c +9 -1
index 2bb2eb99e4..1f198ef9a9 100644
@@ -210,9 +210,17 @@ static int bundle_list_update(const char *key, const char *value,
 	strbuf_release(&id);
 
 	if (!strcmp(subkey, "uri")) {
+		struct strbuf err = STRBUF_INIT;
+
 		if (bundle->uri)
 			return -1;
-		bundle->uri = relative_url(list->baseURI, value, NULL);
+		bundle->uri = relative_url_gently(list->baseURI, value, NULL,
+						  &err);
+		if (!bundle->uri) {
+			error("%s", err.buf);
+			strbuf_release(&err);
+			return -1;
+		}
 		return 0;
 	}
 
t/t5750-bundle-uri-parse.sh +7 -8
index 70ae94c4d0..8abf8f31ae 100755
@@ -68,17 +68,16 @@ test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' '
 		uri = <uri>/bundle.bdl
 	[bundle "two"]
 		uri = bundle.bdl
-	[bundle "three"]
-		uri = <uri>/../bundle.bdl
 	EOF
 
-	# TODO: We would prefer if parsing a bundle list would not cause
-	# a die() and instead would give a warning and allow the rest of
-	# a Git command to continue. This test_must_fail is necessary for
-	# now until the interface for relative_url() allows for reporting
-	# an error instead of die()ing.
+	cat >err.expect <<-\EOF &&
+	error: cannot strip one component off url '\''<uri>'\''
+	error: bad line: '\''bundle.three.uri=../../bundle.bdl'\''
+	EOF
+
 	test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
-	test_grep "fatal: cannot strip one component off url" err
+	test_cmp err.expect err &&
+	test_cmp_config_output expect actual
 '
 
 test_expect_success 'bundle_uri_parse_line() parsing edge cases: empty key or value' '
Signature · 1 line
2.43.0
#8 lore raw
Quoted 11 lines · Aniket Gupta

A bundle list, either advertised by a server or given to "git clone --bundle-uri", may contain relative URIs. They are resolved with relative_url(), which calls die() when there are more "../" components than the base can absorb, so one bad entry aborts the whole clone.

Resolve them with relative_url_gently() instead. When that fails, bundle_list_update() reports the problem as an error and leaves the bundle without a URI. The download code already skips such a bundle, so the clone carries on with the others.

This resolves the TODO in t5750, so update the test to match.

This TODO was added in ebc39479558 (bundle-uri: allow relative URLs in bundle lists, 2022-12-22) by _me_, so clearly I thought that this was a good change at the time. I should retract my concerns about the change from patch 1.

The only thing that is different now is that the feature has existed for a while, so maybe some depend on this behavior that we're changing. That's unlikely to be important.

So I'll officially say: thanks for contributing this cleanup!

Quoted 24 lines
bundle-uri.c +9 -1
@@ -210,9 +210,17 @@ static int bundle_list_update(const char *key, const char *value,
 	strbuf_release(&id);
 
 	if (!strcmp(subkey, "uri")) {
+		struct strbuf err = STRBUF_INIT;
+
 		if (bundle->uri)
 			return -1;
-		bundle->uri = relative_url(list->baseURI, value, NULL);
+		bundle->uri = relative_url_gently(list->baseURI, value, NULL,
+						  &err);
+		if (!bundle->uri) {
+			error("%s", err.buf);
+			strbuf_release(&err);
+			return -1;
+		}
 		return 0;
 	}
 
t/t5750-bundle-uri-parse.sh +0 -0
index 70ae94c4d0..8abf8f31ae 100755

t/t5750-bundle-uri-parse.sh test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' ' · line 71

Quoted 6 lines
t/t5750-bundle-uri-parse.sh +0 -2
@@ -68,17 +68,16 @@ test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' '
 		uri = <uri>/bundle.bdl
 	[bundle "two"]
 		uri = bundle.bdl
-	[bundle "three"]
-		uri = <uri>/../bundle.bdl
 	EOF

In the context of this diff, this looks like you are removing the bad input, but this is actually the expected output of the test-tool.

The input is a few lines earlier:

	cat >in <<-\EOF &&
	bundle.one.uri=bundle.bdl
	bundle.two.uri=../bundle.bdl
	bundle.three.uri=../../bundle.bdl
	EOF

t/t5750-bundle-uri-parse.sh test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' ' · line 77

Quoted 11 lines
t/t5750-bundle-uri-parse.sh +5 -5
-	# TODO: We would prefer if parsing a bundle list would not cause
-	# a die() and instead would give a warning and allow the rest of
-	# a Git command to continue. This test_must_fail is necessary for
-	# now until the interface for relative_url() allows for reporting
-	# an error instead of die()ing.
+	cat >err.expect <<-\EOF &&
+	error: cannot strip one component off url '\''<uri>'\''
+	error: bad line: '\''bundle.three.uri=../../bundle.bdl'\''
+	EOF
+

But this err.expect matches.

t/t5750-bundle-uri-parse.sh test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' ' · line 80

Quoted 4 lines
t/t5750-bundle-uri-parse.sh +2 -1
 	test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
-	test_grep "fatal: cannot strip one component off url" err
+	test_cmp err.expect err &&
+	test_cmp_config_output expect actual

And now we're actually seeing the list match the expectation (minus the broken item).

LGTM.
Thanks,
-Stolee

#9 lore raw
In reply to Aniket Gupta
Quoted 3 lines · Aniket Gupta

A bundle list can contain a relative URI that cannot be resolved, and today one such entry makes the whole clone die. This series lets the bundle code report it and skip that bundle instead.

When reading patch-by-patch, I initially was concerned about the behavior change. However, patch 3 removes a long-standing TODO comment _that I wrote_ so I'm inclined to take the change. I didn't see anything critical to change about these patches.

That said, we may have something for #leftoverbits:

How could we create a toggle to re-enable the die() if desired?

Perhaps a user _wants_ to fail on bad URIs instead of spending time on an incomplete bundle advertisement and expensive fetch from the remote.

Such an option could be a follow-up, now that you've implemented the _gently() version and the bundle URI machinery could decide for itself whether this is a hard failure or not.

Thanks,
-Stolee

#10 lore raw

Hi Stolee,

Thanks for the review.

Quoted 1 line

I'm less convinced on this factor.

Fair point. I tried it with a password in the remote URL but the "failed to download bundle from URI" warning prints it as is. I think this patch will only cover this one message. The reason I did it is that git already hides the password when it shows a remote URL. For eg. in the "From" line of fetch. It is a separate patch so it is easy to drop if you and Patrick think it is not worth it.

While checking this I noticed that my commit message for patch 3 is not accurate. It says that the clone carries on with the other bundles, but the list is only used up to the bad entry (not at all for a list given to --bundle-uri) and then the clone fetches from the remote. I can send a v4 with that fixed if its fine

Aniket

#11 lore raw
Quoted 5 lines · Aniket Gupta

Hi Stolee,

Thanks for the review.

Quoted 1 line

I'm less convinced on this factor.

Huh, where does this quote come from? It's certainly not part of the mail you reply to. Also, you stripped all necessary context for somebody to know what this was even talking about.

Quoted 6 lines

Fair point. I tried it with a password in the remote URL but the "failed to download bundle from URI" warning prints it as is. I think this patch will only cover this one message. The reason I did it is that git already hides the password when it shows a remote URL. For eg. in the "From" line of fetch. It is a separate patch so it is easy to drop if you and Patrick think it is not worth it.

I don't have a strong opinion on whether or not we should strip credentials. It feels like the right thing to do, but I trust Stolee's input here.

But the other part of patch 2 should definitely be retained in my opinion, because printing an error with the URL after we have trimmed "../" components off of it is just plain not useful. There is no way for the user to tell how the URL is broken because we may have already stripped the relevant components here.

Patrick

#12 lore raw
Quoted 3 lines

Huh, where does this quote come from? It's certainly not part of the mail you reply to. Also, you stripped all necessary context for somebody to know what this was even talking about.

Apologies. That line is from Stolee's review of patch 2. I replied on the wrong mail. I have answered on patch 2 thread with context: https://lore.kernel.org/git/[email protected]/

Quoted 3 lines

But the other part of patch 2 should definitely be retained in my opinion, because printing an error with the URL after we have trimmed "../" components off of it is just plain not useful.

Understood. I will keep that part.

#13 lore raw

v4 cover bundle-uri: do not die on unresolvable relative URIs

In reply to Aniket Gupta

A bundle list can contain a relative URI that cannot be resolved and one such entry can result in the entire clone dying. This series lets the bundle code report it and the clone carry on.

Changes since v3:

* Patch 2: dropped the part that hides credentials as Stolee was not convinced it is needed. The part that names the URL that was passed in stays, as Patrick wanted. The patch has a new title and its test no longer uses a password.

* Patch 3: fixed the commit message. Previously it said that the clone carries on with the other bundles. That is true only for a list that a server advertises only for the bundles before the bad entry. A list given to --bundle-uri is rejected as a whole. In both cases, the clone fetches the rest from the remote. No code change.

* Rebased onto current master, no conflicts.

Stolee's idea of having a toggle to bring back the die() is not part of this series. Happy to take that as a follow-up :)

This conflicts a bit with tb/config-null-values in next, which adds a "!value" check in bundle_list_update() right above the line that patch 3 changes. Keeping both is enough, "!value" check first. I tested that merge (t0060, t5558, t5750).

Aniket Gupta (3):
remote: add relative_url_gently()
remote: name the original URL when relative_url() fails
bundle-uri: do not die on unresolvable relative URIs

 bundle-uri.c                | 10 +++++++++-
 remote.c                    | 34 ++++++++++++++++++++++++++++------
 remote.h                    |  8 ++++++++
 t/t0060-path-utils.sh       |  9 +++++++++
 t/t5750-bundle-uri-parse.sh | 15 +++++++--------
 5 files changed, 61 insertions(+), 15 deletions(-)

base-commit: f7de81456940dd6f3274e461149bba12965de609

Signature · 1 line
2.54.0 (Apple Git-157)
#14 lore raw

PATCH v4 1/3 remote: add relative_url_gently()

relative_url() calls die() when there are more "../" components in the URL than the base URL can absorb. A caller that would rather carry on has no way to do so.

Add relative_url_gently(), which returns NULL and adds the reason to a strbuf instead of dying. relative_url() is now a thin wrapper around it that continues to die, so existing callers behave the same.

---
 remote.c | 34 ++++++++++++++++++++++++++++------
 remote.h |  8 ++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)
remote.c +28 -6
index df069e802f..be561d633e 100644
@@ -3101,7 +3101,8 @@ void remote_state_clear(struct remote_state *remote_state)
 }
 
 /*
- * Returns 1 if it was the last chop before ':'.
+ * Returns 1 if it was the last chop before ':', or -1 if there is
+ * nothing left to chop.
  */
 static int chop_last_dir(char **remoteurl, int is_relative)
 {
@@ -3118,16 +3119,15 @@ static int chop_last_dir(char **remoteurl, int is_relative)
 	}
 
 	if (is_relative || !strcmp(".", *remoteurl))
-		die(_("cannot strip one component off url '%s'"),
-			*remoteurl);
+		return -1;
 
 	free(*remoteurl);
 	*remoteurl = xstrdup(".");
 	return 0;
 }
 
-char *relative_url(const char *remote_url, const char *url,
-		   const char *up_path)
+char *relative_url_gently(const char *remote_url, const char *url,
+			  const char *up_path, struct strbuf *err)
 {
 	int is_relative = 0;
 	int colonsep = 0;
@@ -3169,8 +3169,19 @@ char *relative_url(const char *remote_url, const char *url,
 	 */
 	while (*url) {
 		if (starts_with_dot_dot_slash_native(url)) {
+			int chopped;
+
 			url += 3;
-			colonsep |= chop_last_dir(&remoteurl, is_relative);
+			chopped = chop_last_dir(&remoteurl, is_relative);
+			if (chopped < 0) {
+				strbuf_addf(err,
+					    _("cannot strip one component off url '%s'"),
+					    remoteurl);
+				free(remoteurl);
+				strbuf_release(&sb);
+				return NULL;
+			}
+			colonsep |= chopped;
 		} else if (starts_with_dot_slash_native(url))
 			url += 2;
 		else
@@ -3198,6 +3209,17 @@ char *relative_url(const char *remote_url, const char *url,
 	return strbuf_detach(&sb, NULL);
 }
 
+char *relative_url(const char *remote_url, const char *url,
+		   const char *up_path)
+{
+	struct strbuf err = STRBUF_INIT;
+	char *out = relative_url_gently(remote_url, url, up_path, &err);
+
+	if (!out)
+		die("%s", err.buf);
+	return out;
+}
+
 int valid_remote_name(const char *name)
 {
 	int result;
remote.h +8 -0
index cca02033b9..940474fa71 100644
@@ -478,6 +478,14 @@ void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
 char *relative_url(const char *remote_url, const char *url,
 		   const char *up_path);
 
+/*
+ * Like relative_url(), but if `url` cannot be resolved against
+ * `remote_url`, return NULL and add the reason to `err` instead of
+ * dying. `err` must not be NULL.
+ */
+char *relative_url_gently(const char *remote_url, const char *url,
+			  const char *up_path, struct strbuf *err);
+
 int valid_remote_name(const char *name);
 
 #endif
Signature · 1 line
2.54.0 (Apple Git-157)
#15 lore raw

PATCH v4 2/3 remote: name the original URL when relative_url() fails

In reply to Aniket Gupta

When relative_url() cannot resolve a URL, its error message does not name the URL it was given but what is left of it after some "../" components have already been stripped off. With a base URL of "./a/b" and too many "../", it says:

cannot strip one component off url '.'

which does not help a user to tell what is wrong with the URL.

Name the URL that was passed in instead.

---
 remote.c              | 2 +-
 t/t0060-path-utils.sh | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
remote.c +1 -1
index be561d633e..ee10c7a003 100644
@@ -3176,7 +3176,7 @@ char *relative_url_gently(const char *remote_url, const char *url,
 			if (chopped < 0) {
 				strbuf_addf(err,
 					    _("cannot strip one component off url '%s'"),
-					    remoteurl);
+					    remote_url);
 				free(remoteurl);
 				strbuf_release(&sb);
 				return NULL;
t/t0060-path-utils.sh +9 -0
index 56faf5fe73..0d16b2c884 100755
@@ -394,6 +394,15 @@ test_submodule_relative_url "../" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
 test_submodule_relative_url "../" "foo/bar" "../submodule" "../foo/submodule"
 test_submodule_relative_url "../" "foo" "../submodule" "../submodule"
 
+test_expect_success 'relative_url() names the URL it was given when it fails' '
+	cat >expect <<-\EOF &&
+	fatal: cannot strip one component off url '\''https://host.example/a'\''
+	EOF
+	test_must_fail test-tool submodule resolve-relative-url "" \
+		"https://host.example/a" ../../../../../../x 2>actual &&
+	test_cmp expect actual
+'
+
 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c" "../foo/sub/a/b/c"
 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c/" "../foo/sub/a/b/c"
 test_submodule_relative_url "(null)" "../foo/bar/" "../sub/a/b/c" "../foo/sub/a/b/c"
Signature · 1 line
2.54.0 (Apple Git-157)
#16 lore raw

PATCH v4 3/3 bundle-uri: do not die on unresolvable relative URIs

In reply to Aniket Gupta

A bundle list, either advertised by a server or given to "git clone --bundle-uri", may contain relative URIs. They are resolved with relative_url(), which calls die() when there are more "../" components than the base can absorb, so one bad entry aborts the whole clone.

Resolve them with relative_url_gently() instead. When that fails, bundle_list_update() reports the problem as an error and stops parsing the list, leaving the bundle without a URI.

What happens then depends on where the list came from. For a list advertised by a server, the bundles before the bad entry are still used and the ones after it are not. A list given to "--bundle-uri" is read from a file and rejected as a whole. In both cases the clone carries on and fetches what is missing from the remote.

This resolves the TODO in t5750, so update the test to match.

---
 bundle-uri.c                | 10 +++++++++-
 t/t5750-bundle-uri-parse.sh | 15 +++++++--------
 2 files changed, 16 insertions(+), 9 deletions(-)
bundle-uri.c +9 -1
index 9797eaed9a..d2a33b930f 100644
@@ -209,9 +209,17 @@ static int bundle_list_update(const char *key, const char *value,
 	strbuf_release(&id);
 
 	if (!strcmp(subkey, "uri")) {
+		struct strbuf err = STRBUF_INIT;
+
 		if (bundle->uri)
 			return -1;
-		bundle->uri = relative_url(list->baseURI, value, NULL);
+		bundle->uri = relative_url_gently(list->baseURI, value, NULL,
+						  &err);
+		if (!bundle->uri) {
+			error("%s", err.buf);
+			strbuf_release(&err);
+			return -1;
+		}
 		return 0;
 	}
 
t/t5750-bundle-uri-parse.sh +7 -8
index 70ae94c4d0..8abf8f31ae 100755
@@ -68,17 +68,16 @@ test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' '
 		uri = <uri>/bundle.bdl
 	[bundle "two"]
 		uri = bundle.bdl
-	[bundle "three"]
-		uri = <uri>/../bundle.bdl
 	EOF
 
-	# TODO: We would prefer if parsing a bundle list would not cause
-	# a die() and instead would give a warning and allow the rest of
-	# a Git command to continue. This test_must_fail is necessary for
-	# now until the interface for relative_url() allows for reporting
-	# an error instead of die()ing.
+	cat >err.expect <<-\EOF &&
+	error: cannot strip one component off url '\''<uri>'\''
+	error: bad line: '\''bundle.three.uri=../../bundle.bdl'\''
+	EOF
+
 	test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
-	test_grep "fatal: cannot strip one component off url" err
+	test_cmp err.expect err &&
+	test_cmp_config_output expect actual
 '
 
 test_expect_success 'bundle_uri_parse_line() parsing edge cases: empty key or value' '
Signature · 1 line
2.54.0 (Apple Git-157)
#17 lore raw
Quoted 9 lines · Aniket Gupta

A bundle list, either advertised by a server or given to "git clone --bundle-uri", may contain relative URIs. They are resolved with relative_url(), which calls die() when there are more "../" components than the base can absorb, so one bad entry aborts the whole clone.

Resolve them with relative_url_gently() instead. When that fails, bundle_list_update() reports the problem as an error and stops parsing the list, leaving the bundle without a URI.

Quoted 5 lines

What happens then depends on where the list came from. For a list advertised by a server, the bundles before the bad entry are still used and the ones after it are not. A list given to "--bundle-uri" is read from a file and rejected as a whole. In both cases the clone carries on and fetches what is missing from the remote.

This tells us the story much better than the previous rounds.

Quoted 20 lines

This resolves the TODO in t5750, so update the test to match.

---
 bundle-uri.c                | 10 +++++++++-
 t/t5750-bundle-uri-parse.sh | 15 +++++++--------
 2 files changed, 16 insertions(+), 9 deletions(-)
bundle-uri.c +2 -0
index 9797eaed9a..d2a33b930f 100644
@@ -209,9 +209,17 @@ static int bundle_list_update(const char *key, const char *value,
 	strbuf_release(&id);
 
 	if (!strcmp(subkey, "uri")) {
+		struct strbuf err = STRBUF_INIT;
+
 		if (bundle->uri)
 			return -1;

bundle-uri.c static int bundle_list_update(const char *key, const char *value, · line 224

Quoted 10 lines
bundle-uri.c +7 -1
-		bundle->uri = relative_url(list->baseURI, value, NULL);
+		bundle->uri = relative_url_gently(list->baseURI, value, NULL,
+						  &err);
+		if (!bundle->uri) {
+			error("%s", err.buf);
+			strbuf_release(&err);
+			return -1;
+		}
 		return 0;
 	}

This is minor, but because bundle_list_update() places the bundle in the '.bundles' hashmap of 'list' before we come here, a failing entry will stay in the hashmap with '.uri' of 'bundle' set to NULL. If valid bundles precede a bad entry, fetch_bundle_uri_internal() may emit a "error: bundle '<id>' has no uri" while iterating over '.bundles' of 'list' before the clone proceeds with the remote fetch, which may or may not be what we want. After all, we have already issued an error message, so...?

Thanks.

#18 lore raw
Quoted 3 lines

This is minor, but because bundle_list_update() places the bundle in the '.bundles' hashmap of 'list' before we come here, a failing entry will stay in the hashmap with '.uri' of 'bundle' set to NULL.

You are right. I am able to repro it with a server that advertises a bad
entry: after the three errors we already print there is a fourth one, "error: bundle '<id>' has no uri". A list given to --bundle-uri does not show it, as that whole list is rejected before we get there.

Quoted 2 lines

which may or may not be what we want. After all, we have already issued an error message, so...?

I dont think we need it since the earlier errors already say what is wrong. I tried dropping the bundle from the list when its URI cannot be resolved. The clone behaves the same, just without the extra error, and the bundle-uri tests still pass. I did not want to hide it in fetch_bundle_uri_internal(), as a bundle that is advertised without any uri should still be reported, like we do today. Will do the drop in v5 and add a test for it.

#19 lore raw
In reply to Aniket Gupta
Quoted 11 lines · Aniket Gupta

A bundle list can contain a relative URI that cannot be resolved and one such entry can result in the entire clone dying. This series lets the bundle code report it and the clone carry on.

Changes since v3:

* Patch 2: dropped the part that hides credentials as Stolee was not convinced it is needed. The part that names the URL that was passed in stays, as Patrick wanted. The patch has a new title and its test no longer uses a password.

Quoted 7 lines

* Patch 3: fixed the commit message. Previously it said that the clone carries on with the other bundles. That is true only for a list that a server advertises only for the bundles before the bad entry. A list given to --bundle-uri is rejected as a whole. In both cases, the clone fetches the rest from the remote. No code change.

* Rebased onto current master, no conflicts.

Thanks. This version LGTM.

Quoted 2 lines

Stolee's idea of having a toggle to bring back the die() is not part of this series. Happy to take that as a follow-up :)

I agree it doesn't need to be added here.

Thanks,
-Stolee