Sanitizing JSON data for usage as JavaScript object

1.2k views Asked by At

I'm going to be dynamically generating a JSON file which is then passed to SCEditor as the emoticons object; this data will come from the database, so essentially it should be safe, but one can never be 100% sure.

This is how it is being called:

// Create var to store emoticons
var emoticons = false;

$.getJSON('../../images/emoticons/default/emoticons.json')
.done(function(response) {
    emoticons = response;
    console.log(emoticons);
})
.always(function() {
    // always initialize sceditor
    $(".sceditor").sceditor({
        // Other options.....
        plugins: "bbcode",
        emoticons: emoticons,
    });
});

An example of the JSON file would look like:

{
    "dropdown": {
        ":)": "smile.png",
        ":angel:": "angel.png",
        ":angry:": "angry.png",
        "8-)": "cool.png",
        ":'(": "cwy.png",
    }
}

So the emoticon code and filename are pulled from the database. Is there anything I need to do here other than escape double quotes? Whilst this data will be coming from the database, it's possible the codes/filenames will be provided by the user.

When I store them in the database I will be stripping tags with PHP's strip_tags function.

I wanted to avoid turning the data into html entities as it doesn't seem to play nice with the editor as it doesn't turn the emoticons into smileys within the editor if you say set the code as :") - it will literally output in the editor as :") rather than show the smiley.

Edit: To see an example of how the code is used check out the SCEditor demo. Only difference is the demo uses the default codes provided within the JS file itself and mine will be provided via a JSON file passed as an option.

What is are my best options here?

0

There are 0 answers