Feeds

Aspect-oriented programming and security

Working together

High performance access to file storage

Real Example: Adding Input Validation to a Legacy Application

Of course, using AOP to its full potential on existing applications would entail a near rewrite of the application - something that is often not feasible. What should be more interesting to the security community is how to use AOP in an existing code base for security functions that are not already implemented.

Let's take a look at an existing Customer Relationship Management (CRM) application that has several Cross-site Scripting (XSS) vulnerabilities. When I try to enter in a new sales lead in one of the web forms with the description <script>alert(document.cookie)</script>.

I get:

This form is vulnerable to XSS. Since I know there are several parts to the code that share this vulnerability, I decide that I want to protect the NewLead object in the business layer by adding input validation to any time that a setter method on a String attribute is called. Here is a snippet of the NewLead class that has several setter methods:

I created the following code in programming language AspectJ, which can be easily integrated into existing Java applications - to mitigate the XSS in the application. Don't worry if this looks confusing. I'll explain the example in detail:

public aspect InputValidator-

An Aspect in AspectJ closely resembles a class. It can have instance variables and methods inside of it. There are some key differences, however, including pointcuts and advice.

pointcut myPointcut(String argument): execution(* NewLeadBean.set*(String)) && args(argument)-

A pointcut in AOP defines where we want our aspects to interact with the rest of the code. In this case, our pointcut myPoint() is invoked anytime anyone calls setName(), setSalesStage(), etc. from the NewLeadBean class. The syntax NewLead.set*(String) is intuitive: any method that is part of the NewLead class, begins with "set", and has a single string parameter.

The && args(argument) part is saying that we want to save the string parameter to a variable named argument (e.g. if somebody called myNewLeadObject.setName("rohit") then rohit would be saved to the variable argument). Pointcuts can get quite complicated - for a detailed discussion please see the AspectJ programming guide[4].

before (String argument): myPointcut(argument)

This is what we call "advice". It is saying, before the invocation of myPointcut (i.e. before NewLead.set*() is called), execute the following code. Note we could also have specified after or around (i.e. both before and after) innovation of the pointcut. The argument is the string that's passed in as a parameter. This example shows that we are taking the string argument (in our case, "rohit") and checking it to see if it passes white-list validation [7] with a regular expression checker. If I had entered in something along the lines of <script>alert(document.cookie)</script> as the name of my sales lead, I would have seen the following page on screen:

This shows that the aspect successfully stopped the attack.

Combat fraud and increase customer satisfaction

More from The Register

next story
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Oh no, Joe: WinPhone users already griping over 8.1 mega-update
Hang on. Which bit of Developer Preview don't you understand?
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Next Windows obsolescence panic is 450 days from … NOW!
The clock is ticking louder for Windows Server 2003 R2 users
Ditch the sync, paddle in the Streem: Upstart offers syncless sharing
Upload, delete and carry on sharing afterwards?
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
prev story

Whitepapers

SANS - Survey on application security programs
In this whitepaper learn about the state of application security programs and practices of 488 surveyed respondents, and discover how mature and effective these programs are.
Combat fraud and increase customer satisfaction
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Top three mobile application threats
Learn about three of the top mobile application security threats facing businesses today and recommendations on how to mitigate the risk.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.