Stack Exchange API V2.0: SafetyPosted: 2012/02/08
Every method in version 2.0 of the Stack Exchange API (no longer in beta, but an app contest continues) can return either “safe” or “unsafe” results. This is a rather odd concept that deserves some explanation.
Safe results are those for which every field on every object returned can be inlined into HTML without concern for script injections or other “malicious” content. Unsafe results are those for which this is not the case, however that is not to imply that all fields are capable of containing malicious content in an unsafe return.
An application indicates where it wants safe or unsafe results via the filter it passes along with the request. Since most requests should have some filter passed, it seemed reasonable to bundle safety into them.
To the best of my knowledge, this is novel in an API. It’s not exactly something that we set out to add however, there’s a strong historical component to its inclusion.
In version 1.0 of the Stack Exchange API every result is what we would call unsafe in version 2.0. We were simply returning the data stored in our database, without much concern for encoding or escaping. This lead to cases where, for example, question bodies were safe to inline (and in fact, escaping them would be an error) but question titles required encoding lest an application open itself to script injections. This behavior caused difficulties for a number of third-party developers, and bit us internally a few times as well; which is why I resolved to do something to address it in version 2.0.
I feel that a great strength in any API is consistency, and thus the problem was not that you had to encode data, it was that you didn’t always have to. We had two options, we could either make all fields “high fidelity” by returning the most original data we had (ie. exactly what user’s had entered, before tag balancing, entity encoding, and so on) or we could make all fields “safe” by make sure the same sanitization code ran against them regardless of how they were stored in the database.
Strictly speaking, I would have preferred to go with the “high fidelity” option but another consideration forced the “safe” one. The vast majority of the content in the Stack Exchange network is submitted in markdown, which isn’t really standardized (and we’ve added a number of our own extensions). Returning the highest fidelity data would be making the assumption that all of our consumers could render our particular markdown variant. While we have open sourced most of our markdown implementation, even with that we’d be restricting potential consumers to just those built on .NET. Thus the “high fidelity” option wasn’t just difficult to pursue, it was effectively impossible.
Given this train of thought, I was originally going to just make all returns “safe” period. I realize in hindsight that this would have been a pretty grave mistake (thankfully, some of the developers at Stack Exchange, and some from the community, talked me out of it). I think the parallel with C#’s unsafe keyword is a good one, sometimes you need dangerous things and you put a big “I know what I’m doing” signal right there when you do. This parallel ultimately granted the options their names; properly escaped and inline-able returns are “safe”, and those that are pulled straight out of the database are “unsafe”.
Adding a notion of “safety” allows us to be very consistent in how data we return should be treated. It also allows developers to ignore encoding issues if they so desire, at least in the web app case. Of course, if they’re willing to handle encoding themselves they also have the option.