Eval a url

Back to articles

hackvertor

Author:

Gareth Heyes

@hackvertor

Published: Tue, 08 May 2012 08:40:39 GMT

Updated: Sat, 22 Mar 2025 15:38:18 GMT

You might have seen a blog post or came to the conclusion that urls are in fact valid JavaScript such as:


http://thespanner.co.uk 
(label) (comment)

That's weird and cool but how do we execute JavaScript from the url? Something like:


http://thespanner.co.uk\nalert(1) 
(label) (comment) (newLine) (functionCall)

Trouble is the new line isn't allowed inside the browser url bar or is it? ES5 introduced in the standard that line separators and paragraph separators would act as traditional new lines in JavaScript and separate new statements. Thankfully our friend IE allows us to do this directly in the url. Using these characters allows you to create an eval'able url.

[test line sep](http://challenge.hackvertor.co.uk/?challenge=1&input=<svg onload=eval%28URL%29&
alert%28) [test para sep](http://challenge.hackvertor.co.uk/?challenge=1&input=<svg onload=eval%28URL%29&
alert%28)

So now we don't need to do eval(location.hash.slice(1)) we can simply do eval(location) :) I found this while discussing with Mario and Yosuke Hasegawa on what the shortest HTML based XSS injection was. Using this technique it's probably 21 (without using netscape 4).


<svg onload=eval(URL)

You of course must pass your JavaScript as a non-existent query param such as:


&&#x2028;alert(1)

Update...

As Stefano Di Paola points out, using hash will allow you to use this technique on Chrome and Opera.

Back to articles