CVE-2023-29507: Other (NVD-CWE-Other)
value: XWiki Commons are technical libraries common to several other top level XWiki projects. The Document script API returns directly a DocumentAuthors allowing to set any authors to the document, which in consequence can allow subsequent executions of scripts since this author is used for checking rights. The problem has been patched in XWiki 14.10 and 14.4.7 by returning a safe script API.
CWE description: NVD is only using a subset of CWE for mapping instead of the entire CWE, and the weakness type is not covered by that subset.
CVSS v3 base score: 7.2
Published: 2023-04-16T07:15Z
Repository: https://github.com/xwiki/xwiki-platform
Language: Java
Files changed: xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java, xwiki-platform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/model/internal/document/SafeDocumentAuthors.java, xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/api/DocumentTest.java
Commit date: 2022-11-17 14:48:22 +0100
Commit message: XWIKI-20380: Add missing check on document authors

Diff: diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java
index 9618a801d7c7d..f534e6f53eb33 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java
@@ -41,10 +41,11 @@
 import org.xwiki.configuration.ConfigurationSource;
 import org.xwiki.context.Execution;
 import org.xwiki.context.ExecutionContext;
 import org.xwiki.display.internal.DocumentDisplayerParameters;
 import org.xwiki.model.document.DocumentAuthors;
+import org.xwiki.model.internal.document.SafeDocumentAuthors;
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.model.reference.DocumentReferenceResolver;
 import org.xwiki.model.reference.EntityReferenceSerializer;
 import org.xwiki.model.reference.ObjectReference;
 import org.xwiki.model.reference.PageReference;
