Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Documentation/config/http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ http.followRedirects::
the initial request to a remote, but not for subsequent
follow-up HTTP requests. Since git uses the redirected URL as
the base for the follow-up requests, this is generally
sufficient. The default is `initial`.
sufficient. The default is `initial`. Redirects to hosts not
allowed by `safe.host` are refused regardless of this setting.

http.<url>.*::
Any of the http.* options above can be applied selectively to some URLs.
Expand Down
61 changes: 61 additions & 0 deletions Documentation/config/safe.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,64 @@ which id the original user has.
If that is not what you would prefer and want git to only trust
repositories that are owned by root instead, then you can remove
the `SUDO_UID` variable from root's environment before invoking git.

safe.host::
These config entries specify the remote hosts that Git is allowed
to connect to, e.g. when cloning, fetching or pushing. If
`safe.host` is not set, Git connects to any host. Once it is set,
Git refuses to connect to a host that does not match one of the
entries.
+
Each entry has the form `[<scheme>://]<host>[:<port>]`:
+
--
* `<host>` is a host name or IP address, matched case-insensitively.
IPv6 addresses must be enclosed in square brackets, e.g. `[::1]`.
A dot-separated component consisting only of `*` matches exactly one
component of the host name, so `*.example.com` matches
`git.example.com`, but neither `example.com` nor `a.b.example.com`.
A `<host>` of `*` matches any host.
* `<scheme>`, if given, limits the entry to connections made using
that URL scheme, e.g. `https` or `ssh`. Without it, the entry matches
connections made using any scheme. Remote URLs of the scp-like form
`[<user>@]<host>:<path>` use the `ssh` scheme.
* `<port>`, if given, limits the entry to connections to that port.
Without it, the entry matches connections to any port. If the remote
URL does not specify a port, the default port for its scheme is
assumed, e.g. 443 for `https` or 22 for `ssh`.
--
+
For example, `example.com` allows connections to `example.com` using
any scheme and port, `https://example.com` allows only HTTPS
connections to it, `on-prem-tfs:8080` allows connections to
`on-prem-tfs` on port 8080 only, and `https://*` allows HTTPS
connections to any host. Entries with any other form, e.g. containing a
user name or a path, are ignored with a warning.
+
This is a multi-valued setting, i.e. you can add more than one host via
`git config --add`. To reset the list of safe hosts (e.g. to override
any such hosts specified in the system config), add a `safe.host`
entry with an empty value. If no entries follow it, Git will refuse to
connect to any remote host. Setting `safe.host` to `*` allows
connections to all hosts, as if it had not been set at all.
+
This config setting is only respected in protected configuration (see
<<SCOPES>>). This prevents untrusted repositories from tampering with this
value.
+
Repositories on the local filesystem, including those accessed via
`file://` URLs, are not subject to this check. Neither are remote
helpers (see linkgit:gitremote-helpers[7]) other than Git's own helpers
for `http`, `https`, `ftp` and `ftps`, as Git cannot know which hosts
they connect to; use `protocol.allow` to restrict them instead. For
`ssh`, the host checked is the one named in the remote URL, before any
aliasing performed by the SSH client's own configuration. Proxies,
e.g. those configured via `http.proxy` or `core.gitProxy`, are not
checked.
+
For HTTP(S), the host of each redirect (see `http.followRedirects`) and
of each `http-alternates` entry is checked as well, before Git connects
or sends any request to it. If Git was built with, or is running
against, a libcurl older than 7.80.0, it cannot check every request
that follows a redirect, and so does not follow redirects at all when
`safe.host` limits the allowed hosts.
35 changes: 35 additions & 0 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,28 @@ static char *host_end(char **hoststart, int removebrackets)
#define STR_(s) # s
#define STR(s) STR_(s)

/*
* Die unless safe.host allows connecting to "userandhost" (which may
* carry a "user@" prefix) on "port" (NULL for the scheme's default).
*/
static void check_host_allowed(const char *scheme, const char *userandhost,
const char *port)
{
const char *host = strrchr(userandhost, '@');

/*
* No host name contains a '/', but OpenSSH parses a destination
* like "ssh://user@host:port" as a URI, port and all, which would
* slip past the port check below.
*/
if (strchr(userandhost, '/') && safe_hosts_restricted())
die(_("connecting to ssh host '%s' is not allowed by safe.host"),
userandhost);

transport_check_host_allowed(scheme, host ? host + 1 : userandhost,
port ? strtoul(port, NULL, 10) : 0);
}

