001 package railo.runtime.functions.owasp; 002 003 import java.io.PrintStream; 004 005 import org.owasp.esapi.ESAPI; 006 import org.owasp.esapi.Encoder; 007 import org.owasp.esapi.errors.EncodingException; 008 009 import railo.commons.io.DevNullOutputStream; 010 import railo.commons.lang.StringUtil; 011 import railo.runtime.PageContext; 012 import railo.runtime.exp.ApplicationException; 013 import railo.runtime.exp.FunctionException; 014 import railo.runtime.exp.PageException; 015 import railo.runtime.ext.function.Function; 016 import railo.runtime.op.Caster; 017 018 public class ESAPIEncode implements Function { 019 020 private static final long serialVersionUID = -6432679747287827759L; 021 022 public static final short ENC_BASE64=1; 023 public static final short ENC_CSS=2; 024 public static final short ENC_DN=3; 025 public static final short ENC_HTML=4; 026 public static final short ENC_HTML_ATTR=5; 027 public static final short ENC_JAVA_SCRIPT=6; 028 public static final short ENC_LDAP=7; 029 public static final short ENC_OS=8; 030 public static final short ENC_SQl=9; 031 public static final short ENC_URL=10; 032 public static final short ENC_VB_SCRIPT=11; 033 public static final short ENC_XML=12; 034 public static final short ENC_XML_ATTR=13; 035 public static final short ENC_XPATH=14; 036 037 038 public static String encode(String item, short encFor) throws PageException { 039 040 PrintStream out = System.out; 041 try { 042 System.setOut(new PrintStream(DevNullOutputStream.DEV_NULL_OUTPUT_STREAM)); 043 Encoder encoder = ESAPI.encoder(); 044 switch(encFor){ 045 //case ENC_CSS:return encoder.encodeForBase64(item); 046 case ENC_CSS:return encoder.encodeForCSS(item); 047 case ENC_DN:return encoder.encodeForDN(item); 048 case ENC_HTML:return encoder.encodeForHTML(item); 049 case ENC_HTML_ATTR:return encoder.encodeForHTMLAttribute(item); 050 case ENC_JAVA_SCRIPT:return encoder.encodeForJavaScript(item); 051 case ENC_LDAP:return encoder.encodeForLDAP(item); 052 //case ENC_CSS:return encoder.encodeForOS(arg0, arg1)(item); 053 //case ENC_CSS:return encoder.encodeForSQL(arg0, arg1)CSS(item); 054 case ENC_URL:return encoder.encodeForURL(item); 055 case ENC_VB_SCRIPT:return encoder.encodeForVBScript(item); 056 case ENC_XML:return encoder.encodeForXML(item); 057 case ENC_XML_ATTR:return encoder.encodeForXMLAttribute(item); 058 case ENC_XPATH:return encoder.encodeForXPath(item); 059 } 060 throw new ApplicationException("invalid target encoding defintion"); 061 } 062 catch(EncodingException ee){ 063 throw Caster.toPageException(ee); 064 } 065 finally { 066 System.setOut(out); 067 } 068 } 069 070 public static String call(PageContext pc , String strEncodeFor, String value) throws PageException{ 071 short encFor; 072 strEncodeFor=StringUtil.emptyIfNull(strEncodeFor).trim().toLowerCase(); 073 //if("base64".equals(strEncodeFor)) encFor=ENC_BASE64; 074 if("css".equals(strEncodeFor)) encFor=ENC_CSS; 075 else if("dn".equals(strEncodeFor)) encFor=ENC_DN; 076 else if("html".equals(strEncodeFor)) encFor=ENC_HTML; 077 else if("html_attr".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 078 else if("htmlattr".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 079 else if("html-attr".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 080 else if("html attr".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 081 else if("html_attributes".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 082 else if("htmlattributes".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 083 else if("html-attributes".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 084 else if("html attributes".equals(strEncodeFor)) encFor=ENC_HTML_ATTR; 085 else if("js".equals(strEncodeFor)) encFor=ENC_JAVA_SCRIPT; 086 else if("javascript".equals(strEncodeFor)) encFor=ENC_JAVA_SCRIPT; 087 else if("java_script".equals(strEncodeFor)) encFor=ENC_JAVA_SCRIPT; 088 else if("java script".equals(strEncodeFor)) encFor=ENC_JAVA_SCRIPT; 089 else if("java-script".equals(strEncodeFor)) encFor=ENC_JAVA_SCRIPT; 090 else if("ldap".equals(strEncodeFor)) encFor=ENC_LDAP; 091 //else if("".equals(strEncodeFor)) encFor=ENC_OS; 092 //else if("".equals(strEncodeFor)) encFor=ENC_SQl; 093 else if("url".equals(strEncodeFor)) encFor=ENC_URL; 094 else if("vbs".equals(strEncodeFor)) encFor=ENC_VB_SCRIPT; 095 else if("vbscript".equals(strEncodeFor)) encFor=ENC_VB_SCRIPT; 096 else if("vb-script".equals(strEncodeFor)) encFor=ENC_VB_SCRIPT; 097 else if("vb_script".equals(strEncodeFor)) encFor=ENC_VB_SCRIPT; 098 else if("vb script".equals(strEncodeFor)) encFor=ENC_VB_SCRIPT; 099 else if("xml".equals(strEncodeFor)) encFor=ENC_XML; 100 else if("xmlattr".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 101 else if("xml attr".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 102 else if("xml-attr".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 103 else if("xml_attr".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 104 else if("xmlattributes".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 105 else if("xml attributes".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 106 else if("xml-attributes".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 107 else if("xml_attributes".equals(strEncodeFor)) encFor=ENC_XML_ATTR; 108 else if("xpath".equals(strEncodeFor)) encFor=ENC_XPATH; 109 else 110 throw new FunctionException(pc, "ESAPIEncode", 1, "encodeFor", "value ["+strEncodeFor+"] is invalid, valid values are " + 111 "[css,dn,html,html_attr,javascript,ldap,vbscript,xml,xml_attr,xpath]"); 112 return encode(value, encFor); 113 } 114 115 public static String canonicalize(String input, boolean restrictMultiple, boolean restrictMixed) { 116 if(StringUtil.isEmpty(input)) return null; 117 PrintStream out = System.out; 118 try { 119 System.setOut(new PrintStream(DevNullOutputStream.DEV_NULL_OUTPUT_STREAM)); 120 return ESAPI.encoder().canonicalize(input, restrictMultiple, restrictMixed); 121 } 122 finally { 123 System.setOut(out); 124 } 125 } 126 127 }