Blame SOURCES/0003-transport-refactor-protocol-whitelist-code.patch

776a70
From 9b9aabe6ab5d07227c1c02781f03a3c38fbc27b0 Mon Sep 17 00:00:00 2001
776a70
From: Jeff King <peff@peff.net>
776a70
Date: Tue, 22 Sep 2015 18:03:49 -0400
776a70
Subject: [PATCH 3/5] transport: refactor protocol whitelist code
776a70
776a70
The current callers only want to die when their transport is
776a70
prohibited. But future callers want to query the mechanism
776a70
without dying.
776a70
776a70
Let's break out a few query functions, and also save the
776a70
results in a static list so we don't have to re-parse for
776a70
each query.
776a70
776a70
Based-on-a-patch-by: Blake Burkhart <bburky@bburky.com>
776a70
Signed-off-by: Jeff King <peff@peff.net>
776a70
Signed-off-by: Junio C Hamano <gitster@pobox.com>
776a70
---
776a70
 transport.c | 38 ++++++++++++++++++++++++++++++--------
776a70
 transport.h | 15 +++++++++++++--
776a70
 2 files changed, 43 insertions(+), 10 deletions(-)
776a70
776a70
diff --git a/transport.c b/transport.c
776a70
index 733717d..2dbdca0 100644
776a70
--- a/transport.c
776a70
+++ b/transport.c
776a70
@@ -894,18 +894,40 @@ static int external_specification_len(const char *url)
776a70
 	return strchr(url, ':') - url;
776a70
 }
776a70
 
776a70
-void transport_check_allowed(const char *type)
776a70
+static const struct string_list *protocol_whitelist(void)
776a70
 {
776a70
-	struct string_list allowed = STRING_LIST_INIT_DUP;
776a70
-	const char *v = getenv("GIT_ALLOW_PROTOCOL");
776a70
+	static int enabled = -1;
776a70
+	static struct string_list allowed = STRING_LIST_INIT_DUP;
776a70
+
776a70
+	if (enabled < 0) {
776a70
+		const char *v = getenv("GIT_ALLOW_PROTOCOL");
776a70
+		if (v) {
776a70
+			string_list_split(&allowed, v, ':', -1);
776a70
+			sort_string_list(&allowed);
776a70
+			enabled = 1;
776a70
+		} else {
776a70
+			enabled = 0;
776a70
+		}
776a70
+	}
776a70
 
776a70
-	if (!v)
776a70
-		return;
776a70
+	return enabled ? &allowed : NULL;
776a70
+}
776a70
+
776a70
+int is_transport_allowed(const char *type)
776a70
+{
776a70
+	const struct string_list *allowed = protocol_whitelist();
776a70
+	return !allowed || string_list_has_string(allowed, type);
776a70
+}
776a70
 
776a70
-	string_list_split(&allowed, v, ':', -1);
776a70
-	if (!unsorted_string_list_has_string(&allowed, type))
776a70
+void transport_check_allowed(const char *type)
776a70
+{
776a70
+	if (!is_transport_allowed(type))
776a70
 		die("transport '%s' not allowed", type);
776a70
-	string_list_clear(&allowed, 0);
776a70
+}
776a70
+
776a70
+int transport_restrict_protocols(void)
776a70
+{
776a70
+	return !!protocol_whitelist();
776a70
 }
776a70
 
776a70
 struct transport *transport_get(struct remote *remote, const char *url)
776a70
diff --git a/transport.h b/transport.h
776a70
index 2beda7d..7707c27 100644
776a70
--- a/transport.h
776a70
+++ b/transport.h
776a70
@@ -114,12 +114,23 @@ struct transport {
776a70
 struct transport *transport_get(struct remote *, const char *);
776a70
 
776a70
 /*
776a70
+ * Check whether a transport is allowed by the environment. Type should
776a70
+ * generally be the URL scheme, as described in Documentation/git.txt
776a70
+ */
776a70
+int is_transport_allowed(const char *type);
776a70
+
776a70
+/*
776a70
  * Check whether a transport is allowed by the environment,
776a70
- * and die otherwise. type should generally be the URL scheme,
776a70
- * as described in Documentation/git.txt
776a70
+ * and die otherwise.
776a70
  */
776a70
 void transport_check_allowed(const char *type);
776a70
 
776a70
+/*
776a70
+ * Returns true if the user has attempted to turn on protocol
776a70
+ * restrictions at all.
776a70
+ */
776a70
+int transport_restrict_protocols(void);
776a70
+
776a70
 /* Transport options which apply to git:// and scp-style URLs */
776a70
 
776a70
 /* The program to use on the remote side to send a pack */
776a70
-- 
776a70
2.1.0
776a70