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
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
Sign off my IT project or I’ll PHONE your MUM
Honestly, it’s a piece of piss
Return of the Jedi – Apache reclaims web server crown
.london, .hamburg and .公司 - that's .com in Chinese - storm the web server charts
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
Torvalds CONFESSES: 'I'm pretty good at alienating devs'
Admits to 'a metric ****load' of mistakes during work with Linux collaborators
Ploppr: The #VultureTRENDING App of the Now
This organic crowd sourced viro- social fertiliser just got REAL
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
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.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.