CVE-2023-46243: 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. 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. 
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-11-07T20:15Z
Repository: https://github.com/xwiki/xwiki-platform
Language: Java
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 date: 2023-02-28 11:48:06 +0100
Commit message: XWIKI-20385: Improve author handling in the edit action

Diff: diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java
index 25d6ca14bb3ca..09e61111bf4b6 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java
@@ -17,20 +17,24 @@
  * 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.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 import javax.script.ScriptContext;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.math.NumberUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.xwiki.component.annotation.Component;
+import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.rendering.syntax.Syntax;
+import org.xwiki.user.UserReference;
+import org.xwiki.user.UserReferenceResolver;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.doc.XWikiLock;
@@ -48,10 +52,14 @@ public class EditAction extends XWikiAction
     /**
      * The object used for logging.
      */
     private static final Logger LOGGER = LoggerFactory.getLogger(EditAction.class);
 
+    @Inject
+    @Named("document")
+    private UserReferenceResolver<DocumentReference> documentReferenceUserReferenceResolver;
+
     /**
      * Default constructor.
      */
     public EditAction()
     {
@@ -106,10 +114,25 @@ protected XWikiDocument prepareEditedDocument(XWikiContext context) throws XWiki
 
         // The default values from the template can be overwritten by additional request parameters.
         updateDocumentTitleAndContentFromRequest(editedDocument, context);
         editedDocument.readAddedUpdatedAndRemovedObjectsFromForm(editForm, context);
 
+        // 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);
+        }
+
         // Set the current user as creator, author and contentAuthor when the edited document is newly created to avoid
         // using XWikiGuest instead (because those fields were not previously initialized).
         if (editedDocument.isNew()) {
             editedDocument.setCreatorReference(context.getUserReference());
             editedDocument.setAuthorReference(context.getUserReference());
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/test/MockitoOldcore.java b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/test/MockitoOldcore.java
index 4ac4b7fabac28..739f34bd4e5a3 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/test/MockitoOldcore.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/test/MockitoOldcore.java
@@ -53,10 +53,11 @@
 import org.xwiki.context.Execution;
 import org.xwiki.context.ExecutionContext;
 import org.xwiki.context.ExecutionContextManager;
 import org.xwiki.environment.Environment;
 import org.xwiki.environment.internal.ServletEnvironment;
+import org.xwiki.model.document.DocumentAuthors;
 import org.xwiki.model.internal.reference.EntityReferenceFactory;
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.model.reference.DocumentReferenceResolver;
 import org.xwiki.model.reference.LocalDocumentReference;
 import org.xwiki.model.reference.WikiReference;
@@ -790,14 +791,16 @@ public Void answer(InvocationOnMock invocation) throws Throwable
 
                     document.setComment(StringUtils.defaultString(comment));
                     document.setMinorEdit(minorEdit);
 
                     if (document.isContentDirty() || document.isMetaDataDirty()) {
-                        document.setDate(new Date());
+                        Date ndate = new Date();
+                        document.setDate(ndate);
                         if (document.isContentDirty()) {
-                            document.setContentUpdateDate(new Date());
-                            document.setContentAuthorReference(document.getAuthorReference());
+                            document.setContentUpdateDate(ndate);
+                            DocumentAuthors authors = document.getAuthors();
+                            authors.setContentAuthor(authors.getEffectiveMetadataAuthor());
                         }
                         document.incrementVersion();
 
                         document.setContentDirty(false);
                         document.setMetaDataDirty(false);
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/EditActionTest.java b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/EditActionTest.java
new file mode 100644
index 0000000000000..6781260196df4
--- /dev/null
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/EditActionTest.java
@@ -0,0 +1,170 @@
+/*
+ * 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());
+    }
+}

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());
    }
}