| name | bx-esapi |
| description | Use this skill for OWASP ESAPI encoding, decoding, and HTML sanitization in BoxLang: encodeFor(), encodeForHTML(), encodeForJavaScript(), encodeForSQL(), encodeForURL(), decodeFrom(), htmlSanitize() with AntiSamy, and context-aware output encoding to prevent XSS/injection. |
bx-esapi: OWASP ESAPI Security Module
Installation
install-bx-module bx-esapi
box install bx-esapi
Encoding BIFs
| BIF | Description |
|---|
encodeFor( context, value ) | Encode for a named context (see list below) |
encodeForHTML( value, [canonicalize] ) | Encode for HTML body content |
encodeForHTMLAttribute( value, [canonicalize] ) | Encode for HTML attribute values |
encodeForJavaScript( value, [canonicalize] ) | Encode for inline JavaScript |
encodeForCSS( value, [canonicalize] ) | Encode for CSS values |
encodeForURL( value, [canonicalize] ) | URL-encode a value |
encodeForSQL( value, dialect, [canonicalize] ) | Encode for SQL contexts |
encodeForXML( value, [canonicalize] ) | Encode for XML content |
encodeForXMLAttribute( value, [canonicalize] ) | Encode for XML attribute values |
encodeForXPath( value, [canonicalize] ) | Encode for XPath queries |
encodeForLDAP( value, [canonicalize] ) | Encode for LDAP queries |
encodeForDN( value, [canonicalize] ) | Encode for LDAP Distinguished Names |
Available encodeFor() Contexts
CSS, DN, HTML, HTMLAttribute, JavaScript, LDAP, SQL, URL, XML, XMLAttribute, XPath
Output Encoding (XSS Prevention)
<bx:output>
<h2>#encodeForHTML( user.displayName )#</h2>
<p>#encodeForHTML( user.bio )#</p>
</bx:output>
<bx:output>
<a href="#encodeForHTMLAttribute( user.profileUrl )#">Profile</a>
<img src="/img/avatar.png" alt="#encodeForHTMLAttribute( user.displayName )#" />
</bx:output>
<bx:output>
<script>
var username = "#encodeForJavaScript( user.username )#";
var userId = #encodeForJavaScript( user.id )#;
</script>
</bx:output>
<bx:output>
<a href="/search?q=#encodeForURL( form.searchTerm )#">Search</a>
</bx:output>
Generic encodeFor()
safeHtml = encodeFor( "HTML", user.comment )
safeJs = encodeFor( "JavaScript", user.name )
safeUrl = encodeFor( "URL", queryParam )
safeAttr = encodeFor( "HTMLAttribute", user.title )
Decoding BIFs
decoded = decodeFromHTML( "<script>" )
decoded = decodeFromURL( "hello%20world" )
decoded = decodeFromBase64( encodedString )
decoded = decodeFrom( "HTML", "<p>content</p>" )
Canonicalization
Canonicalization normalizes encoded input before encoding. Useful to detect double-encoded attacks:
safe = encodeForHTML( userInput, true )
HTML Sanitization with AntiSamy
clean = htmlSanitize( userHtmlInput )
clean = htmlSanitize( userHtmlInput, "antisamy-slashdot" )
Common Encoding Patterns
function renderUserProfile( user ) {
return "
<div class='profile'>
<h1>#encodeForHTML( user.name )#</h1>
<p class='bio'>#encodeForHTML( user.bio )#</p>
<a href='#encodeForHTMLAttribute( user.website )#'>
#encodeForHTML( user.websiteLabel )#
</a>
</div>
"
}
function buildSearchUrl( term, category ) {
return "/search?q=#encodeForURL( term )#&category=#encodeForURL( category )#"
}
Common Pitfalls
- ❌ Never use
encodeForHTML() inside JavaScript — use encodeForJavaScript() instead
- ❌ Never use
encodeForHTML() inside HTML attribute values — use encodeForHTMLAttribute()
- ✅ Always encode user-supplied data at output time in the context where it's used
- ✅ Use
htmlSanitize() (AntiSamy) when you want to allow SOME HTML but not arbitrary markup
- ❌
encodeForSQL() should not replace parameterized queries — use queryExecute() with proper params instead
- ✅ Use
canonicalize=true when processing input that may have been double-encoded