<?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>Alex Rodriguez &#187; Code</title>
	<atom:link href="http://www.alexbr.com/wblog/?feed=rss2&#038;cat=10" rel="self" type="application/rss+xml" />
	<link>http://www.alexbr.com/wblog</link>
	<description>I don&#039;t play baseball.</description>
	<lastBuildDate>Mon, 07 Jun 2010 14:10:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Workaround for AJAX Cross-Domain Post Restrictions</title>
		<link>http://www.alexbr.com/wblog/?p=109</link>
		<comments>http://www.alexbr.com/wblog/?p=109#comments</comments>
		<pubDate>Wed, 27 Jan 2010 23:22:39 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[ajax cross site domain javascript]]></category>
		<category><![CDATA[bypass]]></category>
		<category><![CDATA[bypass ajax]]></category>

		<guid isPermaLink="false">http://www.alexbr.com/wblog/?p=109</guid>
		<description><![CDATA[We recently developed an application that requires our partners to incorporate a piece of our html code in their browser page. This component would be responsible for collecting information and posting it back to our site via ajax for validation. The inclusion of our code would be done using server side includes so as to [...]]]></description>
			<content:encoded><![CDATA[<p>We recently developed an application that requires our partners to incorporate a piece of our html code in their browser page. This component would be responsible for collecting information and posting it back to our site via ajax for validation. The inclusion of our code would be done using server side includes so as to avoid using frames and for security reasons. The end result would be that the partner would send their page with our html component included in it back to the browser; the browser would show the partner url. All in all very unobtrusive.</p>
<p>Once the user enters their information into a form on that page, our script would take over and submit the information to our server, at a different domain, via ajax. Our server would return the result of the validation of the information.</p>
<p>There is only one very big problem with this approach &#8211; browsers currently consider such a cross-domain ajax post a vulnerability. Browsers will not allow an ajax post to a domain that is different than the domain currently in the browser location. Check out the <a title="same origin policy" href="http://en.wikipedia.org/wiki/Same_origin_policy" target="_blank">same origin policy</a>.</p>
<p>I won&#8217;t dive into an argument for or against allowing cross-site ajax posting here, instead I&#8217;ll just provide the mechanism we used to perform this post without using the standard ajax method, eg. XMLHttpRequest.</p>
<p>At a high level, what we do is this:</p>
<ul>
<li>Capture user input on the client using javascript.</li>
<li>Construct a url containing our user input validation resource, a random identifier, and the query parameters from the user input.</li>
<li>Create a new &#8220;&lt;script&gt;&#8221; DOM node with a src reference to the constructed url. This results in an implicit request being sent by the browser for the javascript source.</li>
<li>Our server handles the request, validates the input, and returns pure javascript as a response. Embedded in this javascript is a function which will call javascript already sitting on the browser once it is loaded.</li>
<li>Once the javascript response is loaded into the DOM by the browser, the script is executed &#8211; performing whatever processing is necessary at the client.</li>
</ul>
<p>Below is the main script. This code handles user input and submits the input asynchronously by inserting a new script node into the DOM. This forces the browser to request the src for that script node. On the server, that request is handled, the input is validated, and javascript is returned back to the client which just executes the &#8216;responseCallback&#8217; method.</p>
<p><strong>Main Script:</strong></p>
<div class="codecolorer-container javascript vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript1.2&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #006600; font-style: italic;">// Validation url</span><br />
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://your.host.com/validationresource&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Callback method</span><br />
<span style="color: #003366; font-weight: bold;">var</span> callbackMethod <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Call this to kick off asynch server validation</span><br />
<span style="color: #003366; font-weight: bold;">function</span> submitRequest<span style="color: #009900;">&#40;</span>input1<span style="color: #339933;">,</span> input2<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Perform client side validation of input first</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// validateInput(input1, input2) &nbsp; &nbsp;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #006600; font-style: italic;">// Assign callback</span><br />
&nbsp; callbackMethod <span style="color: #339933;">=</span> callback<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Find dom node where we'll insert our &quot;script&quot; node</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dynamicjs <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dynamicjs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Remove if it already exists</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dynamicjs <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> dynamicjs.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>dynamicjs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Create url with params and timestamp to prevent caching. I believe</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// the timestamp must be part of the resource url to force the browser</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// to reload the javascript</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> params <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;input1=&quot;</span> <span style="color: #339933;">+</span> input1 <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;input2=&quot;</span> <span style="color: #339933;">+</span> input2<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> jsurl <span style="color: #339933;">=</span> url <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;?&quot;</span> <span style="color: #339933;">+</span> params<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> fileref <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fileref.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;dynamicjs&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fileref.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fileref.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">,</span> jsurl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Insert the node which results in a browser request to the server</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> fileref <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;head&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>fileref<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #006600; font-style: italic;">// Callback method to be executed once a response comes back from</span><br />
<span style="color: #006600; font-style: italic;">// the server validation</span><br />
<span style="color: #003366; font-weight: bold;">function</span> responseCallback<span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>callbackMethod<span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>Below is the server code that handles the request, validates the input from the client, and returns a response that is basically a javascript file. That javascript file contains a script that calls the &#8216;responseCallback&#8217; method above with the result of the input validation.</p>
<p><strong>Server Code (Java):</strong></p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ServletOutputStream out <span style="color: #339933;">=</span> response.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003399;">String</span> result <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ok&quot;</span><span style="color: #339933;">;</span><br />
response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
response.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control&quot;</span>, <span style="color: #0000ff;">&quot;no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;responseCallback('&quot;</span> <span style="color: #339933;">+</span> result <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;');&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
out.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexbr.com/wblog/?feed=rss2&amp;p=109</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
