finding.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package report
  2. import (
  3. "math"
  4. "strings"
  5. )
  6. // Finding contains information about strings that
  7. // have been captured by a tree-sitter query.
  8. type Finding struct {
  9. Description string
  10. StartLine int
  11. EndLine int
  12. StartColumn int
  13. EndColumn int
  14. Line string `json:"-"`
  15. Match string
  16. // Secret contains the full content of what is matched in
  17. // the tree-sitter query.
  18. Secret string
  19. // File is the name of the file containing the finding
  20. File string
  21. SymlinkFile string
  22. Commit string
  23. // Entropy is the shannon entropy of Value
  24. Entropy float32
  25. Author string
  26. Email string
  27. Date string
  28. Message string
  29. Tags []string
  30. // Rule is the name of the rule that was matched
  31. RuleID string
  32. // unique identifier
  33. Fingerprint string
  34. }
  35. // Redact removes sensitive information from a finding.
  36. func (f *Finding) Redact(percent uint) {
  37. secret := maskSecret(f.Secret, percent)
  38. if percent >= 100 {
  39. secret = "REDACTED"
  40. }
  41. f.Line = strings.Replace(f.Line, f.Secret, secret, -1)
  42. f.Match = strings.Replace(f.Match, f.Secret, secret, -1)
  43. f.Secret = secret
  44. }
  45. func maskSecret(secret string, percent uint) string {
  46. if percent > 100 {
  47. percent = 100
  48. }
  49. len := float64(len(secret))
  50. if len <= 0 {
  51. return secret
  52. }
  53. prc := float64(100 - percent)
  54. lth := int64(math.RoundToEven(len * prc / float64(100)))
  55. return secret[:lth] + "..."
  56. }