Hacking onerror/throw

# Gareth Heyes (8 months ago)

I’ve been having fun with onerror and throw to call functions passing the argument without using parentheses or template strings. It’s useful for XSS in certain situations where you have a restricted amount of characters.

For example you can do the following:


Then I saw a filter blocking semi-colon and I thought it would be fun to do the same but without that. Because throw accepts an expression you can define the onerror first and the use the last part of the expression to send the argument.

throw onerror = alert, 'some string', 123, 'haha'

Using a block statement you can also remove the need of the semi-colon.

{onerror=alert}throw 1

So then the next stage was to evaluate code, this can be done quite easily in Chrome because it prefixes exceptions with Uncaught.


But on Firefox it prefixes custom exceptions with a two word value which would be invalid JavaScript so of course I tried to hack that and inspected the Error object to see what properties it had. I constructed an object literal with the same properies as the Error object and it worked! Then I just reduced the properties until I found the minimal required.


@terjanq came along and decided to remove all all the string literals and produce a cool vector.


Then Pepe Vila produced a cool vector that doesn’t even use throw at all. Using type error to send the payload, nice!


That’s it we had lots of fun on Twitter with this hope you enjoyed me sharing it.