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

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