Skip to content

Commit 20e4f60

Browse files
committed
Auto merge of #11381 - ehuss:beta-fix-safe-directory, r=weihanglo
[beta-1.66] Backport fix for git2 safe-directory disable This is a beta backport of #11366.
2 parents 7e484fc + 6774bc1 commit 20e4f60

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/bin/cargo/cli.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Run with 'cargo -Z [FLAG] [COMMAND]'",
149149
}
150150
};
151151
config_configure(config, &expanded_args, subcommand_args, global_args)?;
152-
super::init_git_transports(config);
152+
super::init_git(config);
153153

154154
execute_subcommand(config, cmd, subcommand_args)
155155
}

src/bin/cargo/main.rs

+32-23
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,38 @@ fn search_directories(config: &Config) -> Vec<PathBuf> {
246246
path_dirs
247247
}
248248

249+
/// Initialize libgit2.
250+
fn init_git(config: &Config) {
251+
// Disabling the owner validation in git can, in theory, lead to code execution
252+
// vulnerabilities. However, libgit2 does not launch executables, which is the foundation of
253+
// the original security issue. Meanwhile, issues with refusing to load git repos in
254+
// `CARGO_HOME` for example will likely be very frustrating for users. So, we disable the
255+
// validation.
256+
//
257+
// For further discussion of Cargo's current interactions with git, see
258+
//
259+
// https://github.com/rust-lang/rfcs/pull/3279
260+
//
261+
// and in particular the subsection on "Git support".
262+
//
263+
// Note that we only disable this when Cargo is run as a binary. If Cargo is used as a library,
264+
// this code won't be invoked. Instead, developers will need to explicitly disable the
265+
// validation in their code. This is inconvenient, but won't accidentally open consuming
266+
// applications up to security issues if they use git2 to open repositories elsewhere in their
267+
// code.
268+
unsafe {
269+
git2::opts::set_verify_owner_validation(false)
270+
.expect("set_verify_owner_validation should never fail");
271+
}
272+
273+
init_git_transports(config);
274+
}
275+
276+
/// Configure libgit2 to use libcurl if necessary.
277+
///
278+
/// If the user has a non-default network configuration, then libgit2 will be
279+
/// configured to use libcurl instead of the built-in networking support so
280+
/// that those configuration settings can be used.
249281
fn init_git_transports(config: &Config) {
250282
// Only use a custom transport if any HTTP options are specified,
251283
// such as proxies or custom certificate authorities. The custom
@@ -274,27 +306,4 @@ fn init_git_transports(config: &Config) {
274306
unsafe {
275307
git2_curl::register(handle);
276308
}
277-
278-
// Disabling the owner validation in git can, in theory, lead to code execution
279-
// vulnerabilities. However, libgit2 does not launch executables, which is the foundation of
280-
// the original security issue. Meanwhile, issues with refusing to load git repos in
281-
// `CARGO_HOME` for example will likely be very frustrating for users. So, we disable the
282-
// validation.
283-
//
284-
// For further discussion of Cargo's current interactions with git, see
285-
//
286-
// https://github.com/rust-lang/rfcs/pull/3279
287-
//
288-
// and in particular the subsection on "Git support".
289-
//
290-
// Note that we only disable this when Cargo is run as a binary. If Cargo is used as a library,
291-
// this code won't be invoked. Instead, developers will need to explicitly disable the
292-
// validation in their code. This is inconvenient, but won't accidentally open consuming
293-
// applications up to security issues if they use git2 to open repositories elsewhere in their
294-
// code.
295-
unsafe {
296-
if git2::opts::set_verify_owner_validation(false).is_err() {
297-
return;
298-
}
299-
}
300309
}

0 commit comments

Comments
 (0)