Restricting "com.example.SomeClass"?new(): opinions?
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.devel,gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Something I have added to 2.3.17 is this, and I thought some may have
opinions regarding how it's done. After all, when it will be out on
the 16th, it will have to remain like this forever.
So, when you call ?new in a template, then to resolve the class name
FreeMarker now uses the "new_builtin_class_resolver" setting, which is
of type freemarker.core.TemplateClassResolver (also new), which look
like this:
/**
* Used by built-ins and other template language features that get a class
* based on a string. This can be handy both for implementing security
* restrictions and for working around local class-loader issues.
...
*/
public interface TemplateClassResolver {
...
/**
* Gets a {@link Class} based on the class name.
*
* @param className the full-qualified class name
* @param env the environment in which the template executes
* @param template the template where the operation that require the
* class resolution resides in. This is <code>null</code> if the
* call doesn't come from a template.
*
* @throws TemplateException if the class can't be found or shouldn't be
* accessed from a template for security reasons.
*/
Class resolve(String className, Environment env, Template template) throws TemplateException;
}
So you can add you own implementation (e.g. with
Configuration.setNewBuiltinClassResolver) if you want to keep ?new
under control. This is mostly a security thing. BTW, this interface
isn't tied to ?new, although right now that's the only place where
it's used.
Some out-of-the-box implementations of this interface are:
- TemplateClassResolver.UNRESTRICTED_RESOLVER: The default in 2.3.x.
- TemplateClassResolver.ALLOWS_NOTHING_RESOLVER: As the name implies...
- OptInTemplateClassResolver: Here you can specify what classes are
allowed to be instantiated (by name), hence "opt-it". You can also
specify which templates are trusted (by name, like "foo/bar.ftl", or
by directory, like "lib/*"); the restrictions will not apply in
those.
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today. Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd