File size: 5,825 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 |
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <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 the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <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>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.core.commons.services.vfs.manager;
import java.io.File;
import java.io.InputStream;
import org.olat.basesecurity.BaseSecurity;
import org.olat.basesecurity.IdentityImpl;
import org.olat.core.CoreSpringFactory;
import org.olat.core.commons.services.license.LicenseService;
import org.olat.core.commons.services.license.LicenseType;
import org.olat.core.commons.services.license.model.LicenseTypeImpl;
import org.olat.core.commons.services.vfs.VFSMetadata;
import org.olat.core.commons.services.vfs.VFSRevision;
import org.olat.core.commons.services.vfs.model.VFSMetadataImpl;
import org.olat.core.id.Identity;
import org.olat.core.util.StringHelper;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.version.RevisionFileImpl;
import org.olat.core.util.vfs.version.VersionsFileImpl;
import org.olat.core.util.xml.XStreamHelper;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.security.ExplicitTypePermission;
/**
*
* Initial date: 13 mars 2019<br>
* @author srosse, [email protected], http://www.frentix.com
*
*/
public class VFSXStream {
private static XStream mystream;
static {
mystream = XStreamHelper.createXStreamInstance();
Class<?>[] types = new Class[] {
VersionsFileImpl.class, RevisionFileImpl.class, VFSRevision.class,
VFSMetadata.class, VFSMetadataImpl.class, Identity.class, IdentityImpl.class,
LicenseType.class, LicenseTypeImpl.class
};
// BUG: CWE-91 XML Injection (aka Blind XPath Injection)
// XStream.setupDefaultSecurity(mystream);
// FIXED:
mystream.addPermission(new ExplicitTypePermission(types));
mystream.alias("versions", VersionsFileImpl.class);
mystream.alias("revision", RevisionFileImpl.class);
mystream.omitField(VersionsFileImpl.class, "currentVersion");
mystream.omitField(VersionsFileImpl.class, "versionFile");
mystream.omitField(RevisionFileImpl.class, "current");
mystream.omitField(RevisionFileImpl.class, "container");
mystream.omitField(RevisionFileImpl.class, "file");
mystream.omitField(VFSMetadataImpl.class, "originFile");
mystream.omitField(VFSMetadataImpl.class, "metaFile");
mystream.omitField(VFSMetadataImpl.class, "lockedByIdentKey");
mystream.aliasAttribute(VFSMetadataImpl.class, "cannotGenerateThumbnails", "cannotGenerateThumbnail");
mystream.aliasAttribute(VFSMetadataImpl.class, "author", "authorIdentKey");
mystream.aliasAttribute(VFSMetadataImpl.class, "licenseType", "licenseTypeKey");
mystream.alias("metadata", VFSMetadataImpl.class);
mystream.omitField(VFSMetadataImpl.class, "thumbnail");
mystream.omitField(VFSMetadataImpl.class, "thumbnails");
mystream.registerLocalConverter(VFSMetadataImpl.class, "licenseType", new LicenseTypeConverter());
mystream.registerLocalConverter(VFSMetadataImpl.class, "author", new IdentityConverter());
mystream.registerLocalConverter(RevisionFileImpl.class, "author", new IdentityConverter());
}
public static final Object read(InputStream in) {
return XStreamHelper.readObject(mystream, in);
}
public static final Object read(File file) {
return XStreamHelper.readObject(mystream, file);
}
public static final Object read(VFSLeaf leaf) {
return XStreamHelper.readObject(mystream, leaf);
}
public static final void write(VFSLeaf leaf, VersionsFileImpl versions) {
XStreamHelper.writeObject(mystream, leaf, versions);
}
private static class LicenseTypeConverter implements SingleValueConverter {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
return type.equals(LicenseType.class) || type.equals(LicenseTypeImpl.class);
}
@Override
public String toString(Object obj) {
if(obj instanceof LicenseType) {
Long key = ((LicenseType)obj).getKey();
return key == null ? null : key.toString();
}
return null;
}
@Override
public Object fromString(String str) {
return CoreSpringFactory.getImpl(LicenseService.class).loadLicenseTypeByKey(str);
}
}
private static class IdentityConverter implements SingleValueConverter {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
return type.equals(Identity.class) || type.equals(IdentityImpl.class);
}
@Override
public String toString(Object obj) {
if(obj instanceof Identity) {
Long key = ((Identity)obj).getKey();
return key == null ? null : key.toString();
}
return null;
}
@Override
public Object fromString(String str) {
Identity identity = null;
if(StringHelper.containsNonWhitespace(str) && !"-".equals(str)) {
if(StringHelper.isLong(str)) {
Long identityKey = Long.valueOf(str);
identity = CoreSpringFactory.getImpl(BaseSecurity.class).loadIdentityByKey(identityKey);
}
if(identity == null && !"-".equals(str)) {
identity = CoreSpringFactory.getImpl(BaseSecurity.class).findIdentityByName(str);
}
}
return identity;
}
}
}
|