001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.util; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.ObjectInputStream; 022import java.io.ObjectStreamClass; 023import java.lang.reflect.Proxy; 024import java.util.Arrays; 025import java.util.Collection; 026import java.util.Map; 027 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031public class ClassLoadingAwareObjectInputStream extends ObjectInputStream { 032 033 private static final Logger LOG = LoggerFactory.getLogger(ClassLoadingAwareObjectInputStream.class); 034 private static final ClassLoader FALLBACK_CLASS_LOADER = 035 ClassLoadingAwareObjectInputStream.class.getClassLoader(); 036 037 public static final String[] serializablePackages; 038 039 private final ClassLoader inLoader; 040 041 static { 042 serializablePackages = System.getProperty("org.apache.activemq.SERIALIZABLE_PACKAGES", 043 "java.lang,javax.security,java.util,org.apache.activemq,org.fusesource.hawtbuf,com.thoughtworks.xstream.mapper").split(","); 044 } 045 046 public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException { 047 super(in); 048 inLoader = in.getClass().getClassLoader(); 049 } 050 051 @Override 052 protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException { 053 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 054 Class clazz = load(classDesc.getName(), cl, inLoader); 055 checkSecurity(clazz); 056 return clazz; 057 } 058 059 @Override 060 protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException { 061 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 062 Class[] cinterfaces = new Class[interfaces.length]; 063 for (int i = 0; i < interfaces.length; i++) { 064 cinterfaces[i] = load(interfaces[i], cl); 065 } 066 067 Class clazz = null; 068 try { 069 clazz = Proxy.getProxyClass(cl, cinterfaces); 070 } catch (IllegalArgumentException e) { 071 try { 072 clazz = Proxy.getProxyClass(inLoader, cinterfaces); 073 } catch (IllegalArgumentException e1) { 074 // ignore 075 } 076 try { 077 clazz = Proxy.getProxyClass(FALLBACK_CLASS_LOADER, cinterfaces); 078 } catch (IllegalArgumentException e2) { 079 // ignore 080 } 081 } 082 083 if (clazz != null) { 084 checkSecurity(clazz); 085 return clazz; 086 } else { 087 throw new ClassNotFoundException(null); 088 } 089 } 090 091 public static boolean isAllAllowed() { 092 return serializablePackages.length == 1 && serializablePackages[0].equals("*"); 093 } 094 095 private void checkSecurity(Class clazz) throws ClassNotFoundException { 096 if (!clazz.isPrimitive()) { 097 if (clazz.getPackage() != null && !isAllAllowed()) { 098 boolean found = false; 099 for (String packageName : serializablePackages) { 100 if (clazz.getPackage().getName().equals(packageName) || clazz.getPackage().getName().startsWith(packageName + ".")) { 101 found = true; 102 break; 103 } 104 } 105 106 if (!found) { 107 throw new ClassNotFoundException("Forbidden " + clazz + "! This class is not allowed to be serialized. Add package with 'org.apache.activemq.SERIALIZABLE_PACKAGES' system property."); 108 } 109 } 110 } 111 } 112 113 private Class<?> load(String className, ClassLoader... cl) throws ClassNotFoundException { 114 // check for simple types first 115 final Class<?> clazz = loadSimpleType(className); 116 if (clazz != null) { 117 LOG.trace("Loaded class: {} as simple type -> ", className, clazz); 118 return clazz; 119 } 120 121 // try the different class loaders 122 for (ClassLoader loader : cl) { 123 LOG.trace("Attempting to load class: {} using classloader: {}", className, cl); 124 try { 125 Class<?> answer = Class.forName(className, false, loader); 126 if (LOG.isTraceEnabled()) { 127 LOG.trace("Loaded class: {} using classloader: {} -> ", new Object[]{className, cl, answer}); 128 } 129 return answer; 130 } catch (ClassNotFoundException e) { 131 LOG.trace("Class not found: {} using classloader: {}", className, cl); 132 // ignore 133 } 134 } 135 136 // and then the fallback class loader 137 return Class.forName(className, false, FALLBACK_CLASS_LOADER); 138 } 139 140 /** 141 * Load a simple type 142 * 143 * @param name the name of the class to load 144 * @return the class or <tt>null</tt> if it could not be loaded 145 */ 146 public static Class<?> loadSimpleType(String name) { 147 // code from ObjectHelper.loadSimpleType in Apache Camel 148 149 // special for byte[] or Object[] as its common to use 150 if ("java.lang.byte[]".equals(name) || "byte[]".equals(name)) { 151 return byte[].class; 152 } else if ("java.lang.Byte[]".equals(name) || "Byte[]".equals(name)) { 153 return Byte[].class; 154 } else if ("java.lang.Object[]".equals(name) || "Object[]".equals(name)) { 155 return Object[].class; 156 } else if ("java.lang.String[]".equals(name) || "String[]".equals(name)) { 157 return String[].class; 158 // and these is common as well 159 } else if ("java.lang.String".equals(name) || "String".equals(name)) { 160 return String.class; 161 } else if ("java.lang.Boolean".equals(name) || "Boolean".equals(name)) { 162 return Boolean.class; 163 } else if ("boolean".equals(name)) { 164 return boolean.class; 165 } else if ("java.lang.Integer".equals(name) || "Integer".equals(name)) { 166 return Integer.class; 167 } else if ("int".equals(name)) { 168 return int.class; 169 } else if ("java.lang.Long".equals(name) || "Long".equals(name)) { 170 return Long.class; 171 } else if ("long".equals(name)) { 172 return long.class; 173 } else if ("java.lang.Short".equals(name) || "Short".equals(name)) { 174 return Short.class; 175 } else if ("short".equals(name)) { 176 return short.class; 177 } else if ("java.lang.Byte".equals(name) || "Byte".equals(name)) { 178 return Byte.class; 179 } else if ("byte".equals(name)) { 180 return byte.class; 181 } else if ("java.lang.Float".equals(name) || "Float".equals(name)) { 182 return Float.class; 183 } else if ("float".equals(name)) { 184 return float.class; 185 } else if ("java.lang.Double".equals(name) || "Double".equals(name)) { 186 return Double.class; 187 } else if ("double".equals(name)) { 188 return double.class; 189 } else if ("void".equals(name)) { 190 return void.class; 191 } 192 193 return null; 194 } 195 196}