CVE-2023-37911 (published 2023-10-25T18:17Z)

XWiki Platform is a generic wiki platform offering runtime services for applications built on top of it. Starting in version 9.4-rc-1 and prior to versions 14.10.8 and 15.3-rc-1, when a document has been deleted and re-created, it is possible for users with view right on the re-created document but not on the deleted document to view the contents of the deleted document. Such a situation might arise when rights were added to the deleted document. This can be exploited through the diff feature and, partially, through the REST API by using versions such as `deleted:1` (where the number counts the deletions in the wiki and is thus guessable). Given sufficient rights, the attacker can also re-create the deleted document, thus extending the scope to any deleted document as long as the attacker has edit right in the location of the deleted document. This vulnerability has been patched in XWiki 14.10.8 and 15.3 RC1 by properly checking rights when deleted revisions of a document are accessed. The only workaround is to regularly clean deleted documents to minimize the potential exposure. Extra care should be taken when deleting sensitive documents that are protected individually (and not, e.g., by being placed in a protected space) or deleting a protected space as a whole.

CVSS v3 base score: 6.5, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-668
cwe_name: Exposure of Resource to Wrong Sphere
description: The product exposes a resource to the wrong control sphere, providing unintended actors with inappropriate access to the resource.
extended_description: 
            Resources such as files and directories may be inadvertently exposed through mechanisms such as insecure permissions, or when a program accidentally operates on the wrong object. For example, a program may intend that private files can only be provided to a specific user. This effectively defines a control sphere that is intended to prevent attackers from accessing these private files. If the file permissions are insecure, then parties other than the user will be able to access those files.
            A separate control sphere might effectively require that the user can only access the private files, but not any other files on the system. If the program does not ensure that the user is only requesting private files, then the user might be able to access other files on the system.
            In either case, the end result is that a resource has been exposed to the wrong party.
         
         

Fix information:
repo_url: https://github.com/xwiki/xwiki-platform
commit_details: repo_url: https://github.com/xwiki/xwiki-platform
author: Michael Hamann
author_date: 2023-04-04 17:54:34+02:00
msg: XWIKI-20685: Check access on the revision provider in the script API
repository: repo_url: https://github.com/xwiki/xwiki-platform
repo_name: xwiki/xwiki-platform
description: The XWiki platform
repo_language: Java
owner: xwiki
file_changes: filename: Document.java
change_type: ModificationType.MODIFY
diff: @@ -31,6 +31,7 @@
 import java.util.Vector;
 
 import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,6 +53,7 @@
 import org.xwiki.rendering.block.XDOM;
 import org.xwiki.rendering.parser.ParseException;
 import org.xwiki.rendering.syntax.Syntax;
+import org.xwiki.security.authorization.AuthorizationException;
 import org.xwiki.security.authorization.Right;
 import org.xwiki.stability.Unstable;
 import org.xwiki.user.CurrentUserReference;
