Skip to content

Commit 5088d3b

Browse files
peffgitster
authored andcommitted
transport: refactor protocol whitelist code
The current callers only want to die when their transport is prohibited. But future callers want to query the mechanism without dying. Let's break out a few query functions, and also save the results in a static list so we don't have to re-parse for each query. Based-on-a-patch-by: Blake Burkhart <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33cfccb commit 5088d3b

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

transport.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -909,18 +909,40 @@ static int external_specification_len(const char *url)
909909
return strchr(url, ':') - url;
910910
}
911911

912-
void transport_check_allowed(const char *type)
912+
static const struct string_list *protocol_whitelist(void)
913913
{
914-
struct string_list allowed = STRING_LIST_INIT_DUP;
915-
const char *v = getenv("GIT_ALLOW_PROTOCOL");
914+
static int enabled = -1;
915+
static struct string_list allowed = STRING_LIST_INIT_DUP;
916+
917+
if (enabled < 0) {
918+
const char *v = getenv("GIT_ALLOW_PROTOCOL");
919+
if (v) {
920+
string_list_split(&allowed, v, ':', -1);
921+
string_list_sort(&allowed);
922+
enabled = 1;
923+
} else {
924+
enabled = 0;
925+
}
926+
}
916927

917-
if (!v)
918-
return;
928+
return enabled ? &allowed : NULL;
929+
}
930+
931+
int is_transport_allowed(const char *type)
932+
{
933+
const struct string_list *allowed = protocol_whitelist();
934+
return !allowed || string_list_has_string(allowed, type);
935+
}
919936

920-
string_list_split(&allowed, v, ':', -1);
921-
if (!unsorted_string_list_has_string(&allowed, type))
937+
void transport_check_allowed(const char *type)
938+
{
939+
if (!is_transport_allowed(type))
922940
die("transport '%s' not allowed", type);
923-
string_list_clear(&allowed, 0);
941+
}
942+
943+
int transport_restrict_protocols(void)
944+
{
945+
return !!protocol_whitelist();
924946
}
925947

926948
struct transport *transport_get(struct remote *remote, const char *url)

transport.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,24 @@ struct transport {
132132
/* Returns a transport suitable for the url */
133133
struct transport *transport_get(struct remote *, const char *);
134134

135+
/*
136+
* Check whether a transport is allowed by the environment. Type should
137+
* generally be the URL scheme, as described in Documentation/git.txt
138+
*/
139+
int is_transport_allowed(const char *type);
140+
135141
/*
136142
* Check whether a transport is allowed by the environment,
137-
* and die otherwise. type should generally be the URL scheme,
138-
* as described in Documentation/git.txt
143+
* and die otherwise.
139144
*/
140145
void transport_check_allowed(const char *type);
141146

147+
/*
148+
* Returns true if the user has attempted to turn on protocol
149+
* restrictions at all.
150+
*/
151+
int transport_restrict_protocols(void);
152+
142153
/* Transport options which apply to git:// and scp-style URLs */
143154

144155
/* The program to use on the remote side to send a pack */

0 commit comments

Comments
 (0)