从 .git-credentials 文件说起


=Start=

缘由:

由前段时间发现的一个内部case引出的对git credential操作的测试。这里简单记录一下 git-credential 的相关配置和问题,以及我个人的一些想法,方便以后参考。

正文:

参考解答:
# 当你在一台公用的开发机上执行 git pull/push 命令时,需要注意一下机器的 git config 配置,避免你的账号密码(被动)泄露
# 或者换种说法就是——对于任何需要你输入(重要)账号密码的操作都需要非常谨慎(能不输入尽量不输入,操作完了之后立即改密码),尤其是在环境不太可控的公共机器上

# Attention: This method saves the credentials in plaintext on your PC's disk. Everyone on your computer can access it, e.g. malicious NPM modules.

# 用下面这个命令会在本地磁盘(默认是~/.git-credentials文件)上明文存储你的账号密码,且文件能被机器上的其它人进行读取(git全局配置,对所有人生效)
$ git config --global credential.helper store

# 再执行拉取操作,会提示你输入账号密码,当你输入正确的账号密码之后会进行代码拉取并把账号密码明文存在本地的 ~/.git-credentials 文件中
$ git pull

# 对于不想每次都输入账号密码但又不想把账号密码明文写入文件的,可以试试缓存到内存中的命令
$ git config credential.helper cache
$ git config --global credential.helper cache

# 设置账号密码缓存过期时间为2小时(超过这段时间之后就需要重新输入账号密码)
$ git config --global credential.helper 'cache --timeout 7200'

# Remove a matching credential, if any, from the helper’s storage.(实测并不会从文件中删除已保存的账号密码,只是让已存储的账号密码不能用于认证——重新输入账号密码,把erase换成forget也一样)
$ git config --global credential.helper erase

# 删除已保存的账号密码文件
$ rm -rf ~/.git-credentials

补充,在本地的macOS电脑上测试的时候发现——当你输入过一次正确的git账号密码之后,即便你手动删除了 ~/.git-credentials 文件,当你再执行 git clone 的时候还是可以免密拉取,并且clone完了之后又会自动生成 ~/.git-credentials 文件(当你的配置还是credential.helper=store时)。

究其原因在于macOS上的keychain(钥匙串)功能,如果你把你的git-config配置列出来会发现(默认?)有一个 credential.helper=osxkeychain 配置,它会把你输入过的账号密码存入钥匙串中,当你下次执行git命令需要认证的时候它会帮你提供这些信息。

$ git –version
git version 2.37.0 (Apple Git-136)
$
$ git config –list | head
credential.helper=osxkeychain
init.defaultbranch=main

$

打开【钥匙串访问】,在「默认钥匙串-登录」的「密码」中即可看到对应名称的钥匙串类型和添加时间,如果你有macOS电脑密码的话,还可以看到明文密码。


这个问题简单来说是一个因为员工个人配置不当引起的安全风险,一方面是企业内员工的安全意识不够(加强这方面的安全宣导和培训),另一方面也需要安全团队具备检测和响应能力(主动监测,做好兜底),因为不管是什么原因引起的账号密码泄漏,最终都会归结到账号安全问题上来,要想不背锅,就要提前想好办法。

还有就是这个点可能被黑客知晓用于快速无感收集有效账号密码,需要早做准备。git-config全局设置风险命令检测,引申出的~/.git-credentials文件及其内容监测。还有就是公共机器的安全治理和权责划分等等。


再补充一下测试验证过程中的一些命令:

$ git help -a | grep credential
   credential           Retrieve and store user credentials
   credential-cache     Helper to temporarily store passwords in memory
   credential-store     Helper to store credentials on disk

$ git config --global credential.helper 'cache --timeout 120'

$ ls -alt ~/.git*
$ cat ~/.gitconfig

$ git clone https://github.com/user/reponame.git

$ ls -alt ~/.git
.gitconfig             .git-credential-cache/ .git-credentials

# git的 credential-cache 命令会在 HOME 目录上新建一个 .git-credential-cache/ 目录,里面有一个 socket 文件
$ ls -alt ~/.git-credential-cache/
total 8
drwx------  2 ixyzero ixyzero 4096 Nov  2 14:55 .
drwx------ 15 ixyzero ixyzero 4096 Nov  2 14:55 ..
srwxrwxr-x  1 ixyzero ixyzero    0 Nov  2 14:55 socket

测试发现不论是 forget 还是 erase 都不会清除 ~/.git-credentials 文件或者其已有的内容。

但是设置为 forget/erase 之后,即便 ~/.git-credentials 里面存了账号密码,但是依旧需要手动输入账号密码进行 clone/push 操作。基本可以认为只要不是store就需要手动输入。

git: 'credential-erase' is not a git command. See 'git --help'.

git: 'credential-forget' is not a git command. See 'git --help'.

$ man git-credential-store
$ man gitcredentials

$ git config credential.helper store

$ git config credential.helper 'store --store=/path/to/git-credentials-file'

The .git-credentials file is stored in plaintext. Each credential is stored on its own line as a URL like:
https://user:[email protected]
参考链接:

How can I save username and password in Git?
https://stackoverflow.com/questions/35942754/how-can-i-save-username-and-password-in-git

gitcredentials – Providing usernames and passwords to Git
https://git-scm.com/docs/gitcredentials

git-credential-store – Helper to store credentials on disk
https://git-scm.com/docs/git-credential-store

Git push requires username and password
https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password

=END=


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注