You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by vinodtr <vi...@rediffmail.com> on 2008/08/02 04:38:18 UTC

Escaping HTML in Velocity 1.4

Hi all,

I am now using Apache Velocity 1.4 in my project. I would like to
incorporate the escaping HTML feature in my project without bringing in
Velocity 1.5. I could find that there is a tool called EscapeTool to achieve
it. But to do that, i need to change all my velocity templates to enclose
all fields inside $esc.html($form.bean.XXXX). 

I would like to if there is any way to achieve HTML escaping as a whole with
velocity 1.4 itself.

Thanks in advance,
Vinodtr
-- 
View this message in context: http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18785489.html
Sent from the Velocity - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Escaping HTML in Velocity 1.4

Posted by Jonathan Revusky <re...@gmail.com>.
Will Glass-Husain wrote:
> That article is a little out of date-- it's relative to Velocity 1.4 not
> 1.5.  (we should fix it).
> 
> My take is that the primary problem cross-site-scripting is not the static
> text but user entered text.  I prefer the solution of just using a
> ReferenceInsertionHandler and modify the references themselves as that's the
> only place user-entered text can come into a page.
> 
> If you want to escape the HTML in an entire page, I'd suggest writing a
> simple Servlet filter to do this.  Apache Commons has a EscapeUtils class
> (we use it in Velocity 1.5) that makes escaping easy.

????

Well, how would the filter know which parts need to be escaped? Wouldn't 
it then translate even the opening <html> tag into &lt;html&gt; ??

Of course, the file approach could work, I suppose, but not so simply. 
AFAICS, you could stick in markers in the intermeidate output to the 
filter, that marked where reference output started and ended, so then $x 
gets translated in the intermediate stream into:

<reference>[whatever x actually is]</reference>

and then the filter produces the final output by passing through 
everything that is not a reference element and with the reference 
elements, it removes the reference tags and escapes the text inside them.

But that's a fairly baroque soution to this whole issue... And for 
people to have to jury-rig solutions like this to such basic problems is 
  really a bit much too...

The basic problem here is that Velocity is really underpowered, it lacks 
very basic things. A disposition to deal with this escaping issue is 
just one example. I honestly don't know how many man-hours of labor have 
been wasted out there, where people start using Velocity and then 
discover that it's just not really powerful enough and then have to 
switch to something like FreeMarker that has these kinds of features, 
that are really very basic to templating in the web space.

Jonathan Revusky
--
lead developer, FreeMarker project, http://freemarker.org/


