Raw output for the Velocity (and other script) macros

Hi everyone,

I would like to propose adding a new parameter to all script macros to treat their output as raw HTML (or any raw syntax) instead of as wiki syntax or plain text. The motivation for this is two-fold:

  • security: treating the result as HTML directly avoids XWiki syntax injection and thus reduces the impact of missing escaping from remote code execution to XSS.
  • performance: not going through wiki parsing to identify the single HTML macro in the result of the Velocity macro could help to fix issues like XWIKI-16647 where apparently one of the main issues is a huge HTML macro going through the XWiki syntax parser. It won’t be enough (the HTML macro also enables cleaning, I haven’t yet checked if that’s actually needed), but it can definitely help.

The main questions for me are:

  • what syntax: should we support any syntax as raw output syntax, or just HTML? It’s easy to support all, but it’s exactly the flexibility that could also hurt us later as we can’t take it back. The proposal below is to allow any syntax, but we could also decide to restrict it to SyntaxType.HTML_FAMILY_TYPES now with the option to relax it later.
  • name: what name should the new parameter have, and how does it interact with the existing parameters?
  • cleaning: should we support HTML cleaning, or really just raw HTML?

For context, the parameters we currently have and their behavior (disclaimer: the remaining part of the proposal was written by Claude):

  • ScriptMacroParameters (all script macros): output (boolean, default true), wiki (boolean, default true; false parses the output as plain text), jars.
  • VelocityMacroParameters: filter (indent/html/none), a whitespace filter applied to the script before and after evaluation. Its html value is unrelated to the output format.
  • The output is converted to blocks in AbstractScriptMacro.parseScriptResult(): either the current wiki syntax parser or the plain text parser, then convertToInline in inline mode.

Proposal

Add a rawSyntax parameter to ScriptMacroParameters, of type org.xwiki.rendering.syntax.Syntax, default null:

{{velocity rawSyntax="html/5.0"}}
<div class="foo">$escapetool.xml($value)</div>
{{/velocity}}

When it is set, the script output becomes a single RawBlock in that syntax, exactly as the {{raw}} macro produces it:

  • RawMacro.execute() creates new RawBlock(content, syntax) and runs every RawBlockFilter (sorted by priority, from RawBlockFilterUtils) with default RawBlockFilterParameters (clean=false, restricted=false). The script macro does the same, reusing the internal RawBlockFilterUtils.
  • The filters are run because RawBlockFilter is a public extension point; an extension may rely on seeing all raw content. The included HTMLRawBlockFilter never changes anything here: it only acts when cleaning is requested or in restricted mode, and script macros never run in restricted mode or without script right.
  • No cleaning, and no clean parameter: the behaviour is the raw macro’s, not the HTML macro’s. Content that needs cleaning keeps using {{html}}. A clean parameter can be added later without breaking anything.
  • The syntax is used as given, like in the raw macro (no mapping of HTML syntaxes to the target syntax as the HTML macro does).
  • Inline mode: the raw block is inserted as is; convertToInline doesn’t apply (there are no paragraphs) and there’s no inline-content check, as in the raw macro.
  • output=false still suppresses the output entirely.
  • wiki is ignored when rawSyntax is set (documented). As wiki is a primitive boolean we can’t detect an explicit wiki="true", so a conflict can’t be reported as an error.
  • Any raw syntax is accepted, not only HTML: it’s the same code path, and the raw macro supports it too (see the first question above for another option). The documentation focuses on HTML.

Why the name and the location

  • wiki can’t become a three-way value: it’s a primitive boolean with a public isWiki().
  • syntax/outputSyntax would be ambiguous: the content macro’s syntax means “parse with this syntax”, and html/4.01 has a parser, so outputSyntax="html/5.0" could be read as “parse the HTML into an XDOM”. Keeping “raw” in the name avoids that and leaves room for a parse-with-this-syntax parameter later.
  • The type Syntax matches the raw macro’s syntax parameter.
  • In ScriptMacroParameters/AbstractScriptMacro because that’s where the output is converted, so Groovy and the other script macros get the feature for free.

Backward compatibility

Without explicitly setting the new parameter, nothing changes, the API change is only a new getter/setter on ScriptMacroParameters.

Adoption

Claude found about 75 Velocity macros in xwiki-platform that directly start with an HTML macro, 53 of them use clean="false", 13 a plain {{html}} and 9 {{html wiki="true"}}. Each of the plain HTML macros needs to be checked in detail if the cleaning is necessary, and all of course also if the HTML macro is really the whole content. It’s not a solution for all cases (in particular not the cases where wiki="true" is needed), but there are potentially many that can make use of this new feature. In the long run, the idea of an AST-based Velocity macro would hopefully replace all of them, but I think it’s a good, cheap first step.

Do you agree with this idea, or would you prefer any other option, in particular also regarding the questions I formulated above?

I think I would rather go with the HTML_FAMILY_TYPES at first. So +1 for that but also a +0 for any syntax.
Otherwise, +1 for the proposal

So you are proposing a shortcut for:

{{velocity}}
{{raw syntax="html/5.0"}}
<div class="foo">$escapetool.xml($value)</div>
{{/raw}}
{{/velocity}}

This works for Velocity but not for other script macros like Groovy that use a more strict script language. Groovy could still print the raw macro call:

{{groovy}}
println '{{raw syntax="html/5.0"}}'
println '<div class="foo">$escapetool.xml($value)</div>'
println '{{/raw}}'
{{/groovy}}

In order to make it work with any script macro you want to “embed” the raw macro inside the script macro.

+0. For Velocity at least, I wouldn’t mind using the raw macro explicitly, if it were installed by default. It would be more clear / easier to understand than the rawSyntax parameter.

Thanks,
Marius

+1 for the proposal.

I would also start with SyntaxType.HTML_FAMILY_TYPES and relax it later if needed, since it’s easier to start with more restrictions and loosen them than to start with fewer and add more later

The point here is not that you cannot achieve this with the raw macro, the point is not to send the whole result of the Velocity macro through the XWiki syntax parser again, which a) potentially consumes a lot of resources (compared to just creating a raw block in the XDOM) and b) creates the security risk of XWiki syntax injection.

+1 for the general proposal

I think I would allow anything, but I’m not against SyntaxType.HTML_FAMILY_TYPES if people prefer that.

Your proposal (rawSyntax) looks good, it’s consistent with template’s raw.syntax property which is the exact same concept.

I agree with the no cleaning.