github.com/sleungcy/cli@v7.1.0+incompatible/cf/trace/trace.go (about) 1 package trace 2 3 import ( 4 "fmt" 5 "regexp" 6 7 . "code.cloudfoundry.org/cli/cf/i18n" 8 ) 9 10 var LoggingToStdout bool 11 12 func Sanitize(input string) string { 13 re := regexp.MustCompile(`(?m)^Authorization: .*`) 14 sanitized := re.ReplaceAllString(input, "Authorization: "+PrivateDataPlaceholder()) 15 16 re = regexp.MustCompile(`(?m)^Set-Cookie: .*`) 17 sanitized = re.ReplaceAllString(sanitized, "Set-Cookie: "+PrivateDataPlaceholder()) 18 19 // allow query parameter to contain all characters of the "query" character class, except for & 20 // https://tools.ietf.org/html/rfc3986#appendix-A 21 re = regexp.MustCompile(`([&?]password)=[A-Za-z0-9\-._~!$'()*+,;=:@/?]*`) 22 sanitized = re.ReplaceAllString(sanitized, "$1="+PrivateDataPlaceholder()) 23 24 re = regexp.MustCompile(`([&?]code)=[A-Za-z0-9\-._~!$'()*+,;=:@/?]*`) 25 sanitized = re.ReplaceAllString(sanitized, "$1="+PrivateDataPlaceholder()) 26 27 sanitized = sanitizeJSON("token", sanitized) 28 sanitized = sanitizeJSON("password", sanitized) 29 30 return sanitized 31 } 32 33 func sanitizeJSON(propertySubstring string, json string) string { 34 regex := regexp.MustCompile(fmt.Sprintf(`(?i)"([^"]*%s[^"]*)":\s*"[^\,]*"`, propertySubstring)) 35 return regex.ReplaceAllString(json, fmt.Sprintf(`"$1":"%s"`, PrivateDataPlaceholder())) 36 } 37 38 func PrivateDataPlaceholder() string { 39 return T("[PRIVATE DATA HIDDEN]") 40 }