Add the from_user parameter to the 'is_transport_allowed' function.
This allows callers to query if a transport protocol is allowed, given
that the caller knows that the protocol is coming from the user (1) or
not from the user (0) such as redirects in libcurl.  If unknown a -1
should be provided which falls back to reading `GIT_PROTOCOL_FROM_USER`
to determine if the protocol came from the user.

Signed-off-by: Brandon Williams <bmw...@google.com>
---
 http.c      | 12 ++++++------
 transport.c |  8 +++++---
 transport.h | 13 ++++++++++---
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/http.c b/http.c
index a1c3a0e..2a02941 100644
--- a/http.c
+++ b/http.c
@@ -624,17 +624,17 @@ void setup_curl_trace(CURL *handle)
        curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
 }
 
-static long get_curl_allowed_protocols(void)
+static long get_curl_allowed_protocols(int from_user)
 {
        long allowed_protocols = 0;
 
-       if (is_transport_allowed("http"))
+       if (is_transport_allowed("http", from_user))
                allowed_protocols |= CURLPROTO_HTTP;
-       if (is_transport_allowed("https"))
+       if (is_transport_allowed("https", from_user))
                allowed_protocols |= CURLPROTO_HTTPS;
-       if (is_transport_allowed("ftp"))
+       if (is_transport_allowed("ftp", from_user))
                allowed_protocols |= CURLPROTO_FTP;
-       if (is_transport_allowed("ftps"))
+       if (is_transport_allowed("ftps", from_user))
                allowed_protocols |= CURLPROTO_FTPS;
 
        return allowed_protocols;
@@ -740,7 +740,7 @@ static CURL *get_curl_handle(void)
 #endif
 #if LIBCURL_VERSION_NUM >= 0x071304
        curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
-                        get_curl_allowed_protocols());
+                        get_curl_allowed_protocols(0));
 #else
        warning("protocol restrictions not applied to curl redirects because\n"
                "your curl version is too old (>= 7.19.4)");
diff --git a/transport.c b/transport.c
index 186de9a..8a3597b 100644
--- a/transport.c
+++ b/transport.c
@@ -723,7 +723,7 @@ static enum protocol_allow_config get_protocol_config(const 
char *type)
        return PROTOCOL_ALLOW_USER_ONLY;
 }
 
-int is_transport_allowed(const char *type)
+int is_transport_allowed(const char *type, int from_user)
 {
        const struct string_list *whitelist = protocol_whitelist();
        if (whitelist)
@@ -735,7 +735,9 @@ int is_transport_allowed(const char *type)
        case PROTOCOL_ALLOW_NEVER:
                return 0;
        case PROTOCOL_ALLOW_USER_ONLY:
-               return git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
+               if (from_user < 0)
+                       from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
+               return from_user;
        }
 
        die("BUG: invalid protocol_allow_config type");
@@ -743,7 +745,7 @@ int is_transport_allowed(const char *type)
 
 void transport_check_allowed(const char *type)
 {
-       if (!is_transport_allowed(type))
+       if (!is_transport_allowed(type, -1))
                die("transport '%s' not allowed", type);
 }
 
diff --git a/transport.h b/transport.h
index f4998bc..9820f10 100644
--- a/transport.h
+++ b/transport.h
@@ -153,10 +153,17 @@ extern int transport_summary_width(const struct ref 
*refs);
 struct transport *transport_get(struct remote *, const char *);
 
 /*
- * Check whether a transport is allowed by the environment. Type should
- * generally be the URL scheme, as described in Documentation/git.txt
+ * Check whether a transport is allowed by the environment.
+ *
+ * Type should generally be the URL scheme, as described in
+ * Documentation/git.txt
+ *
+ * from_user specifies if the transport was given by the user.  If unknown pass
+ * a -1 to read from the environment to determine if the transport was given by
+ * the user.
+ *
  */
-int is_transport_allowed(const char *type);
+int is_transport_allowed(const char *type, int from_user);
 
 /*
  * Check whether a transport is allowed by the environment,
-- 
2.8.0.rc3.226.g39d4020

Reply via email to