CVE-2023-29507 (Java, published 2023-04-16T07:15Z)
Description: 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.
Severity: nan (CVSS v3 base score: 7.2)
Weakness: NVD-CWE-Other \u2014 Other
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.
Repository: https://github.com/xwiki/xwiki-platform
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 message: XWIKI-20380: Add missing check on document authors


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