> 
> WILL
> 
> On Sun, Aug 3, 2008 at 7:38 PM, vinodtr <vi...@rediffmail.com> wrote:
> 
>> Hi,
>>
>> I happened to see this article on apache velocity.
>>
>> http://wiki.apache.org/velocity/BuildingSecureWebApplications
>>
>> In this wiki, in the section "Encode HTML special characters to avoid
>> cross-scripting vulnerabilities.", Example 3 says about writing a macro for
>> fixing this issue[unimplemented].
>> Can this concept widened to escape html in whole velocity pages in one shot
>> ?
>>
>>
>>
>> wglass wrote:
>>> Hi,
>>>
>>> The ability to set an event handler in the velocity.properties is new in
>> v
>>> 1.5.  Previously you had to use an API call to attach the event handler
>> to
>>> the context before merging the template.  More info inthe developer guide
>>> for v 1.4.
>>>
>>> WILL
>>>
>>> On Sun, Aug 3, 2008 at 5:17 AM, vinodtr <vi...@rediffmail.com> wrote:
>>>
>>>> Hi Chris,
>>>>
>>>> Thanks for your reply. It was of gr8 help. I would also like to know if
>>>> there is any remote chance of extending the EscapeHtmlReference +
>>>> ReferenceInsertionEventHandler mechanism present in Java 1.5 ??
>>>>
>>>> Velocity 1.4 contains the ReferenceInsertionEventHandler class; but no
>>>> EscapeHtmlReference.
>>>> While loading the velocity engine, velocity.properties is consulted and
>>>> all
>>>> referenceInsertionEventHandlers are loaded. If i can create a class
>>>> MyEscapeHtmlReferenceand specify it in velocity.properties like
>>>>
>>>> eventhandler.referenceinsertion.class =
>>>> org.apache.velocity.app.event.implement.MyEscapeHtmlReference
>>>>
>>>> wil this work ? Its not working for me. What could be the problem ?
>>>> Correct
>>>> me if i am wrong !
>>>>
>>>> Thanks in advance
>>>> vinodtr
>>>>
>>>>
>>>> Christopher Schultz-2 wrote:
>>>>> Vinodtr,
>>>>>
>>>>> vinodtr wrote:
>>>>>> I am now using Apache Velocity 1.4 in my project. I would like to
>>>>>> incorporate the escaping HTML feature in my project without bringing
>>>> in
>>>>>> Velocity 1.5. I could find that there is a tool called EscapeTool to
>>>>>> achieve
>>>>>> it. But to do that, i need to change all my velocity templates to
>>>> enclose
>>>>>> all fields inside $esc.html($form.bean.XXXX).
>>>>> This is the best way to achieve HTML escaping. If you'd like, you can
>>>>> write a short macro like this:
>>>>>
>>>>> #macro(htmlEscape $text)#if($text)$esc.html($text)#end#end
>>>>>
>>>>> This will allow you to change the implementation more easily in the
>>>>> future if you wish.
>>>>>
>>>>>> I would like to if there is any way to achieve HTML escaping as a
>>>> whole
>>>>>> with
>>>>>> velocity 1.4 itself.
>>>>> The only way to do this would be to override the implementation of
>>>>> Velocity's "toString" behavior when writing variable values to an
>>>> output
>>>>> stream.
>>>>>
>>>>> You wouldn't want to do this globally, though, because some values
>>>>> should not be HTML escaped.
>>>>>
>>>>> I think your best bet is to escape them individually as you have
>>>>> demonstrated above. Yes, it's a lot of work, but that tends to be the
>>>>> case when you want to go back and add something like this. That's the
>>>>> penalty for not planning ahead.
>>>>>
>>>>> -chris
>>>>>
>>>>>
>>>>>
>>>>>
>>>> --
>>>> View this message in context:
>>>>
>> http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18798132.html
>>>> Sent from the Velocity - Dev mailing list archive at Nabble.com.
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
>>>> For additional commands, e-mail: dev-help@velocity.apache.org
>>>>
>>>>
>>>
>>> --
>>> Forio Business Simulations
>>>
>>> Will Glass-Husain
>>> wglass@forio.com
>>> www.forio.com
>>>
>>>
>> --
>> View this message in context:
>> http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18804815.html
>> Sent from the Velocity - Dev mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: dev-help@velocity.apache.org
>>
>>
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Escaping HTML in Velocity 1.4

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Will,

Will Glass-Husain wrote:
> You can always have the reference handler to selectively escape or omit
> references.  With the EscapeHtmlReference in Velocity 1.5 you can use
> regular expressions to identify references to escape.

Ooh! That's cool. But there still be dragons in blanket-escaping 
everything IMO.

-chris



Re: Escaping HTML in Velocity 1.4

Posted by Will Glass-Husain <wg...@gmail.com>.
Or just write your own event handler to handle the escaping of references.
It's not that complicated.

WILL

On Sun, Aug 3, 2008 at 9:40 PM, vinodtr <vi...@rediffmail.com> wrote:

>
>
> Hi all,
>
> So after all these discussions, I could understand that this issue of cross
> site scripting can be fixed in all pages at one go only by bringing in
> velocity 1.5. If i am sticking with 1.4, i need to use the EscapeTool to
> wrap all user entered fields. Am i right ? There is no one step fix in
> velocity 1.4 and there is no scope of extending the velocity 1.4 API to
> bring in the 1.5 fix in this, am i right ? correct me if i am wrong !
>
> thanks much
>
>
>
> wglass wrote:
> >
> > You can always have the reference handler to selectively escape or omit
> > references.  With the EscapeHtmlReference in Velocity 1.5 you can use
> > regular expressions to identify references to escape.
> >
> > WILL
> >
> > On Sun, Aug 3, 2008 at 8:46 PM, Christopher Schultz <
> > chris@christopherschultz.net> wrote:
> >
> >> Will,
> >>
> >> Will Glass-Husain wrote:
> >>
> >>> My take is that the primary problem cross-site-scripting is not the
> >>> static
> >>> text but user entered text.  I prefer the solution of just using a
> >>> ReferenceInsertionHandler and modify the references themselves as
> that's
> >>> the
> >>> only place user-entered text can come into a page.
> >>>
> >>
> >> A word of caution, here to the OP: many references are not appropriate
> to
> >> escape. Here's an instance we come across all the time where escaping is
> >> not
> >> appropriate: localized messages stored in bundles.
> >>
> >> We often have a message bundle like this:
> >>
> >> some.link=Please make sure that you know what you are doing!
> >>
> >> In order to access this message using, say, struts' message tool, you
> >> would
> >> do this:
> >>
> >> $msg.get('some.link')
> >>
> >> If you change your reference handler to escape everything, then you'll
> >> end
> >> up rendering "sure" as-is, instead of the intended stress on the word
> >> "sure".
> >>
> >> -chris
> >>
> >>
> >
> >
> > --
> > Forio Business Simulations
> >
> > Will Glass-Husain
> > wglass@forio.com
> > www.forio.com
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18805575.html
> Sent from the Velocity - Dev mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Escaping HTML in Velocity 1.4