@@ -3165,20 +3167,26 @@ public void rename(DocumentReference newReference, List<DocumentReference> backl
      * Allow to easily access any revision of a document
      *
      * @param revision the version to access
-     * @return the document corresponding to the requested revision or {@code null} if the revision does not exist.
+     * @return the document corresponding to the requested revision or {@code null} if the revision does not exist or
+     * access is denied.
      */
     public Document getDocumentRevision(String revision)
     {
         try {
-            XWikiDocument documentRevision = getDocumentRevisionProvider().getRevision(this.doc, revision);
+            DocumentRevisionProvider revisionProvider = getDocumentRevisionProvider();
+            revisionProvider.checkAccess(Right.VIEW, CurrentUserReference.INSTANCE, getDocumentReference(), revision);
+            XWikiDocument documentRevision = revisionProvider.getRevision(this.doc, revision);
 
             return documentRevision != null ? new Document(documentRevision, this.context) : null;
+        } catch (AuthorizationException e) {
+            LOGGER.info("Access denied for loading revision [{}] of document [{}]: [{}]", revision,
+                getDocumentReferenceWithLocale(), ExceptionUtils.getRootCauseMessage(e));
         } catch (Exception e) {
             LOGGER.error("Failed to load revision [{}] of document [{}]", revision, getDocumentReferenceWithLocale(),
                 e);
-
-            return null;
         }
+
+        return null;
     }
 
     /**

programming_language: Java
filename: XWiki.java
change_type: ModificationType.MODIFY
diff: @@ -46,8 +46,10 @@
 import org.xwiki.model.reference.WikiReference;
 import org.xwiki.rendering.renderer.PrintRendererFactory;
 import org.xwiki.rendering.syntax.Syntax;
+import org.xwiki.security.authorization.AuthorizationException;
 import org.xwiki.security.authorization.ContextualAuthorizationManager;
 import org.xwiki.security.authorization.Right;
+import org.xwiki.user.CurrentUserReference;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
@@ -658,12 +660,17 @@ public Document getDocument(DocumentReference reference, String revision) throws
     {
         try {
             if (reference != null && getContextualAuthorizationManager().hasAccess(Right.VIEW, reference)) {
-                XWikiDocument documentRevision = getDocumentRevisionProvider().getRevision(reference, revision);
+                DocumentRevisionProvider revisionProvider = getDocumentRevisionProvider();
+                revisionProvider.checkAccess(Right.VIEW, CurrentUserReference.INSTANCE, reference, revision);
+                XWikiDocument documentRevision = revisionProvider.getRevision(reference, revision);
 
                 if (documentRevision != null) {
                     return new Document(documentRevision, this.context);
                 }
             }
+        } catch (AuthorizationException e) {
+            LOGGER.info("Access denied for loading revision [{}] of document [{}]: [{}]", revision, reference,
+                ExceptionUtils.getRootCauseMessage(e));
         } catch (Exception e) {
             LOGGER.error("Failed to access revision [{}] of document {}", revision, reference, e);
         }

programming_language: Java
filename: DocumentTest.java
change_type: ModificationType.MODIFY
diff: @@ -22,6 +22,9 @@
 import java.util.List;
 
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.xwiki.component.manager.ComponentLookupException;
 import org.xwiki.component.util.DefaultParameterizedType;
 import org.xwiki.model.document.DocumentAuthors;
@@ -29,8 +32,11 @@
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.model.reference.ObjectReference;
 import org.xwiki.observation.ObservationManager;
+import org.xwiki.security.authorization.AuthorizationException;
 import org.xwiki.security.authorization.AuthorizationManager;
 import org.xwiki.security.authorization.Right;
+import org.xwiki.test.LogLevel;
+import org.xwiki.test.junit5.LogCaptureExtension;
 import org.xwiki.test.junit5.mockito.MockComponent;
 import org.xwiki.test.mockito.MockitoComponentManager;
 import org.xwiki.user.CurrentUserReference;
@@ -39,6 +45,7 @@
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.doc.DocumentRevisionProvider;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
 import com.xpn.xwiki.objects.BaseProperty;
@@ -56,6 +63,7 @@
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.isNull;
 import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
@@ -74,6 +82,9 @@
     @MockComponent
     private ObservationManager observationManager;
 
+    @RegisterExtension
+    private LogCaptureExtension logCapture = new LogCaptureExtension(LogLevel.INFO);
+
     @Test
     void toStringReturnsFullName()
     {
@@ -390,4 +401,32 @@ void getAuthors()
         verify(mockAuthorizationManager, times(2)).hasAccess(Right.PROGRAM, userReference, currentDocReference);
         verify(currentDoc).clone();
     }
+
+    @ParameterizedTest
+    @ValueSource(booleans = { false, true })
+    void getDocumentRevision(boolean allowAccess, MockitoComponentManager componentManager) throws Exception
+    {
+        DocumentReference documentReference = new DocumentReference("Wiki", "Space", "Page");
+        XWikiDocument xWikiDocument = new XWikiDocument(documentReference);
+        Document document = new Document(xWikiDocument, this.oldcore.getXWikiContext());
+        DocumentRevisionProvider revisionProvider =
+            componentManager.registerMockComponent(DocumentRevisionProvider.class);
+        String revision = "42.1";
+        XWikiDocument revisionDocument = mock(XWikiDocument.class);
+        when(revisionProvider.getRevision(xWikiDocument, revision)).thenReturn(revisionDocument);
+        String deniedMessage = "Denied";
+        if (!allowAccess) {
+            doThrow(new AuthorizationException(deniedMessage)).when(revisionProvider)
+                .checkAccess(Right.VIEW, CurrentUserReference.INSTANCE, documentReference, revision);
+            assertNull(document.getDocumentRevision(revision));
+            assertEquals(1, this.logCapture.size());
+            assertEquals(String.format("Access denied for loading revision [%s] of document [%s()]: "
+                    + "[AuthorizationException: %s]", revision,
+                documentReference, deniedMessage), this.logCapture.getMessage(0));
+        } else {
+            assertEquals(new Document(revisionDocument, this.oldcore.getXWikiContext()),
+                document.getDocumentRevision(revision));
+        }
+        verify(revisionProvider).checkAccess(Right.VIEW, CurrentUserReference.INSTANCE, documentReference, revision);
+    }
 }

programming_language: Java
filename: XWikiTest.java
change_type: ModificationType.MODIFY
diff: @@ -23,10 +23,15 @@
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.observation.ObservationManager;
 import org.xwiki.rendering.renderer.PrintRendererFactory;
 import org.xwiki.rendering.syntax.Syntax;
+import org.xwiki.security.authorization.AuthorizationException;
+import org.xwiki.security.authorization.ContextualAuthorizationManager;
+import org.xwiki.security.authorization.Right;
 import org.xwiki.test.junit5.mockito.MockComponent;
 import org.xwiki.test.mockito.MockitoComponentManager;
 import org.xwiki.user.CurrentUserReference;
@@ -34,6 +39,7 @@
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.doc.DocumentRevisionProvider;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.doc.XWikiDocumentArchive;
 import com.xpn.xwiki.test.MockitoOldcore;
@@ -45,6 +51,9 @@
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 /**
@@ -56,6 +65,8 @@
 @ReferenceComponentList
 public class XWikiTest
 {
+    private static final DocumentReference DOCUMENT_REFERENCE = new DocumentReference("xwiki", "MilkyWay", "Fidis");
+
     @MockComponent
     private UserReferenceResolver<CurrentUserReference> currentUserReferenceUserReferenceResolver;
 
@@ -78,8 +89,7 @@ public void setup(MockitoOldcore mockitoOldcore) throws XWikiException
             .thenReturn(new XWikiDocumentArchive());
 
         xWikiContext.setUser("Redtail");
-        this.apiDocument =
-            new Document(new XWikiDocument(new DocumentReference("xwiki", "MilkyWay", "Fidis")), xWikiContext);
+        this.apiDocument = new Document(new XWikiDocument(DOCUMENT_REFERENCE), xWikiContext);
         this.apiDocument.getDocument().setCreator("c" + xWikiContext.getUser());
         this.apiDocument.getDocument().setAuthor("a" + xWikiContext.getUser());
         this.apiDocument.save();
@@ -134,4 +144,34 @@ public void getAvailableRendererSyntax(MockitoComponentManager componentManager)
         assertNull(this.apiXWiki.getAvailableRendererSyntax("plai", "1.0"));
         assertNull(this.apiXWiki.getAvailableRendererSyntax("plai", null));
     }
+
+    @ParameterizedTest
+    @ValueSource(booleans = { false, true })
+    void getDocumentRevision(boolean allowAccess, MockitoOldcore mockitoOldcore) throws Exception
+    {
+        DocumentRevisionProvider revisionProvider =
+            mockitoOldcore.getMocker().registerMockComponent(DocumentRevisionProvider.class);
+        ContextualAuthorizationManager contextualAuthorizationManager =
+            mockitoOldcore.getMockContextualAuthorizationManager();
+
+        XWikiDocument deletedDocument = new XWikiDocument(DOCUMENT_REFERENCE);
+        deletedDocument.setContent("Deleted");
+        String revision = "deleted:1";
+        when(revisionProvider.getRevision(DOCUMENT_REFERENCE, revision)).thenReturn(deletedDocument);
+
+        when(contextualAuthorizationManager.hasAccess(Right.VIEW, DOCUMENT_REFERENCE)).thenReturn(true);
+
+        if (!allowAccess) {
+            doThrow(new AuthorizationException("Denied")).when(revisionProvider)
+                .checkAccess(Right.VIEW, CurrentUserReference.INSTANCE, DOCUMENT_REFERENCE, revision);
+            assertNull(this.apiXWiki.getDocument(DOCUMENT_REFERENCE, revision));
+        } else {
+            assertEquals(new Document(deletedDocument, mockitoOldcore.getXWikiContext()),
+                this.apiXWiki.getDocument(DOCUMENT_REFERENCE, revision));
+        }
+
+        verify(revisionProvider, times(allowAccess ? 1 : 0)).getRevision(DOCUMENT_REFERENCE, revision);
+        verify(revisionProvider).checkAccess(Right.VIEW, CurrentUserReference.INSTANCE, DOCUMENT_REFERENCE, revision);
+        verify(contextualAuthorizationManager).hasAccess(Right.VIEW, DOCUMENT_REFERENCE);
+    }
 }

programming_language: Java