Skip to content

Commit 070a723

Browse files
committed
added "acceptProxyClasses" flag to RemoteInvocationSerializingExporter
1 parent d4be29e commit 070a723

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

org.springframework.context/src/main/java/org/springframework/remoting/rmi/CodebaseAwareObjectInputStream.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ public class CodebaseAwareObjectInputStream extends ConfigurableObjectInputStrea
5757

5858
/**
5959
* Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
60-
* @param in the InputStream to read from
60+
* @param in the InputStream to read from
6161
* @param codebaseUrl the codebase URL to load classes from if not found locally
6262
* (can consist of multiple URLs, separated by spaces)
6363
* @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
@@ -68,7 +68,7 @@ public CodebaseAwareObjectInputStream(InputStream in, String codebaseUrl) throws
6868

6969
/**
7070
* Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
71-
* @param in the InputStream to read from
71+
* @param in the InputStream to read from
7272
* @param classLoader the ClassLoader to use for loading local classes
7373
* (may be <code>null</code> to indicate RMI's default ClassLoader)
7474
* @param codebaseUrl the codebase URL to load classes from if not found locally
@@ -82,6 +82,22 @@ public CodebaseAwareObjectInputStream(
8282
this.codebaseUrl = codebaseUrl;
8383
}
8484

85+
/**
86+
* Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
87+
* @param in the InputStream to read from
88+
* @param classLoader the ClassLoader to use for loading local classes
89+
* (may be <code>null</code> to indicate RMI's default ClassLoader)
90+
* @param acceptProxyClasses whether to accept deserialization of proxy classes
91+
* (may be deactivated as a security measure)
92+
* @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
93+
*/
94+
public CodebaseAwareObjectInputStream(
95+
InputStream in, ClassLoader classLoader, boolean acceptProxyClasses) throws IOException {
96+
97+
super(in, classLoader, acceptProxyClasses);
98+
this.codebaseUrl = null;
99+
}
100+
85101

86102
@Override
87103
protected Class resolveFallbackIfPossible(String className, ClassNotFoundException ex)

org.springframework.context/src/main/java/org/springframework/remoting/rmi/RemoteInvocationSerializingExporter.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,6 +57,8 @@ public abstract class RemoteInvocationSerializingExporter extends RemoteInvocati
5757

5858
private String contentType = CONTENT_TYPE_SERIALIZED_OBJECT;
5959

60+
private boolean acceptProxyClasses = true;
61+
6062
private Object proxy;
6163

6264

@@ -70,12 +72,27 @@ public void setContentType(String contentType) {
7072
}
7173

7274
/**
73-
* Return the content type to use for sending remote invocation responses.
75+
* Return the content type to use for sending remote invocation responses.
7476
*/
7577
public String getContentType() {
7678
return this.contentType;
7779
}
7880

81+
/**
82+
* Set whether to accept deserialization of proxy classes.
83+
* <p>Default is "true". May be deactivated as a security measure.
84+
*/
85+
public void setAcceptProxyClasses(boolean acceptProxyClasses) {
86+
this.acceptProxyClasses = acceptProxyClasses;
87+
}
88+
89+
/**
90+
* Return whether to accept deserialization of proxy classes.
91+
*/
92+
public boolean isAcceptProxyClasses() {
93+
return this.acceptProxyClasses;
94+
}
95+
7996

8097
public void afterPropertiesSet() {
8198
prepare();
@@ -102,7 +119,7 @@ protected final Object getProxy() {
102119
* @throws java.io.IOException if creation of the ObjectInputStream failed
103120
*/
104121
protected ObjectInputStream createObjectInputStream(InputStream is) throws IOException {
105-
return new CodebaseAwareObjectInputStream(is, getBeanClassLoader(), null);
122+
return new CodebaseAwareObjectInputStream(is, getBeanClassLoader(), isAcceptProxyClasses());
106123
}
107124

108125
/**

org.springframework.core/src/main/java/org/springframework/core/ConfigurableObjectInputStream.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21+
import java.io.NotSerializableException;
2122
import java.io.ObjectInputStream;
2223
import java.io.ObjectStreamClass;
2324
import java.lang.reflect.Proxy;
@@ -36,16 +37,33 @@ public class ConfigurableObjectInputStream extends ObjectInputStream {
3637

3738
private final ClassLoader classLoader;
3839

40+
private final boolean acceptProxyClasses;
41+
3942

4043
/**
4144
* Create a new ConfigurableObjectInputStream for the given InputStream and ClassLoader.
42-
* @param in the InputStream to read from
45+
* @param in the InputStream to read from
4346
* @param classLoader the ClassLoader to use for loading local classes
4447
* @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
4548
*/
4649
public ConfigurableObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException {
50+
this(in, classLoader, true);
51+
}
52+
53+
/**
54+
* Create a new ConfigurableObjectInputStream for the given InputStream and ClassLoader.
55+
* @param in the InputStream to read from
56+
* @param classLoader the ClassLoader to use for loading local classes
57+
* @param acceptProxyClasses whether to accept deserialization of proxy classes
58+
* (may be deactivated as a security measure)
59+
* @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
60+
*/
61+
public ConfigurableObjectInputStream(
62+
InputStream in, ClassLoader classLoader, boolean acceptProxyClasses) throws IOException {
63+
4764
super(in);
4865
this.classLoader = classLoader;
66+
this.acceptProxyClasses = acceptProxyClasses;
4967
}
5068

5169

@@ -68,6 +86,9 @@ protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, Cl
6886

6987
@Override
7088
protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
89+
if (!this.acceptProxyClasses) {
90+
throw new NotSerializableException("Not allowed to accept serialized proxy classes");
91+
}
7192
if (this.classLoader != null) {
7293
// Use the specified ClassLoader to resolve local proxy classes.
7394
Class[] resolvedInterfaces = new Class[interfaces.length];

0 commit comments

Comments
 (0)