关于版本控制:如何让git接受自签名证书?

How can I make git accept a self signed certificate?

使用Git,有没有办法告诉它接受自签名证书?

我使用https服务器来托管git服务器但是现在证书是自签名的。

当我第一次尝试在那里创建回购时:

1
git push origin master -f

我收到错误:

1
2
3
4
error: Cannot access URL    
https://the server/git.aspx/PocketReferences/, return code 22

fatal: git-http-push failed

  • 你怎么知道问题是证书?
  • 从PC而不是另一个用户的Git工具让他们忽略证书,它的工作原理。 从Mac,我无法弄清楚如何忽略。
  • 我得到的错误,使用git 2.1.1:"致命:无法访问'https://.../project.git/':SSL证书问题:证书链中的自签名证书"
  • 在OSX / macintosh上,似乎git不会使用sslcainfo选项。 如果您可以成功使用curl --cacert来提取您的repo路径但git不起作用,您应该将证书添加到神秘的OSX Keychain程序中。 更多这里superuser.com/questions/605900/…


永久接受特定证书

尝试http.sslCAPathhttp.sslCAInfo。 Adam Spiers的回答给出了一些很好的例子。这是该问题最安全的解决方案。

禁用单个git命令的TLS / SSL验证

尝试使用正确的配置变量将-c传递给git,或者使用Flow的答案:

1
git -c http.sslVerify=false clone https://example.com/path/to/git

禁用特定存储库的SSL验证

如果存储库完全在您的控制之下,您可以尝试:

1
git config http.sslVerify false

全局禁用TLS(/ SSL)证书验证是一种非常不安全的做法。不要这样做。不要使用--global修饰符发出上述命令。

git中有很多SSL配置选项。从git config的手册页:

1
2
3
4
5
6
7
8
9
10
11
12
http.sslVerify
    Whether to verify the SSL certificate when fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_NO_VERIFY environment variable.

http.sslCAInfo
    File containing the certificates to verify the peer with when fetching or pushing
    over HTTPS. Can be overridden by the GIT_SSL_CAINFO environment variable.

http.sslCAPath
    Path containing files with the CA certificates to verify the peer with when
    fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_CAPATH environment variable.

一些其他有用的SSL配置选项:

1
2
3
4
5
6
7
8
9
10
11
12
http.sslCert
    File containing the SSL certificate when fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_CERT environment variable.

http.sslKey
    File containing the SSL private key when fetching or pushing over HTTPS.
    Can be overridden by the GIT_SSL_KEY environment variable.

http.sslCertPasswordProtected
    Enable git's password prompt for the SSL certificate. Otherwise OpenSSL will
    prompt the user, possibly many times, if the certificate or private key is encrypted.
    Can be overridden by the GIT_SSL_CERT_PASSWORD_PROTECTED environment variable.

  • 'git config --global http.sslVerify false'成功了。谢谢!
  • 您永远不应该全局禁用TLS(/ SSL)证书验证。
  • @Flow - 我完全赞同。我已经编辑了这个(现在很老的)答案,对于禁用TLS / SSL证书验证更具争议性。
  • 对我来说这git -c http.sslVerify=false clone https://domain.com/path/to/git解决了我的问题,谢谢......
  • 我非常喜欢克隆项目的一次性语法,该项目为将来的所有请求安装组织证书。
  • @Flow如果我们的工作环境中我们的雇主是MITM,那么全球禁用TLS / SSL的替代方案是什么?
  • 通常"证书固定",可能与首次使用信任(TOFU)相结合,是最后一道防线,在大多数情况下应该可以使用。
  • 我试过:"git -c http.sslVerify = false pull"和"git config --global http.sslVerify false"后跟"git pull",但他们都给了我:致命:无法访问'github.com/ ndattani / AI_ENERGIES.git':SSL连接错误
  • @StevenVascellaro检查AperioOculus的答案如下。我在一个企业环境中,一步一步地为他解决了他的问题。 stackoverflow.com/a/49515609/1047907
  • 晶圆厂!这个工作就像一个魅力git配置http.sslVerify假太多ssl证书安装否则:)谢谢
  • @Flow既然我们在这里迂腐 - 你永远不应该做的,永远不要做出绝对的陈述。语境是王道。随着背景的变化,没有一个声明成立。
  • 是的,不。您始终可以固定证书,而不是完全禁用验证。
  • 正如Steven Vascellaro指出的那样:如果公司网络的唯一出路是公司代理必须处理的证书是公司的证书,并且针对MITM的保护有代理需要照顾。所以在那种情况下git config --global http.sslVerify false没有安全问题。
  • 顺便说一句,以前,我使用了解决方案git config --global http.sslCAInfo /path/to/my-cert。后来我发现在Windows 10上的github.com上git clone其他git存储库时会导致错误"无法获得本地颁发者证书"(git版本2.16.2.windows.1,Git Extensions 2.50.02)。因此,我建议在另一个线程中限制此答案中的http.sslCAInfo主机:stackoverflow.com/a/47196562/1323552(即git config --global http.https://your.host/.sslCAInfo /path/to/your-cert)


您可以将GIT_SSL_NO_VERIFY设置为true

1
GIT_SSL_NO_VERIFY=true git clone https://example.com/path/to/git

或者配置Git不要在命令行上验证连接:

1
git -c http.sslVerify=false clone https://example.com/path/to/git

请注意,如果您未验证SSL / TLS证书,则您很容易受到MitM攻击。

  • 您还可以使用git上的-c标志来修改单个命令的配置值。我认为这种语法更清晰。
  • 啊,我不知道git中的-c。我实际上认为这是更清洁的解决方案,而不是污染环境。 :)
  • @SkylarSaveland请注意,git -c http.sslVerify=false 也可以通过中间人工作。
  • 这个:git config http.sslVerify false,给我工作..谢谢
  • 我应该注意的是,这个解决方案可以让你开启中间人攻击。
  • 答案仅描述最不安全的选项。


