CVE-2023-37909: Improper Control of Generation of Code ('Code Injection') (CWE-94)
value: XWiki Platform is a generic wiki platform offering runtime services for applications built on top of it. Starting in version 5.1-rc-1 and prior to versions 14.10.8 and 15.3-rc-1, any user who can edit their own user profile can execute arbitrary script macros including Groovy and Python macros that allow remote code execution including unrestricted read and write access to all wiki contents. This has been patched in XWiki 14.10.8 and 15.3-rc-1 by adding proper escaping. As a workaround, the patch can be manually applied to the document `Menu.UIExtensionSheet`; only three lines need to be changed.
CWE description: The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
CVSS v3 base score: 8.8
Published: 2023-10-25T18:17Z
Repository: https://github.com/xwiki/xwiki-platform
Language: XML
Files changed: xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/pom.xml, xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/main/resources/Menu/UIExtensionSheet.xml, xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/test/java/org/xwiki/menu/UIExtensionSheetPageTest.java
Commit date: 2023-03-28 11:25:19 +0200
Commit message: XWIKI-20746: Improve escaping in Menu.UIExtensionSheet

* Add a page test

Diff: diff --git a/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/pom.xml b/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/pom.xml
index 936420a0d601d..a1fbb4dab5560 100644
--- a/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/pom.xml
+++ b/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/pom.xml
@@ -120,10 +120,17 @@
       <version>${project.version}</version>
       <type>xar</type>
       <scope>runtime</scope>
       <optional>true</optional>
     </dependency>
+    <!-- Test dependencies. -->
+    <dependency>
+      <groupId>org.xwiki.platform</groupId>
+      <artifactId>xwiki-platform-test-page</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
       <plugin>
         <groupId>org.xwiki.commons</groupId>
diff --git a/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/main/resources/Menu/UIExtensionSheet.xml b/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/main/resources/Menu/UIExtensionSheet.xml
index 0d14a39da3772..b6a2b14595475 100644
--- a/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/main/resources/Menu/UIExtensionSheet.xml
+++ b/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/main/resources/Menu/UIExtensionSheet.xml
@@ -39,13 +39,13 @@
   <content>{{velocity}}
 #macro (displayExtensionPointTitle $id)
   #set ($shortId = $stringtool.removeStart($id, 'org.xwiki.platform.'))
   #set ($shortId = $stringtool.removeStart($shortId, 'platform.'))
   #if ("$!shortId" != '')
-    $services.localization.render("menu.uix.extensionPoint.value.$shortId")
+    $escapetool.xml($services.localization.render("menu.uix.extensionPoint.value.$shortId"))
   #else
-    $services.localization.render('menu.uix.extensionPoint.value.nowhere')
+    $escapetool.xml($services.localization.render('menu.uix.extensionPoint.value.nowhere'))
   #end
 #end
 ##
 ##
 #set ($discard = $doc.use('XWiki.UIExtensionClass'))
@@ -83,11 +83,11 @@
   #if ($xcontext.action == 'edit')
     : {{html}}&lt;select id="XWiki.UIExtensionClass_0_extensionPointId" name="XWiki.UIExtensionClass_0_extensionPointId"&gt;
         &lt;option value=""&gt;$services.localization.render('menu.uix.extensionPoint.value.nowhere')&lt;/option&gt;
         #foreach ($extensionPointId in $extensionPoints.keySet())
           #set ($selected = $extensionPointId == $selectedExtensionPointId)
-          &lt;option value="$extensionPointId"#if ($selected) selected="selected"#end&gt;
+          &lt;option value="$escapetool.xml($extensionPointId)"#if ($selected) selected="selected"#end&gt;
             #displayExtensionPointTitle($extensionPointId)
           &lt;/option&gt;
         #end
       &lt;/select&gt;{{/html}}
   #else
