Monday, September 21, 2015

Cross-Site Request forgery (CSRF) and Corss-origin-Resource-Share (CORS)

As mentioned in http://jonathanblog2000.blogspot.ca/2015/09/http-cross-origin-request-and-http.html, CORS is for preventing malicious javascript code to access response returned from a different domain, so even if user are tricked to open a malicious web page, it cannot load the response returned from user's real server. This is mostly implemented by enforcing the "Access-Control-Allow-Origin" header on client side.

Cross Site Request Forgery (CSRF), on other hand, is for a different kind of attack, it leverages the fact that browser automatically sends the cookie including authentication cookie with the request to the same server, so if the server based on the authentication cookie to validate the request, then the server will accept the malicious request as a valid one. In this case, the malicious request is sent from the same browser instance triggered by a link in an email or a different web page, CORS will not prevent this kind of attack, as the attacker does not need to get the response for his purpose. As long as the request is processed by server, then the attach is achieved.

To avoid the CSRF attack, the server needs to use something to validate the client request other than session cookie, for example, a http header for CSRF token returned from server will be a good choice. The idea is when the app sends any update (POST, PATCH) request to server, this CSRF header needs to be included by javascript code, so the server can valid the request by checking this particular header.  The request will be executed only if the header is correct.

Note, although malicious code can trick user to send a Get requests to the real server, but it cannot get or parse the server response to find the CSRF token from the response, as the malicious js code is loaded from a different domain, and CORS limitation will block it to load the response returned from the real server. So the malicious code can never send a post request with the CSRF token in it to pass the server side check.
  

HTTP Cross Origin request and HTTP access control (CORS)

For http content loaded from one domain (protocol, host, port), it may have link to request from a different domain, such as an image, a css stylesheet or script. This is very common usage.

However, when using a script to send a request to a different domain, it will expose a security risk as unlike web link, malicious script code can inject request to server without user's acknowledge.

To avoid sending cross origin request from script, browser on client side enforces HTTP access control (CORS) based on server response. The server response includes the header of
Access-Control-Allow-Origin: *
which indicates the response can be accessed by which domain in a cross-site manner. The Access-Control-Allow-Origin header should always include the request's Origin header to allow the original web page to access the response, but it can also add another domain into it, or set * to allow all domains to access it.

Note although the server returns the Access-Control-Allow-Origin header to help client to enforce CORS control, it is the browser (not the server) that checks and enforce the restriction. The server will always return the requested resource to client regardless the request data.


Sunday, September 20, 2015

Cordova core plugin list

When creating a cordova project, it just adds the core function to bridge the javascript (cordova.js) API to the native API. And you need to add the cordova core plugins to access native operating system functionality such as the accelerometer, camera, compass, the file system and others. The core plugins are developed by the Apache Cordova team, and are updated and improved with each new version.

You can also add other third party plugins registered at http://plugins.cordova.io/npm/index.html to your project, but you need to be careful about the quality of the third party plugins, unless they are developed by yourself. So you should always check the core plugins first before looking at third party plugins or implementing a new plugin by yourself.

When searching at http://plugins.cordova.io/npm/index.html, the core plugins has a blue bar at the left as shown below

To make it easy the full core plugin list is added below:

cordova-plugin-battery-status
cordova-plugin-camera
cordova-plugin-console
cordova-plugin-contacts
cordova-plugin-device
cordova-plugin-device-motion
cordova-plugin-device-orientation
cordova-plugin-dialogs
cordova-plugin-file
cordova-plugin-file-transfer
cordova-plugin-geolocation
cordova-plugin-globalization
cordova-plugin-inappbrowser
cordova-plugin-media
cordova-plugin-media-capture
cordova-plugin-network-information
cordova-plugin-splashscreen
cordova-plugin-statusbar
cordova-plugin-vibration
cordova-plugin-whitelist  

Friday, September 11, 2015

Fail to empty trash folder on mac