static void get_host_and_port(char **host, const char **port)
{
char *colon, *end;
Expand All @@ -815,6 +837,17 @@ static void get_host_and_port(char **host, const char **port)
}
}

static void check_hostandport_allowed(const char *scheme,
const char *hostandport)
{
char *host = xstrdup(hostandport);
const char *port = NULL;

get_host_and_port(&host, &port);
check_host_allowed(scheme, host, port);
free(host);
}

static void enable_keepalive(int sockfd)
{
int ka = 1;
Expand Down Expand Up @@ -1294,6 +1327,7 @@ static struct child_process *git_connect_git(int fd[2], char *hostandport,
target_host = xstrdup(hostandport);

transport_check_allowed("git");
check_hostandport_allowed("git", hostandport);
if (strchr(target_host, '\n') || strchr(path, '\n'))
die(_("newline is forbidden in git:// hosts and repo paths"));

Expand Down Expand Up @@ -1531,6 +1565,7 @@ struct child_process *git_connect(int fd[2], const char *url,
strbuf_release(&cmd);
return NULL;
}
check_host_allowed("ssh", ssh_host, port);
conn->trace2_child_class = "transport/ssh";
fill_ssh_args(conn, ssh_host, port, version, flags);
} else {
Expand Down
7 changes: 7 additions & 0 deletions git-curl-compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
#define GIT_CURL_HAVE_CURLINFO_RETRY_AFTER 1
#endif

/**
* CURLOPT_PREREQFUNCTION was added in 7.80.0, released in November 2021.
*/
#if LIBCURL_VERSION_NUM >= 0x075000
#define GIT_CURL_HAVE_CURLOPT_PREREQFUNCTION 1
#endif

/**
* CURLOPT_PROTOCOLS_STR and CURLOPT_REDIR_PROTOCOLS_STR were added in 7.85.0,
* released in August 2022.
Expand Down
5 changes: 5 additions & 0 deletions http-walker.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ static int is_alternate_allowed(const char *url)
warning("ignoring alternate with restricted protocol: %s", url);
return 0;
}
if (!http_is_url_allowed(url, 1)) {
warning(_("ignoring alternate on host not allowed by safe.host: %s"),
url);
return 0;
}

return 1;
}
Expand Down
141 changes: 141 additions & 0 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,125 @@ static void proto_list_append(struct strbuf *list, const char *proto)
strbuf_addstr(list, proto);
}

static int parse_url_host(const char *url, char **scheme, char **host,
unsigned int *port)
{
struct url_info info;

if (!url_normalize(url, &info))
return -1;

*scheme = xmemdupz(info.url, info.scheme_len);
*host = xmemdupz(info.url + info.host_off, info.host_len);
*port = info.port_len ? strtoul(info.url + info.port_off, NULL, 10) : 0;
free(info.url);
return 0;
}

int http_is_url_allowed(const char *url, int quiet)
{
char *scheme, *host;
unsigned int port;
int ret;

if (!safe_hosts_restricted())
return 1;

if (parse_url_host(url, &scheme, &host, &port) < 0) {
if (!quiet)
error(_("unable to determine host for safe.host check"));
return 0;
}

ret = is_host_allowed(scheme, host, port);
if (!ret && !quiet) {
struct strbuf entry = STRBUF_INIT;

format_safe_host_entry(&entry, scheme, host, port);
error(_("connecting to '%s' is not allowed by safe.host"),
entry.buf);
strbuf_release(&entry);
}

free(scheme);
free(host);
return ret;
}

static int check_effective_url(CURL *curl)
{
static char *last_reported;
char *effective_url = NULL;
const char *url;

if (curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL,
&effective_url) != CURLE_OK)
effective_url = NULL;
url = effective_url ? effective_url : "";
if (http_is_url_allowed(url, 1))
return 1;

/* curl may try several addresses for one URL; complain only once */
if (!last_reported || strcmp(last_reported, url)) {
http_is_url_allowed(url, 0);
free(last_reported);
last_reported = xstrdup(url);
}
return 0;
}

/*
* curl calls this after creating each socket, but before connecting it,
* so that we never connect to a host that safe.host does not allow,
* e.g. when following a redirect.
*/
static int check_safe_hosts_sockopt(void *curl, curl_socket_t fd UNUSED,
curlsocktype purpose UNUSED)
{
return check_effective_url(curl) ? CURL_SOCKOPT_OK : CURL_SOCKOPT_ERROR;
}

#ifdef GIT_CURL_HAVE_CURLOPT_PREREQFUNCTION
/*
* curl calls this before sending each request, which also covers those
* sent over a reused connection, e.g. to a proxy, where no new socket
* is created.
*/
static int check_safe_hosts_prereq(void *curl,
char *conn_primary_ip UNUSED,
char *conn_local_ip UNUSED,
int conn_primary_port UNUSED,
int conn_local_port UNUSED)
{
return check_effective_url(curl) ?
CURL_PREREQFUNC_OK : CURL_PREREQFUNC_ABORT;
}
#endif

/*
* Install the safe.host callbacks on "curl". Their data pointers must
* be set to each handle duplicated from it; see get_active_slot().
*/
static void init_safe_hosts_callbacks(CURL *curl)
{
int vets_every_request = 0;

curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION,
check_safe_hosts_sockopt);
#ifdef GIT_CURL_HAVE_CURLOPT_PREREQFUNCTION
vets_every_request = curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION,
check_safe_hosts_prereq) == CURLE_OK;
#endif

