CVE-2021-39180 (Java, published 2021-08-31T18:15Z)
Description: value: OpenOLAT is a web-based learning management system (LMS). A path traversal vulnerability exists in versions prior to 15.3.18, 15.5.3, and 16.0.0. Using a specially prepared ZIP file, it is possible to overwrite any file that is writable by the application server user (e.g. the tomcat user). Depending on the configuration this can be limited to files of the OpenOlat user data directory, however, if not properly set up, the attack could also be used to overwrite application server config files, java code or even operating system files. The attack could be used to corrupt or modify any OpenOlat file such as course structures, config files or temporary test data. Those attack would require in-depth knowledge of the installation and thus more theoretical. If the app server configuration allows the execution of jsp files and the path to the context is known, it is also possible to execute java code. If the app server runs with the same user that is used to deploy the OpenOlat code or has write permissions on the OpenOlat code files and the path to the context is know, code injection is possible. The attack requires an OpenOlat user account to upload a ZIP file and trigger the unzip method. It can not be exploited by unregistered users. The problem is fixed in versions 15.3.18, 15.5.3 and 16.0.0. There are no known workarounds aside from upgrading.
Severity: HIGH (CVSS v3 base score: 8.8)
Weakness: CWE-22 \u2014 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
Repository: https://github.com/OpenOLAT/OpenOLAT
Files changed:
src/main/java/org/olat/core/util/PathUtils.java
src/main/java/org/olat/fileresource/types/FileResource.java
src/main/java/org/olat/fileresource/types/ImsCPFileResource.java
src/main/java/org/olat/fileresource/types/ImsQTI21Resource.java
src/main/java/org/olat/fileresource/types/ScormCPFileResource.java
src/main/java/org/olat/modules/wiki/WikiManager.java
src/test/java/org/olat/modules/ims/cp/ImsCPHandlerTest.java
src/test/java/org/olat/modules/wiki/WikiManagerTest.java
src/test/java/org/olat/test/AllTestsJunit4.java
Commit message: OO-5549: fix the wiki import and add some unit tests

Vulnerable code:
	 * Use the closeSubsequentFS method to close the file system.
		    Files.walkFileTree(fPath, visitor);
			IOUtils.closeQuietly(path.getFileSystem());
import org.olat.core.id.OLATResourceable;
		Files.walkFileTree(fPath, rootSearcher);
			Files.walkFileTree(path, new CopyVisitor(path, destDir, filter));
				Files.walkFileTree(fPath, rootSearcher);
				Files.walkFileTree(fPath, rootSearcher);
				Files.walkFileTree(fPath, rootSearcher);
			Files.walkFileTree(path, new ImportVisitor(destDir));
			Path normalizedPath = file.normalize();
			if(!normalizedPath.startsWith(destDir)) {
				throw new IOException("Invalid ZIP");
			}
			

Fixed code:
import java.nio.file.FileVisitOption;
import java.util.EnumSet;
import org.olat.core.logging.OLATRuntimeException;
	 * Use the closeSubsequentFS method to close the file system. The method doesn't
	 * follow sym. links and its depth is limited.
		    Files.walkFileTree(fPath, EnumSet.noneOf(FileVisitOption.class), 32, visitor);
			IOUtils.closeQuietly(path.getFileSystem(), null);
	        Path normalizedPath = destFile.normalize();
			if(!normalizedPath.startsWith(destDir)) {
				throw new OLATRuntimeException("Invalid ZIP");
			}
import java.nio.file.FileVisitOption;
import java.util.EnumSet;
import org.olat.core.id.OLATResourceable;
		Files.walkFileTree(fPath, EnumSet.noneOf(FileVisitOption.class), 16, rootSearcher);
			Files.walkFileTree(path, EnumSet.noneOf(FileVisitOption.class), 24, new CopyVisitor(path, destDir, filter));
import java.nio.file.FileVisitOption;
import java.util.EnumSet;
				Files.walkFileTree(fPath, EnumSet.noneOf(FileVisitOption.class), 16, rootSearcher);
import java.nio.file.FileVisitOption;
import java.util.EnumSet;
				Files.walkFileTree(fPath, EnumSet.noneOf(FileVisitOption.class), 16, rootSearcher);
import java.nio.file.FileVisitOption;
import java.util.EnumSet;
				Files.walkFileTree(fPath, EnumSet.noneOf(FileVisitOption.class), 16, rootSearcher);
import java.nio.file.FileVisitOption;
import java.util.EnumSet;
			Files.walkFileTree(path, EnumSet.noneOf(FileVisitOption.class), 16, new ImportVisitor(destDir));

	        	checkDestinationFile(destFile);
	        	checkDestinationFile(destFile);
				checkDestinationFile(destFile);
		
		private void checkDestinationFile(Path destFile) throws IOException {
			Path normalizedPath = destFile.normalize();
			if(!normalizedPath.startsWith(destDir)) {
				throw new OLATRuntimeException("Invalid ZIP");
			}
		}
package org.olat.modules.ims.cp;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Locale;

import org.junit.Assert;
import org.junit.Test;
import org.olat.core.id.Identity;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.fileresource.FileResourceManager;
import org.olat.fileresource.types.ImsCPFileResource;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.handlers.RepositoryHandler;
import org.olat.repository.handlers.RepositoryHandlerFactory;
import org.olat.test.JunitTestHelper;
import org.olat.test.OlatTestCase;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * Initial date: 22 juin 2021<br>
 * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
 *
 */
public class ImsCPHandlerTest extends OlatTestCase {
	
	@Autowired
	private RepositoryHandlerFactory handlerFactory;
	
	@Test
	public void importImsCP() throws URISyntaxException {
		Identity author = JunitTestHelper.createAndPersistIdentityAsRndAuthor("ims-cp-1");
		URL imsCpUrl = ImsCPHandlerTest.class.getResource("imscp.zip");
		File imsCpFile = new File(imsCpUrl.toURI());
		
		RepositoryHandler cpHandler = handlerFactory.getRepositoryHandler(ImsCPFileResource.TYPE_NAME);	
		RepositoryEntry entry = cpHandler.importResource(author, null, "IMS CP", null, false, null, Locale.ENGLISH, imsCpFile, imsCpFile.getName());
		Assert.assertNotNull(entry);
		
		File cpRoot = FileResourceManager.getInstance().unzipFileResource(entry.getOlatResource());
		File image = new File(cpRoot, "IMG_1482.jpg");
		Assert.assertTrue(image.exists());
		File manifestXml = new File(cpRoot, "imsmanifest.xml");
		Assert.assertTrue(manifestXml.exists());
		File page = new File(cpRoot, "new.html");
		Assert.assertTrue(page.exists());
	}
	
	@Test(expected=OLATRuntimeException.class)
	public void importImsCPSlide() throws URISyntaxException {
		Identity author = JunitTestHelper.createAndPersistIdentityAsRndAuthor("ims-cp-1");
		URL imsCpUrl = ImsCPHandlerTest.class.getResource("imscp_alt.zip");
		File imsCpFile = new File(imsCpUrl.toURI());
		
		RepositoryHandler cpHandler = handlerFactory.getRepositoryHandler(ImsCPFileResource.TYPE_NAME);	
		cpHandler.importResource(author, null, "IMS CP", null, false, null, Locale.ENGLISH, imsCpFile, imsCpFile.getName());
	}

}
package org.olat.modules.wiki;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.util.CodeHelper;
import org.olat.core.util.FileUtils;
import org.olat.core.util.WebappHelper;
import org.olat.test.OlatTestCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.wildfly.common.Assert;

/**
 * 
 * Initial date: 22 juin 2021<br>
 * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
 *
 */
public class WikiManagerTest extends OlatTestCase {
	
	@Autowired
	private WikiManager wikiManager;
	
	private File tmpWikiDir;
	
	@Before
	public void createTmpDir() {
		tmpWikiDir = new File(WebappHelper.getTmpDir(), "wiki" + CodeHelper.getForeverUniqueID());
	}
	
	@After
	public void deleteTmpDir() {
		FileUtils.deleteDirsAndFiles(tmpWikiDir, true, true);
	}
	
	@Test
	public void importWiki() throws URISyntaxException {
		URL wikiUrl = WikiManagerTest.class.getResource("wiki.zip");
		File wikiFile = new File(wikiUrl.toURI());
		wikiManager.importWiki(wikiFile, null, tmpWikiDir);
		
		File image = new File(tmpWikiDir, "media/IMG_1482.jpg");
		Assert.assertTrue(image.exists());
		File imageMetadata = new File(tmpWikiDir, "media/IMG_1482.jpg.metadata");
		Assert.assertTrue(imageMetadata.exists());
		File indexPage = new File(tmpWikiDir, "wiki/SW5kZXg=.wp");
		Assert.assertTrue(indexPage.exists());
	}
	
	@Test(expected=OLATRuntimeException.class)
	public void importWikiSlide() throws URISyntaxException {
		URL wikiUrl = WikiManagerTest.class.getResource("wiki_alt.zip");
		File wikiFile = new File(wikiUrl.toURI());
		wikiManager.importWiki(wikiFile, null, tmpWikiDir);
	}

}
	org.olat.modules.wiki.WikiManagerTest.class,
	org.olat.modules.ims.cp.ImsCPHandlerTest.class,