Escape from Normality
John Carpenter fans know the only way you’ll escape from New York is if Snake Plissken is there to get you out. When it comes to web security, don’t bother waiting for Kurt Russell’s help. You’re on your own.
Imagine an app with a search function. It takes a form field named
q and, instead of reflecting the search term in the field’s value, it updates the
<input> field like so:
<input id="searchResult" type="text" name="q" value="abc">
It’s not necessarily a bad idea to update the element’s
On the other hand, if you move the server-side string concatenation from the
<input> field to a
<script> tag, then you’ve shifted the XSS problem to a different vector. In our target app, the
<script> document.getElementById('searchResult').value = 'abc'; </script>
Rather than strip apostrophes from the search variable’s value, the developers decided to escape them with backslashes. Here’s how it’s expected to work when a user searches for
document.getElementById('searchResult').value = 'abc\\'';
What if the escape is escaped? Say, by throwing in a backslash of your own to search for something like
document.getElementById('searchResult').value = 'abc\\\\'';
// ⬇ end of string token value = 'abc\\\\''; // ⬆ dangling apostrophe
\\ as a single backslash, accepts the apostrophe as the string terminator, and parses the rest of our payload.
document.getElementById('searchResult').value = 'abc\\\\';alert(9)//';
+) to glue the
alert function to the value:
document.getElementById('searchResult').value = 'abc\\\\'+alert(9)//';
Or we could try a payload that uses the modulo operator (
%) between the String and our alert.
Maybe the developers added the
alert function to a denylist, e.g. a regex for
alert\(, by checking for an opening parenthesis. In that case, call the function via the
window object’s property list. This makes it look like an innocuous string to naive regexes:
What happens if the denylist contained the word
alert altogether? Build the string character by character:
A few additional tips when defending against the payloads:
- In code reviews, be suspicious of string concatenation. Use safer methods to bind user-supplied data to HTML.
- If you create output encoding methods rather than relying on frameworks like React, make sure they match the DOM context where the data will be written.
- Normalize data before operating on it, whether this entails character set conversion, character encoding, substitution, or removal.
- Apply security checks after normalization, preferring inclusion lists over exclusion lists – it’s a lot easier to guess what’s safe than assume what’s dangerous.
Normalization is an important first step. Any time you transform data you should reapply security checks. Snake Plissken was never one for offering advice. Instead, think of The Hitchhiker’s Guide to the Galaxy and recall Trillian’s report as the Infinite Improbability Drive powers down (p. 61):
…we have normality, I repeat we have normality….Anything you still can’t cope with is therefore your own problem.
Good luck with normality and trying to get an escape right. Security isn’t certain, but one thing is, at least according to Queen – there’s ”no escape from reality.”