CVE-2023-50721 (XML, published 2023-12-15T19:15Z)
Description: value: XWiki Platform is a generic wiki platform. Starting in 4.5-rc-1 and prior to versions 14.10.15, 15.5.2, and 15.7-rc-1, the search administration interface doesn't properly escape the id and label of search user interface extensions, allowing the injection of XWiki syntax containing script macros including Groovy macros that allow remote code execution, impacting the confidentiality, integrity and availability of the whole XWiki instance. This attack can be executed by any user who can edit some wiki page like the user's profile (editable by default) as user interface extensions that will be displayed in the search administration can be added on any document by any user.  The necessary escaping has been added in XWiki 14.10.15, 15.5.2 and 15.7RC1. As a workaround, the patch can be applied manually applied to the page `XWiki.SearchAdmin`.
Severity: nan (CVSS v3 base score: 8.8)
Weakness: CWE-94 \u2014 Improper Control of Generation of Code ('Code Injection')
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.
Repository: https://github.com/xwiki/xwiki-platform
Files changed:
xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-ui/pom.xml
xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-ui/src/main/resources/XWiki/SearchAdmin.xml
xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-ui/src/test/java/org/xwiki/search/ui/SearchAdminPageTest.java
Commit message: XWIKI-21200: Improve escaping in SearchAdmin

* Add a page test