Posted by Will Glass-Husain <wg...@gmail.com>.
Hi--

Just wanted to correct a mis-statement by the previous poster.

As I'd noted before, you can always have the reference handler to
selectively escape or omit references.  This is true with a custom written
reference event handler (not hard to write), or use the EscapeHtmlReference
in Velocity 1.5 which allows you to use regular expressions to identify
specific references to escape.

I'm offline all tomorrow.  Good luck with your effort.

WILL

Re: Escaping HTML in Velocity 1.4

Posted by Jonathan Revusky <re...@gmail.com>.
vinodtr wrote:
> 
> Hi all,
> 
> So after all these discussions, I could understand that this issue of cross
> site scripting can be fixed in all pages at one go only by bringing in
> velocity 1.5. If i am sticking with 1.4, i need to use the EscapeTool to
> wrap all user entered fields. Am i right ? There is no one step fix in
> velocity 1.4 and there is no scope of extending the velocity 1.4 API to
> bring in the 1.5 fix in this, am i right ? correct me if i am wrong !

I don't think that Velocity 1.5 really fixes this. You can have it 
escape all the variables that the template outputs, but as Christopher 
Schultz points out, you sometimes need to turn off the escaping, and 
AFAICS, Velocity 1.5 doesn't address that (fairly obviously) use case. 
AFAICS, you can only turn on the escaping globally.

Also, there are these niggling details, like the fact that the escaping 
may need to be a tad different  within a <SCRIPT>...<SCRIPT> block that 
contains javascript as opposed to one that contains HTML. So, obviously, 
the ability to escape the output of variables in an entire page is too 
crude an instrument (aside from the fact that there is no disposition 
for turning it off from within the page, at least)...

Now, this kind of thing has been addressed in FreeMarker for ages, like 
5 or even 6 years. See:

http://freemarker.org/docs/ref_directive_escape.html

That said, I actually don't think that this is a complete and 
satisfactory solution to the whole escaping problem. But, of course, it 
is providing you with the means to address this, even if the solution is 
imperfect.

Really, the proposal that you type #htmlesecape($var) every time you 
output a variable strikes me as verging on the absurd. If a flaw this 
basic in FreeMarker were pointed out, I think the discussion would very 
quickly shift to how to add some functionality to the tool that 
addresses the problem. It is very telling IMO that, here, even the 
supposed developers of the tool talk of its feature set as if it was 
something fixed that they can't really doing anything about (!)

Regards,

Jonathan Revusky
--
lead developer, FreeMarker project, http://freemarker.org/

> 
> thanks much
> 
> 
> 
> wglass wrote:
>> You can always have the reference handler to selectively escape or omit
>> references.  With the EscapeHtmlReference in Velocity 1.5 you can use
>> regular expressions to identify references to escape.
>>
>> WILL
>>
>> On Sun, Aug 3, 2008 at 8:46 PM, Christopher Schultz <
>> chris@christopherschultz.net> wrote:
>>
>>> Will,
>>>
>>> Will Glass-Husain wrote:
>>>
>>>> My take is that the primary problem cross-site-scripting is not the
>>>> static
>>>> text but user entered text.  I prefer the solution of just using a
>>>> ReferenceInsertionHandler and modify the references themselves as that's
>>>> the
>>>> only place user-entered text can come into a page.
>>>>
>>> A word of caution, here to the OP: many references are not appropriate to
>>> escape. Here's an instance we come across all the time where escaping is
>>> not
>>> appropriate: localized messages stored in bundles.
>>>
>>> We often have a message bundle like this:
>>>
>>> some.link=Please make sure that you know what you are doing!
>>>
>>> In order to access this message using, say, struts' message tool, you
>>> would
>>> do this:
>>>
>>> $msg.get('some.link')
>>>
>>> If you change your reference handler to escape everything, then you'll
>>> end
>>> up rendering "sure" as-is, instead of the intended stress on the word
>>> "sure".
>>>
>>> -chris
>>>
>>>
>>
>> -- 
>> Forio Business Simulations
>>
>> Will Glass-Husain
>> wglass@forio.com
>> www.forio.com
>>
>>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Escaping HTML in Velocity 1.4

