Feeds

Aspect-oriented programming and security

Working together

Choosing a cloud hosting partner with confidence

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.

Secure remote control for conventional and virtual desktops

More from The Register

next story
That dreaded syncing feeling: Will Microsoft EVER fix OneDrive?
Microsoft's long history of broken Windows sync
Bada-Bing! Mozilla flips Firefox to YAHOO! for search
Microsoft system will be the default for browser in US until 2020
SLURP! Flick your TONGUE around our LOLLIPOP – Google
Android 5 is coming – IF you're lucky enough to have the right gadget
Be real, Apple: In-app goodie grab games AREN'T FREE – EU
Cupertino stands down after Euro legal threats
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Facebook, working on Facebook at Work, works on Facebook. At Work
You don't want your cat or drunk pics at the office
Soz, web devs: Google snatches its Wallet off the table
Killing off web service in 3 months... but app-happy bonkers are fine
prev story

Whitepapers

Why cloud backup?
Combining the latest advancements in disk-based backup with secure, integrated, cloud technologies offer organizations fast and assured recovery of their critical enterprise data.
A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Getting ahead of the compliance curve
Learn about new services that make it easy to discover and manage certificates across the enterprise and how to get ahead of the compliance curve.
Top 5 reasons to deploy VMware with Tegile
Data demand and the rise of virtualization is challenging IT teams to deliver storage performance, scalability and capacity that can keep up, while maximizing efficiency.