diff --git a/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/test/java/org/xwiki/menu/UIExtensionSheetPageTest.java b/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/test/java/org/xwiki/menu/UIExtensionSheetPageTest.java
new file mode 100644
index 0000000000000..f36666b96448a
--- /dev/null
+++ b/xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-ui/src/test/java/org/xwiki/menu/UIExtensionSheetPageTest.java
@@ -0,0 +1,87 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.xwiki.menu;
+
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+import org.junit.jupiter.api.Test;
+import org.xwiki.model.reference.DocumentReference;
+import org.xwiki.test.annotation.ComponentList;
+import org.xwiki.test.page.HTML50ComponentList;
+import org.xwiki.test.page.PageTest;
+import org.xwiki.test.page.XWikiSyntax21ComponentList;
+import org.xwiki.uiextension.internal.UIExtensionClassDocumentInitializer;
+
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.objects.BaseObject;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * Page test for the document {@code Menu.UIExtensionSheet}.
+ *
+ * @version $Id$
+ */
+@ComponentList({
+    UIExtensionClassDocumentInitializer.class
+})
+@HTML50ComponentList
+@XWikiSyntax21ComponentList
+class UIExtensionSheetPageTest extends PageTest
+{
+    private static final DocumentReference SHEET_REFERENCE = new DocumentReference("xwiki", "Menu", "UIExtensionSheet");
+
+    @Test
+    void escaping() throws Exception
+    {
+        DocumentReference testDocumentReference = new DocumentReference("xwiki", "space", "test");
+        XWikiDocument testDocument = new XWikiDocument(testDocumentReference);
+        BaseObject uiExtension =
+            testDocument.newXObject(UIExtensionClassDocumentInitializer.UI_EXTENSION_CLASS, this.context);
+        String extensionID = "\"{{/html}}</option>";
+        String contentValue = "content +" + extensionID;
+        uiExtension.setLargeStringValue(UIExtensionClassDocumentInitializer.CONTENT_PROPERTY, contentValue);
+        uiExtension.setStringValue(UIExtensionClassDocumentInitializer.EXTENSION_POINT_ID_PROPERTY, extensionID);
+        this.xwiki.saveDocument(testDocument, this.context);
+
+        this.context.setDoc(testDocument);
+        this.context.setAction("edit");
+
+        XWikiDocument sheet = loadPage(SHEET_REFERENCE);
+        String htmlContent = sheet.getRenderedContent(this.context);
+        Document renderedDocument = Jsoup.parse(htmlContent);
+
+        Element selectElement = renderedDocument.getElementById("XWiki.UIExtensionClass_0_extensionPointId");
+        assertNotNull(selectElement);
+        Elements optionElement = selectElement.getElementsByAttributeValue("value", extensionID);
+        assertEquals(1, optionElement.size());
+        String extensionIdTitle = "menu.uix.extensionPoint.value." + extensionID;
+        assertEquals(extensionIdTitle, optionElement.get(0).text());
+
+        selectElement = renderedDocument.getElementById("XWiki.UIExtensionClass_0_content");
+        assertNotNull(selectElement);
+        optionElement = selectElement.getElementsByAttributeValue("value", contentValue);
+        assertEquals(1, optionElement.size());
+        assertEquals(extensionIdTitle, optionElement.get(0).text());
+    }
+}

Vulnerable code:     $services.localization.render("menu.uix.extensionPoint.value.$shortId")
    $services.localization.render('menu.uix.extensionPoint.value.nowhere')
          &lt;option value="$extensionPointId"#if ($selected) selected="selected"#end&gt;
Fixed code:     <!-- Test dependencies. -->
    <dependency>
      <groupId>org.xwiki.platform</groupId>
      <artifactId>xwiki-platform-test-page</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>
    $escapetool.xml($services.localization.render("menu.uix.extensionPoint.value.$shortId"))
    $escapetool.xml($services.localization.render('menu.uix.extensionPoint.value.nowhere'))
          &lt;option value="$escapetool.xml($extensionPointId)"#if ($selected) selected="selected"#end&gt;
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.menu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.page.HTML50ComponentList;
import org.xwiki.test.page.PageTest;
import org.xwiki.test.page.XWikiSyntax21ComponentList;
import org.xwiki.uiextension.internal.UIExtensionClassDocumentInitializer;

import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * Page test for the document {@code Menu.UIExtensionSheet}.
 *
 * @version $Id$
 */
@ComponentList({
    UIExtensionClassDocumentInitializer.class
})
@HTML50ComponentList
@XWikiSyntax21ComponentList
class UIExtensionSheetPageTest extends PageTest
{
    private static final DocumentReference SHEET_REFERENCE = new DocumentReference("xwiki", "Menu", "UIExtensionSheet");

    @Test
    void escaping() throws Exception
    {
        DocumentReference testDocumentReference = new DocumentReference("xwiki", "space", "test");
        XWikiDocument testDocument = new XWikiDocument(testDocumentReference);
        BaseObject uiExtension =
            testDocument.newXObject(UIExtensionClassDocumentInitializer.UI_EXTENSION_CLASS, this.context);
        String extensionID = "\"{{/html}}</option>";
        String contentValue = "content +" + extensionID;
        uiExtension.setLargeStringValue(UIExtensionClassDocumentInitializer.CONTENT_PROPERTY, contentValue);
        uiExtension.setStringValue(UIExtensionClassDocumentInitializer.EXTENSION_POINT_ID_PROPERTY, extensionID);
        this.xwiki.saveDocument(testDocument, this.context);

        this.context.setDoc(testDocument);
        this.context.setAction("edit");

        XWikiDocument sheet = loadPage(SHEET_REFERENCE);
        String htmlContent = sheet.getRenderedContent(this.context);
        Document renderedDocument = Jsoup.parse(htmlContent);

        Element selectElement = renderedDocument.getElementById("XWiki.UIExtensionClass_0_extensionPointId");
        assertNotNull(selectElement);
        Elements optionElement = selectElement.getElementsByAttributeValue("value", extensionID);
        assertEquals(1, optionElement.size());
        String extensionIdTitle = "menu.uix.extensionPoint.value." + extensionID;
        assertEquals(extensionIdTitle, optionElement.get(0).text());

        selectElement = renderedDocument.getElementById("XWiki.UIExtensionClass_0_content");
        assertNotNull(selectElement);
        optionElement = selectElement.getElementsByAttributeValue("value", contentValue);
        assertEquals(1, optionElement.size());
        assertEquals(extensionIdTitle, optionElement.get(0).text());
    }
}