MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / JSONBody

Function JSONBody

internal/htmlsanitize/htmlsanitize.go:27–51  ·  view source on GitHub ↗

JSONBody escapes all string values in a JSON document.

(body []byte)

Source from the content-addressed store, hash-verified

25
26// JSONBody escapes all string values in a JSON document.
27func JSONBody(body []byte) ([]byte, bool) {
28 trimmed := bytes.TrimSpace(body)
29 if len(trimmed) == 0 {
30 return body, false
31 }
32
33 decoder := json.NewDecoder(bytes.NewReader(trimmed))
34 decoder.UseNumber()
35 var value any
36 if errDecode := decoder.Decode(&value); errDecode != nil {
37 return body, false
38 }
39 var extra any
40 if errExtra := decoder.Decode(&extra); errExtra != io.EOF {
41 return body, false
42 }
43
44 var buffer bytes.Buffer
45 encoder := json.NewEncoder(&buffer)
46 encoder.SetEscapeHTML(false)
47 if errEncode := encoder.Encode(JSONValue(value)); errEncode != nil {
48 return body, false
49 }
50 return bytes.TrimSuffix(buffer.Bytes(), []byte("\n")), true
51}
52
53// JSONBodyIfLikely escapes JSON bodies when the content type or body shape indicates JSON.
54func JSONBodyIfLikely(body []byte, contentType string) ([]byte, bool) {

Callers 2

JSONBodyIfLikelyFunction · 0.85

Calls 2

JSONValueFunction · 0.85
BytesMethod · 0.80

Tested by 1