What I've done:
I created index.html with xss.js, which calls the jQuery.get () function. Then I opened index.html in a browser (Firefox, Chrome, IE and Opera) and tried to call the ajax request.
Code
Here is my index.html:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>XSS</title> <script src="libs/js/jquery-1.7.2.js" ></script> </head> <body> <button id="request" >fire</button> <script src="libs/js/xss.js" ></script> </body> </html>
and my xss.js:
function init() { $('#request').click(loadContent); } function loadContent() { $.get('http://www.example.com/', null, function(data){ alert('success'); $('body').html(data); }, 'html'); } init();
If I open index.html in a browser ( file:///C:/workspace/xss%20test/index.html ), after clicking the button I get the following answers:
Firefox : no error code ( HTTP/1.1 200 OK ), but the answer is empty
IE : no answer
Chrome : XMLHttpRequest cannot load http://www.example.com/. Origin null is not allowed by Access-Control-Allow-Origin. XMLHttpRequest cannot load http://www.example.com/. Origin null is not allowed by Access-Control-Allow-Origin.
Opera : error code ( HTTP/1.1 200 OK ) and the full html file as an answer, but nothing will be displayed (success callback does not start)
This code will load index.html into my Android WebView:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.setWebChromeClient(new WebChromeClient()); webview.setWebViewClient(new WebViewClient()); webview.loadUrl("file:///android_asset/www/index.html"); } }
calls the success callback and also displays the contents of www.example.com in the body of my index.html file after the button starts.
(The same is possible on iPhone devices - I have not tested this on Windows Phone devices).
tl; dr - Question:
Why can I download content from a remote server to my mobile device - is this not a cross-domain script or am I missing something?
Due to browser security restrictions, most Ajax requests are subject to the same origin policy; The request cannot successfully retrieve data from another domain, subdomain, or protocol.
Also: Why does Opera get a response but doesn't display anything?
Thanks in advance.