github.com/letsencrypt/boulder@v0.20251208.0/va/utf8filter.go (about) 1 package va 2 3 import ( 4 "strings" 5 "unicode/utf8" 6 7 "github.com/letsencrypt/boulder/probs" 8 ) 9 10 // replaceInvalidUTF8 replaces all invalid UTF-8 encodings with 11 // Unicode REPLACEMENT CHARACTER. 12 func replaceInvalidUTF8(input []byte) string { 13 if utf8.Valid(input) { 14 return string(input) 15 } 16 17 var b strings.Builder 18 19 // Ranging over a string in Go produces runes. When the range keyword 20 // encounters an invalid UTF-8 encoding, it returns REPLACEMENT CHARACTER. 21 for _, v := range string(input) { 22 b.WriteRune(v) 23 } 24 return b.String() 25 } 26 27 // Call replaceInvalidUTF8 on all string fields of a ProblemDetails 28 // and return the result. 29 func filterProblemDetails(prob *probs.ProblemDetails) *probs.ProblemDetails { 30 if prob == nil { 31 return nil 32 } 33 return &probs.ProblemDetails{ 34 Type: probs.ProblemType(replaceInvalidUTF8([]byte(prob.Type))), 35 Detail: replaceInvalidUTF8([]byte(prob.Detail)), 36 HTTPStatus: prob.HTTPStatus, 37 } 38 }