CVE-2021-41242 (Java, published 2021-12-10T23:15Z)
Description: value: OpenOlat is a web-basedlearning management system. A path traversal vulnerability exists in OpenOlat prior to versions 15.5.12 and 16.0.5. By providing a filename that contains a relative path as a parameter in some REST methods, it is possible to create directory structures and write files anywhere on the target system. The attack could be used to write files anywhere in the web root folder or outside, depending on the configuration of the system and the properly configured permission of the application server user. The attack requires an OpenOlat user account, an enabled REST API and the rights on a business object to call the vulnerable REST calls. The problem is fixed in version 15.5.12 and 16.0.5. There is a workaround available. The vulnerability requires the REST module to be enabled. Disabling the REST module or limiting the REST module via some firewall or web-server access rules to be accessed only be trusted systems will mitigate the risk.
Severity: HIGH (CVSS v3 base score: 8.1)
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/vfs/LocalFolderImpl.java
src/main/java/org/olat/core/util/vfs/VFSContainer.java
src/main/java/org/olat/modules/fo/restapi/ForumWebService.java
src/main/java/org/olat/restapi/repository/course/CourseElementWebService.java
src/main/java/org/olat/restapi/support/MultipartReader.java
src/test/java/org/olat/restapi/ForumTest.java
Commit message: OO-5819: container can only create file in its own path

Vulnerable code:
		if (!fNewFile.mkdir()) return null;
				log.warn("Could not create a new leaf::" + name + " in container::" + getBasefile().getAbsolutePath() + " - file alreay exists");
			log.error("Error while creating child leaf::" + name + " in container::" + getBasefile().getAbsolutePath(), e);
	 * @return VFSItem if successfull, null otherwise.
	 * @return VFSItem if successfull, null otherwise.
	protected Response attachToPost(Long messageKey, String filename, InputStream file,  HttpServletRequest request) {
	protected Response attachToPost(Message mess, String filename, InputStream file,  HttpServletRequest request) {
			VFSLeaf singleFile = (VFSLeaf) taskFolder.resolve("/" + filename);
				singleFile = taskFolder.createChildLeaf("/" + filename);
					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve("/" + filename);
						singleFile = rootContainer.createChildLeaf("/" + filename);
					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve("/" + filename);
			VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve("/" + filename);
				singleFile = rootContainer.createChildLeaf("/" + filename);
					if(filename != null) {

Fixed code:
		if(!isInPath(name)) {
			log.warn("Could not create a new container::{} in container::{} - file out of parent directory", name, getBasefile().getAbsolutePath());
			return null;
		}
		if (!fNewFile.mkdir()) {
			return null;
		}
			if(!isInPath(name)) {
				log.warn("Could not create a new leaf::{} in container::{} - file out of parent directory", name, getBasefile().getAbsolutePath());
				return null;
			}
				log.warn("Could not create a new leaf::{} in container::{} - file alreay exists", name, getBasefile().getAbsolutePath());
			log.error("Error while creating child leaf::{} in container::{}", name, getBasefile().getAbsolutePath(), e);
	 * @return VFSItem if successful, null otherwise.
	 * @return VFSItem if successful, null otherwise.
	private Response attachToPost(Long messageKey, String filename, InputStream file,  HttpServletRequest request) {
	private Response attachToPost(Message mess, String filename, InputStream file,  HttpServletRequest request) {
			VFSLeaf singleFile = (VFSLeaf)taskFolder.resolve(filename);
				singleFile = taskFolder.createChildLeaf(filename);
					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve(filename);
						singleFile = rootContainer.createChildLeaf(filename);
					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve(filename);
			VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve(filename);
				singleFile = rootContainer.createChildLeaf(filename);
					if(filename != null && !filename.contains("..")) {
	@Test
	public void testUploadAttachmentOutOfBox() throws IOException, URISyntaxException {
		RestConnection conn = new RestConnection();
		assertTrue(conn.login(id1));
		
		URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString())
			.queryParam("authorKey", id1.getKey())
			.queryParam("title", "New message with attachment ")
			.queryParam("body", "A very interesting response in Thread-1 with an attachment").build();
		HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
		HttpResponse response = conn.execute(method);
		assertEquals(200, response.getStatusLine().getStatusCode());
		MessageVO message = conn.parse(response, MessageVO.class);
		assertNotNull(message);
		
		//attachment
		URL portraitUrl = CoursesElementsTest.class.getResource("portrait.jpg");
		assertNotNull(portraitUrl);
		File portrait = new File(portraitUrl.toURI());
		
		//upload portrait
		URI attachUri = getForumUriBuilder().path("posts").path(message.getKey().toString()).path("attachments").build();
		HttpPost attachMethod = conn.createPost(attachUri, MediaType.APPLICATION_JSON);
		conn.addMultipart(attachMethod, "../../portrait.jpg", portrait);
		HttpResponse attachResponse = conn.execute(attachMethod);
		assertEquals(200, attachResponse.getStatusLine().getStatusCode());
		
		//check if the file exists
		VFSContainer container = forumManager.getMessageContainer(message.getForumKey(), message.getKey());
		VFSItem uploadedFile = container.resolve("portrait.jpg");
		Assert.assertNull(uploadedFile);
		
		conn.shutdown();
	}