This issue has been created
 
 
XWiki Commons / cid:jira-generated-image-avatar-cc82dbd7-3b71-4a17-be69-521e9974568a XCOMMONS-3912 Open

Global script bindings override engine bindings and Velocity variables with the same name

 
View issue   ยท   Add comment
 

Issue created

 
cid:jira-generated-image-avatar-be29d6c2-f241-4c33-b5a4-434da6713fea Michael Hamann created this issue on 20/Nov/26 09:36
 
Summary: Global script bindings override engine bindings and Velocity variables with the same name
Issue Type: cid:jira-generated-image-avatar-cc82dbd7-3b71-4a17-be69-521e9974568a Bug
Affects Versions: 8.3-milestone-1
Assignee: Unassigned
Components: Velocity
Created: 20/Nov/26 09:36
Priority: cid:jira-generated-image-static-major-3c16f429-1c18-45a7-85b1-af1063b06210 Major
Reporter: Michael Hamann
Description:

Steps to reproduce

  1. Create a page with the following content (xwiki/2.1 syntax):
    {{unknownmacrox/}}
    
    {{velocity}}#set ($renderingerror = 'my value'){{/velocity}}
    
    {{velocity}}Value: $renderingerror{{/velocity}}
    
  2. View the page.

Expected: after the error box of the unknown macro, Value: my value.
Actual: Value: $renderingerror. Without the unknown macro, the page displays Value: my value.

Cause

ScriptVelocityContext#setScriptContext copies the global scope and then the engine scope bindings of the script context into the Velocity context, before each script execution. The copy uses put(), which also writes the value to the engine scope of the script context, so every synchronization copies each global binding into the engine scope. As a consequence:

  • A global binding overrides an engine binding with the same name, both in Velocity and for the other script languages. This is the opposite of the JSR-223 lookup order (engine scope first), and of the copy order of copyScriptContext, which copies the engine scope last so that it wins.
  • An engine binding that a ScriptContextInitializer sets again before each script execution (e.g. request) is replaced by a global binding with the same name.
  • A Velocity variable whose name is also a global binding doesn't keep its value from one script execution to the next: #set writes the engine scope, and the next synchronization puts the global value back.

In the steps above, XWikiErrorBlockGenerator exposes the error to the xwikirenderingerror/*.vm templates through a global renderingerror binding, and then restores its previous value. As there was none, the global scope keeps a renderingerror binding with a null value for the rest of the request, which replaces the variable set by the first Velocity macro.

Expected: synchronizing the script context into the Velocity context doesn't write to the script context, and an engine binding hides a global binding with the same name, as in JSR-223.

Proposed fix

  • Synchronize the script context into the Velocity context without writing back to the script context, and copy a global binding only when the engine scope doesn't hold a binding with the same name.
  • Keep writing the variables set from Velocity (#set) to the engine scope, which is how other script languages see them.
  • Adapt the code relying on a global binding overriding an engine binding. XWikiErrorBlockGenerator does: with the engine scope winning, a page that set its own $renderingerror variable would hide the error from the xwikirenderingerror/*.vm templates, so it should set (and restore, or remove) the binding in the engine scope instead. RequiredRightsEditConfirmationChecker (analysisResults, exception) needs the same check.
  • Adapt FileUploaderPageTest (xwiki-platform), which overrides the request binding in the global scope, to stub the request of the XWiki context instead.

This is a behavior change for extensions that override an engine binding by setting a global binding with the same name.

Found while fixing a regression of XCOMMONS-3909, which changed when global bindings are written to the engine scope.