Mitrahsoft
  • SERVICES
    • OUR SERVICES
      • SOFTWARE DEVELOPMENT
      • MOBILE DEVELOPMENT
      • BLOCKCHAIN DEVELOPMENT
      • SOFTWARE TESTING
      • CLOUD & DEVOPS SERVICES
      • PRODUCT DEVELOPMENT
      • OFFSHORE DEVELOPMENT
      • SOCIAL MEDIA MARKETING
      TECHNOLOGIES
      • COLDFUSION DEVELOPMENT
      • REACTJS DEVELOPMENT
      • VUEJS DEVELOPMENT
      • ANGULAR DEVELOPMENT
      • NODEJS DEVELOPMENT
      • PYTHON DEVELOPMENT
      • GOLANG DEVELOPMENT
      • GOLANG CORPORATE TRAINING
      COLDFUSION EXPERTISE
      • MURACMS EXPERTISE
      • COLDFUSION APP MIGRATION
      • COLDFUSION HOSTING & SUPPORT
      • CF LEGACY APP MAINTENANCE
      • COLDFUSION REST API
      • ECOMMERCE / SHOPPING CART SOLUTIONS
      • PRESIDECMS EXPERTISE
      MOBILE DEVELOPMENT
      • REACT NATIVE DEVELOPMENT
      • FLUTTER DEVELOPMENT
      • IONIC DEVELOPMENT
      • PHONEGAP DEVELOPMENT
  • ABOUT US
    • ABOUT MITRAHSOFT
    • OUR CLIENTS
    • TESTIMONIALS
    • PORTFOLIO
    • CAREERS
  • CONTACT US
  • BLOG
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
  • OUR SERVICESSOFTWARE DEVELOPMENTMOBILE DEVELOPMENTBLOCKCHAIN DEVELOPMENTSOFTWARE TESTINGCLOUD & DEVOPS SERVICESPRODUCT DEVELOPMENTOFFSHORE DEVELOPMENTSOCIAL MEDIA MARKETING
    TECHNOLOGIESCOLDFUSION DEVELOPMENTREACTJS DEVELOPMENTVUEJS DEVELOPMENTANGULAR DEVELOPMENTNODEJS DEVELOPMENTPYTHON DEVELOPMENTGOLANG DEVELOPMENTGOLANG CORPORATE TRAINING
    COLDFUSION EXPERTISEMURACMS EXPERTISECOLDFUSION APP MIGRATIONCOLDFUSION HOSTING & SUPPORTCF LEGACY APP MAINTENANCECOLDFUSION REST API ECOMMERCE / SHOPPING CART SOLUTIONSPRESIDECMS EXPERTISE
    MOBILE DEVELOPMENTREACT NATIVE DEVELOPMENTFLUTTER DEVELOPMENTIONIC DEVELOPMENTPHONEGAP DEVELOPMENT
  • ABOUT MITRAHSOFTOUR CLIENTSTESTIMONIALSPORTFOLIOCAREERS
  • CONTACT US
  • BLOG

ColdFusion Compatible IsSafeHtml In Lucee Using Antisamy

HomeBlogColdFusion Compatible IsSafeHtml In Lucee Using Antisamy
ColdFusionLucee

ColdFusion Compatible IsSafeHtml In Lucee Using Antisamy

M
Post by MitrahsoftPublished: May 27, 2019

Cross-site scripting:

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side scripts into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy. 

- Ref : Wikipedia

Antisamy:

How do you protect your code from Cross Site Scripting (XSS), when your business requirements state that the user must be able to input HTML? This can be a difficult problem to solve and XSS is very difficult to filter against because there are hundreds of attack vectors. One way is to use any one of the industry standard Java Library ( AntiSamy, JSOUP..etc ) instead of wrote our own custom XSS filtering CFML code. We (at MitrahSoft) prefer to use AntiSamy because of it's flexibility, wide spread usage & support. AntiSamy uses a XML policy file that defines what HTML tags and attributes can be allowed in your application.

Antisamy

isSafeHTML:

Adobe ColdFusion & Lucee provides various build in security functions to handle various security attacks like Cross-Site Request Forgery (CSRF) attacks..etc. Adobe ColdFusion introduced isSafeHTML function in ColdFusion 11 release. This function validates for allowed HTML according to the rules specified in the antisamy policy file. This can be used to prevent unwanted user supplied HTML being used in an application. but this function is not yet implemented in Lucee

Syntax:

IsSafeHTML(inputString [, policyFile])

Returns:

  • It returns boolean value.
  • Returns false if the input violates the allowed HTML rules.

Parameter:

  • inputString - Required. The string to be validated.
  • PolicyFile - Optional. File path for custom AntiSamy policy file. Can be defined in the application scope or if not defined will use ColdFusion server default(coldfusion/lib/antisamy-basic.xml ).

Adobe ColdFusion Example:

cfml
<cfset goodHtmlFormat = "<p>Welcome to all</p>">
<cfsavecontent variable="wrongHtmlFormat">
    <div onmouseover="alert(1)">
</cfsavecontent>
<cfoutput>
	#isSafeHTML(goodHtmlFormat)#  <!--- Result: Yes --->
	#isSafeHTML(wrongHtmlFormat)# <!--- Result: No --->
</cfoutput>
  • This function requires Adobe ColdFusion 11 and up. Not supported on Lucee.
  • To achieve the above example function in lucee, you need to add AntiSamy jar files and refer the following example.

    Invoking AntiSamy from Lucee:

    • AntiSamy requires a couple of jar files, in order to use it's scan & getCleanHTML methods in ColdFusion. You need to add the JAR files to your java classpath or can use JavaLoader library.
    • JavaLoader which allows us to dynamically load jar files, without modifying the java classpath variables, or copying files to particular locations.
    • You can get the antisamy jar files here: Download
    • Using AntiSamy in lucee is actually quite simple, you just need to create an instance of the Java object org.owasp.validator.html.AntiSamy and then invoke the scan(htmlContent, policyFileLocation) method. It returns a CleanResults object which has a bunch of some methods, such as getCleanHTML() which returns sanitized HTML based on your policy.