Sometimes the trash folder is unable to empty for some reason. If it happens, using the below methods to empty it
1. click opt and shift key and right click the trash icon and then select secure empty trash
2. if that does not work, then open terminal, and type
chflags -R nouchg 
with a space after it. Then open trash folder in Finder and select all files and drag&drop files to the terminal to unlock the files
3. Repeat the step 1 again to empty the trash

Sunday, August 30, 2015

Start http web server for static page on mac

Certain web static content (like open ui5 sdk) on local disk must be served from a web server, instead of directly opening from the local file system.

On Mac, once the static web content is available on a local folder, it can be simply started from a terminal.

1. First open terminal app, and navigate to the folder contains the static web content.

2. Run the below comment with the port number as the last parameter
python -m SimpleHTTPServer 8000

3.start the browser, and visit the below url
http://localhost:8000/yourHtmlPage.html

Tuesday, August 4, 2015

Delay javascript starting code for debug when loading html page

1. Include the below js file in the same folder of html file and also include the js file in html file's script element.
The file is also available at  https://github.com/jonathanli2/delaystart

//delaystart.js

var originalFunc;
var waitPeriod = 30;
function setDelayAt(obj, prop, waitSeconds){
originalFunc = obj[prop];
obj[prop] = delayedStartForDebug;
if (waitSeconds){
   waitPeriod = waitSeconds;
}
}

function delayedStartForDebug() {
        var originThis = this;
        var bWait = true;
        var bDone =false;
        var i = 0
        var timer = setInterval(
                function(){
                    if (bWait && i < waitPeriod ){
                        i++;
                        console.log("set bWait to false from console window to continue");
                    }
                    else{
                        if (!bDone){
                            bDone = true;
                            clearInterval(timer);
                            originalFunc.apply(originThis, arguments);
                        }
                    }
             }, 1000);
   }

2. find a method that is used to start the application logic,  just before calling this starting method, add the below code
    setDelayAt(obj, "startingMethodName", 30);
The first parameter is the object that contains the start method, the second parameter is the start method name. The third parameter is optional for seconds to be delayed. 

Monday, August 3, 2015

Problem with javascript xhook, and an alternative for it - xhrhook

xhook is a javascript library that is used to intercept xmlhttprequests, and manipulate the request and response.

However, it needs cautions to use xhook in your javascript project, as it may cause errors if your project customizes xmlhttprequest object. For example, after including xhook.js in your html file,  calling the below code returns undefined instead of a function object as expected.

XMLHttpRequest.prototype.open 

The problem is, xhook uses an proxy approach to override the xmlhttprequest's behavior. Internally xhook overrides the xmlhttprequest construct function, and when caller calls

var xhr = new xmlhttprequest(),;

it just create and return a regular javascript object instead of a real xmlhttprequest object, and this regular javascript object has a private member which points to a real xmlhttprequest object. So when caller calls any xmlhttprequest method, the regular javascript object just forwards the call to the internal real xmlhttprequest to do the job.

Using the proxy approach gives xhook the full control of the request and response. However, the issue is although caller thinks he is dealing with a real xmlhttprequest object,  but actually it is not, so when caller talks to this fake xmlhttprequest object, it may fail to return the expected result as a real xmlhttprequest does.

So, as an alternative, if your project only needs to monitor the request and response data, but does not need to modify the response data, (you can still modify the request data), then xhook may not be the best choice. As it is more reliable and safe to return a real xmlhttprequest object to caller when it is requested,  and then just override each individual methods or event listener. Actually many projects already did the similar thing for the open or send method.

To test with this approach, a new xhrhook project has been started in github at

https://github.com/jonathanli2/xhrhook


Currently xhrhook.js only supports monitor open, send method, and onreadystatechanged and onload event listener. But it should be easy to support all methods and event listeners.

Note all event listener may not be set by application until send method is called, so a default send handler is added automatically when loading xhrhook to hook the event listener handler.