Lost Password?

Go Back   CodeCall Programming Forum > Web Development Forum > JavaScript and CSS

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

JavaScript and CSS Extensible Markup Language, Java Script, and CSS questions here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-10-2008, 05:33 PM
twalters84 twalters84 is offline
Learning Programmer
 
Join Date: Oct 2007
Posts: 56
Credits: 0
Rep Power: 4
twalters84 is on a distinguished road
Default XSS Insertion Prevention

Hello everyone,

I have done pretty well to prevent XSS Insertions by not allowing HTML to be entered in edtible text areas.

However, what if somebody has the following situation. Let's say you wanted to give users a way to enter a URL in an input form. This URL could be a previous employer's website, education institution, or even a portfolio.

Lets say the client entering the URL worked for Google. So they enter Google into the input field and click submit.

This website is dynamic so it will automatic appear as follows:

<a href="http://www.google.com" target="_blank">http://www.google.com</a>

That worked great!!! Now, here is the problem. What if a hacker came along and entered something like this in the text area.

http://www.google.com" onclick="JAVASCRIPT

The link would then appear as follows:

<a href="http://www.google.com" onclick="JAVASCRIPT" target="_blank">http://www.google.com</a>

Obviously, without prevention, a hacker could use any javascript he / she wanted. They could send the person to some other website or even import their own code.

How can I, as a programmer, prevent this from happening?

Thanks for any advice as it is always appreciated.

Sincerely,
Travis Walters
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-10-2008, 06:52 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,237
Last Blog:
Passwords
Credits: 877
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default

There are many things you can do to prevent XSS. However, the general rule of thumb is that XSS prevention an "output thing" not an "input thing."

In your example above, the first thing I would use a regular expression to first validate the URL. Whether you do that in JavaScript, PHP, or some other server side language, thats the first thing you should do. All domain names have a specific format with only certain letters that are allowed. Checking this should be good enough.

However, you should also filter the output. Using a server-side language like PHP, and a function like this: htmlspecialchars should make your site very secure.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-10-2008, 09:08 PM
twalters84 twalters84 is offline
Learning Programmer
 
Join Date: Oct 2007
Posts: 56
Credits: 0
Rep Power: 4
twalters84 is on a distinguished road
Default Hey there

Hey there,

Thanks for the great reply.

What do you mean exactly when you say the first thing you do is validate the URL?

Does this mean you have some script check for

"http://www." + DOMAIN + TOP LEVEL DOMAIN EXTENSION

For the second part of your reply, I found the coldfusion equivalent here:

Entize - trac - Trac

I found something important here as well:

Chris Shiflett: Character Encoding and XSS

It says the following:

"When using htmlspecialchars() without specifying the character encoding, XSS attacks that use UTF-7 are possible."

Does the following prevent UTF-7 attacks?

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Thanks in advance for anymore information you can provide.

Sincerely,
Travis Walters
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-10-2008, 10:37 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,237
Last Blog:
Passwords
Credits: 877
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default

Quote:
Originally Posted by twalters84 View Post
What do you mean exactly when you say the first thing you do is validate the URL?

Does this mean you have some script check for

"http://www." + DOMAIN + TOP LEVEL DOMAIN EXTENSION
Pretty much, but you also have to account for different protocols [http/https] and sub domain's.

[protocol]://[subdomain].[domain].[extension] and each part of the URL has only specific letters that can be used. I know JavaScript can validate it using regular expressions [don't ask me the specific regex, because I have no clue] and I'm sure Coldfusion has regex too. If you are really paranoid, you can try to establish a connection to the specified URL [maybe using cURL library] to make sure that the URL actually exists, not just in the proper format.

And I have read Chris Shiflett's article before, but I haven't done much research on the information in that article, so I can't say any thing for certain about it. But according to that article, and other sources I have read, setting your character encoding in the meta tags only allows that encoding to be used on the page. So in theory it should prevent any UTF-7 attacks.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall

Last edited by John; 01-10-2008 at 10:40 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-10-2008, 11:16 PM
twalters84 twalters84 is offline
Learning Programmer
 
Join Date: Oct 2007
Posts: 56
Credits: 0
Rep Power: 4
twalters84 is on a distinguished road
Default

Hey there,

I just wanted to say thanks for the help.

I know you said that XSS prevention is an output thing, but I would rather be safe and control input and output.

For other readers interested in prevention, I have been using something like the following to control input to a certain extent:

<cfif #REFind("<[^>]*>", FORM.VARIABLE_NAME)#>
<cfset htmlFound = 1>
</cfif>

However, after reading about these security issues, I am going to take further measures.

I will probably hire a security expert eventually as well as an SEO specialist.

Thanks again for the help. Got some work to do now

Sincerely,
Travis Walters
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-11-2008, 03:59 PM
TcM's Avatar   
TcM TcM is offline
Moderator
 
Join Date: Aug 2006
Location: In a technologic world :p
Posts: 8,317
Credits: 0
Rep Power: 74
TcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud of
Default

The htmlspecialchars will still execute the HTML? for example if you have <b>Hello</b> Will Hello still appear bold?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-11-2008, 05:14 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,237
Last Blog:
Passwords
Credits: 877
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default

Quote:
Originally Posted by TheComputerMaster View Post
The htmlspecialchars will still execute the HTML? for example if you have <b>Hello</b> Will Hello still appear bold?

Thanks
No, it would be converted to:
HTML4Strict Code:
  1. &lt;b&gt;Hello&lt;/b&gt;
And be displayed as:
Quote:
<b>Hello</b>
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-12-2008, 05:06 AM
TcM's Avatar   
TcM TcM is offline
Moderator
 
Join Date: Aug 2006
Location: In a technologic world :p
Posts: 8,317
Credits: 0
Rep Power: 74
TcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud of
Default

Hmm exactly as my script did.. I had to remove that so be able to make links....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-13-2008, 12:24 AM
twalters84 twalters84 is offline
Learning Programmer
 
Join Date: Oct 2007
Posts: 56
Credits: 0
Rep Power: 4
twalters84 is on a distinguished road
Default Quick Question

Hey Guys,

The security update is coming along great.

I have a quick question though.

How do you validate whether a URL exists?

Thanks in advance.

Sincerely,
Travis Walters
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-13-2008, 11:21 AM
twalters84 twalters84 is offline
Learning Programmer
 
Join Date: Oct 2007
Posts: 56
Credits: 0
Rep Power: 4
twalters84 is on a distinguished road
Default Should have mentioned..

Hey there,

I should have mentioned that I asked about validating a URL in this thread already.

What I mean by my question in the last post is I would think there is already a function that does this?

Something in coldfusion would be great, but a PHP example could also benefit readers.

Thanks again.

Sincerely,
Travis Walters
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak prevention methodogies c___newbie C and C++ 2 11-18-2007 10:29 AM
Dynamic Email insertion in HTML Gibster HTML Programming 5 07-17-2007 04:22 PM


All times are GMT -5. The time now is 02:21 AM.

Contest Stats

Xav ........ 1322.18
MeTh0Dz|Reb0rn ........ 1053.7
morefood2001 ........ 879.43
John ........ 877.37
marwex89 ........ 869.98
WingedPanther ........ 830.24
Brandon W ........ 735.07
chili5 ........ 309.39
Steve.L ........ 239.84
dcs ........ 216.02

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads