CVE-2023-46243 (Java, published 2023-11-07T20:15Z)
Description: value: XWiki Platform is a generic wiki platform offering runtime services for applications built on top of it. In affected versions it's possible for a user to execute any content with the right of an existing document's content author, provided the user have edit right on it. A crafted URL of the form ` /xwiki/bin/edit//?content=%7B%7Bgroovy%7D%7Dprintln%28%22Hello+from+Groovy%21%22%29%7B%7B%2Fgroovy%7D%7D&xpage=view` can be used to execute arbitrary groovy code on the server. This vulnerability has been patched in XWiki versions 14.10.6 and 15.2RC1. Users are advised to update. There are no known workarounds for this issue. 
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-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java
xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/test/MockitoOldcore.java
xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/EditActionTest.java
Commit message: XWIKI-20385: Improve author handling in the edit action


Vulnerable code:
                        document.setDate(new Date());
                            document.setContentUpdateDate(new Date());
                            document.setContentAuthorReference(document.getAuthorReference());

Fixed code:
import javax.inject.Inject;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.user.UserReference;
import org.xwiki.user.UserReferenceResolver;
    @Inject
    @Named("document")
    private UserReferenceResolver<DocumentReference> documentReferenceUserReferenceResolver;

        // If the metadata is modified, modify the effective metadata author
        if (editedDocument.isMetaDataDirty()) {
            UserReference userReference =
                this.documentReferenceUserReferenceResolver.resolve(context.getUserReference());
            editedDocument.getAuthors().setEffectiveMetadataAuthor(userReference);
            editedDocument.getAuthors().setOriginalMetadataAuthor(userReference);
        }

        // If the content is modified, modify the content author
        if (editedDocument.isContentDirty()) {
            UserReference userReference =
                this.documentReferenceUserReferenceResolver.resolve(context.getUserReference());
            editedDocument.getAuthors().setContentAuthor(userReference);
        }

import org.xwiki.model.document.DocumentAuthors;
                        Date ndate = new Date();
                        document.setDate(ndate);
                            document.setContentUpdateDate(ndate);
                            DocumentAuthors authors = document.getAuthors();
                            authors.setContentAuthor(authors.getEffectiveMetadataAuthor());
/*
 * 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 com.xpn.xwiki.web;

import javax.inject.Named;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.store.TemporaryAttachmentSessionsManager;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;
import org.xwiki.user.UserReference;
import org.xwiki.user.UserReferenceResolver;
import org.xwiki.user.UserReferenceSerializer;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.test.MockitoOldcore;
import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
import com.xpn.xwiki.test.reference.ReferenceComponentList;

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Validate {@link EditAction}.
 * 
 * @version $Id$
 */
@OldcoreTest
@ReferenceComponentList
public class EditActionTest
{
    private static final DocumentReference USER_DOCUMENT_REFERENCE = new DocumentReference("wiki", "XWiki", "user");

    private static final UserReference USER_REFERENCE = mock(UserReference.class);

    private static final UserReference OTHERUSER_REFERENCE = mock(UserReference.class);

    @InjectMockComponents
    private EditAction action;

    @InjectMockitoOldcore
    private MockitoOldcore oldcore;

    @MockComponent
    private TemporaryAttachmentSessionsManager temporaryAttachmentSessionsManager;

    @MockComponent
    @Named("document")
    private UserReferenceResolver<DocumentReference> documentReferenceUserReferenceResolver;

    @MockComponent
    @Named("document")
    private UserReferenceSerializer<DocumentReference> documentReferenceUserReferenceSerializer;

    @Mock
    private XWikiRequest request;

    @BeforeEach
    public void beforeEach()
    {
        when(this.documentReferenceUserReferenceResolver.resolve(USER_DOCUMENT_REFERENCE)).thenReturn(USER_REFERENCE);
        when(this.documentReferenceUserReferenceSerializer.serialize(USER_REFERENCE)).thenReturn(USER_DOCUMENT_REFERENCE);

        this.oldcore.getXWikiContext().setUserReference(USER_DOCUMENT_REFERENCE);
    }

    private String initAndRenderAction() throws XWikiException
    {
        EditForm form = new EditForm();
        form.reset(this.request);

        this.oldcore.getXWikiContext().setForm(form);

        return this.action.render(this.oldcore.getXWikiContext());
    }

    @Test
    void documentAuthorsWhenDocumentDoesNotExist() throws XWikiException
    {
        XWikiDocument document = oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
            this.oldcore.getXWikiContext());
        this.oldcore.getXWikiContext().setDoc(document);

        initAndRenderAction();

        document = this.oldcore.getXWikiContext().getDoc();

        assertSame(USER_REFERENCE, document.getAuthors().getContentAuthor());
        assertSame(USER_REFERENCE, document.getAuthors().getCreator());
        assertSame(USER_REFERENCE, document.getAuthors().getEffectiveMetadataAuthor());
        assertSame(USER_REFERENCE, document.getAuthors().getOriginalMetadataAuthor());
    }

    @Test
    void documentAuthorsWhenDocumentExist() throws XWikiException
    {
        XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
            this.oldcore.getXWikiContext());
        document.getAuthors().setCreator(OTHERUSER_REFERENCE);
        document.getAuthors().setContentAuthor(OTHERUSER_REFERENCE);
        document.getAuthors().setEffectiveMetadataAuthor(OTHERUSER_REFERENCE);
        document.getAuthors().setOriginalMetadataAuthor(OTHERUSER_REFERENCE);
        this.oldcore.getSpyXWiki().saveDocument(document, this.oldcore.getXWikiContext());

        document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
            this.oldcore.getXWikiContext());
        this.oldcore.getXWikiContext().setDoc(document);
        this.oldcore.getXWikiContext().put("tdoc", document);

        initAndRenderAction();

        document = this.oldcore.getXWikiContext().getDoc();

        assertSame(OTHERUSER_REFERENCE, document.getAuthors().getContentAuthor());
        assertSame(OTHERUSER_REFERENCE, document.getAuthors().getCreator());
        assertSame(OTHERUSER_REFERENCE, document.getAuthors().getEffectiveMetadataAuthor());
        assertSame(OTHERUSER_REFERENCE, document.getAuthors().getOriginalMetadataAuthor());
    }

    @Test
    void documentAuthorsWhenDocumentExistAndContentIsModified() throws XWikiException
    {
        XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
            this.oldcore.getXWikiContext());
        document.getAuthors().setCreator(OTHERUSER_REFERENCE);
        document.getAuthors().setContentAuthor(OTHERUSER_REFERENCE);
        document.getAuthors().setEffectiveMetadataAuthor(OTHERUSER_REFERENCE);
        document.getAuthors().setOriginalMetadataAuthor(OTHERUSER_REFERENCE);
        this.oldcore.getSpyXWiki().saveDocument(document, this.oldcore.getXWikiContext());

        document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
            this.oldcore.getXWikiContext());
        this.oldcore.getXWikiContext().setDoc(document);
        this.oldcore.getXWikiContext().put("tdoc", document);

        when(this.request.getParameter("content")).thenReturn("modified content");

        initAndRenderAction();

        document = this.oldcore.getXWikiContext().getDoc();

        assertSame(USER_REFERENCE, document.getAuthors().getContentAuthor());
        assertSame(OTHERUSER_REFERENCE, document.getAuthors().getCreator());
        assertSame(OTHERUSER_REFERENCE, document.getAuthors().getEffectiveMetadataAuthor());
        assertSame(OTHERUSER_REFERENCE, document.getAuthors().getOriginalMetadataAuthor());
    }
}