The topic I want to talk about is securing file uploads to a server via Flex. If you have added file upload functionality to a Flex application you have probably run into the issue of session information being lost during the upload. And this makes server-side security validation a big issue.
Note: This problem description and solution is using Java application server running BlazeDS or LCDS.
The problem
The problem occurs whenever you perform a file upload using Flex. I'm not going to write all the lines of code here, but basically the following lines of code will do the trick:
var req:URLRequest = new URLRequest();
req.method = URLRequestMethod.POST;
req.data = someData;
var fileReference:FileReference = new FileReference();
fileReference.upload( req, "/phoenix/FileServlet" );
Those lines of code will upload whatever file the user selected to the FileServlet servlet under the phoenix context. Under non-IE browsers this operation will occur in a different browser thread thus causing a different session to be created on the server-side. Thus the application session and upload session are different and are not sharing information. This is basically the root cause of the problem. This means that if you wanted to retrieve the login name of the user currently authenticated, you will get no value. So the following line of code will return NULL:
request.getRemoteUser();
Also if you try to validate that the user has the appropriate role using the standard request.isUserInRole( "UPLOAD_ROLE" ); method call, it will always return FALSE. Needless to say this is a critical issue from a security perspective as you need to know who is doing the file upload and if he is allowed to perform the operation. Without this information basically anybody can perform a file upload request and in some cases with malicious intent.
The solution
Since a new session is being created for the file upload operation, we need to tell the server to associate this session with our existing authentication session. We accomplish this in two parts, first by giving Flex our server session ID and then sending it back during the file upload. Here are the details of these two operations.
Sending back the server-session ID
Right after the Flex application initializes, call the remote server method to retrieve the server-session ID. In Java the remote method will look like this:
public String getSessionInfo()
{
return FlexContext.getFlexSession().getId();
}
Sending the session ID during the upload operation
Now that you have the session ID, you need to send it back with the call to the FileServlet along with the session cookie name. So from our sample above, the following line:
fileReference.upload( req, "/phoenix/FileServlet" );
Should be changed to:
fileReference.upload( req, "/phoenix/FileServlet;cookieName=" + sessionID );
Note: The variable cookieName needs to be the actual session cookie name you have configured for your Java web application (ex.: myappcookie).
So now, when the file upload operation occurs it will send back the session ID along with the file and so the server will associated that with your existing authenticated session. Now you can retrieve the login name of the currently authenticated user and validate that the user has the appropriate roles (see sample code above).