clone: do not include authentication data in guessed dir

If the URI contains authentication data and the URI's path component is empty, we fail to guess a sensible directory name. E.g. cloning a repository 'ssh://user:password@example.com/' we guess a directory name 'password@example.com' where we would want the hostname only, e.g. 'example.com'. The naive way of just adding '@' as a path separator would break cloning repositories like 'foo/bar@baz.git' (which would currently become 'bar@baz' but would then become 'baz' only). Instead fix this by first dropping the scheme and then greedily scanning for an '@' sign until we find the first path separator. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 10, 2015 at 17:48 UTC e895986727dfc4105c497132540dafa8ed51ec0a
2 files changed +33 -12
builtin/clone.c
+31 -10
@@ -146,30 +146,51 @@ static char *get_repo_path(const char *repo, int *is_bundle)
146
147 static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
148 {
149 - const char *end = repo + strlen(repo), *start;
149 + const char *end = repo + strlen(repo), *start, *ptr;
150 size_t len;
151 char *dir;
152
153 + /*
154 + * Skip scheme.
155 + */
156 + start = strstr(repo, "://");
157 + if (start == NULL)
158 + start = repo;
159 + else
160 + start += 3;
161 +
162 + /*
163 + * Skip authentication data. The stripping does happen
164 + * greedily, such that we strip up to the last '@' inside
165 + * the host part.
166 + */
167 + for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
168 + if (*ptr == '@')
169 + start = ptr + 1;
170 + }
171 +
172 /*
173 * Strip trailing spaces, slashes and /.git
174 */
156 - while (repo < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
175 + while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
176 end--;
158 - if (end - repo > 5 && is_dir_sep(end[-5]) &&
177 + if (end - start > 5 && is_dir_sep(end[-5]) &&
178 !strncmp(end - 4, ".git", 4)) {
179 end -= 5;
161 - while (repo < end && is_dir_sep(end[-1]))
180 + while (start < end && is_dir_sep(end[-1]))
181 end--;
182 }
183
184 /*
166 - * Find last component, but be prepared that repo could have
167 - * the form "remote.example.com:foo.git", i.e. no slash
168 - * in the directory part.
185 + * Find last component. To remain backwards compatible we
186 + * also regard colons as path separators, such that
187 + * cloning a repository 'foo:bar.git' would result in a
188 + * directory 'bar' being guessed.
189 */
170 - start = end;
171 - while (repo < start && !is_dir_sep(start[-1]) && start[-1] != ':')
172 - start--;
190 + ptr = end;
191 + while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
192 + ptr--;
193 + start = ptr;
194
195 /*
196 * Strip .{bundle,git}.
t/t5603-clone-dirname.sh
+2 -2
@@ -77,11 +77,11 @@ test_clone_dir host:foo/.git/// foo
77 # omitting the path should default to the hostname
78 test_clone_dir ssh://host/ host
79 test_clone_dir ssh://host:1234/ host fail
80 -test_clone_dir ssh://user@host/ host fail
80 +test_clone_dir ssh://user@host/ host
81 test_clone_dir host:/ host fail
82
83 # auth materials should be redacted
84 -test_clone_dir ssh://user:password@host/ host fail
84 +test_clone_dir ssh://user:password@host/ host
85 test_clone_dir ssh://user:password@host:1234/ host fail
86 test_clone_dir ssh://user:passw@rd@host:1234/ host fail
87 test_clone_dir user@host:/ host fail