/*
* Without CURLOPT_PREREQFUNCTION (at build or run time), curl
* could follow a redirect to another host over a connection it
* reuses, e.g. one to a proxy, without asking us. Follow none.
*/
if (!vets_every_request)
http_follow_config = HTTP_FOLLOW_NONE;
}

static long get_curl_allowed_protocols(int from_user, struct strbuf *list)
{
long bits = 0;
Expand Down Expand Up @@ -1391,6 +1510,17 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
char *normalized_url;
struct urlmatch_config config = URLMATCH_CONFIG_INIT;

if (safe_hosts_restricted()) {
char *scheme, *host;
unsigned int port;

if (parse_url_host(url, &scheme, &host, &port) < 0)
die(_("unable to determine host for safe.host check"));
transport_check_host_allowed(scheme, host, port);
free(scheme);
free(host);
}

config.section = "http";
config.key = NULL;
config.collect_fn = http_options;
Expand Down Expand Up @@ -1511,6 +1641,8 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
set_long_from_env(&http_max_retry_time, "GIT_HTTP_MAX_RETRY_TIME");

curl_default = get_curl_handle();
if (safe_hosts_restricted())
init_safe_hosts_callbacks(curl_default);
}

void http_cleanup(void)
Expand Down Expand Up @@ -1610,6 +1742,15 @@ struct active_request_slot *get_active_slot(void)
slot->curl = curl_easy_duphandle(curl_default);
if (!slot->curl)
die("curl_easy_duphandle failed");
if (safe_hosts_restricted()) {
/* point the callbacks at this handle, not curl_default */
curl_easy_setopt(slot->curl, CURLOPT_SOCKOPTDATA,
slot->curl);
#ifdef GIT_CURL_HAVE_CURLOPT_PREREQFUNCTION
curl_easy_setopt(slot->curl, CURLOPT_PREREQDATA,
slot->curl);
#endif
}
curl_session_count++;
}

Expand Down
6 changes: 6 additions & 0 deletions http.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ void step_active_slots(void);
void http_init(struct remote *remote, const char *url,
int proactive_auth);
void http_cleanup(void);

/*
* Returns 1 if safe.host allows connecting to the host named in "url",
* or 0 otherwise, in which case an error is reported unless "quiet".
*/
int http_is_url_allowed(const char *url, int quiet);
struct curl_slist *http_copy_default_headers(void);

extern long int git_curl_ipresolve;
Expand Down
1 change: 1 addition & 0 deletions t/lib-httpd/apache.conf
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ RewriteRule ^/smart-redir-temp/(.*)$ /smart/$1 [R=302]
RewriteRule ^/smart-redir-auth/(.*)$ /auth/smart/$1 [R=301]
RewriteRule ^/smart-redir-limited/(.*)/info/refs$ /smart/$1/info/refs [R=301]
RewriteRule ^/ftp-redir/(.*)$ ftp://localhost:1000/$1 [R=302]
RewriteRule ^/smart-redir-localhost/(.*)$ %{REQUEST_SCHEME}://localhost:%{SERVER_PORT}/smart/$1 [R=302]

RewriteRule ^/loop-redir/x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-(.*) /$1 [R=302]
RewriteRule ^/loop-redir/(.*)$ /loop-redir/x-$1 [R=302]
Expand Down
2 changes: 2 additions & 0 deletions t/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ integration_tests = [
't5813-proto-disable-ssh.sh',
't5814-proto-disable-ext.sh',
't5815-submodule-protos.sh',
't5816-safe-host.sh',
't5817-safe-host-http.sh',
't5900-repo-selection.sh',
't6000-rev-list-misc.sh',
't6001-rev-list-graft.sh',
Expand Down
Loading
Loading