Importing JSON feed should the content be sanitized?

There are two aspects here

  1. obviously all input should be sanitized
  2. JSON is just a wrapper no different then any other type of container which is used to aggregate data for transmission. You almost never sanitize the container as usually in case of an error you will just not be able to extract the data from it, but each piece of data should be sanitized. Since sanitization depends on context it is just impossible to have a generic rules that can be applied to all data in all the possible contexts.

So you need to do sanitization, but can you just trust core API to do it for you? It again depends on context. If you just need the data to be stored in the DB without breaking anything then the DB access APIs will do everything for you, but if you have to have the content of a post be XHTML complient then you will have to write your own validation.

In one line: Always sanitize input as much as possible. Once everything is sanitized on the “data model” level, you can trust wordpress API to not generate additional gotca moments.

Leave a Comment