[GODRIVER-473] toExtJSON does not escape double quotes in strings Created: 20/Jun/18  Updated: 28/Oct/23  Resolved: 25/Oct/18

Status: Closed
Project: Go Driver
Component/s: JSON & ExtJSON
Affects Version/s: None
Fix Version/s: 0.0.17

Type: Bug Priority: Critical - P2
Reporter: Gregoire Pean Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I am pulling entries from a collection, and converting each to JSON.

The JSON which is generated is not always "legal": the mongo go driver does not seem to escape quotes in value strings, so the generated JSON is not compliant.

This seems like a pretty serious bug - Am I doing anything wrong? Relevant code pasted below.

 

func streamMongoCursorAsJSONResponse(w http.ResponseWriter, cur mongo.Cursor) {
    w.Header().Set("Content-Type", "application/json")
    w.Write([]byte("["))
    count := 0
    for cur.Next(context.Background()) {
        elem := bson.NewDocument()
        err := cur.Decode(elem)
        if err != nil {
            continue
        }
        if count != 0 {
            w.Write([]byte(",\n"))
        }
        w.Write([]byte(elem.ToExtJSON(false)))
        count++
    }
    w.Write([]byte("]"))
}

 



 Comments   
Comment by Kristofer Brandow (Inactive) [ 25/Oct/18 ]

Hi gpean,

I'm moving this to closed. If you are still having this issue, please comment on this ticket or open a new one.

Thanks,

Kris

Comment by Kristofer Brandow (Inactive) [ 19/Oct/18 ]

Hi gpean,

We have implemented a new extended JSON encoder and decoder. Can you update your code and see if escaping is working properly for you?

Thanks,

Kris

Comment by Ayush Gupta [X] [ 11/Aug/18 ]

Maybe we can do:

s = fmt.Sprintf("%s", s)

If it works?

Comment by Gregoire Pean [ 27/Jun/18 ]

Updated

https://github.com/mongodb/mongo-go-driver/pull/70/commits/9b87979bcb0773023f6cad069c369b7e6478c08e

Comment by Gregoire Pean [ 27/Jun/18 ]

Good point!

Comment by Юрий Соколов [ 27/Jun/18 ]

I think, it is still not valid JSON escaping. http://www.ietf.org/rfc/rfc4627.txt

> All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

Comment by Gregoire Pean [ 20/Jun/18 ]

https://github.com/mongodb/mongo-go-driver/pull/70

Comment by Gregoire Pean [ 20/Jun/18 ]

Working patch:

diff --git a/bson/extjson_bytes_converter.go b/bson/extjson_bytes_converter.go
index 712ed0c..33bc855 100644
--- a/bson/extjson_bytes_converter.go
+++ b/bson/extjson_bytes_converter.go
@@ -50,6 +50,7 @@ func ToExtJSON(canonical bool, bson []byte) (string, error) {
 }
 
 func (w *extJSONWriter) writeStringLiteral(s string) error {
+       s = strings.Replace(s, `"`, `\"`, -1)
        s = `"` + s + `"`
        _, err := w.Write([]byte(s))

 

Comment by Gregoire Pean [ 20/Jun/18 ]

Looking at the driver code below it seems we simplistically append strings without any attempt at escaping?

 

func (w *extJSONWriter) writeStringLiteral(s string) error {
  s = `"` + s + `"`	
  _, err := w.Write([]byte(s))
  return err
}

 

 

Generated at Thu Feb 08 08:34:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.