Lucee Example:

cfml
<cfsavecontent variable="wrongHtmlFormat">
    <div onmouseover="alert(1)">Hello world</div>
</cfsavecontent>

<cfset policyFile = ExpandPath("./antisamy-slashdot-1.4.1.xml")>

<cfset jarArray = [
    ExpandPath("lib/antisamy-bin.1.4.1.jar"),
    ExpandPath("lib/antisamy-required-libs/batik-css.jar"),
    ExpandPath("lib/antisamy-required-libs/batik-util.jar"),
    ExpandPath("lib/antisamy-required-libs/nekohtml.jar"),
    ExpandPath("lib/antisamy-required-libs/xercesImpl.jar")
]>

<!--- using Java Loader to avoid adding jar files to classpath --->

<cfset classLoader = CreateObject(
    "component",
    "lib.javaloader.JavaLoader"
).init(jarArray)>

<cfset antiSamy = classLoader.create(
    "org.owasp.validator.html.AntiSamy"
).init()>

<cfset goodResultExample = antiSamy.scan(
    goodHtmlFormat,
    policyFile
)>

<cfset wrongDataExample = antiSamy.scan(
    wrongHtmlFormat,
    policyFile
)>

<cfoutput>
    #goodResultExample.getCleanHTML()# <br/>
    #wrongDataExample.getCleanHTML()# <br/>
</cfoutput>

Policy File:

You can define parsing rules here that will be used for each tag individually. The following section shows what happens to tags according to what actions AntiSamy has decided to perform on it.

For example,

  • If you want to validate span tag means, you can add the following code in antisamy-slashdot-1.4.1.xml.
xml
<tag name="span" action="validate">
  • If you want to remove a span tag means, you can remove the following code in antisamy-slashdot-1.4.1.xml.
xml
<tag name="span" action="remove">
  • remove - Will remove entire tag when it encountered.
  • validate - Validate the tag attributes based on defined rules.

remove:

Behavior when the tag-rule action is set to "remove" for given tag. Tag is deleted with all of its child text.

validate:

Behavior when the tag-rule action is set to "validate" for given tag. Verify that its attributes and children elements follow rules defined in policy file.

Some methods of CleanResults():

getCleanHTML():

  • Return the filtered HTML as a String.
  • A String object which contains the serialized, safe HTML.

getStartOfScan():

  • Return the time when scan started.
  • A Date object indicating the moment the scan started.Start ScangetEndOfScan():
    • Return the time when scan finished.
    • A Date object indicating the moment the scan finished.
    getScanTime():
    • Return the time elapsed during the scan.
    • A double primitive indicating the amount of time elapsed between the beginning and end of the scan in seconds.Scan TimeaddErrorMessage():
      • Add an error message to the aggregate list of error messages during filtering.
      getErrorMessages():
      • Return a list of error messages.
      • An ArrayList object which contain the error messages after a scan.
      Get ErrorgetNumberOfErrors():
      • Return the number of errors encountered during filtering.
      Number of Error

Tags

ADYENAMAZON-SESANDROIDAPACHEAPACHE-JMETERAWSBARCODE-SCANNERCOLDBOXCOLDFUSIONCOLDFUSION-API-INTEGRATIONCOLDFUSIONBUILDERECHARTSEVENT-GATEWAYFFMPEGFW1JQUERYJSOUPLUCEEMURACMSMURACMS-THEMENODEJSONESIGNALOSSPAYFLOW-PROPAYMENT-GATEWAYPAYPALPRESIDECMSPUPPETEERRAILORAZUNAREACTJSREACTNATIVERESTSASS-COMPILATIONSEMANTIC-UISUBLIMETEXTTINYMCETWILIOYUI-LIBRARY

Archives

JAN 20DEC 19AUG 19JUL 19JUN 19MAY 19APR 19MAR 19FEB 19JAN 19DEC 18NOV 18OCT 18MAR 16NOV 15SEP 15MAY 15MAY 14OCT 13JUN 13MAY 13APR 13MAR 13FEB 13JAN 13

Follow us

Mitrahsoft
United StatesUnited States

Hurst, Texas, United States

+1 (817) 606-8684usa-sales@mitrahsoft.com

IndiaIndia

Head Office

126G2/5, Thiru malai nagaram,
Kovilpatti - 628 501

Madurai

1/B, 2nd & 3rd floor, GV Complex,

Bye Pass Road, SS Colony,

Madurai - 625 016

Coimbatore

2nd Floor, Sri Narmadha Towers,

Murugan Nagar Rd, Thoppampatti Pirivu,

 K. Vadamadurai, Coimbatore - 641 017

+91 9092480924

business@mitrahsoft.com

ColdFusion
  • eCommerce / Shopping Cart Solutions
  • Payment Gateway Integrations
  • Shipping API Integrations
  • ColdFusion Development Services
  • REST API Development
  • MuraCMS Plugin Creation & Personalization
Technology Services
  • ReactJS Development
  • VueJS Development
  • AngularJS Development
  • NodeJS API Development
  • Python Development
  • Golang Development
  • React Native Development
  • D3.JS data Visualization
Other Services
  • Software Development
  • Mobile Development
  • Blockchain Development
  • Cloud & Devops Services

Connect with MitrahSoft

Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field

© 2026 All Rights Reserved. MitrahSoft Solutions Pvt Ltd

Home|About Us|Careers