Enhance url parsing to include passwords
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
diff --git a/src/netops.c b/src/netops.c
index 1273814..fd788bc 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -578,11 +578,22 @@ int gitno_select_in(gitno_buffer *buf, long int sec, long int usec)
return select((int)buf->socket->socket + 1, &fds, NULL, NULL, &tv);
}
-int gitno_extract_host_and_port(char **host, char **port, char **username, const char *url, const char *default_port)
+int gitno_extract_url_parts(
+ char **host,
+ char **port,
+ char **username,
+ char **password,
+ const char *url,
+ const char *default_port)
{
- char *colon, *slash, *at, *delim;
+ char *colon, *slash, *at, *end;
const char *start;
+ /*
+ *
+ * ==> [user[:pass]@]hostname.tld[:port]/resource
+ */
+
colon = strchr(url, ':');
slash = strchr(url, '/');
at = strchr(url, '@');
@@ -592,6 +603,19 @@ int gitno_extract_host_and_port(char **host, char **port, char **username, const
return -1;
}
+ start = url;
+ if (at && at < slash) {
+ start = at+1;
+ *username = git__strndup(url, at - url);
+ }
+
+ if (colon && colon < at) {
+ git__free(*username);
+ *username = git__strndup(url, colon-url);
+ *password = git__strndup(colon+1, at-colon-1);
+ colon = strchr(at, ':');
+ }
+
if (colon == NULL) {
*port = git__strdup(default_port);
} else {
@@ -599,15 +623,9 @@ int gitno_extract_host_and_port(char **host, char **port, char **username, const
}
GITERR_CHECK_ALLOC(*port);
- delim = colon == NULL ? slash : colon;
-
- start = url;
- if (at && at < slash) {
- start = at+1;
- *username = git__strndup(url, at - url);
- }
+ end = colon == NULL ? slash : colon;
- *host = git__strndup(start, delim - start);
+ *host = git__strndup(start, end - start);
GITERR_CHECK_ALLOC(*host);
return 0;
diff --git a/src/netops.h b/src/netops.h
index bb2624a..d352bf3 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -66,6 +66,12 @@ int gitno_send(gitno_socket *socket, const char *msg, size_t len, int flags);
int gitno_close(gitno_socket *s);
int gitno_select_in(gitno_buffer *buf, long int sec, long int usec);
-int gitno_extract_host_and_port(char **host, char **port, char **username, const char *url, const char *default_port);
+int gitno_extract_url_parts(
+ char **host,
+ char **port,
+ char **username,
+ char **password,
+ const char *url,
+ const char *default_port);
#endif
diff --git a/src/transports/git.c b/src/transports/git.c
index 5c816e1..21de4d7 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -179,7 +179,7 @@ static int _git_uploadpack_ls(
const char *url,
git_smart_subtransport_stream **stream)
{
- char *host, *port, *user;
+ char *host, *port, *user, *pass;
git_stream *s;
*stream = NULL;
@@ -192,7 +192,7 @@ static int _git_uploadpack_ls(
s = (git_stream *)*stream;
- if (gitno_extract_host_and_port(&host, &port, &user, url, GIT_DEFAULT_PORT) < 0)
+ if (gitno_extract_url_parts(&host, &port, &user, &pass, url, GIT_DEFAULT_PORT) < 0)
goto on_error;
if (gitno_connect(&s->socket, host, port, 0) < 0)
@@ -202,6 +202,7 @@ static int _git_uploadpack_ls(
git__free(host);
git__free(port);
git__free(user);
+ git__free(pass);
return 0;
on_error:
@@ -234,7 +235,7 @@ static int _git_receivepack_ls(
const char *url,
git_smart_subtransport_stream **stream)
{
- char *host, *port, *user;
+ char *host, *port, *user, *pass;
git_stream *s;
*stream = NULL;
@@ -247,7 +248,7 @@ static int _git_receivepack_ls(
s = (git_stream *)*stream;
- if (gitno_extract_host_and_port(&host, &port, &user, url, GIT_DEFAULT_PORT) < 0)
+ if (gitno_extract_url_parts(&host, &port, &user, &pass, url, GIT_DEFAULT_PORT) < 0)
goto on_error;
if (gitno_connect(&s->socket, host, port, 0) < 0)
@@ -257,6 +258,7 @@ static int _git_receivepack_ls(
git__free(host);
git__free(port);
git__free(user);
+ git__free(pass);
return 0;
on_error:
diff --git a/src/transports/http.c b/src/transports/http.c
index 1449064..6c116d8 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -61,6 +61,7 @@ typedef struct {
char *host;
char *port;
char *user_from_url;
+ char *pass_from_url;
git_cred *cred;
http_authmechanism_t auth_mechanism;
unsigned connected : 1,
@@ -744,8 +745,8 @@ static int http_action(
if (!default_port)
return -1;
- if ((ret = gitno_extract_host_and_port(&t->host, &t->port, &t->user_from_url,
- url, default_port)) < 0)
+ if ((ret = gitno_extract_url_parts(&t->host, &t->port,
+ &t->user_from_url, &t->pass_from_url, url, default_port)) < 0)
return ret;
t->path = strchr(url, '/');
@@ -821,6 +822,16 @@ static int http_close(git_smart_subtransport *subtransport)
t->port = NULL;
}
+ if (t->user_from_url) {
+ git__free(t->user_from_url);
+ t->user_from_url = NULL;
+ }
+
+ if (t->pass_from_url) {
+ git__free(t->pass_from_url);
+ t->pass_from_url = NULL;
+ }
+
return 0;
}
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 544e52f..4ac085e 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -788,7 +788,8 @@ static int winhttp_connect(
t->use_ssl = 1;
}
- if ((ret = gitno_extract_host_and_port(&t->host, &t->port, &t->parent.user_from_url, url, default_port)) < 0)
+ if ((ret = gitno_extract_url_parts(&t->host, &t->port, &t->parent.user_from_url,
+ &t->parent.pass_from_url, url, default_port)) < 0)
return ret;
t->path = strchr(url, '/');
@@ -944,6 +945,16 @@ static int winhttp_close(git_smart_subtransport *subtransport)
t->port = NULL;
}
+ if (t->user_from_url) {
+ git__free(t->user_from_url);
+ t->user_from_url = NULL;
+ }
+
+ if (t->pass_from_url) {
+ git__free(t->pass_from_url);
+ t->pass_from_url = NULL;
+ }
+
if (t->cred) {
t->cred->free(t->cred);
t->cred = NULL;
diff --git a/tests-clar/network/urlparse.c b/tests-clar/network/urlparse.c
new file mode 100644
index 0000000..29d0506
--- /dev/null
+++ b/tests-clar/network/urlparse.c
@@ -0,0 +1,78 @@
+#include "clar_libgit2.h"
+#include "netops.h"
+
+void test_network_urlparse__trivial(void)
+{
+ char *host, *port, *user, *pass;
+
+ cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass,
+ "example.com/resource", "8080"));
+ cl_assert_equal_s(host, "example.com");
+ cl_assert_equal_s(port, "8080");
+ cl_assert_equal_sz(user, NULL);
+ cl_assert_equal_sz(pass, NULL);
+}
+
+void test_network_urlparse__user(void)
+{
+ char *host, *port, *user, *pass;
+
+ cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass,
+ "user@example.com/resource", "8080"));
+ cl_assert_equal_s(host, "example.com");
+ cl_assert_equal_s(port, "8080");
+ cl_assert_equal_s(user, "user");
+ cl_assert_equal_sz(pass, NULL);
+}
+
+void test_network_urlparse__user_pass(void)
+{
+ char *host, *port, *user, *pass;
+
+ /* user:pass@hostname.tld/resource */
+ cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass,
+ "user:pass@example.com/resource", "8080"));
+ cl_assert_equal_s(host, "example.com");
+ cl_assert_equal_s(port, "8080");
+ cl_assert_equal_s(user, "user");
+ cl_assert_equal_s(pass, "pass");
+}
+
+void test_network_urlparse__port(void)
+{
+ char *host, *port, *user, *pass;
+
+ /* hostname.tld:port/resource */
+ cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass,
+ "example.com:9191/resource", "8080"));
+ cl_assert_equal_s(host, "example.com");
+ cl_assert_equal_s(port, "9191");
+ cl_assert_equal_sz(user, NULL);
+ cl_assert_equal_sz(pass, NULL);
+}
+
+void test_network_urlparse__user_port(void)
+{
+ char *host, *port, *user, *pass;
+
+ /* user@hostname.tld:port/resource */
+ cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass,
+ "user@example.com:9191/resource", "8080"));
+ cl_assert_equal_s(host, "example.com");
+ cl_assert_equal_s(port, "9191");
+ cl_assert_equal_s(user, "user");
+ cl_assert_equal_sz(pass, NULL);
+}
+
+void test_network_urlparse__user_pass_port(void)
+{
+ char *host, *port, *user, *pass;
+
+ /* user:pass@hostname.tld:port/resource */
+ cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass,
+ "user:pass@example.com:9191/resource", "8080"));
+ cl_assert_equal_s(host, "example.com");
+ cl_assert_equal_s(port, "9191");
+ cl_assert_equal_s(user, "user");
+ cl_assert_equal_s(pass, "pass");
+}
diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c
index 12b9e03..0bc7440 100644
--- a/tests-clar/online/clone.c
+++ b/tests-clar/online/clone.c
@@ -7,6 +7,7 @@
#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
#define BB_REPO_URL "https://libgit2@bitbucket.org/libgit2/testgitrepository.git"
+#define BB_REPO_URL_WITH_PASS "https://libgit2:libgit2@bitbucket.org/libgit2/testgitrepository.git"
static git_repository *g_repo;
static git_clone_options g_options;
@@ -167,4 +168,8 @@ void test_online_clone__bitbucket_style(void)
cl_git_pass(git_clone(&g_repo, BB_REPO_URL, "./foo", &g_options));
git_repository_free(g_repo); g_repo = NULL;
cl_fixture_cleanup("./foo");
+
+ cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_PASS, "./foo", &g_options));
+ git_repository_free(g_repo); g_repo = NULL;
+ cl_fixture_cleanup("./foo");
}