github.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package rules
  2. import (
  3. "regexp"
  4. "github.com/zricethezav/gitleaks/v8/cmd/generate/config/utils"
  5. "github.com/zricethezav/gitleaks/v8/config"
  6. "github.com/zricethezav/gitleaks/v8/cmd/generate/secrets"
  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 := utils.GenerateSampleSecrets("github", "ghp_"+secrets.NewSecret(utils.AlphaNumeric("36")))
  28. fps := []string{
  29. "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  30. }
  31. return utils.Validate(r, tps, fps)
  32. }
  33. func GitHubFineGrainedPat() *config.Rule {
  34. // define rule
  35. r := config.Rule{
  36. RuleID: "github-fine-grained-pat",
  37. Description: "Found a GitHub Fine-Grained Personal Access Token, risking unauthorized repository access and code manipulation.",
  38. Regex: regexp.MustCompile(`github_pat_\w{82}`),
  39. Entropy: 3,
  40. Keywords: []string{"github_pat_"},
  41. }
  42. // validate
  43. tps := utils.GenerateSampleSecrets("github", "github_pat_"+secrets.NewSecret(utils.AlphaNumeric("82")))
  44. fps := []string{
  45. "github_pat_xxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  46. }
  47. return utils.Validate(r, tps, fps)
  48. }
  49. func GitHubOauth() *config.Rule {
  50. // define rule
  51. r := config.Rule{
  52. RuleID: "github-oauth",
  53. Description: "Discovered a GitHub OAuth Access Token, posing a risk of compromised GitHub account integrations and data leaks.",
  54. Regex: regexp.MustCompile(`gho_[0-9a-zA-Z]{36}`),
  55. Entropy: 3,
  56. Keywords: []string{"gho_"},
  57. }
  58. // validate
  59. tps := utils.GenerateSampleSecrets("github", "gho_"+secrets.NewSecret(utils.AlphaNumeric("36")))
  60. fps := []string{
  61. "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  62. }
  63. return utils.Validate(r, tps, fps)
  64. }
  65. func GitHubApp() *config.Rule {
  66. // define rule
  67. r := config.Rule{
  68. RuleID: "github-app-token",
  69. Description: "Identified a GitHub App Token, which may compromise GitHub application integrations and source code security.",
  70. Regex: regexp.MustCompile(`(?:ghu|ghs)_[0-9a-zA-Z]{36}`),
  71. Entropy: 3,
  72. Keywords: []string{"ghu_", "ghs_"},
  73. Allowlists: githubAllowlist,
  74. }
  75. // validate
  76. tps := utils.GenerateSampleSecrets("github", "ghs_"+secrets.NewSecret(utils.AlphaNumeric("36")))
  77. tps = append(tps, utils.GenerateSampleSecrets("github", "ghu_"+secrets.NewSecret(utils.AlphaNumeric("36")))...)
  78. fps := []string{
  79. "ghu_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  80. "ghs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  81. }
  82. return utils.Validate(r, tps, fps)
  83. }
  84. func GitHubRefresh() *config.Rule {
  85. // define rule
  86. r := config.Rule{
  87. RuleID: "github-refresh-token",
  88. Description: "Detected a GitHub Refresh Token, which could allow prolonged unauthorized access to GitHub services.",
  89. Regex: regexp.MustCompile(`ghr_[0-9a-zA-Z]{36}`),
  90. Entropy: 3,
  91. Keywords: []string{"ghr_"},
  92. }
  93. // validate
  94. tps := utils.GenerateSampleSecrets("github", "ghr_"+secrets.NewSecret(utils.AlphaNumeric("36")))
  95. fps := []string{
  96. "ghr_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  97. }
  98. return utils.Validate(r, tps, fps)
  99. }