Posted by vinodtr <vi...@rediffmail.com>.

Hi all,

So after all these discussions, I could understand that this issue of cross
site scripting can be fixed in all pages at one go only by bringing in
velocity 1.5. If i am sticking with 1.4, i need to use the EscapeTool to
wrap all user entered fields. Am i right ? There is no one step fix in
velocity 1.4 and there is no scope of extending the velocity 1.4 API to
bring in the 1.5 fix in this, am i right ? correct me if i am wrong !

thanks much



wglass wrote:
> 
> You can always have the reference handler to selectively escape or omit
> references.  With the EscapeHtmlReference in Velocity 1.5 you can use
> regular expressions to identify references to escape.
> 
> WILL
> 
> On Sun, Aug 3, 2008 at 8:46 PM, Christopher Schultz <
> chris@christopherschultz.net> wrote:
> 
>> Will,
>>
>> Will Glass-Husain wrote:
>>
>>> My take is that the primary problem cross-site-scripting is not the
>>> static
>>> text but user entered text.  I prefer the solution of just using a
>>> ReferenceInsertionHandler and modify the references themselves as that's
>>> the
>>> only place user-entered text can come into a page.
>>>
>>
>> A word of caution, here to the OP: many references are not appropriate to
>> escape. Here's an instance we come across all the time where escaping is
>> not
>> appropriate: localized messages stored in bundles.
>>
>> We often have a message bundle like this:
>>
>> some.link=Please make sure that you know what you are doing!
>>
>> In order to access this message using, say, struts' message tool, you
>> would
>> do this:
>>
>> $msg.get('some.link')
>>
>> If you change your reference handler to escape everything, then you'll
>> end
>> up rendering "sure" as-is, instead of the intended stress on the word
>> "sure".
>>
>> -chris
>>
>>
> 
> 
> -- 
> Forio Business Simulations
> 
> Will Glass-Husain
> wglass@forio.com
> www.forio.com
> 
> 

-- 
View this message in context: http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18805575.html
Sent from the Velocity - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Escaping HTML in Velocity 1.4

Posted by Will Glass-Husain <wg...@gmail.com>.
You can always have the reference handler to selectively escape or omit
references.  With the EscapeHtmlReference in Velocity 1.5 you can use
regular expressions to identify references to escape.

WILL

On Sun, Aug 3, 2008 at 8:46 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> Will,
>
> Will Glass-Husain wrote:
>
>> My take is that the primary problem cross-site-scripting is not the static
>> text but user entered text.  I prefer the solution of just using a
>> ReferenceInsertionHandler and modify the references themselves as that's
>> the
>> only place user-entered text can come into a page.
>>
>
> A word of caution, here to the OP: many references are not appropriate to
> escape. Here's an instance we come across all the time where escaping is not
> appropriate: localized messages stored in bundles.
>
> We often have a message bundle like this:
>
> some.link=Please make <i>sure</i> that you know what you are doing!
>
> In order to access this message using, say, struts' message tool, you would
> do this:
>
> $msg.get('some.link')
>
> If you change your reference handler to escape everything, then you'll end
> up rendering "<i>sure</i>" as-is, instead of the intended stress on the word
> "sure".
>
> -chris
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Escaping HTML in Velocity 1.4

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Will,

Will Glass-Husain wrote:
> My take is that the primary problem cross-site-scripting is not the static
> text but user entered text.  I prefer the solution of just using a
> ReferenceInsertionHandler and modify the references themselves as that's the
> only place user-entered text can come into a page.

A word of caution, here to the OP: many references are not appropriate 
to escape. Here's an instance we come across all the time where escaping 
is not appropriate: localized messages stored in bundles.

We often have a message bundle like this:

some.link=Please make <i>sure</i> that you know what you are doing!

In order to access this message using, say, struts' message tool, you 
would do this:

$msg.get('some.link')

