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