CVE-2022-23620: Improper Encoding or Escaping of Output (CWE-116)
value: XWiki Platform is a generic wiki platform offering runtime services for applications built on top of it. In affected versions AbstractSxExportURLFactoryActionHandler#processSx does not escape anything from SSX document references when serializing it on filesystem, it is possible to for the HTML export process to contain reference elements containing filesystem syntax like "../", "./". or "/" in general. The referenced elements are not properly escaped. This issue has been resolved in version 13.6-rc-1. This issue can be worked around by limiting or disabling document export.
CWE description: The product prepares a structured message for communication with another component, but encoding or escaping of the data is either missing or done incorrectly. As a result, the intended structure of the message is not preserved.
CVSS v3 base score: 5.4
Published: 2022-02-09T22:15Z
Repository: https://github.com/xwiki/xwiki-platform
Language: Java
Files changed: xwiki-platform-core/xwiki-platform-skin/xwiki-platform-skin-skinx/src/main/java/org/xwiki/skinx/internal/AbstractSxExportURLFactoryActionHandler.java
Commit date: 2021-07-07 16:03:10 +0200
Commit message: XWIKI-18819: It's possible to save pretty much anything anywhere by creating and using an SSX/JSX containing "../" in its reference

Diff: diff --git a/xwiki-platform-core/xwiki-platform-skin/xwiki-platform-skin-skinx/src/main/java/org/xwiki/skinx/internal/AbstractSxExportURLFactoryActionHandler.java b/xwiki-platform-core/xwiki-platform-skin/xwiki-platform-skin-skinx/src/main/java/org/xwiki/skinx/internal/AbstractSxExportURLFactoryActionHandler.java
index 0b9af4e39b9d..29ff0d1db61c 100644
--- a/xwiki-platform-core/xwiki-platform-skin/xwiki-platform-skin-skinx/src/main/java/org/xwiki/skinx/internal/AbstractSxExportURLFactoryActionHandler.java
+++ b/xwiki-platform-core/xwiki-platform-skin/xwiki-platform-skin-skinx/src/main/java/org/xwiki/skinx/internal/AbstractSxExportURLFactoryActionHandler.java
@@ -98,17 +98,17 @@ public URL createURL(String spaces, String name, String queryString, String anch
 
         Map<String, Object> backup = new HashMap<>();
         XWikiDocument.backupContext(backup, context);
         try {
             sxDocument.setAsContextDoc(context);
-            return processSx(spaceNames, name, queryString, context, exportContext);
+            return processSx(sxDocument.getId(), queryString, context, exportContext);
         } finally {
             XWikiDocument.restoreContext(backup, context);
         }
     }
 
-    private URL processSx(List<String> spaceNames, String name, String queryString, XWikiContext context,
+    private URL processSx(long id, String queryString, XWikiContext context,
         FilesystemExportContext exportContext) throws Exception
     {
         SxSource sxSource = null;
 
         // Check if we have the JAR_RESOURCE_REQUEST_PARAMETER parameter in the query string
@@ -126,11 +126,11 @@ private URL processSx(List<String> spaceNames, String name, String queryString,
 
         String content = getContent(sxSource, exportContext);
 
         // Write the content to file
         // We need a unique name for that SSX content
-        String targetPath = String.format("%s/%s/%s", getSxPrefix(), StringUtils.join(spaceNames, '/'), name);
+        String targetPath = String.format("%s/%s", getSxPrefix(), id);
         File targetDirectory = new File(exportContext.getExportDir(), targetPath);
         if (!targetDirectory.exists()) {
             targetDirectory.mkdirs();
         }
         File targetLocation = File.createTempFile(getSxPrefix(), "." + getFileSuffix(), targetDirectory);
@@ -144,33 +144,29 @@ private URL processSx(List<String> spaceNames, String name, String queryString,
         // subdirectories, we need to compute the path to them.
         path.append(StringUtils.repeat("../", exportContext.getDocParentLevel()));
 
         path.append(getSxPrefix());
         path.append(URL_PATH_SEPARATOR);
-        for (String spaceName : spaceNames) {
-            path.append(encodeURLPart(spaceName));
-            path.append(URL_PATH_SEPARATOR);
-        }
-        path.append(encodeURLPart(name));
+        path.append(id);
         path.append(URL_PATH_SEPARATOR);
         path.append(encodeURLPart(targetLocation.getName()));
 
         return new URL(path.toString());
     }
 
     protected String getContent(SxSource sxSource, FilesystemExportContext exportContext)
     {
         String content;
 
-        // We know we're inside a SX file located at "<S|J>sx/<Space>/<Page>/<s|j>sx<NNN>.<css|js>". Inside this CSS
+        // We know we're inside a SX file located at "<S|J>sx/<id>/<s|j>sx<NNN>.<css|js>". Inside this CSS
         // there can be URLs and we need to ensure that the prefix for these URLs lead to the root of the path, i.e.
-        // 3 levels up ("../../../").
+        // 3 levels up ("../../").
         // To make this happen we reuse the Doc Parent Level from FileSystemExportContext to a fixed value of 3.
         // We also make sure to put back the original value
         int originalDocParentLevel = exportContext.getDocParentLevel();
         try {
-            exportContext.setDocParentLevels(3);
+            exportContext.setDocParentLevels(2);
             content = sxSource.getContent();
         } finally {
             exportContext.setDocParentLevels(originalDocParentLevel);
         }
 

Vulnerable code:             return processSx(spaceNames, name, queryString, context, exportContext);
    private URL processSx(List<String> spaceNames, String name, String queryString, XWikiContext context,
        String targetPath = String.format("%s/%s/%s", getSxPrefix(), StringUtils.join(spaceNames, '/'), name);
        for (String spaceName : spaceNames) {
            path.append(encodeURLPart(spaceName));
            path.append(URL_PATH_SEPARATOR);
        }
        path.append(encodeURLPart(name));
        // We know we're inside a SX file located at "<S|J>sx/<Space>/<Page>/<s|j>sx<NNN>.<css|js>". Inside this CSS
        // 3 levels up ("../../../").
            exportContext.setDocParentLevels(3);
Fixed code:             return processSx(sxDocument.getId(), queryString, context, exportContext);
    private URL processSx(long id, String queryString, XWikiContext context,
        String targetPath = String.format("%s/%s", getSxPrefix(), id);
        path.append(id);
        // We know we're inside a SX file located at "<S|J>sx/<id>/<s|j>sx<NNN>.<css|js>". Inside this CSS
        // 3 levels up ("../../").
            exportContext.setDocParentLevels(2);