AST-based Velocity Macro
- XWiki
- Feature
- Idea
- No
Description
The motivation for this proposal is basically the same as Secure By Default Scripting: It is too hard to write scripts that properly escape content. The goal of this proposal is to introduce a new script macro that should gradually replace most uses of the currently used Velocity macro that makes it much easier to write code that doesn't contain security vulnerabilities through unescaped content. The main idea is that the scripts are executed based on an abstract syntax tree (AST) of the parsed content instead working on plain text content, therefore the name.
Goals
- Familiar Velocity scripting language with existing XWiki API, type conversions and security features
- Template engine inspired by Vue.js for a familiar experience for front-end developers (as XWiki is increasingly adopting Vue.js as front-end library)
- Mostly a drop-in replacement, most existing Velocity code should be adjustable without requiring a major rewrite
- Good integration with XWiki's rendering system, making it easy to use rendering macros, internal links or images in the content
- The output of the macro shouldn't be an opaque raw HTML block but an XDOM that only contains raw HTML blocks for the parts that cannot be represented in regular XDOM blocks.
Non-Goals
- Use a better/more modern scripting language than Velocity. The aim is to keep things simple and familiar regarding the scripting.
- Support XWiki syntax as input language. In contrast to the proposal at Secure By Default Scripting, the goal here is to have a single input language that is HTML/XML-based to avoid having to deal with the complexities of the existing XWiki syntax parser and parameters that have special meaning and special places in XWiki syntax like the reference parameter on images or links that makes it impossible to prefix the parameter name.
Syntax
HTML that is valid XML to simplify parsing. Prefix elements and attributes with v- for Velocity and xw- for XWiki elements.
| Concept | Proposed Syntax Example | Explanation |
| Conditionals | <div v-if="$doc.isNew()">...</div> | Evaluates the Velocity expression. If false, the node is removed from the DOM. |
| Loops | <li v-for="$item in $doc.getAttachments()">...</li> | Repeats the DOM node. Notice the standard Velocity variable syntax ($item). |
| Text Output | <span v-text="$doc.title"></span> | Sets the textContent of the node. Automatically escapes HTML, solving the XSS issue. |
| Raw HTML | <div v-html="$doc.getRenderedContent()"></div> | Explicit opt-in for unescaped HTML injection. |
| Attributes | <a v-bind:href="$doc.getURL()">Link</a> or <a :href="$doc.getURL()">Link</a> | Binds the result of the Velocity evaluation to the HTML attribute. |
| Variable Setting | <v-set var="$myVar" value="$doc.title" /> | A custom tag used purely for script logic where no visual HTML wrapper is needed. |
| XWiki Macros | <xw-macro-warning>Watch out!</xw-macro-warning> | Custom elements to represent an XWiki macro. Attributes are passed as macro parameters. |
| XWiki Images | <xw-img src="attach:logo.png" /> | Custom element for XWiki-specific image references, translating to the correct XDOM block. |
Fragments
The syntax should support re-usable fragments similar to Velocity macros. Syntax idea:
<v-fragment name="userCard">
<div class="user-card">
<xw-img v-bind:src="$user.avatar" />
<span v-text="$user.name"></span>
</div>
</v-fragment>
<ul>
<li v-for="$user in $users">
<v-render fragment="userCard" v-bind:user="$user" />
</li>
</ul>Including a page or template should make fragments of that page available in the current page.
Boolean Attributes
We should support boolean attributes similar to Vue.js as they need to be removed when the value should be false. Idea:
<button :disabled="$isButtonDisabled">Click Me</button>The best option for this would be to have a well-maintained library of HTML attributes that are actually boolean, then we could convert whatever value we get to boolean and give more flexibility to the developer. jsoup's Attribute.isBooleanAttribute(String key) seems like a perfect fit for that.
Script Tags
For longer script blocks that either define more complex variables or that contain application logic (not desirable, but needed for painless legacy migrations) we should support a script tag with some attribute that clearly marks the script tag as being evaluated by Velocity. We should either support one global script tag (similar to Vue.js) or just script tags wherever the user wants, evaluated as part of the regular evaluation flow.
Handling of Velocity Flow Control Constructs
There is a fundamental question how we evaluate attributes: Do we try evaluating them as true expressions by using a construct like #set($tmp_value = ATTRIBUTE_VALUE) which would only allow expressions that are valid in that context (we could check the AST to verify that we got something valid) and would allow us to retrieve actual objects or should we evaluate attributes as a regular template. If it's the latter, there is nothing really that prevents the user from calling Velocity macros and control constructs like #if, #foreach or macros inside attributes. And in fact, this could even be desirable, for example <template v-html="#displayUser($user)" /> could be used to re-use the existing Velocity macro for displaying a user, using the template tag similar to Vue.js such that it won't be part of resulting DOM (or a dedicated tag, maybe we want to allow producing the template tag in actual output). We could also decide to support/try both, i.e., if an attribute content is a valid expression, we use that, and if not, we evaluate it as a regular template.
Open Questions
Should we only evaluate Velocity in attributes with a prefix as suggested above, or should we evaluate Velocity in all attributes (if they contain $ or #) and also in element contents? If we do that, users might be tempted to write
<ul>
#foreach ($user in $users)
<li>$user.name</li>
#end
</li>which won't work as the start and the end of the loop will be part of different Velocity fragments.
However,
<p>
#foreach ($user in $users)
${user.name},
#end
</p> would work and might feel more natural than the corresponding equivalent with the new tags. However, the potential for confusion seems real, so the idea might be bad even though it could improve the usability of the macro in some ways.
Output
The idea is to transform the output into an XDOM that is then further processed by the standard macro transformation. The idea would be to:
- Map XWiki specific tags to the respective XDOM blocks
- Map plain text content and HTML tags that have a direct equivalent to the respective XDOM blocks
- Map HTML elements that have no direct equivalent in XDOM into raw HTML blocks for start/end tag
There are some challenges like what to do with macros that have nested tags as macro blocks cannot have children, but that seems solvable one way or another. The most realistic option seems to be just not support any child elements in macros.
Technical Considerations
Performance
The DOM and the Velocity fragments should be parsed during a preparation phase to speed up the execution of the macro. We need to carefully examine if there are any blockers that would make this much slower than executing one big Velocity fragment.
Error Handling
We need to be careful to implement some form of location tracking to provide some way for users to track errors back to their source.
Alternatives to Velocity
Java Expression Language (JEXL)
Apache Commons JEXL looks like a very fitting project:
- It provides an expression language that is made for evaluating short snippets as we need it here, so we don't need workarounds to access the result of an expression
- The language is closer to JavaScript syntax so a lot more familiar for many developers
- The language is more powerful, e.g., allowing to construct new objects or creating lambdas and seems to eliminate major pain points of Velocity
- Sandboxing is a core concept of JEXL and there is a fine-grained permission concept. We can implement our own strategy which classes/methods should be allowed and also which constructors are available/not available. From what I understand, there is no need to have a fixed list, we can implement our own validation routines. It seems possible to have a permission setup similar to Velocity, but we could also have stricter rules.
- While different from Velocity, there seem to be a lot of similarities, for example, there seems to be similar support for resolving foo.bar to foo.getBar() or foo.get('bar').
- It might even be possible to use our existing argument converters (see GPT 5.5 answer on the question if we can use the same approach as we have in Velocity) - if we want to keep them and not enforce stricter types, as with JEXL it is easy to, e.g., construct a document reference.
- Interface for the script context seems simple enough that we could implement a thin wrapper around the existing Velocity context, allowing full re-use of all variables without any performance penalty.
- There is also support for whole scripts, not just expressions, so we could also implement the idea of the script tag presented above.
Disadvantages:
- Obviously no re-use of Velocity macros.
- Possibly not so direct/seamless conversion of existing code.
- Difficult to replace the use of Velocity in titles or in properties of UI extensions where we really use the fact that Velocity is a template engine and not just a script language - use $var and you have a script, use regular text and you get regular text. Further, we have textarea class types that explicitly mention Velocity. If we can't entirely replace Velocity, it feels a bad idea from a maintenance and security point of view to introduce another script engine.
We could also try supporting both Velocity and JEXL with different prefixes, at least for use cases like using Velocity macros to insert HTML snippets.
Michael Hamann