<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>Spencer Creasey</title><generator>Tumblr (3.0; @spencercreasey)</generator><link>http://spencercreasey.com/</link><item><title>Javascript Eval: A lesser Evil?</title><description>&lt;p&gt;The &lt;a href="http://www.jslint.com/lint.html#evil"&gt;evilness&lt;/a&gt; of &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval"&gt;window.eval&lt;/a&gt; has been beaten to death. I’ll save you a recap, but the biggest drawback is its potential for &lt;a href="http://en.wikipedia.org/wiki/Cross-site_scripting"&gt;XSS&lt;/a&gt;. However, there are still valid (though limited) uses of eval—mostly to parse JSON when native methods are not available.&lt;/p&gt;

&lt;p&gt;Futzing around, I noticed that it possible to execute javascript without eval.&lt;/p&gt;

&lt;pre&gt;
var div = document.createElement("div");
div.innerHTML = "&lt;input onchange=\"alert('hi');\" /&gt;";
div.firstChild.onchange();
&lt;/pre&gt;

&lt;p&gt;There isn’t much to gain from this. But if moved into an iframe and fire the onclick event rather than calling it directly, could it be possible to dereference the parent window? That might allow a more secure state for using our new “eval”.&lt;/p&gt;

&lt;p&gt;The following does just this. The parent window is detached by first removing the iframe from the document. Afterward, window.top no longer references the parent window.&lt;/p&gt;

&lt;pre&gt;
window.SECRET = "!"; // something to hide!
window.IFRAME = document.createElement("iframe");
document.lastChild.appendChild(IFRAME); 
var doc = IFRAME.contentWindow.document;
doc.open();
doc.write("&lt;input onclick=\"" + 
    "var c = top.console; " + 
    "c.log('Secret: ' + top.SECRET); " + 
    "top.IFRAME.parentNode.removeChild(top.IFRAME); " +
    "c.log('Secret: ' + (top &amp;&amp; top.SECRET ? top.SECRET : 'Whaa!')); " + 
    "\" /&gt;");
doc.close();
var input = doc.getElementsByTagName("input")[0];
if (doc.createEvent) {
    var event = doc.createEvent("MouseEvent");
    event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, 
        false, false, false, false, 0, null);
    input.dispatchEvent(event);
} else if (doc.fireEvent) {
    input.fireEvent("onclick");
}
&lt;/pre&gt;

&lt;p&gt;Prints:&lt;/p&gt;

&lt;pre&gt;
Secret: 1
Secret: Whaa!
&lt;/pre&gt;

&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;I’ve mocked up a simple test using this concept. Enter javascript below and run. Note: you won’t have access to any of the usual functions (alert, console, etc.).&lt;/p&gt;

&lt;p&gt;&lt;input type="text" id="codetoeval" value="window.top" style="font-size: 13pt;border:none;-webkit-box-shadow: 0 0 6px rgba(0,0,0,0.5);box-shadow: 0 0 6px rgba(0,0,0,0.5);padding:4px;width: 323px;"&gt;&lt;input type="button" value="Run" style="-webkit-box-shadow: 0 0 6px rgba(0,0,0,0.5);box-shadow: 0 0 6px rgba(0,0,0,0.5);border:none;background-color:white;padding: 4px 12px;font-size: 13pt;width:53px;margin-left: 7px;" onclick="runSafeEval()"&gt;&lt;/p&gt;

&lt;pre id="codereturn" style="display:none"&gt;&lt;/pre&gt;

&lt;p&gt;Tested in IE, Chrome, and Firefox.
(working out some of the kinks of this demo - bare with me :)&lt;/p&gt;

&lt;script type="text/javascript"&gt;
window.runSafeEval = function() {
    var return_div = document.getElementById("codereturn");
    var code_div = document.getElementById("codetoeval");
    return_div.style.display = "block";
    var return_value = safeEval(code_div.value);
    if (return_value === null) {
        return_value = "null";
    }
    return_div.innerText = return_value;
    return_div.textContent = return_value;
};

