@@ -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.
249281fn 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