If you change your reference handler to escape everything, then you'll 
end up rendering "<i>sure</i>" as-is, instead of the intended stress on 
the word "sure".

-chris


Re: Escaping HTML in Velocity 1.4

Posted by Will Glass-Husain <wg...@gmail.com>.
That article is a little out of date-- it's relative to Velocity 1.4 not
1.5.  (we should fix it).

My take is that the primary problem cross-site-scripting is not the static
text but user entered text.  I prefer the solution of just using a
ReferenceInsertionHandler and modify the references themselves as that's the
only place user-entered text can come into a page.

If you want to escape the HTML in an entire page, I'd suggest writing a
simple Servlet filter to do this.  Apache Commons has a EscapeUtils class
(we use it in Velocity 1.5) that makes escaping easy.

WILL

On Sun, Aug 3, 2008 at 7:38 PM, vinodtr <vi...@rediffmail.com> wrote:

>
> Hi,
>
> I happened to see this article on apache velocity.
>
> http://wiki.apache.org/velocity/BuildingSecureWebApplications
>
> In this wiki, in the section "Encode HTML special characters to avoid
> cross-scripting vulnerabilities.", Example 3 says about writing a macro for
> fixing this issue[unimplemented].
> Can this concept widened to escape html in whole velocity pages in one shot
> ?
>
>
>
> wglass wrote:
> >
> > Hi,
> >
> > The ability to set an event handler in the velocity.properties is new in
> v
> > 1.5.  Previously you had to use an API call to attach the event handler
> to
> > the context before merging the template.  More info inthe developer guide
> > for v 1.4.
> >
> > WILL
> >
> > On Sun, Aug 3, 2008 at 5:17 AM, vinodtr <vi...@rediffmail.com> wrote:
> >
> >>
> >> Hi Chris,
> >>
> >> Thanks for your reply. It was of gr8 help. I would also like to know if
> >> there is any remote chance of extending the EscapeHtmlReference +
> >> ReferenceInsertionEventHandler mechanism present in Java 1.5 ??
> >>
> >> Velocity 1.4 contains the ReferenceInsertionEventHandler class; but no
> >> EscapeHtmlReference.
> >> While loading the velocity engine, velocity.properties is consulted and
> >> all
> >> referenceInsertionEventHandlers are loaded. If i can create a class
> >> MyEscapeHtmlReferenceand specify it in velocity.properties like
> >>
> >> eventhandler.referenceinsertion.class =
> >> org.apache.velocity.app.event.implement.MyEscapeHtmlReference
> >>
> >> wil this work ? Its not working for me. What could be the problem ?
> >> Correct
> >> me if i am wrong !
> >>
> >> Thanks in advance
> >> vinodtr
> >>
> >>
> >> Christopher Schultz-2 wrote:
> >> >
> >> > Vinodtr,
> >> >
> >> > vinodtr wrote:
> >> >> I am now using Apache Velocity 1.4 in my project. I would like to
> >> >> incorporate the escaping HTML feature in my project without bringing
> >> in
> >> >> Velocity 1.5. I could find that there is a tool called EscapeTool to
> >> >> achieve
> >> >> it. But to do that, i need to change all my velocity templates to
> >> enclose
> >> >> all fields inside $esc.html($form.bean.XXXX).
> >> >
> >> > This is the best way to achieve HTML escaping. If you'd like, you can
> >> > write a short macro like this:
> >> >
> >> > #macro(htmlEscape $text)#if($text)$esc.html($text)#end#end
> >> >
> >> > This will allow you to change the implementation more easily in the
> >> > future if you wish.
> >> >
> >> >> I would like to if there is any way to achieve HTML escaping as a
> >> whole
> >> >> with
> >> >> velocity 1.4 itself.
> >> >
> >> > The only way to do this would be to override the implementation of
> >> > Velocity's "toString" behavior when writing variable values to an
> >> output
> >> > stream.
> >> >
> >> > You wouldn't want to do this globally, though, because some values
> >> > should not be HTML escaped.
> >> >
> >> > I think your best bet is to escape them individually as you have
> >> > demonstrated above. Yes, it's a lot of work, but that tends to be the
> >> > case when you want to go back and add something like this. That's the
> >> > penalty for not planning ahead.
> >> >
> >> > -chris
> >> >
> >> >
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18798132.html
> >> Sent from the Velocity - Dev mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> >> For additional commands, e-mail: dev-help@velocity.apache.org
> >>
> >>
> >
> >
> > --
> > Forio Business Simulations
> >
> > Will Glass-Husain
> > wglass@forio.com
> > www.forio.com
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18804815.html
> Sent from the Velocity - Dev mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Escaping HTML in Velocity 1.4