Vulnerable code:
  <syntaxId>xwiki/2.0</syntaxId>
        &lt;option value="$availableSearchExtension.id" #if($availableSearchExtension.id == $searchEngine)selected='selected'#end&gt;$availableSearchExtension.parameters.label&lt;/option&gt;
        &lt;a href="#${searchEngine}Config" aria-controls="${searchEngine}Config" role="tab" data-toggle="tab"&gt;
          $searchExtension.parameters.label
      (% role="tabpanel" class="tab-pane active" id="${searchEngine}Config" %)(((
        {{include reference="$searchAdminPage" context='new'}}

Fixed code:
    <!-- Test dependencies. -->
    <dependency>
      <groupId>org.xwiki.platform</groupId>
      <artifactId>xwiki-platform-test-page</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.xwiki.platform</groupId>
      <artifactId>xwiki-platform-uiextension-api</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
      <type>test-jar</type>
    </dependency>
    <!-- Provides the component list for RenderingScriptService. -->
    <dependency>
      <groupId>org.xwiki.platform</groupId>
      <artifactId>xwiki-platform-rendering-xwiki</artifactId>
      <version>${project.version}</version>
      <type>test-jar</type>
      <scope>test</scope>
    </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>
        &lt;option value="$escapetool.xml($availableSearchExtension.id)" #if($availableSearchExtension.id == $searchEngine)
        selected='selected'#end &gt;$escapetool.xml($availableSearchExtension.parameters.label)&lt;/option&gt;
        &lt;a href="#${escapetool.xml($searchEngine)}Config" aria-controls="${escapetool.xml($searchEngine)}Config"
          role="tab"
          data-toggle="tab"&gt;
          $escapetool.xml($searchExtension.parameters.label)
      (% role="tabpanel" class="tab-pane active" id="${services.rendering.escape($searchEngine, 'xwiki/2.1')}Config"
      %)(((
        {{include reference="$services.rendering.escape($searchAdminPage, 'xwiki/2.1')" context='new'}}
/*
 * 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.search.ui;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xwiki.bridge.event.DocumentCreatedEvent;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.wiki.internal.bridge.DefaultWikiObjectComponentManagerEventListener;
import org.xwiki.context.internal.concurrent.DefaultContextStoreManager;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.observation.EventListener;
import org.xwiki.rendering.RenderingScriptServiceComponentList;
import org.xwiki.rendering.internal.configuration.DefaultRenderingConfigurationComponentList;
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 org.xwiki.uiextension.internal.UIExtensionClassDocumentInitializer;
import org.xwiki.uiextension.internal.WikiUIExtensionConstants;
import org.xwiki.uiextension.script.UIExtensionScriptServiceComponentList;

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;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

/**
 * Page test for {@code XWiki.SearchAdmin}.
 *
 * @version $Id$
 */
@ComponentList({ UIExtensionClassDocumentInitializer.class, DefaultContextStoreManager.class, TestNoScriptMacro.class })
@UIExtensionScriptServiceComponentList
@RenderingScriptServiceComponentList
@DefaultRenderingConfigurationComponentList
@HTML50ComponentList
@XWikiSyntax21ComponentList
class SearchAdminPageTest extends PageTest
{
    private static final String WIKI_NAME = "xwiki";

    private static final String XWIKI_SPACE = "XWiki";

    private static final DocumentReference SEARCH_ADMIN_SHEET =
        new DocumentReference(WIKI_NAME, XWIKI_SPACE, "SearchAdmin");

    private static final DocumentReference ADMIN_REFERENCE = new DocumentReference(WIKI_NAME, XWIKI_SPACE, "Admin");

    @BeforeEach
    void setUp() throws Exception
    {
        // Initialize the UIExtension class.
        this.xwiki.initializeMandatoryDocuments(this.context);

        when(this.oldcore.getMockAuthorizationManager().hasAccess(any(), eq(ADMIN_REFERENCE), any())).thenReturn(true);

        // Register the component manager as wiki component manager so that the UIX can be registered on the wiki.
        this.componentManager.registerComponent(ComponentManager.class, "wiki", this.componentManager);
    }

    @Test
    void escapesExtensionProperties() throws Exception
    {
        // Create a document with a UI extension to test on.
        DocumentReference docRef = new DocumentReference(WIKI_NAME, XWIKI_SPACE, "Test");
        XWikiDocument document = new XWikiDocument(docRef);
        // Set the author to admin so the UIX can be registered at wiki level.
        document.setAuthorReference(ADMIN_REFERENCE);
        BaseObject uiExtensionObject =
            document.newXObject(WikiUIExtensionConstants.UI_EXTENSION_CLASS, this.context);
        uiExtensionObject.setStringValue(WikiUIExtensionConstants.EXTENSION_POINT_ID_PROPERTY,
            "org.xwiki.platform.search");
        String id = "\")}}{{noscript /}}";
        uiExtensionObject.setStringValue(WikiUIExtensionConstants.ID_PROPERTY, id);
        String label = "Label {{noscript /}}";
        String adminPageName = "\"}}{{noscript /}}Admin";
        String adminPage = XWIKI_SPACE + "." + adminPageName;
        uiExtensionObject.setLargeStringValue(WikiUIExtensionConstants.PARAMETERS_PROPERTY, "label=" + label + "\n"
            + "admin=" + adminPage);
        uiExtensionObject.setStringValue(WikiUIExtensionConstants.SCOPE_PROPERTY, "wiki");
        this.xwiki.saveDocument(document, this.context);

        // The event listeners are not registered by default. We trigger it manually so that the UIX is registered and
        // can be found and rendered.
        this.componentManager
            .<EventListener>getInstance(EventListener.class,
                DefaultWikiObjectComponentManagerEventListener.EVENT_LISTENER_NAME)
            .onEvent(new DocumentCreatedEvent(), document, null);

        // Create the fake search admin page.
        DocumentReference searchAdminReference = new DocumentReference(WIKI_NAME, XWIKI_SPACE, adminPageName);
        XWikiDocument searchAdminDocument = new XWikiDocument(searchAdminReference);
        String searchAdminPageContent = "Search Admin Page Content";
        searchAdminDocument.setContent(searchAdminPageContent);
        this.xwiki.saveDocument(searchAdminDocument, this.context);

        // Load XWiki.SearchCode and XWiki.SearchConfigClass as the SearchAdmin page uses them.
        loadPage(new DocumentReference(WIKI_NAME, XWIKI_SPACE, "SearchCode"));
        loadPage(new DocumentReference(WIKI_NAME, XWIKI_SPACE, "SearchConfigClass"));

        Document htmlPage = renderHTMLPage(SEARCH_ADMIN_SHEET);

        Element optionElement = htmlPage.selectFirst("select option");
        assertNotNull(optionElement);
        assertEquals(id, optionElement.attr("value"));
        assertEquals(label, optionElement.text());

        Element tabLink = htmlPage.selectFirst("ul.nav-tabs li a");
        assertNotNull(tabLink);
        String tabId = String.format("%sConfig", id);
        assertEquals("#" + tabId, tabLink.attr("href"));
        assertEquals(tabId, tabLink.attr("aria-controls"));
        assertEquals(label, tabLink.text());

        Element tabContent = htmlPage.selectFirst("div.tab-content div.tab-pane");
        assertNotNull(tabContent);
        assertEquals(tabId, tabContent.attr("id"));
        assertEquals(searchAdminPageContent, tabContent.text());
    }
}