Vulnerability: Path traversal in xwiki-platform-skin-skinx XWiki Platform is a generic wiki platform offering runtime services for applications built on top of it. AbstractSxExportURLFactoryActionHandler#processSx does not escape anything from SSX document reference when serializing it on filesystem, so it's easy to mess up the HTML export process with reference elements containing filesystem syntax like "../", "./". or "/" in general (the last two not causing any security threat, but can cause conflicts with others serialized files). Patch can be found in 13.6-rc-1. Giving script or subwiki admin right only to trusted people and disabling HTML/PDF export can be done as workaround.
Vulnerability ID: GHSA-7ph6-5cmq-xgjq
CWE: CWE-22
Severity score: 6.8
Project: https://github.com/xwiki/xwiki-platform
Published: 2022-02-09
File: xwiki-platform-core/xwiki-platform-skin/xwiki-platform-skin-skinx/src/main/java/org/xwiki/skinx/internal/AbstractSxExportURLFactoryActionHandler.java
Commit message: XWIKI-18819: It's possible to save pretty much anything anywhere by creating and using an SSX/JSX containing "../" in its reference
Commit author: Thomas Mortagne

Vulnerable version (before fix):
 public URL createURL(String spaces, String name, String queryString, String anch         XWikiDocument.backupContext(backup, context);         try {             sxDocument.setAsContextDoc(context);             return processSx(spaceNames, name, queryString, context, exportContext);         } finally {             XWikiDocument.restoreContext(backup, context);         }     }      private URL processSx(List<String> spaceNames, String name, String queryString, XWikiContext context,         FilesystemExportContext exportContext) throws Exception     {         SxSource sxSource = null; private URL processSx(List<String> spaceNames, String name, String queryString,          // 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);         File targetDirectory = new File(exportContext.getExportDir(), targetPath);         if (!targetDirectory.exists()) {             targetDirectory.mkdirs(); private URL processSx(List<String> spaceNames, String name, String queryString,          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(URL_PATH_SEPARATOR);         path.append(encodeURLPart(targetLocation.getName()));  protected String getContent(SxSource sxSource, FilesystemExportContext exportCon     {         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         // 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 ("../../../").         // 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);             content = sxSource.getContent();         } finally {             exportContext.setDocParentLevels(originalDocParentLevel);

Fixed version (after fix):
 public URL createURL(String spaces, String name, String queryString, String anch         XWikiDocument.backupContext(backup, context);         try {             sxDocument.setAsContextDoc(context);             return processSx(sxDocument.getId(), queryString, context, exportContext);         } finally {             XWikiDocument.restoreContext(backup, context);         }     }      private URL processSx(long id, String queryString, XWikiContext context,         FilesystemExportContext exportContext) throws Exception     {         SxSource sxSource = null; private URL processSx(List<String> spaceNames, String name, String queryString,          // Write the content to file         // We need a unique name for that SSX content         String targetPath = String.format("%s/%s", getSxPrefix(), id);         File targetDirectory = new File(exportContext.getExportDir(), targetPath);         if (!targetDirectory.exists()) {             targetDirectory.mkdirs(); private URL processSx(List<String> spaceNames, String name, String queryString,          path.append(getSxPrefix());         path.append(URL_PATH_SEPARATOR);         path.append(id);         path.append(URL_PATH_SEPARATOR);         path.append(encodeURLPart(targetLocation.getName()));  protected String getContent(SxSource sxSource, FilesystemExportContext exportCon     {         String content;          // 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 ("../../").         // 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(2);             content = sxSource.getContent();         } finally {             exportContext.setDocParentLevels(originalDocParentLevel);

Patch:
patch: @@ -100,13 +100,13 @@ public URL createURL(String spaces, String name, String queryString, String anch
         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;
@@ -128,7 +128,7 @@ private URL processSx(List<String> spaceNames, String name, String queryString,
 
         // 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();
@@ -146,11 +146,7 @@ private URL processSx(List<String> spaceNames, String name, String queryString,
 
         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()));
 
@@ -161,14 +157,14 @@ protected String getContent(SxSource sxSource, FilesystemExportContext exportCon
     {
         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);
path: xwiki-platform-core/xwiki-platform-skin/xwiki-platform-skin-skinx/src/main/java/org/xwiki/skinx/internal/AbstractSxExportURLFactoryActionHandler.java