sanitize.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package influxql
  2. import (
  3. "bytes"
  4. "regexp"
  5. )
  6. var (
  7. sanitizeSetPassword = regexp.MustCompile(`(?i)password\s+for[^=]*=\s+(["']?[^\s"]+["']?)`)
  8. sanitizeCreatePassword = regexp.MustCompile(`(?i)with\s+password\s+(["']?[^\s"]+["']?)`)
  9. )
  10. // Sanitize attempts to sanitize passwords out of a raw query.
  11. // It looks for patterns that may be related to the SET PASSWORD and CREATE USER
  12. // statements and will redact the password that should be there. It will attempt
  13. // to redact information from common invalid queries too, but it's not guaranteed
  14. // to succeed on improper queries.
  15. //
  16. // This function works on the raw query and attempts to retain the original input
  17. // as much as possible.
  18. func Sanitize(query string) string {
  19. if matches := sanitizeSetPassword.FindAllStringSubmatchIndex(query, -1); matches != nil {
  20. var buf bytes.Buffer
  21. i := 0
  22. for _, match := range matches {
  23. buf.WriteString(query[i:match[2]])
  24. buf.WriteString("[REDACTED]")
  25. i = match[3]
  26. }
  27. buf.WriteString(query[i:])
  28. query = buf.String()
  29. }
  30. if matches := sanitizeCreatePassword.FindAllStringSubmatchIndex(query, -1); matches != nil {
  31. var buf bytes.Buffer
  32. i := 0
  33. for _, match := range matches {
  34. buf.WriteString(query[i:match[2]])
  35. buf.WriteString("[REDACTED]")
  36. i = match[3]
  37. }
  38. buf.WriteString(query[i:])
  39. query = buf.String()
  40. }
  41. return query
  42. }