mirror of
https://github.com/pocketbase/pocketbase.git
synced 2026-10-25 06:20:25 +01:00
allow invalid utf8 characters when marshalizing json responses
This commit is contained in:
1 parent
50f5f83ace
commit
756b326bfa
9 files changed
+57
-20
No files matched your search
@@ -185,6 +185,9 @@ const jsonFieldsParam = "fields"
|
||||
// It also provides a generic response data fields picker if the "fields" query parameter is set.
|
||||
// For example, if you are requesting `?fields=a,b` for `e.JSON(200, map[string]int{ "a":1, "b":2, "c":3 })`,
|
||||
// it should result in a JSON response like: `{"a":1, "b": 2}`.
|
||||
//
|
||||
// Note that invalid UTF8 characters are mangled for compatibility
|
||||
// with earlier versions and to prevent unnecessery causing a response error.
|
||||
func (e *Event) JSON(status int, data any) error {
|
||||
e.setResponseHeaderIfEmpty(headerContentType, "application/json")
|
||||
e.Response.WriteHeader(status)
|
||||
@@ -193,7 +196,7 @@ func (e *Event) JSON(status int, data any) error {
|
||||
|
||||
// error response or no fields to pick
|
||||
if rawFields == "" || status < 200 || status > 299 {
|
||||
return json.MarshalWrite(e.Response, data)
|
||||
return json.MarshalWrite(e.Response, data, jsontext.AllowInvalidUTF8(true))
|
||||
}
|
||||
|
||||
// pick only the requested fields
|
||||
@@ -202,7 +205,7 @@ func (e *Event) JSON(status int, data any) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.MarshalWrite(e.Response, modified)
|
||||
return json.MarshalWrite(e.Response, modified, jsontext.AllowInvalidUTF8(true))
|
||||
}
|
||||
|
||||
// XML writes an XML response.
|
||||
|
||||
@@ -409,9 +409,14 @@ func TestEventHTML(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEventJSON(t *testing.T) {
|
||||
body := map[string]any{"a": 123, "b": 456, "c": "test"}
|
||||
body := map[string]any{
|
||||
"a": 123,
|
||||
"b": true,
|
||||
"c": "test",
|
||||
"d": "\xc3", /* invalid utf8 char to test mangling */
|
||||
}
|
||||
expectedPickedBody := `{"a":123,"c":"test"}`
|
||||
expectedFullBody := `{"a":123,"b":456,"c":"test"}`
|
||||
expectedFullBody := `{"a":123,"b":true,"c":"test","d":"�"}`
|
||||
|
||||
scenarios := []testResponseWriteScenario[any]{
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@ package router
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json/jsontext"
|
||||
"encoding/json/v2"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -176,8 +177,13 @@ func ErrorHandler(resp http.ResponseWriter, req *http.Request, err error) {
|
||||
resp.WriteHeader(apiErr.Status)
|
||||
|
||||
if req.Method != http.MethodHead {
|
||||
// note: deterministic because some logs may depend on the exact serialized error
|
||||
jsonErr := json.MarshalWrite(resp, apiErr, json.Deterministic(true))
|
||||
jsonErr := json.MarshalWrite(
|
||||
resp,
|
||||
apiErr,
|
||||
// note: deterministic because some logs may depend on the exact serialized error
|
||||
json.Deterministic(true),
|
||||
jsontext.AllowInvalidUTF8(false),
|
||||
)
|
||||
if jsonErr != nil {
|
||||
log.Println(jsonErr) // truly rare case, log to stderr only for dev purposes
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user