我不是[EDIT:现有答案的原始版本]的忠实粉丝,因为禁用安全检查应该是最后的手段,而不是第一个提供的解决方案。即使您在第一次收到时无法信任自签名证书而没有其他一些额外的验证方法,但使用证书进行后续git操作至少会使您的生活更加困难,这种攻击只有在您下载证书后才会发生。换句话说,如果您下载的证书是真实的,那么从那时起您就会很好。相比之下,如果您只是禁用验证,那么您在任何时候都可以接受任何类型的中间人攻击。

举一个具体的例子:着名的repo.or.cz存储库提供了一个自签名证书。我可以下载该文件,将其放在/etc/ssl/certs之类的地方,然后执行:

1
2
3
4
5
6
7
# Initial clone
GIT_SSL_CAINFO=/etc/ssl/certs/rorcz_root_cert.pem \
    git clone https://repo.or.cz/org-mode.git

# Ensure all future interactions with origin remote also work
cd org-mode
git config http.sslCAInfo /etc/ssl/certs/rorcz_root_cert.pem

请注意,在此处使用local git config(即不使用--global)意味着此自签名证书仅受此特定存储库的信任,这很好。它也比使用GIT_SSL_CAPATH更好,因为它消除了git通过可能被泄露的不同证书颁发机构进行验证的风险。

  • 巧合的是,http.sslCAPath使用了libcurl的ssl_capath逻辑。我认为您实际上可以在/etc/ssl/certs/目录中存储任意数量的证书,并且它可以有效地整理您需要的所有内容。我没有测试过这个,请注意,但是它可能允许你使用带有一大堆证书的--global。值得测试。
  • 考虑到完全禁用SSL验证的风险,以及问题是"如何让git接受自签名证书?",这应该是公认的答案。
  • 在理想的世界中,会有类似git config http.validCertFingerprint 的东西
  • @AdamSpiers:所以repo.or.cz提供了自签名证书,但GitHub呢?
  • @AlLelopath GitHub的证书由DigiCert签署。你为什么要问?
  • 我正在尝试从GitHub克隆(并获得'自签名证书错误')。我是否从Digicert获得证书?有成本吗?
  • @AlLelopath这很奇怪。请指定您使用的确切git clone命令。
  • 我发布了一个单独的问题:stackoverflow.com/questions/32276466/…
  • 互联网上唯一真正适用于我的场景的答案。这是私有的Composer VCS库,托管在SSL上的自托管Gitlab上,我需要在git版本化的项目中使用。
  • 是否有必要再次克隆代码。我使用ssh克隆了代码,然后运行GIT_SSL_CAINFO = / etc / ssl / certs / xyz.cer和git config http.sslCAInfo /etc/ssl/certs/xyz.cer但它仍然给我错误"错误设置证书验证位置:CAfile:/etc/ssl/certs/xyz.cer CApath:none"
  • 用于首先设置环境变量然后更改基于项目的配置的绝佳用例。在设置Git服务器时遇到类似的问题。我试图将私钥传递给git clone。首先设置环境变量,然后设置"core.sshCommand"为我解决了问题。