Posted by vinodtr <vi...@rediffmail.com>.
Hi,

I happened to see this article on apache velocity.

http://wiki.apache.org/velocity/BuildingSecureWebApplications

In this wiki, in the section "Encode HTML special characters to avoid
cross-scripting vulnerabilities.", Example 3 says about writing a macro for
fixing this issue[unimplemented].
Can this concept widened to escape html in whole velocity pages in one shot
?



wglass wrote:
> 
> Hi,
> 
> The ability to set an event handler in the velocity.properties is new in v
> 1.5.  Previously you had to use an API call to attach the event handler to
> the context before merging the template.  More info inthe developer guide
> for v 1.4.
> 
> WILL
> 
> On Sun, Aug 3, 2008 at 5:17 AM, vinodtr <vi...@rediffmail.com> wrote:
> 
>>
>> Hi Chris,
>>
>> Thanks for your reply. It was of gr8 help. I would also like to know if
>> there is any remote chance of extending the EscapeHtmlReference +
>> ReferenceInsertionEventHandler mechanism present in Java 1.5 ??
>>
>> Velocity 1.4 contains the ReferenceInsertionEventHandler class; but no
>> EscapeHtmlReference.
>> While loading the velocity engine, velocity.properties is consulted and
>> all
>> referenceInsertionEventHandlers are loaded. If i can create a class
>> MyEscapeHtmlReferenceand specify it in velocity.properties like
>>
>> eventhandler.referenceinsertion.class =
>> org.apache.velocity.app.event.implement.MyEscapeHtmlReference
>>
>> wil this work ? Its not working for me. What could be the problem ?
>> Correct
>> me if i am wrong !
>>
>> Thanks in advance
>> vinodtr
>>
>>
>> Christopher Schultz-2 wrote:
>> >
>> > Vinodtr,
>> >
>> > vinodtr wrote:
>> >> I am now using Apache Velocity 1.4 in my project. I would like to
>> >> incorporate the escaping HTML feature in my project without bringing
>> in
>> >> Velocity 1.5. I could find that there is a tool called EscapeTool to
>> >> achieve
>> >> it. But to do that, i need to change all my velocity templates to
>> enclose
>> >> all fields inside $esc.html($form.bean.XXXX).
>> >
>> > This is the best way to achieve HTML escaping. If you'd like, you can
>> > write a short macro like this:
>> >
>> > #macro(htmlEscape $text)#if($text)$esc.html($text)#end#end
>> >
>> > This will allow you to change the implementation more easily in the
>> > future if you wish.
>> >
>> >> I would like to if there is any way to achieve HTML escaping as a
>> whole
>> >> with
>> >> velocity 1.4 itself.
>> >
>> > The only way to do this would be to override the implementation of
>> > Velocity's "toString" behavior when writing variable values to an
>> output
>> > stream.
>> >
>> > You wouldn't want to do this globally, though, because some values
>> > should not be HTML escaped.
>> >
>> > I think your best bet is to escape them individually as you have
>> > demonstrated above. Yes, it's a lot of work, but that tends to be the
>> > case when you want to go back and add something like this. That's the
>> > penalty for not planning ahead.
>> >
>> > -chris
>> >
>> >
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18798132.html
>> Sent from the Velocity - Dev mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: dev-help@velocity.apache.org
>>
>>
> 
> 
> -- 
> Forio Business Simulations
> 
> Will Glass-Husain
> wglass@forio.com
> www.forio.com
> 
> 

-- 
View this message in context: http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18804815.html
Sent from the Velocity - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Escaping HTML in Velocity 1.4

Posted by Will Glass-Husain <wg...@gmail.com>.
Hi,

The ability to set an event handler in the velocity.properties is new in v
1.5.  Previously you had to use an API call to attach the event handler to
the context before merging the template.  More info inthe developer guide
for v 1.4.

WILL

On Sun, Aug 3, 2008 at 5:17 AM, vinodtr <vi...@rediffmail.com> wrote:

