gitleaks/gitleaks

GitHub: gitleaks/gitleaks

Gitleaks 是一款用 Go 编写的密钥泄露检测工具,用于扫描 Git 仓库、文件和输入流中的密码、API 密钥与 Token 等敏感信息,防止凭证意外泄露到代码仓库中。

Stars: 27708 | Forks: 2110

# Gitleaks ``` ┌─○───┐ │ │╲ │ │ │ ○ │ │ ○ ░ │ └─░───┘ ``` [![GitHub Action Test](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/758d919f45061811.svg)][build] [![Docker Hub](https://img.shields.io/docker/pulls/zricethezav/gitleaks.svg)][dockerhub] [![Gitleaks Playground](https://img.shields.io/badge/gitleaks%20-playground-blue)][gitleaks-playground] [![Gitleaks Action](https://img.shields.io/badge/protected%20by-gitleaks-blue)][gitleaks-action] [![GoDoc](https://pkg.go.dev/badge/github.com/gitleaks/gitleaks/v8?status)][go-docs] [![GoReportCard](https://goreportcard.com/badge/github.com/gitleaks/gitleaks/v8)][go-report-card] [![License](https://img.shields.io/github/license/gitleaks/gitleaks.svg)][license] Gitleaks 是一个用于**检测** git 仓库、文件以及任何你希望通过 `stdin` 传入的内容中的密码、API key 和 token 等秘密信息的工具。如果你想深入了解检测引擎的工作原理,请阅读这篇博客:[Regex is (almost) all you need](https://lookingatcomputer.substack.com/p/regex-is-almost-all-you-need)。 ``` ➜ ~/code(master) gitleaks git -v ○ │╲ │ ○ ○ ░ ░ gitleaks Finding: "export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef", Secret: cafebabe:deadbeef RuleID: sidekiq-secret Entropy: 2.609850 File: cmd/generate/config/rules/sidekiq.go Line: 23 Commit: cd5226711335c68be1e720b318b7bc3135a30eb2 Author: John Email: john@users.noreply.github.com Date: 2022-08-03T12:31:40Z Fingerprint: cd5226711335c68be1e720b318b7bc3135a30eb2:cmd/generate/config/rules/sidekiq.go:sidekiq-secret:23 ``` ## 入门指南 Gitleaks 可以通过 Homebrew、Docker 或 Go 进行安装。在 [releases 页面](https://github.com/gitleaks/gitleaks/releases) 也提供了适用于许多流行平台和操作系统类型的二进制文件。此外,Gitleaks 还可以作为 pre-commit hook 直接在你的仓库中使用,或者通过 [Gitleaks-Action](https://github.com/gitleaks/gitleaks-action) 作为 GitHub Action 使用。 ### 安装 ``` # MacOS brew install gitleaks # Docker (DockerHub) docker pull zricethezav/gitleaks:latest docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] [OPTIONS] [SOURCE_PATH] # Docker (ghcr.io) docker pull ghcr.io/gitleaks/gitleaks:latest docker run -v ${path_to_host_folder_to_scan}:/path ghcr.io/gitleaks/gitleaks:latest [COMMAND] [OPTIONS] [SOURCE_PATH] # 从源码安装(请确保已安装 `go`) git clone https://github.com/gitleaks/gitleaks.git cd gitleaks make build ``` ### GitHub Action 查看官方的 [Gitleaks GitHub Action](https://github.com/gitleaks/gitleaks-action) ``` name: gitleaks on: [pull_request, push, workflow_dispatch] jobs: scan: name: gitleaks runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - uses: gitleaks/gitleaks-action@v2 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE}} # Only required for Organizations, not personal accounts. ``` ### Pre-Commit 1. 从 https://pre-commit.com/#install 安装 pre-commit 2. 在你的仓库根目录下创建一个 `.pre-commit-config.yaml` 文件,包含以下内容: repos: - repo: https://github.com/gitleaks/gitleaks rev: v8.24.2 hooks: - id: gitleaks 用于 [gitleaks 的原生执行](https://github.com/gitleaks/gitleaks/releases),或者使用 [`gitleaks-docker` pre-commit ID](https://github.com/gitleaks/gitleaks/blob/master/.pre-commit-hooks.yaml) 通过 [官方 Docker 镜像](#docker) 来执行 gitleaks 3. 执行 `pre-commit autoupdate` 自动更新配置到最新仓库版本 4. 使用 `pre-commit install` 进行安装 5. 现在你已经全部设置完毕了! ``` ➜ git commit -m "this commit contains a secret" Detect hardcoded secrets.................................................Failed ``` 注意:要禁用 gitleaks pre-commit hook,你可以在 commit 命令前加上 `SKIP=gitleaks` 这样它就会跳过运行 gitleaks ``` ➜ SKIP=gitleaks git commit -m "skip gitleaks check" Detect hardcoded secrets................................................Skipped ``` ## 使用说明 ``` Gitleaks scans code, past or present, for secrets Usage: gitleaks [command] Available Commands: completion Generate the autocompletion script for the specified shell dir scan directories or files for secrets git scan git repositories for secrets help Help about any command stdin detect secrets from stdin version display gitleaks version Flags: -b, --baseline-path string path to baseline with issues that can be ignored -c, --config string config file path order of precedence: 1. --config/-c 2. env var GITLEAKS_CONFIG 3. env var GITLEAKS_CONFIG_TOML with the file content 4. (target path)/.gitleaks.toml If none of the four options are used, then gitleaks will use the default config --diagnostics string enable diagnostics (http OR comma-separated list: cpu,mem,trace). cpu=CPU prof, mem=memory prof, trace=exec tracing, http=serve via net/http/pprof --diagnostics-dir string directory to store diagnostics output files when not using http mode (defaults to current directory) --enable-rule strings only enable specific rules by id --exit-code int exit code when leaks have been encountered (default 1) -i, --gitleaks-ignore-path string path to .gitleaksignore file or folder containing one (default ".") -h, --help help for gitleaks --ignore-gitleaks-allow ignore gitleaks:allow comments -l, --log-level string log level (trace, debug, info, warn, error, fatal) (default "info") --max-archive-depth int allow scanning into nested archives up to this depth (default "0", no archive traversal is done) --max-decode-depth int allow recursive decoding up to this depth (default "0", no decoding is done) --max-target-megabytes int files larger than this will be skipped --no-banner suppress banner --no-color turn off color for verbose output --redact uint[=100] redact secrets from logs and stdout. To redact only parts of the secret just apply a percent value from 0..100. For example --redact=20 (default 100%) -f, --report-format string output format (json, csv, junit, sarif, template) -r, --report-path string report file --report-template string template file used to generate the report (implies --report-format=template) --timeout int set a timeout for gitleaks commands in seconds (default "0", no timeout is set) -v, --verbose show verbose output from scan --version version for gitleaks Use "gitleaks [command] --help" for more information about a command. ``` ### 命令 ⚠️ v8.19.0 引入了一项更改,废弃了 `detect` 和 `protect`。这些命令仍然可用,但 隐藏在 `--help` 菜单中。请查看这个 [gist](https://gist.github.com/zricethezav/b325bb93ebf41b9c0b0507acf12810d2) 以便轻松转换命令。 如果你发现 v8.19.0 破坏了现有的命令(`detect`/`protect`),请提交一个 issue。 共有三种扫描模式:`git`、`dir` 和 `stdin`。 #### Git `git` 命令允许你扫描本地的 git 仓库。在底层,gitleaks 使用 `git log -p` 命令来扫描补丁。 你可以使用 `log-opts` 选项来配置 `git log -p` 的行为。 例如,如果你想在一系列 commit 上运行 gitleaks,可以使用以下 命令:`gitleaks git -v --log-opts="--all commitA..commitB" path_to_repo`。有关更多信息,请参阅 [git log](https://git-scm.com/docs/git-log) 文档。 如果没有指定目标作为位置参数,gitleaks 将尝试将当前工作目录作为 git 仓库进行扫描。 #### 目录 `dir` 命令(别名包括 `files`、`directory`)允许你扫描目录和文件。示例:`gitleaks dir -v path_to_directory_or_file`。 如果没有指定目标作为位置参数,gitleaks 将扫描当前工作目录。 #### Stdin 你也可以使用 `stdin` 命令将数据流传输给 gitleaks。示例:`cat some_file | gitleaks -v stdin` ### 创建基线 当扫描大型仓库或具有较长历史记录的仓库时,使用基线会非常方便。使用基线时, gitleaks 将忽略基线中存在的所有旧发现。基线可以是任何 gitleaks 报告。要创建 gitleaks 报告,请使用 `--report-path` 参数运行 gitleaks。 ``` gitleaks git --report-path gitleaks-report.json # This will save the report in a file called gitleaks-report.json ``` 一旦创建了基线,就可以在再次运行 detect 命令时应用它: ``` gitleaks git --baseline-path gitleaks-report.json --report-path findings.json ``` 在使用 --baseline-path 参数运行 detect 命令后,报告输出(findings.json)将只包含新问题。 ## Pre-Commit 钩子 你可以将示例 `pre-commit.py` 脚本复制到 你的 `.git/hooks/` 目录中,将 Gitleaks 作为 pre-commit hook 运行。 ## 加载配置 优先级顺序如下: 1. `--config/-c` 选项: gitleaks git --config /home/dev/customgitleaks.toml . 2. 环境变量 `GITLEAKS_CONFIG`,其值为文件路径: export GITLEAKS_CONFIG="/home/dev/customgitleaks.toml" gitleaks git . 3. 环境变量 `GITLEAKS_CONFIG_TOML`,其值为文件内容: export GITLEAKS_CONFIG_TOML=`cat customgitleaks.toml` gitleaks git . 4. 目标路径中的 `.gitleaks.toml` 文件: gitleaks git . 如果以上四个选项都未使用,则 gitleaks 将使用默认配置。 ## 配置 Gitleaks 提供了一种配置格式,你可以按照该格式编写自定义的秘密检测规则: ``` # gitleaks 配置文件的标题。 title = "Custom Gitleaks configuration" # 对于您的自定义配置,基本上有两个选项: # # 1. 定义您自己的配置,默认规则将不适用 # # 例如,使用默认配置作为起点: # https://github.com/gitleaks/gitleaks/blob/master/config/gitleaks.toml # # 2. 扩展一个配置,规则将被覆盖或扩展 # # 当您扩展配置时,扩展的规则优先于 # 默认规则。也就是说,如果在扩展配置 # 和默认配置中都存在重复的规则,则扩展的规则或 # 其属性将覆盖默认规则。 # 关于扩展配置的另一个需要了解的问题是,您可以 # 将多个配置文件链接在一起,深度最多为 2。Allowlist 数组是 # 追加的,并且可以包含重复项。 # useDefault 和 path 不能同时使用。请任选其一。 [extend] # useDefault 将扩展二进制文件中内置的默认 gitleaks 配置 # 最新版本位于: # https://github.com/gitleaks/gitleaks/blob/master/config/gitleaks.toml useDefault = true # 或者您可以提供要扩展的配置的路径。 # 该路径是相对于调用 gitleaks 的位置而言的, # 而不是基础配置所在的位置。 # path = "common_config.toml" # 如果有任何您不想继承的规则,可以在此处指定。 disabledRules = [ "generic-api-key"] # 一个包含多个表的数组,这些表包含了定义 # 如何检测 secrets 的指令信息 [[rules]] # 此规则的唯一标识符 id = "awesome-rule-1" # 规则的简短易读描述。 description = "awesome rule 1" # 用于检测 secrets 的 Golang 正则表达式。请注意 Golang 的正则表达式引擎 # 不支持前向断言 (lookaheads)。 regex = '''one-go-style-regex-for-this-rule''' # 用于从正则表达式匹配中提取 secret 的整数,如果设置了 `entropy`, # 它也将作为检查其熵值的组。 secretGroup = 3 # 一个浮点数,表示正则表达式组要被视为 secret 所必须具有的最小 shannon 熵。 entropy = 3.5 # 用于匹配路径的 Golang 正则表达式。它可以作为独立规则使用,也可以 # 与有效的 `regex` 条目结合使用。 path = '''a-file-path-regex''' # 关键字用于正则表达式检查前的过滤。包含 # 关键字的规则将执行快速的字符串比较检查,以确保 # 内容中包含该关键字。理想情况下,这些值应该 # 是标识符的一部分,或者是该规则的正则表达式所特有的字符串 # (在 v8.6.0 中引入) keywords = [ "auth", "password", "token", ] # 用于元数据和报告目的的字符串数组。 tags = ["tag","another tag"] # ⚠️ In v8.21.0 `[rules.allowlist]` was replaced with `[[rules.allowlists]]`. # This change was backwards-compatible: instances of `[rules.allowlist]` still work. # # You can define multiple allowlists for a rule to reduce false positives. # A finding will be ignored if _ANY_ `[[rules.allowlists]]` matches. [[rules.allowlists]] description = "ignore commit A" # When multiple criteria are defined the default condition is "OR". # e.g., this can match on |commits| OR |paths| OR |stopwords|. condition = "OR" commits = [ "commit-A", "commit-B"] paths = [ '''go\.mod''', '''go\.sum''' ] # note: stopwords targets the extracted secret, not the entire regex match # like 'regexes' does. (stopwords introduced in 8.8.0) stopwords = [ '''client''', '''endpoint''', ] [[rules.allowlists]] # The "AND" condition can be used to make sure all criteria match. # e.g., this matches if |regexes| AND |paths| are satisfied. condition = "AND" # note: |regexes| defaults to check the _Secret_ in the finding. # Acceptable values for |regexTarget| are "secret" (default), "match", and "line". regexTarget = "match" regexes = [ '''(?i)parseur[il]''' ] paths = [ '''package-lock\.json''' ] # 您可以扩展默认配置中的特定规则。例如,gitlab-pat # 如果您在您的 GitLab 实例上定义了自定义 token 前缀 [[rules]] id = "gitlab-pat" # 默认规则的所有其他属性都将被继承 [[rules.allowlists]] regexTarget = "line" regexes = [ '''MY-glpat-''' ] # ⚠️ 在 v8.25.0 中 `[allowlist]` 已被替换为 `[[allowlists]]`。 # # 全局 allowlists 的优先级高于特定于规则的 allowlists。 # 如果遇到以下 `commits` 字段中列出的 commit,则该 commit 将被跳过,并且 # 不会为该 commit 检测到任何 secrets。相同的逻辑也适用于 regexes 和 paths。 [[allowlists]] description = "global allow list" commits = [ "commit-A", "commit-B", "commit-C"] paths = [ '''gitleaks\.toml''', '''(.*?)(jpg|gif|doc)''' ] # 注意:(全局) regexTarget 默认检查发现结果中的 _Secret_。 # regexTarget 的可接受值是 "match" 和 "line" regexTarget = "match" regexes = [ '''219-09-9999''', '''078-05-1120''', '''(9[0-9]{2}|666)-\d{2}-\d{4}''', ] # 注意:stopwords 针对的是提取出来的 secret,而不是像 # 'regexes' 那样针对整个正则表达式匹配。(stopwords 在 8.8.0 中引入) stopwords = [ '''client''', '''endpoint''', ] # ⚠️ 在 v8.25.0 中,`[[allowlists]]` 拥有了一个名为 |targetRules| 的新字段。 # # 常见的 allowlists 只需定义一次,即可使用 |targetRules| 分配给多个规则。 # 这只会在指定的规则上运行,而不是全局运行。 [[allowlists]] targetRules = ["awesome-rule-1", "awesome-rule-2"] description = "Our test assets trigger false-positives in a couple rules." paths = ['''tests/expected/._\.json$'''] ``` 有关示例,请参考默认的 [gitleaks 配置](https://github.com/gitleaks/gitleaks/blob/master/config/gitleaks.toml);如果你想为默认配置做贡献,请遵循[贡献指南](https://github.com/gitleaks/gitleaks/blob/master/CONTRIBUTING.md)。此外,你可以查看[这篇 gitleaks 博客文章](https://blog.gitleaks.io/stop-leaking-secrets-configuration-2-3-aeed293b1fbf),其中涵盖了高级配置设置。 ### 附加配置 #### 复合规则(多部分或 `required` 规则) 在 v8.28.0 中,Gitleaks 引入了复合规则,这些规则由单个“主”规则和一个或多个辅助或 `required` 规则组成。要创建复合规则,请在主规则中添加一个 `[[rules.required]]` 表,指定一个 `id`,并可选地指定 `withinLines` 和/或 `withinColumns` 邻近约束。片段是 Gitleaks 一次处理的内容块(通常是文件、文件的一部分或 git diff),邻近匹配指示主规则只有在辅助的 `required` 规则也在片段的指定区域内找到匹配项时,才报告发现。 **邻近匹配:** 使用 `withinLines` 和 `withinColumns` 字段指示主规则只有在辅助的 `required` 规则也在指定的邻近范围内找到匹配项时,才报告发现。你可以设置: - **`withinLines: N`** - 必须匹配的发现必须在 N 行之内(垂直) - **`withinColumns: N`** - 必须匹配的发现必须在 N 个字符之内(水平) - **两者兼具** - 创建一个矩形搜索区域(两个约束都必须满足) - **两者皆无** - 片段级匹配(必须匹配的发现可以在同一片段中的任何位置) 以下是说明每种邻近行为的图解: ``` p = primary captured secret a = auxiliary (required) captured secret fragment = section of data gitleaks is looking at *Fragment-level proximity* Any required finding in the fragment ┌────────┐ ┌──────┤fragment├─────┐ │ └──────┬─┤ │ ┌───────┐ │ │a│◀────┼─│✓ MATCH│ │ ┌─┐└─┘ │ └───────┘ │┌─┐ │p│ │ ││a│ ┌─┐└─┘ │ ┌───────┐ │└─┘ │a│◀──────────┼─│✓ MATCH│ └─▲─────┴─┴───────────┘ └───────┘ │ ┌───────┐ └────│✓ MATCH│ └───────┘ *Column bounded proximity* `withinColumns = 3` ┌────────┐ ┌────┬─┤fragment├─┬───┐ │ └──────┬─┤ │ ┌───────────┐ │ │ │a│◀┼───┼─│+1C ✓ MATCH│ │ ┌─┐└─┘ │ └───────────┘ │┌─┐ │ │p│ │ │ ┌──▶│a│ ┌─┐ └─┘ │ ┌───────────┐ │ │└─┘ ││a│◀────────┼───┼─│-2C ✓ MATCH│ │ │ ┘ │ └───────────┘ │ └── -3C ───0C─── +3C ─┘ │ ┌─────────┐ │ │ -4C ✗ NO│ └──│ MATCH │ └─────────┘ *Line bounded proximity* `withinLines = 4` ┌────────┐ ┌─────┤fragment├─────┐ +4L─ ─ ┴────────┘─ ─ ─│ │ │ │ ┌─┐ │ ┌────────────┐ │ ┌─┐ │a│◀──┼─│+1L ✓ MATCH │ 0L ┌─┐ │p│ └─┘ │ ├────────────┤ │ │a│◀──┴─┴────────┼─│-1L ✓ MATCH │ │ └─┘ │ └────────────┘ │ │ ┌─────────┐ -4L─ ─ ─ ─ ─ ─ ─ ─┌─┐─│ │-5L ✗ NO │ │ │a│◀┼─│ MATCH │ └────────────────┴─┴─┘ └─────────┘ *Line and column bounded proximity* `withinLines = 4` `withinColumns = 3` ┌────────┐ ┌─────┤fragment├─────┐ +4L ┌└────────┴ ┐ │ │ ┌─┐ │ ┌───────────────┐ │ │ │a│◀┼───┼─│+2L/+1C ✓ MATCH│ │ ┌─┐└─┘ │ └───────────────┘ 0L │ │p│ │ │ │ └─┘ │ │ │ │ │ ┌────────────┐ -4L ─ ─ ─ ─ ─ ─┌─┐ │ │-5L/+3C ✗ NO│ │ │a│◀┼─│ MATCH │ └───-3C────0L───+3C┴─┘ └────────────┘ ```
关于复合规则的一些最终简要想法。这是一个实验性功能!它可能会发生变化,所以不要在上面构建新的 B2B SaaS 功能拿去卖。基于扫描类型(git 对 dir)的上下文很有趣。我正在关注这个情况。复合规则对于 git 扫描可能不是特别有用,因为 gitleaks 只会查看 git 历史中的新增内容。扫描 git 历史中非新增内容以用作 `required` 规则可能会有用。哦,对,这是一个 readme,我现在闭嘴了。
#### gitleaks:allow 如果你故意提交了一个会被 gitleaks 捕获的测试秘密,你可以向该行添加一个 `gitleaks:allow` 注释,这将指示 gitleaks 忽略该秘密。示例: ``` class CustomClass: discord_client_secret = '8dyfuiRyq=vVc3RRr_edRk-fK__JItpZ' #gitleaks:allow ``` #### .gitleaksignore 你可以通过在仓库根目录创建一个 `.gitleaksignore` 文件来忽略特定的发现。在 v8.10.0 版本中,Gitleaks 在报告中添加了一个 `Fingerprint` 值。每个泄露或发现都有一个能唯一标识该秘密的指纹。将此指纹添加到 `.gitleaksignore` 文件中即可忽略该特定秘密。有关示例,请参见 Gitleaks 的 [.gitleaksignore](https://github.com/gitleaks/gitleaks/blob/master/.gitleaksignore)。注意:此功能是实验性的,未来可能会发生变化。 #### 解码 有时秘密信息的编码方式会使仅通过正则表达式难以找到它们。现在你可以告诉 gitleaks 自动查找并解码编码文本。`--max-decode-depth` 标志启用了此功能(默认值“0”表示默认禁用此功能)。 由于解码后的文本也可能包含编码文本,因此支持递归解码。`--max-decode-depth` 标志用于设置递归限制。当没有新的编码文本段需要解码时,递归就会停止,因此设置非常高的最大深度并不意味着它会进行那么多次遍历。它只会根据需要进行尽可能多的操作来解码文本。总的来说,解码只会极少地增加扫描时间。 编码文本的发现与正常发现的不同之处在于: - 位置指向编码文本的边界 - 如果规则在编码文本之外匹配,边界将被调整以将其包含在内 - 匹配项和秘密包含解码后的值 - 添加了两个标签 `decoded:` 和 `decode-depth:` 当前支持的编码: - **percent** - 任何可打印的 ASCII 百分比编码值 - **hex** - 任何可打印的 ASCII 十六进制编码值(>= 32 个字符) - **base64** - 任何可打印的 ASCII base64 编码值(>= 16 个字符) #### 归档扫描 有时秘密信息被封装在 zip 文件或 tar 包等归档文件中, 使得它们难以被发现。现在你可以告诉 gitleaks 自动提取 并扫描归档文件的内容。`--max-archive-depth` 标志为 `dir` 和 `git` 扫描类型启用了此功能。“0”的默认值 表示默认禁用此功能。 由于归档文件也可以包含其他归档文件,因此支持递归扫描。`--max-archive-depth` 标志用于设置递归限制。当没有新的归档文件需要提取时,递归就会停止,因此设置非常高的最大深度只是设定了可能达到的最大深度限制。它只会根据需要进行深入的扫描。 归档文件中秘密的发现将包含指向归档文件内部该文件的路径。内部路径使用 `!` 分隔。 发现示例(为简便起见已缩短): ``` Finding: DB_PASSWORD=8ae31cacf141669ddfb5da ... File: testdata/archives/nested.tar.gz!archives/files.tar!files/.env.prod Line: 4 Commit: 6e6ee6596d337bb656496425fb98644eb62b4a82 ... Fingerprint: 6e6ee6596d337bb656496425fb98644eb62b4a82:testdata/archives/nested.tar.gz!archives/files.tar!files/.env.prod:generic-api-key:4 Link: https://github.com/leaktk/gitleaks/blob/6e6ee6596d337bb656496425fb98644eb62b4a82/testdata/archives/nested.tar.gz ``` 这表示在 `files/.env.prod.` 的第 4 行检测到了一个秘密,该文件位于 `archives/files.tar` 中,而 `archives/files.tar` 又位于 `testdata/archives/nested.tar.gz` 中。 当前支持的格式: 支持 mholt 的 [archives 包](https://github.com/mholt/archives) 所支持的 [compression](https://github.com/mholt/archives?tab=readme-ov-file#supported-compression-formats) 和 [archive](https://github.com/mholt/archives?tab=readme-ov-file#supported-archive-formats) 格式。 #### 报告 Gitleaks 内置支持多种报告格式:[`json`](https://github.com/gitleaks/gitleaks/blob/master/testdata/expected/report/json_simple.json)、[`csv`](https://github.com/gitleaks/gitleaks/blob/master/testdata/expected/report/csv_simple.csv?plain=1)、[`junit`](https://github.com/gitleaks/gitleaks/blob/master/testdata/expected/report/junit_simple.xml) 和 [`sarif`](https://github.com/gitleaks/gitleaks/blob/master/testdata/expected/report/sarif_simple.sarif)。 如果这些格式都不符合你的需求,你可以使用 [Go `text/template` .tmpl 文件](https://www.digitalocean.com/community/tutorials/how-to-use-templates-in-go#step-4-writing-a-template) 和 `--report-template` 标志创建自定义报告格式。该模板可以使用 [`Masterminds/sprig` 模板库的扩展功能](https://masterminds.github.io/sprig/)。 例如,以下模板提供了自定义的 JSON 输出: ``` # jsonextra.tmpl [{{ $lastFinding := (sub (len . ) 1) }} {{- range $i, $finding := . }}{{with $finding}} { "Description": {{ quote .Description }}, "StartLine": {{ .StartLine }}, "EndLine": {{ .EndLine }}, "StartColumn": {{ .StartColumn }}, "EndColumn": {{ .EndColumn }}, "Line": {{ quote .Line }}, "Match": {{ quote .Match }}, "Secret": {{ quote .Secret }}, "File": "{{ .File }}", "SymlinkFile": {{ quote .SymlinkFile }}, "Commit": {{ quote .Commit }}, "Entropy": {{ .Entropy }}, "Author": {{ quote .Author }}, "Email": {{ quote .Email }}, "Date": {{ quote .Date }}, "Message": {{ quote .Message }}, "Tags": [{{ $lastTag := (sub (len .Tags ) 1) }}{{ range $j, $tag := .Tags }}{{ quote . }}{{ if ne $j $lastTag }},{{ end }}{{ end }}], "RuleID": {{ quote .RuleID }}, "Fingerprint": {{ quote .Fingerprint }} }{{ if ne $i $lastFinding }},{{ end }} {{- end}}{{ end }} ] ``` 使用方法: ``` $ gitleaks dir ~/leaky-repo/ --report-path "report.json" --report-format template --report-template testdata/report/jsonextra.tmpl ``` ## 退出码 你始终可以使用 --exit-code 标志来设置遇到泄露时的退出码。默认退出码如下: ``` 0 - no leaks present 1 - leaks or error encountered 126 - unknown flag ``` ### 加入 Discord! [![Discord](https://img.shields.io/discord/1102689410522284044.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/8Hzbrnkr7E)
标签:API密钥, CI/CD安全, DevSecOps, Docker, EVTX分析, GitHub Actions, Gitleaks, Google AI, Go语言, Llama, Python安全, SDK, 上游代理, 代码安全, 令牌扫描, 多包管理, 安全助手, 安全防御评估, 密码检测, 开源安全工具, 提示注入防御, 日志审计, 源代码安全, 漏洞枚举, 程序破解, 聊天机器人, 自动笔记, 请求拦截, 逆向工程平台, 错误基检测, 静态代码分析