Question

Stripping out html tags / Security

Asked by: bradderick

Hi All,

I am currently building an auction website in coldfusion.

The user can input various things like location, title etc for their auction. One of the things that I am having a bit of trouble with, security-wise, is the BODY text.

So when they add their classified they have a textarea that they can type their body text into.

Now, having done a little bit of expermintation pasting different stuff in there, it seems that HTML is accepted as basically it just pastes the whole field (it is a TEXT type field in MSSQL server) straight into the final 'auction-detail' page that the user views.

There are several problems here as far as security goes, like I think i need to strip out certain tags. For example, if one puts in a <body bgcolor="black"> tag then the entire page has a black background and the text up the top cannot be read.

I was thinking their may be security flaws there as far as javascript goes as well. I imagine they could probably NOT pass coldfusion tags in as these would go in after the server has run through tags. Am i incorrect here? Could they put in CF tags and compromise security?

So, what is the best way to strip out the dangerous tags and what are the tags in question, while still allowing the user to pretty up their auction text a little?

On the other hand, i have been toying with the idea of making several buttons along the side of the textarea, [background colour], [text colour], [bold] [italics] etc etc and then just implementing these if the person selects text in the text area and clicks the button. This would be in some kind of language, just created for this purpose that would allow this stuff in the translation process to be turned into usable html.

Help and code would be great. Thanks for your time!

Cheers,
Brad

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-07-03 at 22:37:48ID20668376
Tags

html

,

coldfusion

,

tags

,

out

,

stripping

Topic

ColdFusion Application Server

Participating Experts
5
Points
175
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. stripping tags
    i have a search engine for searching my site but it doesnt strip tags please see what you can do <?php // You can change the colours of the search results using the variables below: $page_title = '' ; // Enter your own page title here. $this_file = 'searcher.php' ; ...
  2. Strip - Remove HTML tags from string
    What is the best/fastest and most efficient way to remove HTML tags from a string in VFP. Example string: This<br> is an <font color="red"><b>example</b></font>. Would then be returned as: This is an example. Thanks!
  3. Strip ColdFusion tags?
    I have a form that is saving HTML page layout to a field in the database. I'm using this data to create write out physical .html files (.cfm files, actually). But I want to ensure that I'm stripping out any and all ColdFusion tags, since they could be used to really screw t...
  4. cfloop / cfquery cf_tag "only for experts"
    Hi there, This is my question: I am creating a CF_TAG wich i can use to display an amount of articles on specific page. ---------------------------------------------------------------------------------------------------------- This is my cf_tag: <cf_article view="2...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: anandkpPosted on 2003-07-04 at 02:30:31ID: 8855334

Hi Brad

pls copy & paste this file on ur machien & run it - let me know - if thats what ur looking for ?

<!--- Anand K Patel --->

<CFparam DEFAULT="" NAME="x">

<form name"frm" ACTION="x.cfm" METHOD="post">
    <textarea name="x" ROWS="10" COLS="50" wrap="virtual">
    type in ur text here with html & cf tags + type as much test u want here
    so as to make it to "XX" chars & see what output u get
    </TEXTAREA>
    <INPUT TYPE="submit">
</FORM>


===================================================================================================================

<BR>
from CFDOCS : Returns HTML escaped string. All carriage returns are removed from string, and all special characters (> < " &) are escaped.
all carriage returns are trimmed off automatically
<BR>
<CFOUTPUT>      
   <P><B>Output :</B>#HTMLEditFormat(x)#</p>  
</CFOUTPUT>

===================================================================================================================

Option -> carriage returns & special chars - taken care off automatically
<BR>
<CFOUTPUT>      
   <P><B>Output :</B>#HTMLCodeFormat(x)#</p>  
</CFOUTPUT>
<BR>

K'Rgds
Anand

 

by: CrazeePosted on 2003-07-04 at 05:18:08ID: 8856036

I would allow only basic tags like bold or italic ( up to 4-5 ), as common user probally won't bother with other anyway...but it depends on the stucture of your page and the whole idea. In such case you wouldn't have problems with <script> tags... Just scan the string and remove everything with <> except for tags you want to allow.

cheers

 

by: jyokumPosted on 2003-07-04 at 08:51:21ID: 8857264

here's a UDF that could help. just modify the badTags and badEvents list to suit your needs.

<cfscript>
/**
 * Removes potentially nasty HTML text.
 * Version 2 by Lena Aleksandrova - changes include fixing a bug w/ arguments and use of REreplace where REreplaceNoCase should have been used.
 *
 * @param text        String to be modified. (Required)
 * @param strip        Boolean value (defaults to false) that determines if HTML should be stripped or just escaped out. (Optional)
 * @param badTags        A list of bad tags. Has a long default list. Consult source. (Optional)
 * @param badEvents        A list of bad HTML events. Has a long default list. Consult source. (Optional)
 * @return Returns a string.
 * @author Nathan Dintenfass (nathan@changemedia.com)
 * @version 3, March 19, 2003
 */
function safetext(text) {
      //default mode is "escape"
      var mode = "escape";
      //the things to strip out (badTags are HTML tags to strip and badEvents are intra-tag stuff to kill)
      //you can change this list to suit your needs
      var badTags = "SCRIPT,OBJECT,APPLET,EMBED,FORM,LAYER,ILAYER,FRAME,IFRAME,FRAMESET,PARAM,META";
      var badEvents = "bgcolor,onClick,onDblClick,onKeyDown,onKeyPress,onKeyUp,onMouseDown,onMouseOut,onMouseUp,onMouseOver,onBlur,onChange,onFocus,onSelect,javascript:";
      var stripperRE = "";
      
      //set up variable to parse and while we're at it trim white space
      var theText = trim(text);
      //find the first open bracket to start parsing
      var obracket = find("<",theText);            
      //var for badTag
      var badTag = "";
      //var for the next start in the parse loop
      var nextStart = "";
      //if there is more than one argument and the second argument is boolean TRUE, we are stripping
      if(arraylen(arguments) GT 1 AND isBoolean(arguments[2]) AND arguments[2]) mode = "strip";
      if(arraylen(arguments) GT 2 and len(arguments[3])) badTags = arguments[3];
      if(arraylen(arguments) GT 3 and len(arguments[4])) badEvents = arguments[4];
      //the regular expression used to stip tags
      stripperRE = "</?(" & listChangeDelims(badTags,"|") & ")[^>]*>";      
      //Deal with "smart quotes" and other "special" chars from MS Word
      theText = replaceList(theText,chr(8216) & "," & chr(8217) & "," & chr(8220) & "," & chr(8221) & "," & chr(8212) & "," & chr(8213) & "," & chr(8230),"',',"","",--,--,...");
      //if escaping, run through the code bracket by bracket and escape the bad tags.
      if(mode is "escape"){
            //go until no more open brackets to find
            while(obracket){
                  //find the next instance of one of the bad tags
                  badTag = REFindNoCase(stripperRE,theText,obracket,1);
                  //if a bad tag is found, escape it
                  if(badTag.pos[1]){
                        theText = replace(theText,mid(TheText,badtag.pos[1],badtag.len[1]),HTMLEditFormat(mid(TheText,badtag.pos[1],badtag.len[1])),"ALL");
                        nextStart = badTag.pos[1] + badTag.len[1];
                  }
                  //if no bad tag is found, move on
                  else{
                        nextStart = obracket + 1;
                  }
                  //find the next open bracket
                  obracket = find("<",theText,nextStart);
            }
      }
      //if not escaping, assume stripping
      else{
            theText = REReplaceNoCase(theText,stripperRE,"","ALL");
      }
      //now kill the bad "events" (intra tag text)
      theText = REReplaceNoCase(theText,(ListChangeDelims(badEvents,"|")),"","ALL");
      //return theText
      return theText;
}
</cfscript>


<cfset string = "<html><body bgcolor='black'>this is some <b>text</b> that I wrote.<br><script>window.alert('boo');</script></body></html>">
<cfoutput>
#safetext(string)#
</cfoutput>

 

by: crosenblumPosted on 2003-07-07 at 10:50:11ID: 8870988

http://www.cfdev.com/activedit/

ActiveEdit is one of many rich ie editing tools. Specifically for allowing non-html html coding...

Or try this...

http://faq.ozoneasylum.com/771

 

by: substandPosted on 2003-07-07 at 12:35:24ID: 8871737

one thing i just want to ask, is why are you worried about a user messing up thier own auction?   my thought would be who cares?  they paid for it, and they can make it unreadable if they want.  its not like they'll be able to do anything that would harm a viewers computer.

but if you do care, i'd go with jyokums... that looks good to me.


 

by: bradderickPosted on 2003-07-07 at 17:46:08ID: 8873500

Hi All,

Thanks for the great comments i'm going to try them all out today and see how we go!

Cheers,
Brad

 

by: bradderickPosted on 2003-07-07 at 21:01:39ID: 8874153

Anand, thanks for the information on those tags, they allow you to submit exactly what the user types rather than HTML. This does take out the security problems but means that the user can't enter HTML.

jyokum, the code looks good and is a great way to take out bad tags. It is halfway to where i wanted to be.

Looking at

http://www.cfdev.com/activedit/

It looks pretty much like what I am looking for (with the exception that the cost is slightly prohibitive). It is the best of what is available along the lines of an html editor at the moment? Is there anything like this which is as good but cheaper or is this the best solution available?

It does also take out all code that is Non-HTML as well as dangerous tags inside html.

Thanks for all the different answers guys!

Cheers,
Brad

 

by: crosenblumPosted on 2003-07-07 at 21:06:51ID: 8874178

If you check out my bottom link, it was to a barebones javascript/dhtml version of active edit.

 

by: bradderickPosted on 2003-07-07 at 21:14:40ID: 8874214

Thanks crosen, I did have a look at it but unfortunately because of my level of coding skill I am looking more towards a complete solution that needs minimal customisation if possible.

Cheers,
Brad

 

by: anandkpPosted on 2003-07-07 at 21:41:36ID: 8874336

The list of bad tags can be never ending - aint it ???

what ive used is the same logic as used on EE i guess - thats why whatever code we write -
appears as it is ... without getting executed

why on earth - wld u want users to enter GOOD HTML TAGS ???

K'Rgds
Anand

 

by: bradderickPosted on 2003-07-07 at 22:06:29ID: 8874461

Hi anand,

Lol well you definitely have a point there that many many tags can be used badly but I guess you have to decide where the tradeoff is between letting the user have their pretty bold and italic text and security problems!

Cheers,
Brad

 

by: crosenblumPosted on 2003-07-08 at 06:26:37ID: 8876846

Or you can just add an approval layer, emailing that this user updated/added his body content, and it may/may not contain html code. Then you can hand edit it if it contains bad html or not.

 

by: anandkpPosted on 2003-07-08 at 06:36:40ID: 8876942

Imagine if EE was to go thru the BAD & GOOD HTML tags ... & decide what has to go up & what shldn't .............

depends on what u require & what functionalities u want to open up to users

its teh way u look at it :)

K'Rgds
Anand

 

by: bradderickPosted on 2003-07-12 at 20:17:42ID: 8910907

Hi Guys,

Well its seems like we're going to use Activedit I think. I will probably use your tag as well jyokum but for a bad word filter (working on it later) rather than a bad tag filter.

Cheers to all for the help!

Brad

 

by: jyokumPosted on 2003-07-13 at 00:02:03ID: 8911294

I have a pretty good word filter if you're interested.

 

by: bradderickPosted on 2003-07-13 at 06:30:11ID: 8912043

Sure, if you've got some code would love to have a look at it!

Cheers
Brad

 

by: jyokumPosted on 2003-07-13 at 08:34:32ID: 8912282

Usage would be something like
<cfoutput>
#pottyMouth(myString)#
</cfoutput>





<cfscript>
/**
 * Searches for unwanted words and replaces them with a different string
 * Original version by Jeff Yokum
 *
 * @param dirtyString      The string to be searched. (required)
 * @param wholeWords       Matchs whole words only if set to true. Default is true (optional)
 * @param ignoreCase       Ignores case if set to true. Default is true (optional)
 * @param replaceWith      The character to use when replacing of the unwanted word. Default is **** (optional)
 * @param wordList         An optional comma delimited list of words to search for. This will replace the default list (optional)
 * @return Returns a string.
 * @author Jeff Yokum (jyokum<.AT.>yahoo<.DOT.>com)
 * @version 1, July 13, 2003
 */
function pottyMouth(dirtyString){
      var wholeWords = true;
      var ignoreCase = true;
      var replaceWith = '****';
      /*
       * The only reason the word list is encrypted here is to protect the eyes
       * of sensitive programmers or those who may stumble upon this code.
       * When using this UDF, you can set wordList to the list of words you want
       * to replace and the encryptedList variable can be removed.
       */
      var encryptedList = '111C07581D11021A580A180600581F050C1F581F050C1F1D171743161D0D130758040C031C0D5817190813110B5C0C011A0D';
      var wordList = cfusion_decrypt(encryptedList,"potty"); // this is the list of words we don't want
      var pattern = "";
      
      if(ArrayLen(arguments) gte 2) wholeWords = arguments[2];
      if(ArrayLen(arguments) gte 3) ignoreCase = arguments[3];
      if(ArrayLen(arguments) gte 4) replaceWith = arguments[4];
      if(ArrayLen(arguments) gte 5) wordList = arguments[5];
      
      pattern = "(" & ListChangeDelims(wordList,"|",",") & ")";
      if(wholeWords) pattern = "\b" & pattern & "\b";
      if(ignoreCase) pattern = "(?i)" & pattern;
      return REReplace(dirtyString,pattern,replaceWith,"ALL");
}
</cfscript>

 

by: bradderickPosted on 2003-07-13 at 17:34:12ID: 8913800

Thanks jyokum that looks excellent. I will definitely use it in the verification part of the site.

Please check here:

http://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_20677226.html

Thanks again!
Brad

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...