如何使用Git-Secrets防止将敏感信息意外上传至Git库

关于Git-Secrets

Git-secrets是一款功能强大的开发安全工具,该工具可以防止开发人员意外将密码和其他敏感信息上传到Git库中。

Git-secrets首先会扫描提交的代码和说明,当与用户预先配置的正则表达式模式匹配时,便会阻止此次提交。该工具的优势在于可以集成到CI/CD管道中以实时监控提交信息,但不足之处在于,该工具主要基于正则表达式实现其检测功能,因此可能会有一定程度的误报。

工具安装

广大研究人员可以使用下列命令将该项目源码克隆至本地:

复制代码
git clone https://github.com/awslabs/git-secrets.git

然后切换到项目目录中,根据对应的操作系统执行不同的安装命令:

复制代码
cd git-secrets

*nix(Linux/macOS)

复制代码
make install

Windows

复制代码
PS > ./install.ps1

Homebrew安装

复制代码
brew install git-secrets

工具使用命令

复制代码
git secrets --scan [-r|--recursive] [--cached] [--no-index] [--untracked] [<files>...]

git secrets --scan-history

git secrets --install [-f|--force] [<target-directory>]

git secrets --list [--global]

git secrets --add [-a|--allowed] [-l|--literal] [--global] <pattern>

git secrets --add-provider [--global] <command> [arguments...]

git secrets --register-aws [--global]

git secrets --aws-provider [<credentials-file>]

--scan命令

扫描代码库中所有文件:

复制代码
git secrets --scan

扫描单个文件:

复制代码
git secrets --scan /path/to/file

递归扫描目录:

复制代码
git secrets --scan -r /path/to/directory

扫描多个文件:

复制代码
git secrets --scan /path/to/file /path/to/other/file

全局扫描:

复制代码
git secrets --scan /path/to/directory/*

从stdin扫描:

复制代码
echo 'hello!' | git secrets --scan -

--add命令

向当前代码库添加禁止的正则模式:

复制代码
git secrets --add '[A-Z0-9]{20}'

向全局git配置添加禁止的正则模式:

复制代码
git secrets --add --global '[A-Z0-9]{20}'

添加一个逐字扫描的字符串(+字符会被转义):

复制代码
git secrets --add --literal 'foo+bar'

添加允许的正则模式:

复制代码
git secrets --add -a 'allowed pattern'

工具使用样例

假设我们给定下列主题的文本信息(存储在/tmp/example中):

复制代码
This is a test!

password=ex@mplepassword

password=******

More test...

并注册以下正则检测模式:

复制代码
git secrets --add 'password\s*=\s*.+'

git secrets --add --allowed --literal 'ex@mplepassword'

运行下列命令:

复制代码
git secrets --scan /tmp/example

执行后工具会输出下列错误信息:

复制代码
/tmp/example:3:password=******

 

[ERROR] Matched prohibited pattern

 

Possible mitigations:

- Mark false positives as allowed using: git config --add secrets.allowed ...

- List your configured patterns: git config --get-all secrets.patterns

- List your configured allowed patterns: git config --get-all secrets.allowed

- Use --no-verify if this is a one-time false positive

上述正则表达式模式"password\s*=\s*.+"将匹配下列内容:

复制代码
/tmp/example:2:password=ex@mplepassword

/tmp/example:3:password=******

不过,第一行password会被忽略,因为它匹配第一个允许通过的正则表达式"'ex@mplepassword'"。

我们还可以使用下列命令将整个文件列入白名单中:

复制代码
git secrets --add --allowed '/tmp/example:.*'

git secrets --scan /tmp/example && echo $?

# Outputs: 0

或者指定某个文件的指定行代码列入白名单:

复制代码
git secrets --add --allowed '/tmp/example:3:.*'

git secrets --scan /tmp/example && echo $?

# Outputs: 0

许可证协议

本项目的开发与发布遵循Apache-2.0开源许可证协议。

项目地址

Git-Secrets :【GitHub传送门

相关推荐
夜流冰21 分钟前
Git - GitForWindows
git
阿里嘎多学长1 小时前
2025-12-29 GitHub 热点项目精选
开发语言·程序员·github·代码托管
逛逛GitHub1 小时前
GitHub 上 10 个令人惊艳的 Agent 开发平台,太顶了。
github
一个很帅的帅哥1 小时前
部署chat-vue
大数据·git·elasticsearch
芒鸽1 小时前
鸿蒙PC使用ffmpeg+electron实现视频中音频的提取
git
海阔天空092 小时前
Cesium三维地形渲染
vue.js·github
CoderJia程序员甲2 小时前
GitHub 热榜项目 - 日榜(2025-12-29)
ai·开源·llm·github
SunkingYang2 小时前
使用TortoiseGit工具推送(push)时,忘了先拉取(Pull),提示冲突,后续怎么处理冲突?
git·tortoisegit·pull·push·推送·冲突·拉取
BestOrNothing_20153 小时前
Git 团队开发冲突合并全流程:本地是否改动 + 远端是否更新,如何正确同步并合并(同分支/不同分支下的几种场景)
git·分支管理·git pull·git merge·分支冲突·git协作·git fetch
zzlyyds3 小时前
工作的时候遇到的git版本冲突与合并问题
git