Skip to content

Commit 31c043d

Browse files
authored
Merge pull request #135 from arduino/per1234/remove-report-html-encoding
Don't HTML-sanitize JSON output
2 parents 840d45e + 155947e commit 31c043d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

internal/result/result.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
"io"
2324
"text/template"
2425

2526
"github.com/arduino/arduino-lint/internal/configuration"
@@ -222,12 +223,18 @@ func (results Type) JSONReport() string {
222223

223224
// jsonReportRaw returns the report marshalled into JSON format in byte encoding.
224225
func (results Type) jsonReportRaw() []byte {
225-
reportJSON, err := json.MarshalIndent(results, "", " ")
226+
var marshalledReportBuffer bytes.Buffer
227+
jsonEncoder := json.NewEncoder(io.Writer(&marshalledReportBuffer))
228+
// By default, the json package HTML-sanitizes strings during marshalling (https://golang.org/pkg/encoding/json/#Marshal)
229+
// This means that the simple json.MarshalIndent() approach would result in the report containing gibberish.
230+
jsonEncoder.SetEscapeHTML(false)
231+
jsonEncoder.SetIndent("", " ")
232+
err := jsonEncoder.Encode(results)
226233
if err != nil {
227234
panic(fmt.Sprintf("Error while formatting rules report: %v", err))
228235
}
229236

230-
return reportJSON
237+
return marshalledReportBuffer.Bytes()
231238
}
232239

233240
// WriteReport writes a report for all projects to the specified file.

0 commit comments

Comments
 (0)