我想在 secretlint 中应用 gitleaks 中使用的正则表达式

I want to apply the regular expression used in gitleaks in secretlint

我现在正尝试从 gitleaks 迁移到一个名为 secretlint 的工具。

原来在执行gitleaks的时候在generic-api-key规则中有警告,但是改用secretlint后警告就没有了

具体的,我写了gitleaks.toml provided by gitleaks in the secretlint configuration file .secretlintrc.json according to the format of @secretlint-rule-pattern provided by secretlint的正则表达式。

[[rules]]
id = "generic-api-key"
description = "Generic API Key"
regex = '''(?i)((key|api[^Version]|token|secret|password|auth)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]'''
entropy = 3.7
secretGroup = 4
keywords = [
    "key",
    "api",
    "token",
    "secret",
    "password",
    "auth",
]

{
  "rules": [
    {
      "id": "@secretlint/secretlint-rule-pattern",
      "options": {
        "patterns": [
          {
            "name": "Generic API key",
            "pattern": "/(?i)((key|api[^Version]|token|secret|password|auth)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]/"
          }
        ]
      }
    }
  ]
}

我在想,也许我没有正确迁移正则表达式,但如果有人能告诉我哪里出错了,我想知道。

主要问题是 JavaScript 正则表达式引擎不支持内联 (?i) 修饰符。您必须在第二个正则表达式定界符 (/.../i) 之后使用正常的 i 标志。

此外,api[^Version] 是典型的用户错误。如果你想说 api 后面没有 Version,你需要 api(?!Version).

所以你可以使用

"pattern": "/((key|api(?!Version)|token|secret|password|auth)[\w .,-]{0,25})([=>:]|:=|\|\|:|<=|=>).{0,5}['\"]([\w=-]{8,64})['\"]/i"

请注意,我将[A-Za-z0-9_]“缩小”为一个\w,它们在这里是等价的。请注意 - 字符在字符 class.

的末尾(或开始)使用时不需要转义