Git自签名证书配置

TL;博士

NEVER disable all SSL verification!

This creates a bad security culture. Don't be that person.

您所追求的配置键是:

  • http.sslverify - 总是如此。见上文说明。

这些用于配置您信任的主机证书

  • http.sslCAPath
  • http.sslCAInfo

这些用于配置您的证书以响应SSL挑战。

  • http.sslCert
  • http.sslCertPasswordProtected

有选择地将上述设置应用于特定主机。

  • http..*

自签名证书颁发机构的全局.gitconfig

对于我自己和同事们来说,这里是我们如何设法让自签名证书在不禁用sslVerify的情况下工作。编辑.gitconfig以使用git config --global -e添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Specify the scheme and host as a 'context' that only these settings apply
# Must use Git v1.8.5+ for these contexts to work
[credential"https://your.domain.com"]
  username = user.name

  # Uncomment the credential helper that applies to your platform
  # Windows
  # helper = manager

  # OSX
  # helper = osxkeychain

  # Linux (in-memory credential helper)
  # helper = cache

  # Linux (permanent storage credential helper)
  # https://askubuntu.com/a/776335/491772

# Specify the scheme and host as a 'context' that only these settings apply
# Must use Git v1.8.5+ for these contexts to work
[http"https://your.domain.com"]
  ##################################
  # Self Signed Server Certificate #
  ##################################

  # MUST be PEM format
  # Some situations require both the CAPath AND CAInfo
  sslCAInfo = /path/to/selfCA/self-signed-certificate.crt
  sslCAPath = /path/to/selfCA/
  sslVerify = true

  ###########################################
  # Private Key and Certificate information #
  ###########################################

  # Must be PEM format and include BEGIN CERTIFICATE / END CERTIFICATE,
  # not just the BEGIN PRIVATE KEY / END PRIVATE KEY for Git to recognise it.
  sslCert = /path/to/privatekey/myprivatecert.pem

  # Even if your PEM file is password protected, set this to false.
  # Setting this to true always asks for a password even if you don't have one.
  # When you do have a password, even with this set to false it will prompt anyhow.
  sslCertPasswordProtected = 0

参考文献:

  • Git凭证
  • Git Credential商店
  • 使用Gnome Keyring作为凭证存储
  • Git Config http。。* Git v1.8.5支持

git clone -ing时指定config

如果您需要在每个repo的基础上应用它,文档会告诉您只需在repo目录中运行git config --local。那么当你没有在当地克隆回购时这没用,现在是吗?

您可以通过如上设置全局配置来执行global -> local hokey-pokey,然后在克隆后将这些设置复制到本地repo配置...

或者你可以做的是在git clone指定配置命令,一旦克隆就应用于目标仓库。

1
2
3
4
5
6
7
8
# Declare variables to make clone command less verbose    
OUR_CA_PATH=/path/to/selfCA/
OUR_CA_FILE=$OUR_CA_PATH/self-signed-certificate.crt
MY_PEM_FILE=/path/to/privatekey/myprivatecert.pem
SELF_SIGN_CONFIG="-c http.sslCAPath=$OUR_CA_PATH -c http.sslCAInfo=$OUR_CA_FILE -c http.sslVerify=1 -c http.sslCert=$MY_PEM_FILE -c http.sslCertPasswordProtected=0"

# With this environment variable defined it makes subsequent clones easier if you need to pull down multiple repos.
git clone $SELF_SIGN_CONFIG https://mygit.server.com/projects/myproject.git myproject/

一个班轮

编辑:请参阅VonC的答案,指出有关特定git版本从2.14.x / 2.15到这一个班轮的绝对路径和相对路径的警告

