Skip to content

Commit 5cd6873

Browse files
web: No longer escape HTML characters in SendError (#8533)
1 parent 533c6a2 commit 5cd6873

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

web/send_error.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package web
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -63,10 +64,19 @@ func SendError(
6364
prob.SubProblems[i].Type = probs.ProblemType(probs.ErrorNS) + prob.SubProblems[i].Type
6465
}
6566

66-
problemDoc, err := json.MarshalIndent(prob, "", " ")
67+
var problemDoc []byte
68+
var buf bytes.Buffer
69+
enc := json.NewEncoder(&buf)
70+
enc.SetIndent("", " ")
71+
// Avoid escaping characters: <, >, &, some of our log messages contain
72+
// these characters and we want them to be human readable.
73+
enc.SetEscapeHTML(false)
74+
err := enc.Encode(prob)
6775
if err != nil {
6876
log.AuditErrf("Could not marshal error message: %s - %+v", err, prob)
6977
problemDoc = []byte("{\"detail\": \"Problem marshalling error message.\"}")
78+
} else {
79+
problemDoc = bytes.TrimSuffix(buf.Bytes(), []byte("\n"))
7080
}
7181

7282
response.Write(problemDoc)

web/send_error_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,14 @@ func TestSendErrorPausedProblemLoggingSuppression(t *testing.T) {
103103

104104
test.AssertEquals(t, logEvent.Error, "429 :: rateLimited :: account/ident pair is paused")
105105
}
106+
107+
func TestSendErrorDoesNotEscapeHTML(t *testing.T) {
108+
rw := httptest.NewRecorder()
109+
logEvent := RequestEvent{}
110+
SendError(log.NewMock(), rw, &logEvent, probs.Malformed("nonce less than lowest eligible nonce: 1 < 2"), nil)
111+
112+
test.AssertEquals(t, logEvent.Error, "400 :: malformed :: nonce less than lowest eligible nonce: 1 < 2")
113+
body := rw.Body.String()
114+
test.AssertNotContains(t, body, "\\u003c")
115+
test.AssertContains(t, body, "nonce less than lowest eligible nonce: 1 < 2")
116+
}

0 commit comments

Comments
 (0)