>
> Hi Chris,
>
> Thanks for your reply. It was of gr8 help. I would also like to know if
> there is any remote chance of extending the EscapeHtmlReference +
> ReferenceInsertionEventHandler mechanism present in Java 1.5 ??
>
> Velocity 1.4 contains the ReferenceInsertionEventHandler class; but no
> EscapeHtmlReference.
> While loading the velocity engine, velocity.properties is consulted and all
> referenceInsertionEventHandlers are loaded. If i can create a class
> MyEscapeHtmlReferenceand specify it in velocity.properties like
>
> eventhandler.referenceinsertion.class =
> org.apache.velocity.app.event.implement.MyEscapeHtmlReference
>
> wil this work ? Its not working for me. What could be the problem ? Correct
> me if i am wrong !
>
> Thanks in advance
> vinodtr
>
>
> Christopher Schultz-2 wrote:
> >
> > Vinodtr,
> >
> > vinodtr wrote:
> >> I am now using Apache Velocity 1.4 in my project. I would like to
> >> incorporate the escaping HTML feature in my project without bringing in
> >> Velocity 1.5. I could find that there is a tool called EscapeTool to
> >> achieve
> >> it. But to do that, i need to change all my velocity templates to
> enclose
> >> all fields inside $esc.html($form.bean.XXXX).
> >
> > This is the best way to achieve HTML escaping. If you'd like, you can
> > write a short macro like this:
> >
> > #macro(htmlEscape $text)#if($text)$esc.html($text)#end#end
> >
> > This will allow you to change the implementation more easily in the
> > future if you wish.
> >
> >> I would like to if there is any way to achieve HTML escaping as a whole
> >> with
> >> velocity 1.4 itself.
> >
> > The only way to do this would be to override the implementation of
> > Velocity's "toString" behavior when writing variable values to an output
> > stream.
> >
> > You wouldn't want to do this globally, though, because some values
> > should not be HTML escaped.
> >
> > I think your best bet is to escape them individually as you have
> > demonstrated above. Yes, it's a lot of work, but that tends to be the
> > case when you want to go back and add something like this. That's the
> > penalty for not planning ahead.
> >
> > -chris
> >
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18798132.html
> Sent from the Velocity - Dev mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Escaping HTML in Velocity 1.4

Posted by vinodtr <vi...@rediffmail.com>.
Hi Chris,

Thanks for your reply. It was of gr8 help. I would also like to know if
there is any remote chance of extending the EscapeHtmlReference +
ReferenceInsertionEventHandler mechanism present in Java 1.5 ??

Velocity 1.4 contains the ReferenceInsertionEventHandler class; but no
EscapeHtmlReference.
While loading the velocity engine, velocity.properties is consulted and all
referenceInsertionEventHandlers are loaded. If i can create a class
MyEscapeHtmlReferenceand specify it in velocity.properties like 

eventhandler.referenceinsertion.class =
org.apache.velocity.app.event.implement.MyEscapeHtmlReference

wil this work ? Its not working for me. What could be the problem ? Correct
me if i am wrong !

Thanks in advance
vinodtr


Christopher Schultz-2 wrote:
> 
> Vinodtr,
> 
> vinodtr wrote:
>> I am now using Apache Velocity 1.4 in my project. I would like to
>> incorporate the escaping HTML feature in my project without bringing in
>> Velocity 1.5. I could find that there is a tool called EscapeTool to
>> achieve
>> it. But to do that, i need to change all my velocity templates to enclose
>> all fields inside $esc.html($form.bean.XXXX). 
> 
> This is the best way to achieve HTML escaping. If you'd like, you can 
> write a short macro like this:
> 
> #macro(htmlEscape $text)#if($text)$esc.html($text)#end#end
> 
> This will allow you to change the implementation more easily in the 
> future if you wish.
> 
>> I would like to if there is any way to achieve HTML escaping as a whole
>> with
>> velocity 1.4 itself.
> 
> The only way to do this would be to override the implementation of 
> Velocity's "toString" behavior when writing variable values to an output 
> stream.
> 
> You wouldn't want to do this globally, though, because some values 
> should not be HTML escaped.
> 
> I think your best bet is to escape them individually as you have 
> demonstrated above. Yes, it's a lot of work, but that tends to be the 
> case when you want to go back and add something like this. That's the 
> penalty for not planning ahead.
> 
> -chris
> 
> 
>  
> 

-- 
View this message in context: http://www.nabble.com/Escaping-HTML-in-Velocity-1.4-tp18785489p18798132.html
Sent from the Velocity - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Escaping HTML in Velocity 1.4

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Jonathan,