1
git clone -c http.sslCAPath="/path/to/selfCA" -c http.sslCAInfo="/path/to/selfCA/self-signed-certificate.crt" -c http.sslVerify=1 -c http.sslCert="/path/to/privatekey/myprivatecert.pem" -c http.sslCertPasswordProtected=0 https://mygit.server.com/projects/myproject.git myproject/

CentOS unable to load client key

如果你在CentOS上尝试这个,你的.pem文件给你

1
unable to load client key:"-8178 (SEC_ERROR_BAD_KEY)"

然后,您将需要此StackOverflow答案,了解curl如何使用NSS而不是Open SSL。

你想要从源代码重建curl

1
2
3
4
5
6
7
8
9
10
11
12
git clone http://github.com/curl/curl.git curl/
cd curl/
# Need these for ./buildconf
yum install autoconf automake libtool m4 nroff perl -y
#Need these for ./configure
yum install openssl-devel openldap-devel libssh2-devel -y

./buildconf
su # Switch to super user to install into /usr/bin/curl
./configure --with-openssl --with-ldap --with-libssh2 --prefix=/usr/
make
make install

重启计算机,因为libcurl仍然作为共享库在内存中

Python,pip和conda

相关:如何将自定义CA根证书添加到Windows中的pip使用的CA Store?

  • 在Git接受之前,我必须确保自签名服务器证书是PEM格式的。此外,上面的一些答案表明只需要使用http.sslCAPath提供cert文件夹的路径。就我而言,我必须使用http.sslCAInfo来指定特定文件。允许Git连接到我们的私有GitHub而不禁用SSL验证。
  • @Zarepheth感谢您提供的信息。我遇到了同样需要CAPath和CAInfo的问题。由于我们的CA Cert是PEM格式,我忽略了记录它。我已经用这些补充更新了答案。很高兴您能够安全地连接。
  • 如果您被迫使用HTTPS进行克隆,并且不能仅使用SSH来绕过证书混乱,这可能是最好的长期"修复"答案。
  • 我正准备添加这个答案!很高兴其他人已经发现了它。


我一直遇到这个问题,所以编写了一个脚本从服务器下载自签名证书并将其安装到?/ .gitcerts,然后更新git-config指向这些证书。它存储在全局配置中,因此您只需要为每个远程运行一次。

https://github.com/iwonbigbro/tools/blob/master/bin/git-remote-install-cert.sh

  • 很好,虽然选择使用本地配置而不是全局更好。
  • 您可以随时分叉并删除--global选项;-)
  • 这非常棒,它是批量生产的吗?


这个答案摘自Michael Kauffman撰写的这篇文章。

使用Git for Windows和公司SSL证书

问题:

如果您拥有公司SSL证书并希望从控制台或VSCode克隆您的存储库,则会出现以下错误:

致命:无法访问'https:// myserver / tfs / DefaultCollection / _git / Proj /':SSL证书问题:无法获取本地颁发者证书