var safeEval = function(code){
    code = code || "";
    window.VALUEOBJ = { value: null };
    // Setup iframe
    window.IFRAME = document.createElement("iframe");
    window.IFRAME.style.display = "none";
    document.lastChild.appendChild(IFRAME);
    // Append custom code as an onclick event
    var doc = IFRAME.contentWindow.document;
    doc.open();
    doc.write("&lt;input onclick=\"" +
        "var valueObj = top.VALUEOBJ; " +
        "top.IFRAME.parentNode.removeChild(top.IFRAME); " +
        "window.parent = undefined; " +
        "try { " +
        "  valueObj.value = " + code+ "; " +
        "} catch (ex) { " +
        "  valueObj.value = 'Error: ' + ex.message; " +
        "} " +
        "\" /&gt;");
    doc.close();
    // Find element and fire onclick event
    var input = doc.getElementsByTagName("input")[0];
    if (doc.createEvent) {
        // Good boys...
        var event = doc.createEvent("MouseEvent");
        event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
            false, false, false, false, 0, null);
        input.dispatchEvent(event);
    } else if (doc.fireEvent) {
        // IE
        input.fireEvent("onclick");
    }
    return window.VALUEOBJ.value;
};
&lt;/script&gt;</description><link>http://spencercreasey.com/post/14234918900</link><guid>http://spencercreasey.com/post/14234918900</guid><pubDate>Wed, 14 Dec 2011 18:24:00 -0500</pubDate></item><item><title>Gah. So much wasted energy.</title><description>&lt;p&gt;Perhaps you’ve noticed that my previous content is gone. If you didn’t, no worries, it wasn’t worth reading.&lt;/p&gt;

&lt;p&gt;I’ve been struggling with my blog for the last couple months. I’ve wanted to post, but my shitty-themed wordpress laden with useless posts kept even its author away. After some fresh inspiration from a new job (at &lt;a href="http://monetate.com"&gt;Monetate&lt;/a&gt;) and quitting serious gaming, I’ve finally gotten around to doing something about it.&lt;/p&gt;

&lt;p&gt;I wanted to start over (or start, really). I prototyped a design, but I wasn’t sure how I was going to set it up. I considered updating my existing wordpress design, writing my own blog software, hosting on &lt;a href="http://aws.amazon.com/ec2/"&gt;EC2&lt;/a&gt;, &lt;a href="http://www.rackspace.com/cloud"&gt;Rackspace Cloud&lt;/a&gt;, or &lt;a href="http://mediatemple.net/webhosting/gs/"&gt;MediaTemple’s (gs)&lt;/a&gt;. But all this never made much sense. Writing my own blog wasn’t something I was passionate about. I was sick of Wordpress’s constant tugging to update. And paying for blog hosting wasn’t something I needed. I know plenty of awesome people who do host their own blog, and while there &lt;em&gt;are&lt;/em&gt; good reasons to do so, I found there to be too much to leverage from managed hosting. At least, for now.&lt;/p&gt;

&lt;h2&gt;Tumblr&lt;/h2&gt;

&lt;p&gt;I chose to move to Tumblr for a couple reasons:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;&lt;!doctype&gt; to &lt;/html&gt; customization&lt;/li&gt;
&lt;li&gt;Clean admin and draft-to-publish workflow&lt;/li&gt;
&lt;li&gt;Custom domain&lt;/li&gt;
&lt;li&gt;Additional pages&lt;/li&gt;
&lt;li&gt;Tumblr core &amp; community (permalinks, short-urls, likes, followers, etc.)&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Anyways, I’m happier now that I’m here. Maybe.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.tumblr.com/photo/1280/9578146359/1/tumblr_lqq7tjvfpV1r2vu57"&gt;&lt;img src="http://25.media.tumblr.com/tumblr_lqq7tjvfpV1r2vu57o1_500.png" alt="Prototype"/&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://spencercreasey.com/post/9493688855</link><guid>http://spencercreasey.com/post/9493688855</guid><pubDate>Sun, 28 Aug 2011 15:16:00 -0400</pubDate></item></channel></rss>

