Repository navigation
safe.host: limit the hosts Git connects to - #2329
Draft
mjcheetham wants to merge 3 commits into
Draft
mjcheetham wants to merge 3 commits into
mjcheetham wants to merge 3 commits into
Conversation
The '*' glob matching of host names used by the http.<url>.* family of configuration is buried inside match_urls(), and requires both sides to be wrapped up in a fully normalized url_info. An upcoming safe.host setting wants to match host names using the same rules, so that '*.example.com' means the same thing there as it does in http.<url>.*, but its entries are not URLs and so cannot be fed through url_normalize(). Split the label-wise comparison out into urlmatch_host(), which takes a bare host and pattern, and keep match_host() as a thin wrapper over it. Assisted-by: Claude Opus 5.5 Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
There is no way to limit which remote hosts Git will talk to. protocol.allow can restrict *how* Git connects, but once a protocol such as ssh is allowed, a remote URL, whether typed by the user, recorded in .gitmodules or produced by url.<base>.insteadOf, can point Git at any host. Environments that want to make sure source code only travels to and from known hosting services, e.g. an organisation's GitHub or Azure DevOps instance, have to resort to network-level controls. Introduce a multi-valued safe.host setting, modelled on safe.directory, that lists the hosts Git may connect to. When it is unset every host is allowed, preserving today's behaviour. Like the other safe.* settings it is only read from protected configuration, so that a repository cannot widen (or narrow) the list for itself. Entries take the form "[<scheme>://]<host>[:<port>]". The scheme and port are optional and match anything when omitted, so that trusting a host is simply a matter of naming it, while "https://example.com" or "on-prem-tfs:8080" can be used to be more specific. Requiring a scheme would make the common case more verbose, and would leave scp-like ssh URLs, which have no scheme, awkward to describe. Treating a missing port as "the scheme's default port" was considered too, but the trust being expressed is in the host, and users are unlikely to expect "example.com" to reject ssh://example.com:2222. Host names accept the same single-component '*' wildcard as http.<url>.*, plus a bare '*' to match any host so that e.g. "https://*" can require HTTPS without naming hosts. Enforce the setting for the git and ssh transports in git_connect(), where the host and port have already been parsed out of the URL, just before connecting. HTTP is left to a following commit, as curl may be redirected to other hosts behind our back. Other remote helpers are not covered, since Git cannot know where they connect; protocol.allow remains the tool for those. Assisted-by: Claude Opus 5.5 Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
The previous commit taught the git and ssh transports to refuse to connect to hosts not listed in safe.host. HTTP needs more care, as the URL handed to the remote helper is only where the conversation starts: curl may follow redirects to other hosts (for the initial request by default, or for any request with http.followRedirects set to true), and the dumb HTTP walker can be pointed at other hosts via http-alternates. Check the remote URL up front in http_init(), so that a disallowed remote fails before any connection is attempted, with the same advice the native transports give. This also covers the other callers of http_init(), such as "git http-fetch" downloading packfile URIs, and bundle URIs fetched through the remote helper. For everything else, have curl call back to check its effective URL: - CURLOPT_SOCKOPTFUNCTION runs after curl creates each socket but before it connects it, so a redirect to a disallowed host is refused before any TCP or TLS traffic (and so before any client certificate is presented). Unlike CURLOPT_OPENSOCKETFUNCTION it does not require us to create the socket ourselves, which would not mix well with our socket() wrapper on Windows. - CURLOPT_PREREQFUNCTION runs before each request is sent. This catches requests that curl sends over a connection it reuses, e.g. one to an HTTP proxy, where no new socket is created. The callbacks are only installed when safe.host restricts the allowed hosts, so the default configuration is unaffected. Name resolution of a disallowed redirect target can still happen, as there is no hook that sees every lookup (e.g. those satisfied from curl's DNS cache). CURLOPT_PREREQFUNCTION needs libcurl 7.80.0, which is newer than the oldest version we support. Rather than letting redirects slip through reused connections when built against, or running with, an older libcurl, do not follow redirects at all in that case when safe.host is restricted. This breaks setups that rely on redirects, but only for users who opted into safe.host, and fails closed. Check http-alternates explicitly as well, so that an alternate on a disallowed host is skipped with a warning, just like one using a disallowed protocol, rather than surfacing as a failed request. Assisted-by: Claude Opus 5.5 Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
mjcheetham
force-pushed
the
safe-hosts
branch
from
November 24, 2026 16:45
480fdd3 to
8189ec1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Git gives users a fair amount of control over how it talks to remotes (
protocol.allow,GIT_ALLOW_PROTOCOL), but none over where. Once a protocol such as HTTPS or SSH is allowed, a remote URL can point Git at any host, whether it was typed by the user, recorded in .gitmodules, rewritten byurl.<base>.insteadOf, or reached by following an HTTP redirect or an http-alternates entry.A major motivation is that many attacks on Git clients depend on the victim connecting to a remote the attacker controls, whether to serve a malicious repository or to exploit a bug in how the client handles the server's responses. Usually that is within the user's control: they choose what to clone or fetch, and can inspect a URL before using it. Recursive submodule clones take that choice away, as Git fetches from whatever URLs the superproject's .gitmodules lists, and then from those listed by each submodule in turn, before the user has had any chance to look at them.
Organisations that want to be sure their source code only travels to and from hosting services they trust, e.g. their GitHub or Azure DevOps instance or a server they host internally, today have only network-level controls such as proxies and firewalls to rely on, and those are not always in place on developer machines and build agents.
This series adds a multi-valued safe.host setting, modelled on safe.directory, that lists the hosts Git is allowed to connect to:
When
safe.hostis not set, every host is allowed, so nothing changes unless a user or administrator opts in. As with the othersafe.*settings, it is only read from protected configuration (system, global and command line), so a repository cannot change the list for itself. An empty value resets the list, and"*"allows all hosts, just likesafe.directory. A refused connection fails with advice on how to allow it, again likesafe.directory.To be clear,
safe.hostdoes not replace network-level protections in environments that need them. It only governs the connections Git itself makes, and it is a default rather than a lock: a user can reset a list set in the system config from their own global config. Nor does it know who owns the repositories on a trusted host, so trusting github.com trusts every repository hosted there. It is meant as one more layer of defence in depth, so that Git cannot be quietly pointed at a server nobody chose to trust.Entry syntax
Each entry has the form "[://][:]". The scheme and port are both optional, and match anything when omitted:
If the remote URL has no port, the scheme's default port is assumed, so "example.com:443" matches https://example.com/repo.git. scp-like URLs ("user@host:path") use the ssh scheme. A host component of
"*"matches exactly one DNS label, as inhttp.<url>.*, so "*.example.com" matches git.example.com but neither example.com nor a.b.example.com. IPv6 literals must be bracketed. Entries containing a path or a user name are ignored with a warning rather than being silently misread.The main design question was whether scheme and port should be supported, required or forbidden. I went with optional:
Where it is enforced
git_connect(), using the host and port it has already parsed, just before connecting.http_init(). The harder part is that curl can be sent to another host after that check, by a redirect or an http-alternates entry. Patch 3 handles this with two curl callbacks that inspect the effective URL:CURLOPT_SOCKOPTFUNCTIONrefuses before a new connection is made, andCURLOPT_PREREQFUNCTIONrefuses before a request is sent over a reused one (e.g. to a proxy). An http-alternates entry on a disallowed host is skipped with a warning, like one using a disallowed protocol.Some things are deliberately out of scope:
"foo::<url>", unknown schemes) are not checked, as Git cannot know where they connect; protocol.allow already covers them.http.proxy,core.gitProxy) are not checked either.Known limitations
CURLOPT_PREREQFUNCTIONneeds libcurl 7.80.0, newer than our minimum of 7.61.0. When built against, or running with, an older libcurl, Git refuses to follow redirects at all while safe.host is restricted, rather than following them unchecked. This fails closed, and only affects people who have opted in.Question for reviewers
On old libcurl, is disabling redirects the right fallback, or would people rather Git refuse to use HTTP at all while safe.host is set?
Patches
1/3 urlmatch: expose host glob matching
A small refactor so that safe.host can share the wildcard matching used by http..*.
2/3 transport: add safe.host to restrict remote hosts
The setting and its documentation, and enforcement for git:// and ssh, with tests in t5816.
3/3 http: enforce safe.host
Enforcement for HTTP(S) and FTP(S), including redirects and http-alternates, with tests in t5817.
Thanks,
Matthew
cc: Johannes Schindelin johannes.schindelin@gmx.de