views:

209

answers:

4

Hi Guys,

A bit of a noob question here...

I have a javascript function on a list of table rows

<tr onclick="ClosePopup('{ScenarioID}', '{Name}');" />

However, the {Name} value can sometimes contain the character "'" (single quote). At the moment the error Expected: ')' comes up as a result because it is effectivly ending the javascript function early and destroying the syntax.

What is the best way to prohibit the single quotes in {Name} value from effecting the javascript?

Cheers!

+6  A: 

You're committing the first mortal sin of insecure web template programming - not escaping the content of the values being rendered into the template. I can almost guarantee you that if you take that approach, your web app will be vulnerable to XSS (cross site scripting) and any third party will be able to run custom javascript in your page, stealing user data and wreaking havoc as they wish.

Check it out. http://en.wikipedia.org/wiki/Cross-site%5Fscripting

The solution is to escape the content. And to do that properly in the javascript, which is also inside html, is a lot more than just putting escape sequences in front of backslashes.

Any decent templating engine out there should provide you a way to escape content as it's written to the template. Your database values can be left as-is, the important part is escaping it at output time. If your template engine or dynamic web app framework doesn't allow for this, change to one that does. :)

Josh
+1 word!
some
You forgot to explain how to escape data using the backslash and possibly the replace method. Still +1 though.
fair call, however the values being obtained are done so from rendered XML (produced from a secure database call) using XSLT. And considering im just "getting" data is this really a mortal sin? I would have assumed this only really applies during the saving of data
Scozzard
+1  A: 

In support of the prior comment please read the following to gain a better understanding of why the security advice is so important.

http://eval.symantec.com/mktginfo/enterprise/white%5Fpapers/b-whitepaper%5Fweb%5Fbased%5Fattacks%5F03-2009.en-us.pdf

A: 

I would think that you could kill just about any code injection by, for example, replacing

"Hello"

with

String.fromCharCode(72,101,108,108,111)
Robert L
A: 

Although the security information provided by everyone is very valuable, it was not so relevant to me in this situation as everything in this instance is clientside, security measures are applied when getting the data and rendering the XML. The page is also protected through windows authentication (adminsitration section only) and the web app framework cannot be changed. The answer i was looking for was really quite simple in the end.

<tr onclick='ClosePopup("{ScenarioID}", "{Name}");' />
Scozzard
@Scozzard: And what happens if the data contains `"`?
Grant Wagner
existing data does not contain " and regex validation for inputting data has been updated so only alphanumeric values will be accepted.
Scozzard
But yes this needs to be updated to something way more solid. It is not the ideal fix.
Scozzard