4
0

finding.go 1.4 KB

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