Jonathan Revusky wrote:
> Christopher Schultz wrote:
>> #macro(htmlEscape $text)#if($text)$esc.html($text)#end#end
> 
> So, IOW, every time, that you want to write $foo you write instead:
> 
> #hmlescape($foo)
> 
> Doesn't that get a bit ugly and repetitious? If you want to apply the 
> same routine to every output reference on the page, shouldn't there be a 
> way of doing that?

There is a way of doing that, but I'm arguing that blanket-escaping of 
everything is incorrect.

>> I think your best bet is to escape them individually as you have 
>> demonstrated above. Yes, it's a lot of work, but that tends to be the 
>> case when you want to go back and add something like this. That's the 
>> penalty for not planning ahead.
> 
> I can't see how this is a penalty for not planning ahead. It is simply a 
> characteristic of the problem space that you will very likely want to 
> escape the output of variables this way.

True, but his application was built without considering that he might 
want to escape a lot of data on the way out.

-chris


Re: Escaping HTML in Velocity 1.4

Posted by Jonathan Revusky <re...@gmail.com>.
Christopher Schultz wrote:
> Vinodtr,
> 
> vinodtr wrote:
>> I am now using Apache Velocity 1.4 in my project. I would like to
>> incorporate the escaping HTML feature in my project without bringing in
>> Velocity 1.5. I could find that there is a tool called EscapeTool to 
>> achieve
>> it. But to do that, i need to change all my velocity templates to enclose
>> all fields inside $esc.html($form.bean.XXXX). 
> 
> This is the best way to achieve HTML escaping. If you'd like, you can 
> write a short macro like this:
> 
> #macro(htmlEscape $text)#if($text)$esc.html($text)#end#end

So, IOW, every time, that you want to write $foo you write instead:

#hmlescape($foo)

Doesn't that get a bit ugly and repetitious? If you want to apply the 
same routine to every output reference on the page, shouldn't there be a 
way of doing that?

> 
> This will allow you to change the implementation more easily in the 
> future if you wish.
> 
>> I would like to if there is any way to achieve HTML escaping as a 
>> whole with
>> velocity 1.4 itself.
> 
> The only way to do this would be to override the implementation of 
> Velocity's "toString" behavior when writing variable values to an output 
> stream.
> 
> You wouldn't want to do this globally, though, because some values 
> should not be HTML escaped.
> 
> I think your best bet is to escape them individually as you have 
> demonstrated above. Yes, it's a lot of work, but that tends to be the 
> case when you want to go back and add something like this. That's the 
> penalty for not planning ahead.

I can't see how this is a penalty for not planning ahead. It is simply a 
characteristic of the problem space that you will very likely want to 
escape the output of variables this way.

Actually, it seems to me that it's the penalty for using an underpowered 
tool. Something like this is so basic for a templating tool that outputs 
to the web that it is staggering that in its 8th year of existence, 
Apache Velocity completely fails to address this.

In FreeMarker, for example, you just put your page (or the block to 
which you want this to apply) inside an escape block as follows:

<#escape x as (x)?html>


</#escape>


Of course, you sometimes need to turn it off, so for those cases, you have:

<#unescape>
   ...
</#unescape>

within the escape block.

For more complete information, see:

http://freemarker.org/docs/ref_directive_escape.html

Jonathan Revusky
--
lead developer, FreeMarker project, http://freemarker.org/

> 
> -chris
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Escaping HTML in Velocity 1.4

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Vinodtr,

vinodtr wrote:
> I am now using Apache Velocity 1.4 in my project. I would like to
> incorporate the escaping HTML feature in my project without bringing in
> Velocity 1.5. I could find that there is a tool called EscapeTool to achieve
> it. But to do that, i need to change all my velocity templates to enclose
> all fields inside $esc.html($form.bean.XXXX). 

This is the best way to achieve HTML escaping. If you'd like, you can 
write a short macro like this:

#macro(htmlEscape $text)#if($text)$esc.html($text)#end#end

This will allow you to change the implementation more easily in the 
future if you wish.

> I would like to if there is any way to achieve HTML escaping as a whole with
> velocity 1.4 itself.

The only way to do this would be to override the implementation of 
Velocity's "toString" behavior when writing variable values to an output 
stream.

You wouldn't want to do this globally, though, because some values 
should not be HTML escaped.

I think your best bet is to escape them individually as you have 
demonstrated above. Yes, it's a lot of work, but that tends to be the 
case when you want to go back and add something like this. That's the 
penalty for not planning ahead.

-chris