Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

JSON serialization with optional HTML escaping #1876

Merged
merged 5 commits into from
Dec 5, 2017

Conversation

jackfrancis
Copy link
Member

What this PR does / why we need it: See the description of this method here:

https://golang.org/pkg/encoding/json/#Marshal

The important part:

The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" to keep some browsers from misinterpreting JSON output as HTML. Ampersand "&" is also escaped to "\u0026" for the same reason. This escaping can be disabled using an Encoder that had SetEscapeHTML(false) called on it.

We aren't sensitive to the HTML browser issue described above, and we are unknowingly doing this conversion in places where we don't want to be.

Release note:

JSON serialization with optional HTML escaping

@jackfrancis
Copy link
Member Author

@anhowe @JackQuincy would be great to get your feedback on this change. I'm not aware of any need to HTML-escape < and > in the modified JSON serialization/deserialization flows changed in this PR. Am I missing anything?

Copy link
Contributor

@JackQuincy JackQuincy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good two small feedbacks.

package helpers

import (
// "fmt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

)

// JSONMarshalIndent marshals formatted JSON w/ optional SetEscapeHTML
func JSONMarshalIndent(content interface{}, prefix, indent string, escape bool) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escape only makes sense if you read the method comments. Which I guess that is okay. Other wise from the method name and variable name I'm not sure what is being escaped. Is that normal in go? I know go likes brevity, but I'm not sure if naming depending on the user reading the comment is normal. Also I don't think we ever set escape to true. So we could just make it assumed as false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

We could change the name to JSONMarshalIndentWithEscape, but kinda long.

The reason I made it configurable is because I wasn't sure if we'd never want to include the standard golang implementation (angle brackets "<" and ">" are escaped to "\u003c" and "\u003e")

@jackfrancis jackfrancis merged commit 4e65f30 into Azure:master Dec 5, 2017
@ghost ghost removed the in progress label Dec 5, 2017
@jackfrancis jackfrancis deleted the unicode-less-than-sign branch December 5, 2017 00:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants