Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Extending ColdFusion

Extending ColdFusion

Welcome to another installment of Extending ColdFusion. This month I'm going to talk about a UDF (user-defined function) that will help deal with users. Users are wonderful. Without people visiting your site, your Web page may as well not exist. On the other hand, users have a way of doing the most stup... err crazy things that you can (or most likely cannot) imagine.

You should always assume that your site visitors will not do what you intend; in other words, you should build your Web site to handle every situation - a goal that is not easy to achieve. One of the (many) ways that users can "do the wrong thing" is when you give them a chance to enter information. Almost all Web sites allow users to enter information via forms. However, as soon as you let the user enter any kind of information, you need to make sure the information isn't something that will throw your site into a tailspin.

Consider forums. Many Web sites have a forum of some type that allows users to have threaded discussions. When posting to the forum, the Web application may ask for the user's name, the subject of his or her post, and then the body of the message. This information is then stored in a database and displayed on the Web page. However, what would happen if the user entered this for a name?

Jacob <b>Camden</b>

Instead of just entering his name, the user entered HTML as well. In this case, the user wrapped his last name with bold tags. This isn't horrible, but when the forum posting is viewed, his name will not appear in the same manner in which other forum postings appear. Next, consider this input:

Jacob <b>Camden

This time, the user entered incorrect HTML. When the Web page is viewed, the layout could be totally off from what it should be. If the user had entered a <table> tag instead, the entire page content may actually disappear in Netscape. In this case, the user just made a stupid mistake, but consider this input:

Jacob Camden<script>document.location.href=
"http://www.whitehouse.gov";</script>

This time the input will do something far worse. When the page is viewed, it will execute JavaScript code that will send visitors of your Web site to the official Web page of the U.S. president. However, if the ".gov" was switched to ".com", they would be sent to a porn site instead. Visitors may actually think that your site was the one hosting the porn.

So, how do we fix this? ColdFusion provides us with two functions that can easily and quickly "clean" HTML code input: HTMLCodeFormat and HTMLEditFormat. The only problem with these two functions is that they will remove all code. What if you want to allow some HTML like the bold or italics tags? The first UDF we will look at will allow you to specify the tags you don't want, and thereby allow other tags that are safe for your Web site.

The UDF, SafeText (www.cflib.org/udf.cfm?ID=56), was written by Nathan Dintenfass (who was featured in my last article as well; see CFDJ, Vol. 4, issue 10). The UDF takes one required argument, text (the text to be cleaned), and three optional arguments. You can specify if bad code should be escaped or removed (defaults to being removed). You can specify a list of bad tags, although the default list is intensive. Finally, you can create a list of bad "events". This allows you to remove JavaScript events like onClick or onMouseOver. Listing 1 contains the UDF.

The UDF begins with a set of var statements. These are variables that exist only in the UDF itself. The important ones are badTags and badEvents. These values are the defaults that define which tags and JavaScript events to strip from the string. After the var statements, we have a code branch that splits between replacing the bad tags or simply removing them. If we need to replace them, a while loop is used to search over the text. If bad tags should simply be removed, one regular expression call is used.

Why is it easier to just remove them? Replacing means calling htmlEditFormat on the particular tag that was matched. If this could be done with a simple regex, please e-mail the author (and me!). However, you can completely remove the bad tags with one call. Next, a regular expression call is used to remove the bad JavaScript events. Last, the cleaned text is removed. Listing 2 contains a simple example. I use <cfsavecontent> to store text into a variable. Notice how I use some good HTML, along with some bad HTML and JavaScript events. The good HTML will remain; the bad will be stripped out.

That's it for this installment. In the next one we'll look at another UDF that we can use to deal with user input. In the meantime, if you have any ideas for UDFs, custom tags, or CFCs that you would like to see discussed, please send them to me at [email protected].

More Stories By Ray Camden

A longtime ColdFusion user, Raymond is a co-author of the "Mastering ColdFusion" series published by Sybex Inc, as well as the lead author for the ColdFusion MX Developer's Handbook. He also presents at numerous conferences and contributes to online webzines. He and Rob Brooks-Bilson created and run the Common Function Library Project (www.cflib.org), an open source repository of ColdFusion UDFs. Raymond has helped form three ColdFusion User Groups and is the manager of the Acadiana MMUG.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
SYS-CON Events announced today that IoT Global Network has been named “Media Sponsor” of SYS-CON's @ThingsExpo, which will take place on June 6–8, 2017, at the Javits Center in New York City, NY. The IoT Global Network is a platform where you can connect with industry experts and network across the IoT community to build the successful IoT business of the future.
CloudEXPO New York 2018, colocated with DXWorldEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering Cloud Expo and @ThingsExpo will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at Cloud Expo. Product announcements during our show provide your company with the most reach through our targeted audiences.
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
Disruption, Innovation, Artificial Intelligence and Machine Learning, Leadership and Management hear these words all day every day... lofty goals but how do we make it real? Add to that, that simply put, people don't like change. But what if we could implement and utilize these enterprise tools in a fast and "Non-Disruptive" way, enabling us to glean insights about our business, identify and reduce exposure, risk and liability, and secure business continuity?
DXWorldEXPO LLC announced today that Telecom Reseller has been named "Media Sponsor" of CloudEXPO | DXWorldEXPO 2018 New York, which will take place on November 11-13, 2018 in New York City, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
In this Women in Technology Power Panel at 15th Cloud Expo, moderated by Anne Plese, Senior Consultant, Cloud Product Marketing at Verizon Enterprise, Esmeralda Swartz, CMO at MetraTech; Evelyn de Souza, Data Privacy and Compliance Strategy Leader at Cisco Systems; Seema Jethani, Director of Product Management at Basho Technologies; Victoria Livschitz, CEO of Qubell Inc.; Anne Hungate, Senior Director of Software Quality at DIRECTV, discussed what path they took to find their spot within the tec...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...