@@ -2159,10 +2160,13 @@ public boolean hasAccessLevel(String level, String user)
     }
 
     /**
      * Verifies if the user identified by {@code userReference} has the access identified by {@code right} on this
      * document.
+     * Note that this method does not override {@link Api#hasAccess(Right, DocumentReference)}: they share same
+     * signature but on the {@code Api} one the {@link DocumentReference} parameter is about the entity where to check
+     * the right, while here it's about the user to check right for.
      * 
      * @param right the right to check
      * @param userReference the user to check the right for
      * @return {@code true} if the user has the specified right on this document, {@code false} otherwise
      * @since 10.6RC1
@@ -2170,10 +2174,23 @@ public boolean hasAccessLevel(String level, String user)
     public boolean hasAccess(Right right, DocumentReference userReference)
     {
         return getAuthorizationManager().hasAccess(right, userReference, getDocumentReference());
     }
 
+    /**
+     * Verifies if the context user has the access identified by {@code right} on the current context document.
+     * @param right the right to check
+     * @return {@code true} if the user has the specified right on this document, {@code false} otherwise
+     * @since 14.10RC1
+     * @since 14.4.7
+     */
+    @Unstable
+    public boolean hasAccess(Right right)
+    {
+        return hasAccess(right, getXWikiContext().getUserReference());
+    }
+
     public boolean getLocked()
     {
         try {
             XWikiLock lock = this.doc.getLock(getXWikiContext());
             if (lock != null && !getXWikiContext().getUser().equals(lock.getUserName())) {
@@ -2593,11 +2610,11 @@ private UserReferenceResolver<CurrentUserReference> getCurrentUserReferenceResol
 
     public void save(String comment, boolean minorEdit) throws XWikiException
     {
         if (hasAccessLevel("edit")) {
 
-            DocumentAuthors authors = this.getAuthors();
+            DocumentAuthors authors = getDoc().getAuthors();
             authors.setOriginalMetadataAuthor(getCurrentUserReferenceResolver().resolve(CurrentUserReference.INSTANCE));
             // If the current author does not have PR don't let it set current user as author of the saved document
             // since it can lead to right escalation
             if (hasProgrammingRights() || !getConfiguration().getProperty("security.script.save.checkAuthor", true)) {
                 saveDocument(comment, minorEdit);
@@ -2690,11 +2707,11 @@ public void saveAsAuthor(String comment) throws XWikiException
      */
     public void saveAsAuthor(String comment, boolean minorEdit) throws XWikiException
     {
         XWikiContext xcontext = getXWikiContext();
 
-        getAuthors()
+        getDoc().getAuthors()
             .setOriginalMetadataAuthor(getCurrentUserReferenceResolver().resolve(CurrentUserReference.INSTANCE));
         DocumentReference author = getEffectiveAuthorReference();
         if (hasAccess(Right.EDIT, author)) {
             DocumentReference currentUser = xcontext.getUserReference();
             try {
@@ -3308,8 +3325,14 @@ public int getLocalReferenceMaxLength()
      * @since 14.0RC1
      */
     @Unstable
     public DocumentAuthors getAuthors()
     {
-        return doc.getAuthors();
+        if (this.hasAccess(Right.PROGRAM)) {
+            // We're using getDoc here to ensure to have a cloned doc
+            return getDoc().getAuthors();
+        } else {
+            // in this case we don't care if the doc is cloned or not since it's readonly
+            return new SafeDocumentAuthors(this.doc.getAuthors());
+        }
     }
 }
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/model/internal/document/SafeDocumentAuthors.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/model/internal/document/SafeDocumentAuthors.java
new file mode 100644
index 0000000000000..fafae3dc7c4ee
--- /dev/null
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/model/internal/document/SafeDocumentAuthors.java
@@ -0,0 +1,117 @@
+/*
+ * 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.model.internal.document;
+
+import java.util.Objects;
+
+import org.xwiki.model.document.DocumentAuthors;
+import org.xwiki.script.internal.safe.AbstractSafeObject;
+import org.xwiki.script.wrap.AbstractWrappingObject;
+import org.xwiki.user.UserReference;
+
+/**
+ * Safe implementation of {@link DocumentAuthors} that doesn't allow to use any setters. This implementation aims at
+ * being used in script services. All setters throw an {@link UnsupportedOperationException}.
+ *
+ * @version $Id$
+ * @since 14.10RC1
+ * @since 14.4.7
+ */
+public class SafeDocumentAuthors extends AbstractWrappingObject<DocumentAuthors> implements DocumentAuthors
+{
+    /**
+     * Default constructor.
+     *
+     * @param documentAuthors the wrapped authors to use for getters.
+     */
+    public SafeDocumentAuthors(DocumentAuthors documentAuthors)
+    {
+        super(documentAuthors);
+    }
+
+    @Override
+    public UserReference getContentAuthor()
+    {
+        return getWrapped().getContentAuthor();
+    }
+
+    @Override
+    public void setContentAuthor(UserReference contentAuthor)
+    {
+        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
+    }
+
+    @Override
+    public UserReference getEffectiveMetadataAuthor()
+    {
+        return getWrapped().getEffectiveMetadataAuthor();
+    }
+
+    @Override
+    public void setEffectiveMetadataAuthor(UserReference metadataAuthor)
+    {
+        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
+    }
+
+    @Override
+    public UserReference getOriginalMetadataAuthor()
+    {
+        return getWrapped().getOriginalMetadataAuthor();
+    }
+
+    @Override
+    public void setOriginalMetadataAuthor(UserReference originalMetadataAuthor)
+    {
+        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
+    }
+
+    @Override
+    public UserReference getCreator()
+    {
+        return getWrapped().getCreator();
+    }
+
+    @Override
+    public void setCreator(UserReference creator)
+    {
+        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
+    }
+
+    @Override
+    public void copyAuthors(DocumentAuthors documentAuthors)
+    {
+        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (obj instanceof SafeDocumentAuthors) {
+            return Objects.equals(this.getWrapped(), ((SafeDocumentAuthors) obj).getWrapped());
+        }
+        return super.equals(obj);
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return super.hashCode();
+    }
+}
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/api/DocumentTest.java b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/api/DocumentTest.java
index 573663ec66f1e..a1ebacd7700e6 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/api/DocumentTest.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/api/DocumentTest.java
@@ -22,13 +22,16 @@
 import java.util.List;
 
 import org.junit.jupiter.api.Test;
 import org.xwiki.component.manager.ComponentLookupException;
 import org.xwiki.component.util.DefaultParameterizedType;
+import org.xwiki.model.document.DocumentAuthors;
+import org.xwiki.model.internal.document.SafeDocumentAuthors;
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.model.reference.ObjectReference;
 import org.xwiki.observation.ObservationManager;
+import org.xwiki.security.authorization.AuthorizationManager;
 import org.xwiki.security.authorization.Right;
 import org.xwiki.test.junit5.mockito.MockComponent;
 import org.xwiki.test.mockito.MockitoComponentManager;
 import org.xwiki.user.CurrentUserReference;
 import org.xwiki.user.UserReference;
@@ -45,15 +48,19 @@
 import com.xpn.xwiki.test.reference.ReferenceComponentList;
 import com.xpn.xwiki.user.api.XWikiRightService;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.isNull;
 import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @OldcoreTest
 @ReferenceComponentList
 class DocumentTest
@@ -351,6 +358,36 @@ void saveAsAuthorWhenNoPR(MockitoComponentManager componentManager) throws XWiki
 
         document.save();
 
         assertEquals(userContextReference, document.getAuthors().getEffectiveMetadataAuthor());
     }
+
+    @Test
+    void getAuthors()
+    {
+        DocumentAuthors documentAuthors = mock(DocumentAuthors.class);
+        AuthorizationManager mockAuthorizationManager = this.oldcore.getMockAuthorizationManager();
+        XWikiContext context = this.oldcore.getXWikiContext();
+        DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "Foo");
+        context.setUserReference(userReference);
+        DocumentReference currentDocReference = mock(DocumentReference.class, "currentDocRef");
+        XWikiDocument currentDoc = mock(XWikiDocument.class);
+        when(currentDoc.getAuthors()).thenReturn(documentAuthors);
+        Document document = new Document(currentDoc, context);
+
+        when(currentDoc.getDocumentReference()).thenReturn(currentDocReference);
+
+        when(mockAuthorizationManager.hasAccess(Right.PROGRAM, userReference, currentDocReference)).thenReturn(false);
+        DocumentAuthors obtainedAuthors = document.getAuthors();
+        assertTrue(obtainedAuthors instanceof SafeDocumentAuthors);
+        assertEquals(new SafeDocumentAuthors(documentAuthors), obtainedAuthors);
+
+        verify(mockAuthorizationManager).hasAccess(Right.PROGRAM, userReference, currentDocReference);
+
+        when(mockAuthorizationManager.hasAccess(Right.PROGRAM, userReference, currentDocReference)).thenReturn(true);
+        when(currentDoc.clone()).thenReturn(currentDoc);
+        obtainedAuthors = document.getAuthors();
+        assertSame(documentAuthors, obtainedAuthors);
+        verify(mockAuthorizationManager, times(2)).hasAccess(Right.PROGRAM, userReference, currentDocReference);
+        verify(currentDoc).clone();
+    }
 }

Vulnerable code:             DocumentAuthors authors = this.getAuthors();
        getAuthors()
        return doc.getAuthors();
Fixed code: import org.xwiki.model.internal.document.SafeDocumentAuthors;
     * Note that this method does not override {@link Api#hasAccess(Right, DocumentReference)}: they share same
     * signature but on the {@code Api} one the {@link DocumentReference} parameter is about the entity where to check
     * the right, while here it's about the user to check right for.
    /**
     * Verifies if the context user has the access identified by {@code right} on the current context document.
     * @param right the right to check
     * @return {@code true} if the user has the specified right on this document, {@code false} otherwise
     * @since 14.10RC1
     * @since 14.4.7
     */
    @Unstable
    public boolean hasAccess(Right right)
    {
        return hasAccess(right, getXWikiContext().getUserReference());
    }

            DocumentAuthors authors = getDoc().getAuthors();
        getDoc().getAuthors()
        if (this.hasAccess(Right.PROGRAM)) {
            // We're using getDoc here to ensure to have a cloned doc
            return getDoc().getAuthors();
        } else {
            // in this case we don't care if the doc is cloned or not since it's readonly
            return new SafeDocumentAuthors(this.doc.getAuthors());
        }
/*
 * 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.model.internal.document;

import java.util.Objects;

import org.xwiki.model.document.DocumentAuthors;
import org.xwiki.script.internal.safe.AbstractSafeObject;
import org.xwiki.script.wrap.AbstractWrappingObject;
import org.xwiki.user.UserReference;

/**
 * Safe implementation of {@link DocumentAuthors} that doesn't allow to use any setters. This implementation aims at
 * being used in script services. All setters throw an {@link UnsupportedOperationException}.
 *
 * @version $Id$
 * @since 14.10RC1
 * @since 14.4.7
 */
public class SafeDocumentAuthors extends AbstractWrappingObject<DocumentAuthors> implements DocumentAuthors
{
    /**
     * Default constructor.
     *
     * @param documentAuthors the wrapped authors to use for getters.
     */
    public SafeDocumentAuthors(DocumentAuthors documentAuthors)
    {
        super(documentAuthors);
    }

    @Override
    public UserReference getContentAuthor()
    {
        return getWrapped().getContentAuthor();
    }

    @Override
    public void setContentAuthor(UserReference contentAuthor)
    {
        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
    }

    @Override
    public UserReference getEffectiveMetadataAuthor()
    {
        return getWrapped().getEffectiveMetadataAuthor();
    }

    @Override
    public void setEffectiveMetadataAuthor(UserReference metadataAuthor)
    {
        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
    }

    @Override
    public UserReference getOriginalMetadataAuthor()
    {
        return getWrapped().getOriginalMetadataAuthor();
    }

    @Override
    public void setOriginalMetadataAuthor(UserReference originalMetadataAuthor)
    {
        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
    }

    @Override
    public UserReference getCreator()
    {
        return getWrapped().getCreator();
    }

    @Override
    public void setCreator(UserReference creator)
    {
        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
    }

    @Override
    public void copyAuthors(DocumentAuthors documentAuthors)
    {
        throw new UnsupportedOperationException(AbstractSafeObject.FORBIDDEN);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof SafeDocumentAuthors) {
            return Objects.equals(this.getWrapped(), ((SafeDocumentAuthors) obj).getWrapped());
        }
        return super.equals(obj);
    }

    @Override
    public int hashCode()
    {
        return super.hashCode();
    }
}
import org.xwiki.model.document.DocumentAuthors;
import org.xwiki.model.internal.document.SafeDocumentAuthors;
import org.xwiki.security.authorization.AuthorizationManager;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

    @Test
    void getAuthors()
    {
        DocumentAuthors documentAuthors = mock(DocumentAuthors.class);
        AuthorizationManager mockAuthorizationManager = this.oldcore.getMockAuthorizationManager();
        XWikiContext context = this.oldcore.getXWikiContext();
        DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "Foo");
        context.setUserReference(userReference);
        DocumentReference currentDocReference = mock(DocumentReference.class, "currentDocRef");
        XWikiDocument currentDoc = mock(XWikiDocument.class);
        when(currentDoc.getAuthors()).thenReturn(documentAuthors);
        Document document = new Document(currentDoc, context);

        when(currentDoc.getDocumentReference()).thenReturn(currentDocReference);

        when(mockAuthorizationManager.hasAccess(Right.PROGRAM, userReference, currentDocReference)).thenReturn(false);
        DocumentAuthors obtainedAuthors = document.getAuthors();
        assertTrue(obtainedAuthors instanceof SafeDocumentAuthors);
        assertEquals(new SafeDocumentAuthors(documentAuthors), obtainedAuthors);

        verify(mockAuthorizationManager).hasAccess(Right.PROGRAM, userReference, currentDocReference);

        when(mockAuthorizationManager.hasAccess(Right.PROGRAM, userReference, currentDocReference)).thenReturn(true);
        when(currentDoc.clone()).thenReturn(currentDoc);
        obtainedAuthors = document.getAuthors();
        assertSame(documentAuthors, obtainedAuthors);
        verify(mockAuthorizationManager, times(2)).hasAccess(Right.PROGRAM, userReference, currentDocReference);
        verify(currentDoc).clone();
    }