sanitize.go 985 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package geminicli
  2. import "strings"
  3. const maxLogBodyLen = 2048
  4. func SanitizeBodyForLogs(body string) string {
  5. body = truncateBase64InMessage(body)
  6. if len(body) > maxLogBodyLen {
  7. body = body[:maxLogBodyLen] + "...[truncated]"
  8. }
  9. return body
  10. }
  11. func truncateBase64InMessage(message string) string {
  12. const maxBase64Length = 50
  13. result := message
  14. offset := 0
  15. for {
  16. idx := strings.Index(result[offset:], ";base64,")
  17. if idx == -1 {
  18. break
  19. }
  20. actualIdx := offset + idx
  21. start := actualIdx + len(";base64,")
  22. end := start
  23. for end < len(result) && isBase64Char(result[end]) {
  24. end++
  25. }
  26. if end-start > maxBase64Length {
  27. result = result[:start+maxBase64Length] + "...[truncated]" + result[end:]
  28. offset = start + maxBase64Length + len("...[truncated]")
  29. continue
  30. }
  31. offset = end
  32. }
  33. return result
  34. }
  35. func isBase64Char(c byte) bool {
  36. return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/' || c == '='
  37. }