sourcegraph / jsonrpc2

Package jsonrpc2 provides a client and server implementation of JSON-RPC 2.0 (http://www.jsonrpc.org/specification)
MIT License
196 stars 62 forks source link

Fix send UTF-8 special html symbols #16

Closed asuleymanov closed 7 years ago

asuleymanov commented 7 years ago

Fix send UTF-8 special html symbols

keegancsmith commented 7 years ago

Which client or server has an issue with the escaping used by the default json serialization setting?

asuleymanov commented 7 years ago

To be honest, I do not know what server is on which the processing of u003c and u003e does not work. But I ran into this and the solution was found only in this code. Even if I do not accept my revision as it is. I think it's worth thinking about the possibility of customization. Since this can occur anywhere.

keegancsmith commented 7 years ago

This change won't necessarily work. For example if the value you pass into SetResult or SetError is a *json.RawMessage then that bypasses serialisation. Or if the value implements json.Marshaller. I know at least at Sourcegraph we use that pattern a lot in our infra for proxying messages around.

As such, what you really need to do is ensure that anything going over the wire does not use the escaping for your bad server/client. The way to do that is to just implement your own ObjectCodec. That ObjectCodec is responsible for writing the JSON value over the wire, so can use your JSONMarshal implementation. However, like I mentioned above it will just skip over values which implement Marshaller. So you would have to transform the encoded bytes to the actual value, or marshal -> unmarshal -> marshal with SetEscapeHTML to false.

asuleymanov commented 7 years ago

Thanks for the answer. Most likely you are right and I will think about it.