Add NO_PROXY env support Item 2 of 3 from #4164 Signed-off-by: Mathieu Parent <math.parent@gmail.com>
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
diff --git a/src/remote.c b/src/remote.c
index 73375b3..e63f54a 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -849,11 +849,70 @@ int git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote
return remote->transport->ls(out, size, remote->transport);
}
-int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url)
+int git_remote__get_http_proxy_bypass(git_net_url *url, git_buf *no_proxy_env, bool *bypass)
+{
+ int error = 0;
+ char *p_start = no_proxy_env->ptr;
+ size_t p_length = 0;
+ char c;
+ git_buf hostport = GIT_BUF_INIT;
+
+ error = git_buf_printf(&hostport, "%s:%s", url->host, url->port);
+ if (error < 0)
+ return error;
+
+ *bypass = false;
+
+ do {
+ c = *(p_start + p_length);
+ if ((c == ',') || (c == 0)) {
+ if ((p_length == 1) && (*p_start == '*')) {
+ // wildcard match (*)
+ goto found;
+ } else if ((p_length == strlen(url->host)) && !memcmp(p_start, url->host, p_length)) {
+ // exact host match
+ goto found;
+ } else if ((p_length == strlen(hostport.ptr)) && !memcmp(p_start, hostport.ptr, p_length)) {
+ // exact host:port match
+ goto found;
+ } else {
+ if ((p_length >= 2) && (*p_start == '*') && (*(p_start + 1) == '.')) {
+ // *.foo == .foo
+ p_start++;
+ p_length--;
+ }
+ if ((*p_start == '.') && (strlen(url->host) > p_length) && !memcmp(p_start, url->host + strlen(url->host) - p_length, p_length)) {
+ // host suffix match (.example.org)
+ goto found;
+ } else if ((*p_start == '.') && (strlen(hostport.ptr) > p_length) && !memcmp(p_start, hostport.ptr + strlen(hostport.ptr) - p_length, p_length)) {
+ // host:port suffix match (.example.org:443)
+ goto found;
+ }
+ }
+ p_start += p_length + 1;
+ p_length = 0;
+ } else {
+ p_length++;
+ }
+ } while(c != 0);
+
+ goto end;
+
+found:
+ *bypass = true;
+
+end:
+ git_buf_dispose(&hostport);
+ return 0;
+}
+
+int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, git_net_url *url, char **proxy_url)
{
git_config *cfg;
git_config_entry *ce = NULL;
- git_buf val = GIT_BUF_INIT;
+ git_buf proxy_env = GIT_BUF_INIT;
+ git_buf no_proxy_env = GIT_BUF_INIT;
+ bool bypass = false;
int error;
GIT_ASSERT_ARG(remote);
@@ -898,11 +957,11 @@ int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_ur
}
/* http_proxy / https_proxy environment variables */
- error = git__getenv(&val, use_ssl ? "https_proxy" : "http_proxy");
+ error = git__getenv(&proxy_env, use_ssl ? "https_proxy" : "http_proxy");
/* try uppercase environment variables */
if (error == GIT_ENOTFOUND)
- error = git__getenv(&val, use_ssl ? "HTTPS_PROXY" : "HTTP_PROXY");
+ error = git__getenv(&proxy_env, use_ssl ? "HTTPS_PROXY" : "HTTP_PROXY");
if (error < 0) {
if (error == GIT_ENOTFOUND) {
@@ -913,13 +972,35 @@ int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_ur
return error;
}
- *proxy_url = git_buf_detach(&val);
+ /* no_proxy/NO_PROXY environment variables */
+ error = git__getenv(&no_proxy_env, "no_proxy");
+ if (error == GIT_ENOTFOUND)
+ error = git__getenv(&no_proxy_env, "NO_PROXY");
+
+ if (error == GIT_ENOTFOUND) {
+ git_error_clear();
+ error = 0;
+ } else if (error < 0) {
+ goto cleanup;
+ } else {
+ error = git_remote__get_http_proxy_bypass(url, &no_proxy_env, &bypass);
+ }
+
+ if (bypass) {
+ git_buf_dispose(&proxy_env);
+ goto cleanup;
+ } else {
+ *proxy_url = git_buf_detach(&proxy_env);
+ }
found:
GIT_ERROR_CHECK_ALLOC(*proxy_url);
+
+cleanup:
+ git_buf_dispose(&no_proxy_env);
git_config_entry_free(ce);
- return 0;
+ return error;
}
/* DWIM `refspecs` based on `refs` and append the output to `out` */
diff --git a/src/remote.h b/src/remote.h
index df75ed3..ffcefdf 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -9,6 +9,7 @@
#include "common.h"
+#include "net.h"
#include "git2/remote.h"
#include "git2/transport.h"
#include "git2/sys/transport.h"
@@ -46,7 +47,8 @@ typedef struct git_remote_connection_opts {
int git_remote__connect(git_remote *remote, git_direction direction, const git_remote_callbacks *callbacks, const git_remote_connection_opts *conn);
int git_remote__urlfordirection(git_buf *url_out, struct git_remote *remote, int direction, const git_remote_callbacks *callbacks);
-int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url);
+int git_remote__get_http_proxy_bypass(git_net_url *url, git_buf *no_proxy_env, bool *bypass);
+int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, git_net_url *url, char **proxy_url);
git_refspec *git_remote__matching_refspec(git_remote *remote, const char *refname);
git_refspec *git_remote__matching_dst_refspec(git_remote *remote, const char *refname);
diff --git a/src/transports/http.c b/src/transports/http.c
index 691bceb..5468674 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -306,7 +306,7 @@ static int lookup_proxy(
remote = transport->owner->owner;
use_ssl = !strcmp(transport->server.url.scheme, "https");
- error = git_remote__get_http_proxy(remote, use_ssl, &config);
+ error = git_remote__get_http_proxy(remote, use_ssl, &transport->server.url, &config);
if (error || !config)
goto done;
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index ea2195a..8dc39d8 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -373,6 +373,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
{
winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
git_buf buf = GIT_BUF_INIT;
+ bool use_ssl;
char *proxy_url = NULL;
wchar_t ct[MAX_CONTENT_TYPE_LEN];
LPCWSTR types[] = { L"*/*", NULL };
@@ -429,7 +430,8 @@ static int winhttp_stream_connect(winhttp_stream *s)
proxy_opts = &t->owner->proxy;
if (proxy_opts->type == GIT_PROXY_AUTO) {
/* Set proxy if necessary */
- if (git_remote__get_http_proxy(t->owner->owner, (strcmp(t->server.url.scheme, "https") == 0), &proxy_url) < 0)
+ use_ssl = strcmp(t->server.url.scheme, "https") == 0;
+ if (git_remote__get_http_proxy(t->owner->owner, use_ssl, &t->server.url, &proxy_url) < 0)
goto on_error;
}
else if (proxy_opts->type == GIT_PROXY_SPECIFIED) {
diff --git a/tests/online/clone.c b/tests/online/clone.c
index dbf45dc..d9b1837 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -36,6 +36,7 @@ static char *_remote_expectcontinue = NULL;
static int _orig_proxies_need_reset = 0;
static char *_orig_http_proxy = NULL;
static char *_orig_https_proxy = NULL;
+static char *_orig_no_proxy = NULL;
static int ssl_cert(git_cert *cert, int valid, const char *host, void *payload)
{
@@ -110,9 +111,11 @@ void test_online_clone__cleanup(void)
if (_orig_proxies_need_reset) {
cl_setenv("HTTP_PROXY", _orig_http_proxy);
cl_setenv("HTTPS_PROXY", _orig_https_proxy);
+ cl_setenv("NO_PROXY", _orig_no_proxy);
git__free(_orig_http_proxy);
git__free(_orig_https_proxy);
+ git__free(_orig_no_proxy);
}
git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, NULL, NULL);
@@ -854,6 +857,7 @@ void test_online_clone__proxy_credentials_in_environment(void)
_orig_http_proxy = cl_getenv("HTTP_PROXY");
_orig_https_proxy = cl_getenv("HTTPS_PROXY");
+ _orig_no_proxy = cl_getenv("NO_PROXY");
_orig_proxies_need_reset = 1;
g_options.fetch_opts.proxy_opts.type = GIT_PROXY_AUTO;
@@ -865,6 +869,7 @@ void test_online_clone__proxy_credentials_in_environment(void)
cl_setenv("HTTP_PROXY", url.ptr);
cl_setenv("HTTPS_PROXY", url.ptr);
+ cl_setenv("NO_PROXY", NULL);
cl_git_pass(git_clone(&g_repo, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options));
@@ -893,6 +898,67 @@ void test_online_clone__proxy_credentials_in_url_https(void)
git_buf_dispose(&url);
}
+struct no_proxy_test_entry {
+ char no_proxy[128];
+ bool bypass;
+};
+
+static struct no_proxy_test_entry no_proxy_test_entries[] = {
+ {"*", true},
+ {"github.com", true},
+ {"github.com:443", true},
+ {"github.com:80", false},
+ {".github.com", false},
+ {"*.github.com", false},
+ {".com", true},
+ {"*.com", true},
+ {".com:443", true},
+ {"*.com:443", true},
+ {".com:80", false},
+ {"*.com:80", false},
+ {"", false}
+};
+
+void test_online_clone__no_proxy_in_environment(void)
+{
+ int error = 0;
+ unsigned int i;
+ git_buf proxy_url = GIT_BUF_INIT;
+
+ _orig_http_proxy = cl_getenv("HTTP_PROXY");
+ _orig_https_proxy = cl_getenv("HTTPS_PROXY");
+ _orig_no_proxy = cl_getenv("NO_PROXY");
+ _orig_proxies_need_reset = 1;
+
+ g_options.fetch_opts.proxy_opts.type = GIT_PROXY_AUTO;
+ g_options.fetch_opts.proxy_opts.certificate_check = proxy_cert_cb;
+
+ cl_git_pass(git_buf_printf(&proxy_url, "http://does-not-exists.example.org:1234/"));
+
+ cl_setenv("HTTP_PROXY", proxy_url.ptr);
+ cl_setenv("HTTPS_PROXY", proxy_url.ptr);
+
+
+ for (i = 0; i < ARRAY_SIZE(no_proxy_test_entries); ++i) {
+ cl_setenv("NO_PROXY", no_proxy_test_entries[i].no_proxy);
+ error = git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options);
+
+ if (no_proxy_test_entries[i].bypass) {
+ cl_assert_(error == 0, no_proxy_test_entries[i].no_proxy);
+ } else {
+ cl_assert_(error == -1, no_proxy_test_entries[i].no_proxy);
+ }
+
+ if (g_repo) {
+ git_repository_free(g_repo);
+ g_repo = NULL;
+ }
+ cl_fixture_cleanup("./foo");
+ }
+
+ git_buf_dispose(&proxy_url);
+}
+
void test_online_clone__proxy_auto_not_detected(void)
{
g_options.fetch_opts.proxy_opts.type = GIT_PROXY_AUTO;
diff --git a/tests/remote/no_proxy.c b/tests/remote/no_proxy.c
new file mode 100644
index 0000000..4f75841
--- /dev/null
+++ b/tests/remote/no_proxy.c
@@ -0,0 +1,40 @@
+#include "clar_libgit2.h"
+#include "remote.h"
+
+/* Suite data */
+struct no_proxy_test_entry {
+ char url[128];
+ char no_proxy[128];
+ bool bypass;
+};
+
+static struct no_proxy_test_entry no_proxy_test_entries[] = {
+ {"https://example.com/", "", false},
+ {"https://example.com/", "example.org", false},
+ {"https://example.com/", "*", true},
+ {"https://example.com/", "example.com,example.org", true},
+ {"https://example.com/", ".example.com,example.org", false},
+ {"https://foo.example.com/", ".example.com,example.org", true},
+ {"https://example.com/", "foo.example.com,example.org", false},
+
+};
+
+void test_remote_no_proxy__entries(void)
+{
+ unsigned int i;
+ git_net_url url = GIT_NET_URL_INIT;
+ git_buf no_proxy = GIT_BUF_INIT;
+ bool bypass = false;
+
+ for (i = 0; i < ARRAY_SIZE(no_proxy_test_entries); ++i) {
+ cl_git_pass(git_net_url_parse(&url, no_proxy_test_entries[i].url));
+ cl_git_pass(git_buf_sets(&no_proxy, no_proxy_test_entries[i].no_proxy));
+ cl_git_pass(git_remote__get_http_proxy_bypass(&url, &no_proxy, &bypass));
+
+ cl_assert_(bypass == no_proxy_test_entries[i].bypass, no_proxy_test_entries[i].no_proxy);
+
+ git_net_url_dispose(&url);
+ git_buf_dispose(&no_proxy);
+ }
+
+}