CVE-2023-50723: Improper Control of Generation of Code ('Code Injection') (CWE-94)
value: XWiki Platform is a generic wiki platform. Starting in 2.3 and prior to versions 14.10.15, 15.5.2, and 15.7-rc-1, anyone who can edit an arbitrary wiki page in an XWiki installation can gain programming right through several cases of missing escaping in the code for displaying sections in the administration interface. This impacts the confidentiality, integrity and availability of the whole XWiki installation. Normally, all users are allowed to edit their own user profile so this should be exploitable by all users of the XWiki instance. This has been fixed in XWiki 14.10.15, 15.5.2 and 15.7RC1. The patches can be manually applied to the `XWiki.ConfigurableClassMacros` and `XWiki.ConfigurableClass` pages.
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-12-15T19:15Z
Repository: https://github.com/xwiki/xwiki-platform
Language: XML
Files changed: xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/pom.xml, xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClass.xml, xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClassMacros.xml, xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/test/java/org/xwiki/administration/ConfigurableClassPageTest.java
Commit date: 2023-07-31 13:34:52 +0200
Commit message: XWIKI-21122: Improve escaping of configuration section headings

* Add a page test
* Change syntax to xwiki/2.1 as otherwise link escaping doesn't properly
  work (# is not allowed as part of the link).

Diff: diff --git a/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/pom.xml b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/pom.xml
index 88448672d58a0..c27e2b1c7f64d 100644
--- a/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/pom.xml
+++ b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/pom.xml
@@ -102,10 +102,24 @@
       <groupId>org.xwiki.commons</groupId>
       <artifactId>xwiki-commons-script</artifactId>
       <version>${commons.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.xwiki.platform</groupId>
+      <artifactId>xwiki-platform-rendering-xwiki</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+      <type>test-jar</type>
+    </dependency>
+    <dependency>
+      <groupId>org.xwiki.platform</groupId>
+      <artifactId>xwiki-platform-rendering-configuration-default</artifactId>
+      <version>${project.version}</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
       <plugin>
         <groupId>org.xwiki.commons</groupId>
diff --git a/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClass.xml b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClass.xml
index c2f793a175c0b..7cf43fa097386 100644
--- a/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClass.xml
+++ b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClass.xml
@@ -32,11 +32,11 @@
   <contentAuthor>xwiki:XWiki.Admin</contentAuthor>
   <version>1.1</version>
   <title>$services.localization.render('xe.admin.configurable.title')</title>
   <comment/>
   <minorEdit>false</minorEdit>
-  <syntaxId>xwiki/2.0</syntaxId>
+  <syntaxId>xwiki/2.1</syntaxId>
   <hidden>true</hidden>
   <content>{{include reference="XWiki.ConfigurableClassMacros" /}}
 
 {{velocity}}
 #*
diff --git a/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClassMacros.xml b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClassMacros.xml
index c57f5828e0847..b36ab996561f7 100644
--- a/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClassMacros.xml
+++ b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/main/resources/XWiki/ConfigurableClassMacros.xml
@@ -32,11 +32,11 @@
   <contentAuthor>xwiki:XWiki.Admin</contentAuthor>
   <version>1.1</version>
   <title>$services.localization.render('xe.admin.configurable.macros.title')</title>
   <comment/>
   <minorEdit>false</minorEdit>
-  <syntaxId>xwiki/2.0</syntaxId>
+  <syntaxId>xwiki/2.1</syntaxId>
   <hidden>true</hidden>
   <content>{{velocity output="false"}}
 ## Constants:
 #set($redirectParameter = 'xredirect')
 #set($nameOfThisDocument = 'XWiki.ConfigurableClass')
@@ -323,12 +323,14 @@
  * $headingAlreadyShowing (boolean) If true then we don't make another heading. Otherwise it is set to true.
  *###
 #macro(showHeading, $appName, $headingAlreadyShowing)
   #if(!$headingAlreadyShowing)
     #set($headingAlreadyShowing = true)
+    #set($escapedAppName = $services.rendering.escape($appName, 'xwiki/2.1'))
+    #set($doubleEscapedAppName = $services.rendering.escape($escapedAppName, 'xwiki/2.1'))
 
-    = {{translation key="admin.customize"/}} [[$appName&gt;&gt;$appName]]: =
+    = {{translation key="admin.customize"/}} [[$doubleEscapedAppName&gt;&gt;$escapedAppName]]: =
   #end
 #end
 
 #define($formHtml)
   #if ($objClass.getPropertyNames().size() &gt; 0)
diff --git a/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/test/java/org/xwiki/administration/ConfigurableClassPageTest.java b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/test/java/org/xwiki/administration/ConfigurableClassPageTest.java
new file mode 100644
index 0000000000000..b402dff9cb7de
--- /dev/null
+++ b/xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-ui/src/test/java/org/xwiki/administration/ConfigurableClassPageTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.administration;
+
+import java.util.List;
+import java.util.Map;
+
+import org.jsoup.nodes.Document;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.xwiki.localization.macro.internal.TranslationMacro;
+import org.xwiki.model.reference.DocumentReference;
+import org.xwiki.query.internal.ScriptQuery;
+import org.xwiki.query.script.QueryManagerScriptService;
+import org.xwiki.rendering.RenderingScriptServiceComponentList;
+import org.xwiki.rendering.internal.configuration.DefaultRenderingConfigurationComponentList;
+import org.xwiki.rendering.internal.macro.message.ErrorMessageMacro;
+import org.xwiki.script.service.ScriptService;
+import org.xwiki.template.script.TemplateScriptService;
+import org.xwiki.test.annotation.ComponentList;
+import org.xwiki.test.page.HTML50ComponentList;
+import org.xwiki.test.page.PageTest;
+import org.xwiki.test.page.TestNoScriptMacro;
+import org.xwiki.test.page.XWikiSyntax21ComponentList;
+
+import com.xpn.xwiki.doc.XWikiDocument;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.when;
+
+/**
+ * Page test of {@code XWiki.ConfigurableClass}.
+ *
+ * @version $Id$
+ */
+@HTML50ComponentList
+@XWikiSyntax21ComponentList
+@RenderingScriptServiceComponentList
+@DefaultRenderingConfigurationComponentList
+@ComponentList({
+    TestNoScriptMacro.class,
+    TranslationMacro.class,
+    ErrorMessageMacro.class,
+    TemplateScriptService.class
+})
+class ConfigurableClassPageTest extends PageTest
+{
+    private static final String WIKI_NAME = "xwiki";
+
+    private static final String SPACE_NAME = "XWiki";
+
+    private static final DocumentReference CONFIGURABLE_CLASS =
+        new DocumentReference(WIKI_NAME, SPACE_NAME, "ConfigurableClass");
+
+    private static final DocumentReference CONFIGURABLE_CLASS_MACROS =
+        new DocumentReference(WIKI_NAME, SPACE_NAME, "ConfigurableClassMacros");
+
+    private static final DocumentReference MY_SECTION =
+        new DocumentReference(WIKI_NAME, SPACE_NAME, "]]{{noscript /}}");
+
+    private static final String MY_SECTION_SERIALIZED = "XWiki.]]{{noscript /}}";
+
+    @Mock
+    private QueryManagerScriptService queryService;
+
+    @Mock
+    private ScriptQuery query;
+
+    @BeforeEach
+    void setUp() throws Exception
+    {
+        // Load the macros page so it can be included.
+        loadPage(CONFIGURABLE_CLASS_MACROS);
+
+        // Mock the query.
+        this.oldcore.getMocker().registerComponent(ScriptService.class, "query", this.queryService);
+        when(this.queryService.hql(anyString())).thenReturn(this.query);
+        when(this.query.addFilter(anyString())).thenReturn(this.query);
+        when(this.query.setLimit(anyInt())).thenReturn(this.query);
+        when(this.query.setOffset(anyInt())).thenReturn(this.query);
+        when(this.query.bindValues(any(Map.class))).thenReturn(this.query);
+        when(this.query.bindValues(any(List.class))).thenReturn(this.query);
+    }
+
+    @Test
+    void escapeHeadingForError() throws Exception
+    {
+        this.request.put("section", "other");
+        when(this.query.execute()).thenReturn(List.of(MY_SECTION_SERIALIZED)).thenReturn(List.of());
+
+        XWikiDocument mySectionDoc = new XWikiDocument(MY_SECTION);
+        this.xwiki.saveDocument(mySectionDoc, this.context);
+
+        Document htmlPage = renderHTMLPage(CONFIGURABLE_CLASS);
+        assertEquals(String.format("admin.customize %s:", MY_SECTION_SERIALIZED),
+            htmlPage.selectFirst("h1").text());
+    }
+}

Vulnerable code:   <syntaxId>xwiki/2.0</syntaxId>
  <syntaxId>xwiki/2.0</syntaxId>
    = {{translation key="admin.customize"/}} [[$appName&gt;&gt;$appName]]: =
Fixed code:     <dependency>
      <groupId>org.xwiki.platform</groupId>
      <artifactId>xwiki-platform-rendering-xwiki</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
      <type>test-jar</type>
    </dependency>
    <dependency>
      <groupId>org.xwiki.platform</groupId>
      <artifactId>xwiki-platform-rendering-configuration-default</artifactId>
      <version>${project.version}</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  <syntaxId>xwiki/2.1</syntaxId>
  <syntaxId>xwiki/2.1</syntaxId>
    #set($escapedAppName = $services.rendering.escape($appName, 'xwiki/2.1'))
    #set($doubleEscapedAppName = $services.rendering.escape($escapedAppName, 'xwiki/2.1'))
    = {{translation key="admin.customize"/}} [[$doubleEscapedAppName&gt;&gt;$escapedAppName]]: =
/*
 * 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.administration;

import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.localization.macro.internal.TranslationMacro;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.query.internal.ScriptQuery;
import org.xwiki.query.script.QueryManagerScriptService;
import org.xwiki.rendering.RenderingScriptServiceComponentList;
import org.xwiki.rendering.internal.configuration.DefaultRenderingConfigurationComponentList;
import org.xwiki.rendering.internal.macro.message.ErrorMessageMacro;
import org.xwiki.script.service.ScriptService;
import org.xwiki.template.script.TemplateScriptService;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.page.HTML50ComponentList;
import org.xwiki.test.page.PageTest;
import org.xwiki.test.page.TestNoScriptMacro;
import org.xwiki.test.page.XWikiSyntax21ComponentList;

import com.xpn.xwiki.doc.XWikiDocument;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

/**
 * Page test of {@code XWiki.ConfigurableClass}.
 *
 * @version $Id$
 */
@HTML50ComponentList
@XWikiSyntax21ComponentList
@RenderingScriptServiceComponentList
@DefaultRenderingConfigurationComponentList
@ComponentList({
    TestNoScriptMacro.class,
    TranslationMacro.class,
    ErrorMessageMacro.class,
    TemplateScriptService.class
})
class ConfigurableClassPageTest extends PageTest
{
    private static final String WIKI_NAME = "xwiki";

    private static final String SPACE_NAME = "XWiki";

    private static final DocumentReference CONFIGURABLE_CLASS =
        new DocumentReference(WIKI_NAME, SPACE_NAME, "ConfigurableClass");

    private static final DocumentReference CONFIGURABLE_CLASS_MACROS =
        new DocumentReference(WIKI_NAME, SPACE_NAME, "ConfigurableClassMacros");

    private static final DocumentReference MY_SECTION =
        new DocumentReference(WIKI_NAME, SPACE_NAME, "]]{{noscript /}}");

    private static final String MY_SECTION_SERIALIZED = "XWiki.]]{{noscript /}}";

    @Mock
    private QueryManagerScriptService queryService;

    @Mock
    private ScriptQuery query;

    @BeforeEach
    void setUp() throws Exception
    {
        // Load the macros page so it can be included.
        loadPage(CONFIGURABLE_CLASS_MACROS);

        // Mock the query.
        this.oldcore.getMocker().registerComponent(ScriptService.class, "query", this.queryService);
        when(this.queryService.hql(anyString())).thenReturn(this.query);
        when(this.query.addFilter(anyString())).thenReturn(this.query);
        when(this.query.setLimit(anyInt())).thenReturn(this.query);
        when(this.query.setOffset(anyInt())).thenReturn(this.query);
        when(this.query.bindValues(any(Map.class))).thenReturn(this.query);
        when(this.query.bindValues(any(List.class))).thenReturn(this.query);
    }

    @Test
    void escapeHeadingForError() throws Exception
    {
        this.request.put("section", "other");
        when(this.query.execute()).thenReturn(List.of(MY_SECTION_SERIALIZED)).thenReturn(List.of());

        XWikiDocument mySectionDoc = new XWikiDocument(MY_SECTION);
        this.xwiki.saveDocument(mySectionDoc, this.context);

        Document htmlPage = renderHTMLPage(CONFIGURABLE_CLASS);
        assertEquals(String.format("admin.customize %s:", MY_SECTION_SERIALIZED),
            htmlPage.selectFirst("h1").text());
    }
}