解:

  • 将根自签名证书导出到文件。您可以在浏览器中执行此操作。

  • 在git文件夹中找到"ca-bundle.crt"文件(当前版本C: Program Files Git usr ssl certs,但过去已更改)。将文件复制到您的用户配置文件。使用VSCode等文本编辑器打开它,并将导出的证书的内容添加到文件的末尾。

  • 现在我们必须配置git来使用新文件:

    git config --global http.sslCAInfo C:/Users//ca-bundle.crt

    这会将以下条目添加到用户配置文件根目录中的.gitconfig文件中。

    [http]
    sslCAInfo = C:/Users//ca-bundle.crt


    检查您的防病毒和防火墙设置。

    从一天到另一天,git不再起作用了。通过以上描述,我发现卡巴斯基在中间放置了一个自签名的反病毒个人根证书。我没有按照上面的说明让Git接受该证书。我放弃了。对我有用的是禁用扫描加密连接的功能。

  • 打开卡巴斯基
  • 设置>其他>网络>不扫描加密连接
  • 在此之后,git再次启用sslVerify。

    注意。这对我来说仍然不能令人满意,因为我希望我的防病毒功能处于活动状态。在高级设置中,卡巴斯基显示了一个不适用于该功能的网站列表。 Github未被列为其中之一。我将在卡巴斯基论坛上查看。似乎有一些主题,例如
    https://forum.kaspersky.com/index.php?/topic/395220-kis-interfering-with-git/&tab=comments#comment-2801211


    我的回答可能会迟到但对我有用。它可能对某人有所帮助。

    我尝试了上面提到的步骤,但没有解决问题。

    试试这个git config --global http.sslVerify false


    在Windows上使用64位版本的Git,只需将自签名CA证书添加到这些文件中:

    • C: Program Files Git mingw64 ssl certs ca-bundle.crt
    • C: Program Files Git mingw64 ssl certs ca-bundle.trust.crt

    如果只是服务器自签名证书,请将其添加到

    • C: Program Files Git mingw64 ssl cert.pem

    当您使用sslKey或sslCert使用一个衬垫时要小心,如Josh Peak的答案:

    1
    2
    3
    4
    5
    6
    git clone -c http.sslCAPath="/path/to/selfCA" \
      -c http.sslCAInfo="/path/to/selfCA/self-signed-certificate.crt" \
      -c http.sslVerify=1 \
      -c http.sslCert="/path/to/privatekey/myprivatecert.pem" \
      -c http.sslCertPasswordProtected=0 \
    https://mygit.server.com/projects/myproject.git myproject

    只有Git 2.14.x / 2.15(2015年第3季度)才能正确解释像~username/mykey这样的路径(虽然它仍然可以解释像/path/to/privatekey这样的绝对路径)。

    见Junio C Hamano(gitster)的提交8d15496(2017年7月20日)。
    帮助:Charles Bailey(hashpling)。
    (Junio C Hamano合并 - gitster - 提交17b1e1d,2017年8月11日)

    http.c: http.sslcert and http.sslkey are both pathnames

    Back when the modern http_options() codepath was created to parse
    various http.* options at 29508e1 ("Isolate shared HTTP request
    functionality", 2005-11-18, Git 0.99.9k), and then later was corrected for
    interation between the multiple configuration files in 7059cd9
    ("http_init(): Fix config file parsing", 2009-03-09, Git 1.6.3-rc0), we parsed
    configuration variables like http.sslkey, http.sslcert as plain
    vanilla strings, because git_config_pathname() that understands
    "~[username]/" prefix did not exist.

    Later, we converted some of them (namely, http.sslCAPath and http.sslCAInfo) to use the function, and added variables like http.cookeyFile http.pinnedpubkey to use the function from the beginning. Because of that, these variables all understand"~[username]/" prefix.

    Make the remaining two variables, http.sslcert and http.sslkey, also
    aware of the convention, as they are both clearly pathnames to
    files.


    在Windows上,这对我有用:

    将自签名证书的内容添加到ca-bundle文件的末尾。包括----- BEGIN CERTIFICATE -----和----- END CERTIFICATE -----行

    ca-bundle文件的位置通常是C: Program Files Git mingw64 ssl certs

    然后,将ca-bundle文件的路径添加到全局git config。以下命令可以解决这个问题:git config --global http.sslCAInfo"C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt"

    备注:路径取决于您的ca-bundle文件的本地路径!


    我是这样做的:

    1
    2
    3
    git init
    git config --global http.sslVerify false
    git clone https://myurl/myrepo.git

    • 不要使用--global!很多教程都显示--global,但总的来说这是一个非常糟糕的主意,尤其是http.sslVerify。只要您从计算机上的不同项目,公司,团队中获得多个克隆,您就会很快遇到麻烦。例如,从一个项目泄漏到下一个项目的用户ID和电子邮件可能非常令人尴尬。在http.sslVerify上使用--global可以打开各种安全问题。所以:不要使用--global - 除非你完全了解副作用并准备承担风险。


    在.gitconfig文件中,您可以添加以下给定值以使自签名证书可接受

    sslCAInfo = /home/XXXX/abc.crt

    • 这相当于亚当答案的第二步


    我使用的是Windows机器,这篇文章帮助了我。基本上我在记事本中打开了ca-bundle.crt并在其中添加了链证书(所有这些)。这个问题通常发生在我们让中间人坐在系统和git repo之间的公司网络上。我们需要导出证书链中的所有证书,除了基本64格式的叶证书,并将它们全部添加到ca-bundle.crt,然后为这个修改过的crt文件配置git。