Search code examples
coldfusionsanitizationcoldfusion-11

How do you protect css id


I have a value coming in that will ultimately be an html id= attribute. I don't have control over what set the value, so it is possible that it is not safe. I know to check for single quotes and double quotes, but ow do I check to make sure that it clean?

                                variables.result &= '<div class="alert alert-danger"';
if(attributes.id        != "")  variables.result &= ' id="#attributes.id#"';

Solution

  • If using ColdFusion to generate the variable name, you could use the "variablise" method of the Inflector CFC. It will convert any string into a safe underscore-separated list that can be used as a ColdFusion variable name. (Inflector is based on the Ruby on Rails ActiveSupport::Inflector class.)

    https://github.com/timblair/coldfusion-inflector

    <cffunction name="variablise" access="public" returntype="string" output="no" hint="Converts a string to a variable name, e.g. CamelCase becomes camel_case, 'big CSSDogThing' becomes big_css_dog_thing etc.">
        <cfargument name="string" type="string" required="yes" hint="The string to variablise">
        <cfset arguments.string = replace(trim(rereplace(arguments.string, "([^[:alnum:]_-]+)", " ", "ALL")), " ", "-", "ALL")>
        <cfset arguments.string = rereplace(arguments.string, "([A-Z]+)([A-Z][a-z])", "\1_\2", "ALL")>
        <cfset arguments.string = rereplace(arguments.string, "([a-z\d])([A-Z])", "\1_\2", "ALL")>
        <cfreturn lcase(replace(arguments.string, "-", "_", "ALL"))>
    </cffunction>