File size: 10,017 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.
* <p>
*/
package org.olat.core.commons.modules.bc.commands;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.olat.core.commons.modules.bc.FileSelection;
import org.olat.core.commons.modules.bc.components.FolderComponent;
import org.olat.core.commons.services.notifications.NotificationsManager;
import org.olat.core.commons.services.notifications.SubscriptionContext;
import org.olat.core.commons.services.vfs.VFSMetadata;
import org.olat.core.commons.services.vfs.VFSRepositoryService;
import org.olat.core.commons.services.vfs.model.VFSMetadataImpl;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.controller.BasicController;
import org.olat.core.gui.control.generic.modal.DialogBoxController;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity;
import org.olat.core.logging.AssertException;
import org.olat.core.util.StringHelper;
import org.olat.core.util.ZipUtil;
import org.olat.core.util.vfs.Quota;
import org.olat.core.util.vfs.VFSConstants;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.VFSManager;
import org.olat.core.util.vfs.callbacks.VFSSecurityCallback;
import org.springframework.beans.factory.annotation.Autowired;
public class CmdUnzip extends BasicController implements FolderCommand {
private int status = FolderCommandStatus.STATUS_SUCCESS;
private Translator translator;
private DialogBoxController lockedFiledCtr;
@Autowired
private VFSRepositoryService vfsRepositoryService;
@Autowired
private NotificationsManager notificationsManager;
public CmdUnzip(UserRequest ureq, WindowControl wControl) {
super(ureq, wControl);
}
@Override
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wContr, Translator trans) {
this.translator = trans;
// BUG: CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
// FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
// FIXED:
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainer(), folderComponent.getCurrentContainerPath());
VFSContainer currentContainer = folderComponent.getCurrentContainer();
if (currentContainer.canWrite() != VFSConstants.YES)
throw new AssertException("Cannot unzip to folder. Writing denied.");
//check if command is executed on a file containing invalid filenames or paths - checks if the resulting folder has a valid name
if(selection.getInvalidFileNames().size()>0) {
status = FolderCommandStatus.STATUS_INVALID_NAME;
return null;
}
List<String> lockedFiles = new ArrayList<>();
for (String sItem:selection.getFiles()) {
VFSItem vfsItem = currentContainer.resolve(sItem);
if (vfsItem instanceof VFSLeaf) {
try {
lockedFiles.addAll(checkLockedFiles((VFSLeaf)vfsItem, currentContainer, ureq.getIdentity()));
} catch (Exception e) {
String name = vfsItem == null ? "NULL" : vfsItem.getName();
getWindowControl().setError(translator.translate("FileUnzipFailed", new String[]{name}));
}
}
}
if(!lockedFiles.isEmpty()) {
String msg = FolderCommandHelper.renderLockedMessageAsHtml(trans, lockedFiles);
List<String> buttonLabels = Collections.singletonList(trans.translate("ok"));
lockedFiledCtr = activateGenericDialog(ureq, trans.translate("lock.title"), msg, buttonLabels, lockedFiledCtr);
return null;
}
VFSItem currentVfsItem = null;
try {
boolean fileNotExist = false;
for (String sItem:selection.getFiles()) {
currentVfsItem = currentContainer.resolve(sItem);
if (currentVfsItem instanceof VFSLeaf) {
if (!doUnzip((VFSLeaf)currentVfsItem, currentContainer, ureq, wContr)) {
status = FolderCommandStatus.STATUS_FAILED;
break;
}
} else {
fileNotExist = true;
break;
}
}
if (fileNotExist) {
status = FolderCommandStatus.STATUS_FAILED;
getWindowControl().setError(translator.translate("FileDoesNotExist"));
}
VFSContainer inheritingCont = VFSManager.findInheritingSecurityCallbackContainer(folderComponent.getRootContainer());
if(inheritingCont != null) {
VFSSecurityCallback secCallback = inheritingCont.getLocalSecurityCallback();
if(secCallback != null) {
SubscriptionContext subsContext = secCallback.getSubscriptionContext();
if (subsContext != null) {
notificationsManager.markPublisherNews(subsContext, ureq.getIdentity(), true);
}
}
}
} catch (IllegalArgumentException e) {
logError("Corrupted ZIP", e);
String name = currentVfsItem == null ? "NULL" : currentVfsItem.getName();
getWindowControl().setError(translator.translate("FileUnzipFailed", new String[]{name}));
}
return null;
}
private List<String> checkLockedFiles(VFSLeaf vfsItem, VFSContainer currentContainer, Identity identity) {
String name = vfsItem.getName();
if (!name.toLowerCase().endsWith(".zip")) {
return Collections.emptyList();
}
if(currentContainer.canVersion() != VFSConstants.YES) {
//this command don't overwrite existing folders
return Collections.emptyList();
}
String sZipContainer = name.substring(0, name.length() - 4);
VFSItem zipContainer = currentContainer.resolve(sZipContainer);
if(zipContainer == null) {
return Collections.emptyList();
} else if (zipContainer instanceof VFSContainer) {
return ZipUtil.checkLockedFileBeforeUnzipNonStrict(vfsItem, (VFSContainer)zipContainer, identity);
} else {
//replace a file with a folder ???
return Collections.emptyList();
}
}
private boolean doUnzip(VFSLeaf vfsItem, VFSContainer currentContainer, UserRequest ureq, WindowControl wControl) {
String name = vfsItem.getName();
if (!name.toLowerCase().endsWith(".zip")) {
wControl.setError(translator.translate("FileUnzipFailed", new String[] {vfsItem.getName()}));
return false;
}
// we make a new folder with the same name as the zip file
String sZipContainer = name.substring(0, name.length() - 4);
boolean versioning = currentContainer.canVersion() == VFSConstants.YES;
VFSContainer zipContainer = currentContainer.createChildContainer(sZipContainer);
if (zipContainer == null) {
if(versioning) {
VFSItem resolvedItem = currentContainer.resolve(sZipContainer);
if(resolvedItem instanceof VFSContainer) {
zipContainer = (VFSContainer)resolvedItem;
} else {
String numberedFilename = findContainerName(currentContainer, sZipContainer);
if(StringHelper.containsNonWhitespace(numberedFilename)) {
zipContainer = currentContainer.createChildContainer(numberedFilename);
}
if(zipContainer == null) {// we try our best
wControl.setError(translator.translate("unzip.alreadyexists", new String[] {sZipContainer}));
return false;
}
}
} else {
// folder already exists... issue warning
wControl.setError(translator.translate("unzip.alreadyexists", new String[] {sZipContainer}));
return false;
}
} else if (zipContainer.canMeta() == VFSConstants.YES) {
VFSMetadata info = zipContainer.getMetaInfo();
if(info instanceof VFSMetadataImpl) {
((VFSMetadataImpl)info).setFileInitializedBy(ureq.getIdentity());
vfsRepositoryService.updateMetadata(info);
}
}
if (!ZipUtil.unzipNonStrict(vfsItem, zipContainer, ureq.getIdentity(), versioning)) {
// operation failed - rollback
zipContainer.delete();
wControl.setError(translator.translate("failed"));
return false;
} else {
// check quota
long quotaLeftKB = VFSManager.getQuotaLeftKB(currentContainer);
if (quotaLeftKB != Quota.UNLIMITED && quotaLeftKB < 0) {
// quota exceeded - rollback
zipContainer.delete();
wControl.setError(translator.translate("QuotaExceeded"));
return false;
}
}
return true;
}
private String findContainerName(VFSContainer container, String filename) {
String newName = filename;
VFSItem newFile = container.resolve(newName);
for(int count=1; newFile != null && count < 999 ; count++) {
newName = filename + "_" + count;
newFile = container.resolve(newName);
}
if(newFile == null) {
return newName;
}
return null;
}
@Override
public int getStatus() {
return status;
}
@Override
public boolean runsModal() {
return false;
}
@Override
public String getModalTitle() {
return null;
}
@Override
protected void doDispose() {
//autodisposed by BasicController
}
@Override
protected void event(UserRequest ureq, Component source, Event event) {
// no events to catch
}
@Override
public void event(UserRequest ureq, Controller source, Event event) {
// no events to catch
}
}
|