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