http.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License Version 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2016-present Datadog, Inc.
  5. package obfuscate
  6. import (
  7. "net/url"
  8. "strings"
  9. )
  10. // ObfuscateURLString obfuscates the given URL. It must be a valid URL and at least one
  11. // HTTP obfuscation option must be enabled at Obfuscator instantiation time.
  12. func (o *Obfuscator) ObfuscateURLString(val string) string {
  13. if !o.opts.HTTP.RemoveQueryString && !o.opts.HTTP.RemovePathDigits {
  14. // nothing to do
  15. return val
  16. }
  17. u, err := url.Parse(val)
  18. if err != nil {
  19. // should not happen for valid URLs, but better obfuscate everything
  20. // rather than expose sensitive information when this option is on.
  21. return "?"
  22. }
  23. if o.opts.HTTP.RemoveQueryString && u.RawQuery != "" {
  24. u.ForceQuery = true // add the '?'
  25. u.RawQuery = ""
  26. }
  27. if o.opts.HTTP.RemovePathDigits {
  28. segs := strings.Split(u.Path, "/")
  29. var changed bool
  30. for i, seg := range segs {
  31. for _, ch := range []byte(seg) {
  32. if ch >= '0' && ch <= '9' {
  33. // we can not set the question mark directly here because the url
  34. // package will escape it into %3F, so we use this placeholder and
  35. // replace it further down.
  36. segs[i] = "/REDACTED/"
  37. changed = true
  38. break
  39. }
  40. }
  41. }
  42. if changed {
  43. u.Path = strings.Join(segs, "/")
  44. }
  45. }
  46. return strings.Replace(u.String(), "/REDACTED/", "?", -1)
  47. }