Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape double quotes in JSTL function / EL?

Tags:

I need to change " to \" with JSTL replace function to use the string in input tag like:

<input type="hidden" name="text" size="40" value="${text}"> 

If the ${text} has the ", the HTML will be broken.

So I tried

<input type="hidden" name="text" size="40" value="${fn:replace(text, "\"", "\\\""}"> 

and

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '\"'}"> 

but didn't worked. The page makes errors like

org.apache.el.parser.ParseException: Encountered " "}" "} "" at line 1, column 32. Was expecting one of: "." ... ")" ... "[" ... "," ... ">" ... "gt" ... "<" ... "lt" ... ">=" ... "ge" ... "<=" ... "le" ... "==" ... "eq" ... "!=" ... "ne" ... "&&" ... "and" ... "||" ... "or" ... "*" ... "+" ... "-" ... "/" ... "div" ... "%" ... "mod" ...

How can I do this?

Update

I missed a close paren of replace function. The right one was this one with a close paren:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '\"')}"> 

Update2

I found out that when posting texts, using \ is not a good idea because of this reason why can't use \" in HTML input tag?. The code should be like this:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '&quot;')}"> 
like image 793
Sanghyun Lee Avatar asked Aug 18 '11 01:08

Sanghyun Lee


People also ask

How do you escape double quotes in C#?

C# Language Verbatim Strings Escaping Double Quotes Double Quotes inside verbatim strings can be escaped by using 2 sequential double quotes "" to represent one double quote " in the resulting string.

How do you escape quotes in VB net?

To display a double quote, you need to escape the inner double quotes. Normally in most of the languages, the escape character is backslash ( \ ). In VBScript, the escape character is a double quote ( ” ) itself. The first and last quot marked in red are enclosing the complete string as you would normally do.


2 Answers

It doesn't work because the \ is an escape character in Java string. To represent it literally, you need to escape it with another \ again. Also the " is a special character in EL, you also need to escape it to represent it literally. So, the proper syntax would have been:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '\"', '\\\"')}"> 

But, you should actually be using fn:escapeXml() to prevent XSS. It not only escapes quotes, but also other characters.

<input type="hidden" name="text" size="40" value="${fn:escapeXml(text)}"> 

###See also:

  • XSS prevention in JSP/Servlet web application
like image 62
BalusC Avatar answered Oct 16 '22 23:10

BalusC


You are doing it wrong (with fn:replace).

The correct way is:

<input type="hidden" name="text" size="40" value="<c:out value='${text}'/>"> (actually tested code - works 100%) 

Edit: Upon more thinking:

  • the way by using fn:escapeXml (as written by BalusC) works too and looks nicer (no nested tags)
  • using fn:replace to mimick fn:escapeXml is asking for trouble. You will forget to include some character that should be escaped. Just use the existing, tried and tested fn:escapeXml (or c:out)
like image 44
David Balažic Avatar answered Oct 17 '22 00:10

David Balažic