<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Couch &#187; Iframe</title>
	<atom:link href="http://www.codecouch.com/tag/iframe/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codecouch.com</link>
	<description>The ramblings of two code monkeys</description>
	<lastBuildDate>Thu, 19 Nov 2009 15:47:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Enabling cross-site scripting XSS via an iframe</title>
		<link>http://www.codecouch.com/2008/10/cross-site-scripting-xss-using-iframes/</link>
		<comments>http://www.codecouch.com/2008/10/cross-site-scripting-xss-using-iframes/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 12:24:41 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Iframe]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=173</guid>
		<description><![CDATA[Communication from an iframe that has content from a domain other than the one the iframe is contained within is a constant source of frustration - as browsers prevent this kind of activity due to built-in security policies. Here is a technique to perform cross-site scripting (XSS) in just such an environment with example code and a real-world example.]]></description>
			<content:encoded><![CDATA[<p>The scenario is relatively common &#8211; you have a page that contains an iframe pointing to some content hosted on another domain. Nothing wrong with this &#8211; iframes were designed with this in mind! The following diagram shows the sort of setup we are talking about.</p>
<p><img class="size-full wp-image-176" title="Iframed content diagram" src="http://www.codecouch.com/wp-content/uploads/2008/10/xssdiagraminitial.gif" alt="Diagram showing an iframe within a web page" width="394" height="186" /></p>
<p>Now you want to click a link within the iframe content and have that perform some action on the parent page (maybe you want to submit a form on the parent page, maybe you want to adjust the height of the iframe on the parent page). And you start getting frustrated because the browser won&#8217;t let you access all your javascript functions/properties on another domain.</p>
<p>Communication from an iframe that has content from a domain other than the one the iframe is contained within is a constant source of frustration &#8211; as browsers prevent this kind of activity due to built-in security policies. The solution is actually relatively painless requiring the addition of an iframe (see the following diagram).</p>
<p><img class="size-full wp-image-172" title="XSS Diagram" src="http://www.codecouch.com/wp-content/uploads/2008/10/xssdiagram.gif" alt="Diagram showing page structure for XSS via an iframe" width="394" height="186" /></p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>Real world example</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>You can see a working example of this over at <a href="http://movies.sky.com/cinema-finder" title="Sky Movies cinema finder page">Sky Movies</a> where the iframe is resized as the content within the iframe changes. This is being controlled from within the iframe.</p>
<p>Why do we need to implement a solution like this in the first place?</p>
<p>Because whilst the main page at movies.sky.com can set the height of the iframe, it cannot get the height of the iframe content&#8230; and the iframe document can get it&#8217;s height but it can&#8217;t set the height of it&#8217;s enclosing iframe.</p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>An explaination of each component of the solution</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>Using the following example pages, we can create a demonstration of this technique. Following is the source of the main page (marked as [1] in the figure above) which contains the iframe and the function that will be used to resize the iframe.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict" style="color: #000000;"><span class="sc2"><span class="kw2">&lt;html&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;head&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;title&gt;</span></span>Page hosted on example.com<span class="sc2"><span class="kw2">&lt;/title&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;script</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="kw2">&gt;</span></span>
	function resizeIframeHeight(nHeight) {
		var iframe = document.getElementById('mainIframe');
		iframe.setAttribute('height', nHeight);
	}
	<span class="sc2"><span class="kw2">&lt;/script&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/head&gt;</span></span>
&nbsp;
<span class="sc2"><span class="kw2">&lt;body&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;h1&gt;</span></span>This page is hosted on the example.com domain<span class="sc2"><span class="kw2">&lt;/h1&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;p&gt;</span></span>The iframe below is hosted on the example.org domain<span class="sc2"><span class="kw2">&lt;/p&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;iframe</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;mainIframe&quot;</span> <span class="kw3">width</span><span class="sy0">=</span><span class="st0">&quot;400&quot;</span> <span class="kw3">height</span><span class="sy0">=</span><span class="st0">&quot;200&quot;</span> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://example.org/iframedDocument.html&quot;</span><span class="kw2">&gt;</span></span>Iframes not supported.<span class="sc2"><span class="kw2">&lt;/iframe&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/body&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/html&gt;</span></span></pre></div></div></div>

<p>Following is the source of the iframe page (marked as [2] in the figure above) which contains the extra iframe and some javascript that allows this technique to work.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict" style="color: #000000;"><span class="sc2"><span class="kw2">&lt;html&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;head&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;title&gt;</span></span>Page hosted on example.org<span class="sc2"><span class="kw2">&lt;/title&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;script</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="kw2">&gt;</span></span>
	function updateIframeHeight() {
		var iframe = document.getElementById('hiddenIframe');
		var newHeight = parseInt(document.body.offsetHeight,10) + 10;
		iframe.src = 'http://example.com/xssEnabler.html?height=' + newHeight;
	}
	<span class="sc2"><span class="kw2">&lt;/script&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/head&gt;</span></span>
&nbsp;
<span class="sc2"><span class="kw2">&lt;body</span> <span class="kw3">onload</span><span class="sy0">=</span><span class="st0">&quot;updateIframeHeight()&quot;</span><span class="kw2">&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;h1&gt;</span></span>This page is hosted on the example.org domain<span class="sc2"><span class="kw2">&lt;/h1&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;p&gt;</span></span>The iframe below is hosted on the example.com domain (and be styled to be hidden)<span class="sc2"><span class="kw2">&lt;/p&gt;</span></span>
	<span class="sc2"><span class="kw2">&lt;iframe</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;hiddenIframe&quot;</span> <span class="kw3">width</span><span class="sy0">=</span><span class="st0">&quot;100&quot;</span> <span class="kw3">height</span><span class="sy0">=</span><span class="st0">&quot;100&quot;</span> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://example.com/xssEnabler.html&quot;</span><span class="kw2">&gt;</span></span>Iframes not supported.<span class="sc2"><span class="kw2">&lt;/iframe&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/body&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/html&gt;</span></span></pre></div></div></div>

<p>The final source file resides on the original domain (marked as [3] in the figure above) and contains some javascript to strip out the height parameter and then pass this on to the function on the main page that resizes the iframe. It is referred to in the code above, as the content of the file at http://example.org/xssEnabler.html &#8211; and whilst it is saved as a .html file, it contains just the code you see below.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
	<span class="kw2">function</span> getFirstParamFromLocation<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="kw2">var</span> pair <span class="sy0">=</span> window.<span class="me1">location</span>.<span class="me1">search</span>.<span class="me1">substring</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw2">var</span> parts <span class="sy0">=</span> pair.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">'='</span><span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw1">return</span> parts<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
	<span class="kw2">var</span> nHeight <span class="sy0">=</span> getFirstParamFromLocation<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw1">try</span> <span class="br0">&#123;</span>
		window.<span class="me1">top</span>.<span class="me1">resizeIframeHeight</span><span class="br0">&#40;</span>height<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span> <span class="kw1">catch</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div></div>

<p>Obviously you would not be using example.com and example.org for your solution, they are used merely to identify the different domains you would use for each file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/10/cross-site-scripting-xss-using-iframes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
