github.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package rules
  2. import (
  3. "github.com/zricethezav/gitleaks/v8/cmd/generate/config/utils"
  4. "regexp"
  5. "github.com/zricethezav/gitleaks/v8/cmd/generate/secrets"
  6. "github.com/zricethezav/gitleaks/v8/config"
  7. )
  8. var githubAllowlist = []config.Allowlist{
  9. {
  10. Paths: []*regexp.Regexp{
  11. // https://github.com/octokit/auth-token.js/?tab=readme-ov-file#createtokenauthtoken-options
  12. regexp.MustCompile(`(^|/)@octokit/auth-token/README\.md$`),
  13. },
  14. },
  15. }
  16. func GitHubPat() *config.Rule {
  17. // define rule
  18. r := config.Rule{
  19. RuleID: "github-pat",
  20. Description: "Uncovered a GitHub Personal Access Token, potentially leading to unauthorized repository access and sensitive content exposure.",
  21. Regex: regexp.MustCompile(`ghp_[0-9a-zA-Z]{36}`),
  22. Entropy: 3,
  23. Keywords: []string{"ghp_"},
  24. Allowlists: githubAllowlist,
  25. }
  26. // validate
  27. tps := []string{
  28. utils.GenerateSampleSecret("github", "ghp_"+secrets.NewSecret(utils.AlphaNumeric("36"))),
  29. }
  30. fps := []string{
  31. "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  32. }
  33. return utils.Validate(r, tps, fps)
  34. }
  35. func GitHubFineGrainedPat() *config.Rule {
  36. // define rule
  37. r := config.Rule{
  38. RuleID: "github-fine-grained-pat",
  39. Description: "Found a GitHub Fine-Grained Personal Access Token, risking unauthorized repository access and code manipulation.",
  40. Regex: regexp.MustCompile(`github_pat_\w{82}`),
  41. Entropy: 3,
  42. Keywords: []string{"github_pat_"},
  43. }
  44. // validate
  45. tps := []string{
  46. utils.GenerateSampleSecret("github", "github_pat_"+secrets.NewSecret(utils.AlphaNumeric("82"))),
  47. }
  48. fps := []string{
  49. "github_pat_xxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  50. }
  51. return utils.Validate(r, tps, fps)
  52. }
  53. func GitHubOauth() *config.Rule {
  54. // define rule
  55. r := config.Rule{
  56. RuleID: "github-oauth",
  57. Description: "Discovered a GitHub OAuth Access Token, posing a risk of compromised GitHub account integrations and data leaks.",
  58. Regex: regexp.MustCompile(`gho_[0-9a-zA-Z]{36}`),
  59. Entropy: 3,
  60. Keywords: []string{"gho_"},
  61. }
  62. // validate
  63. tps := []string{
  64. utils.GenerateSampleSecret("github", "gho_"+secrets.NewSecret(utils.AlphaNumeric("36"))),
  65. }
  66. fps := []string{
  67. "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  68. }
  69. return utils.Validate(r, tps, fps)
  70. }
  71. func GitHubApp() *config.Rule {
  72. // define rule
  73. r := config.Rule{
  74. RuleID: "github-app-token",
  75. Description: "Identified a GitHub App Token, which may compromise GitHub application integrations and source code security.",
  76. Regex: regexp.MustCompile(`(?:ghu|ghs)_[0-9a-zA-Z]{36}`),
  77. Entropy: 3,
  78. Keywords: []string{"ghu_", "ghs_"},
  79. Allowlists: githubAllowlist,
  80. }
  81. // validate
  82. tps := []string{
  83. utils.GenerateSampleSecret("github", "ghu_"+secrets.NewSecret(utils.AlphaNumeric("36"))),
  84. utils.GenerateSampleSecret("github", "ghs_"+secrets.NewSecret(utils.AlphaNumeric("36"))),
  85. }
  86. fps := []string{
  87. "ghu_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  88. "ghs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  89. }
  90. return utils.Validate(r, tps, fps)
  91. }
  92. func GitHubRefresh() *config.Rule {
  93. // define rule
  94. r := config.Rule{
  95. RuleID: "github-refresh-token",
  96. Description: "Detected a GitHub Refresh Token, which could allow prolonged unauthorized access to GitHub services.",
  97. Regex: regexp.MustCompile(`ghr_[0-9a-zA-Z]{36}`),
  98. Entropy: 3,
  99. Keywords: []string{"ghr_"},
  100. }
  101. // validate
  102. tps := []string{
  103. utils.GenerateSampleSecret("github", "ghr_"+secrets.NewSecret(utils.AlphaNumeric("36"))),
  104. }
  105. fps := []string{
  106. "ghr_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  107. }
  108. return utils.Validate(r, tps, fps)
  109. }