Skip to content

Commit cdb59eb

Browse files
committed
Added some controls on the classes that can be deserialized; Bumped up some plugin dependencies, and some dependencies; Fixed some javadoc issues; Fixed some Maven issues
1 parent 88cb553 commit cdb59eb

File tree

19 files changed

+1553
-78
lines changed

19 files changed

+1553
-78
lines changed

mina-core/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<packaging>bundle</packaging>
3333

3434
<dependencies>
35+
<!-- Test dependencies -->
3536
<dependency>
3637
<groupId>org.easymock</groupId>
3738
<artifactId>easymock</artifactId>
@@ -52,6 +53,7 @@
5253
<Export-Package>
5354
org.apache.mina.core;version=${project.version};-noimport:=true,
5455
org.apache.mina.core.buffer;version=${project.version};-noimport:=true,
56+
org.apache.mina.core.buffer.matcher;version=${project.version};-noimport:=true,
5557
org.apache.mina.core.file;version=${project.version};-noimport:=true,
5658
org.apache.mina.core.filterchain;version=${project.version};-noimport:=true,
5759
org.apache.mina.core.future;version=${project.version};-noimport:=true,

mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,17 @@
4343
import java.nio.charset.CharsetEncoder;
4444
import java.nio.charset.CoderResult;
4545
import java.nio.charset.StandardCharsets;
46+
import java.util.ArrayList;
4647
import java.util.EnumSet;
48+
import java.util.List;
4749
import java.util.Set;
50+
import java.util.regex.Pattern;
51+
52+
import org.apache.mina.core.buffer.matcher.ClassNameMatcher;
53+
import org.apache.mina.core.buffer.matcher.FullClassNameMatcher;
54+
import org.apache.mina.core.buffer.matcher.RegexpClassNameMatcher;
55+
import org.apache.mina.core.buffer.matcher.WildcardClassNameMatcher;
56+
4857

4958
/**
5059
* A base implementation of {@link IoBuffer}. This implementation assumes that
@@ -80,6 +89,8 @@ public abstract class AbstractIoBuffer extends IoBuffer {
8089
/** A mask for an int */
8190
private static final long INT_MASK = 0xFFFFFFFFL;
8291

92+
private final List<ClassNameMatcher> acceptMatchers = new ArrayList<>();
93+
8394
/**
8495
* We don't have any access to Buffer.markValue(), so we need to track it down,
8596
* which will cause small extra overhead.
@@ -2154,18 +2165,22 @@ public Object getObject(final ClassLoader classLoader) throws ClassNotFoundExcep
21542165
@Override
21552166
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
21562167
int type = read();
2168+
21572169
if (type < 0) {
21582170
throw new EOFException();
21592171
}
2172+
21602173
switch (type) {
2161-
case 0: // NON-Serializable class or Primitive types
2162-
return super.readClassDescriptor();
2163-
case 1: // Serializable class
2164-
String className = readUTF();
2165-
Class<?> clazz = Class.forName(className, true, classLoader);
2166-
return ObjectStreamClass.lookup(clazz);
2167-
default:
2168-
throw new StreamCorruptedException("Unexpected class descriptor type: " + type);
2174+
case 0: // NON-Serializable class or Primitive types
2175+
return super.readClassDescriptor();
2176+
2177+
case 1: // Serializable class
2178+
String className = readUTF();
2179+
Class<?> clazz = Class.forName(className, true, classLoader);
2180+
return ObjectStreamClass.lookup(clazz);
2181+
2182+
default:
2183+
throw new StreamCorruptedException("Unexpected class descriptor type: " + type);
21692184
}
21702185
}
21712186

@@ -2181,7 +2196,21 @@ protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, Clas
21812196
return super.resolveClass(desc);
21822197
}
21832198
} else {
2184-
return clazz;
2199+
boolean found = false;
2200+
String className = desc.getName();
2201+
2202+
for (ClassNameMatcher matcher : acceptMatchers) {
2203+
if (matcher.matches(className)) {
2204+
found = true;
2205+
break;
2206+
}
2207+
}
2208+
2209+
if (found) {
2210+
return clazz;
2211+
}
2212+
2213+
throw new ClassNotFoundException();
21852214
}
21862215
}
21872216
}) {
@@ -2737,4 +2766,62 @@ private static void checkFieldSize(int fieldSize) {
27372766
throw new IllegalArgumentException("fieldSize cannot be negative: " + fieldSize);
27382767
}
27392768
}
2769+
2770+
/**
2771+
* Accept the specified classes for deserialization, unless they
2772+
* are otherwise rejected.
2773+
*
2774+
* @param classes Classes to accept
2775+
* @return this object
2776+
*/
2777+
public IoBuffer accept(Class<?>... classes) {
2778+
for (Class<?> clazz:classes) {
2779+
acceptMatchers.add(new FullClassNameMatcher(clazz.getName()));
2780+
}
2781+
2782+
return this;
2783+
}
2784+
2785+
/**
2786+
* {@inheritDoc}
2787+
*/
2788+
@Override
2789+
public IoBuffer accept(ClassNameMatcher m) {
2790+
acceptMatchers.add(m);
2791+
2792+
return this;
2793+
}
2794+
2795+
/**
2796+
* {@inheritDoc}
2797+
*/
2798+
@Override
2799+
public IoBuffer accept(Pattern pattern) {
2800+
acceptMatchers.add(new RegexpClassNameMatcher(pattern));
2801+
2802+
return this;
2803+
}
2804+
2805+
/**
2806+
* {@inheritDoc}
2807+
*/
2808+
@Override
2809+
public IoBuffer accept(String... patterns) {
2810+
for (String pattern:patterns) {
2811+
acceptMatchers.add(new WildcardClassNameMatcher(pattern));
2812+
}
2813+
2814+
return this;
2815+
}
2816+
2817+
/**
2818+
* {@inheritDoc}
2819+
*/
2820+
public void setMatchers(List<ClassNameMatcher> matchers) {
2821+
acceptMatchers.clear();
2822+
2823+
for (ClassNameMatcher matcher:matchers) {
2824+
acceptMatchers.add(matcher);
2825+
}
2826+
}
27402827
}

mina-core/src/main/java/org/apache/mina/core/buffer/IoBuffer.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
import java.nio.charset.CharsetDecoder;
3636
import java.nio.charset.CharsetEncoder;
3737
import java.util.EnumSet;
38+
import java.util.List;
3839
import java.util.Set;
40+
import java.util.regex.Pattern;
3941

42+
import org.apache.mina.core.buffer.matcher.ClassNameMatcher;
4043
import org.apache.mina.core.session.IoSession;
4144

4245
/**
@@ -2108,4 +2111,39 @@ public abstract IoBuffer putPrefixedString(CharSequence val, int prefixLength, i
21082111
* @return the modified IoBuffer
21092112
*/
21102113
public abstract <E extends Enum<E>> IoBuffer putEnumSetLong(int index, Set<E> set);
2114+
2115+
/**
2116+
* Accept class names where the supplied ClassNameMatcher matches for
2117+
* deserialization, unless they are otherwise rejected.
2118+
*
2119+
* @param m the matcher to use
2120+
* @return this object
2121+
*/
2122+
public abstract IoBuffer accept(ClassNameMatcher m);
2123+
2124+
/**
2125+
* Accept class names that match the supplied pattern for
2126+
* deserialization, unless they are otherwise rejected.
2127+
*
2128+
* @param pattern standard Java regexp
2129+
* @return this object
2130+
*/
2131+
public abstract IoBuffer accept(Pattern pattern);
2132+
2133+
/**
2134+
* Accept the wildcard specified classes for deserialization,
2135+
* unless they are otherwise rejected.
2136+
*
2137+
* @param patterns Wildcard file name patterns as defined by
2138+
* {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String) FilenameUtils.wildcardMatch}
2139+
* @return this object
2140+
*/
2141+
public abstract IoBuffer accept(String... patterns);
2142+
2143+
/**
2144+
* Set the list of class matchers for in incoming buffer
2145+
*
2146+
* @param matchers The list of matchers
2147+
*/
2148+
public abstract void setMatchers(List<ClassNameMatcher> matchers);
21112149
}

mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
import java.nio.charset.CharacterCodingException;
3434
import java.nio.charset.CharsetDecoder;
3535
import java.nio.charset.CharsetEncoder;
36+
import java.util.List;
3637
import java.util.Set;
38+
import java.util.regex.Pattern;
39+
40+
import org.apache.mina.core.buffer.matcher.ClassNameMatcher;
41+
import org.apache.mina.core.buffer.matcher.RegexpClassNameMatcher;
42+
import org.apache.mina.core.buffer.matcher.WildcardClassNameMatcher;
3743

3844
/**
3945
* A {@link IoBuffer} that wraps a buffer and proxies any operations to it.
@@ -1535,4 +1541,35 @@ public IoBuffer putUnsigned(int index, long value) {
15351541
buf.putUnsigned(index, value);
15361542
return this;
15371543
}
1544+
1545+
/**
1546+
* {@inheritDoc}
1547+
*/
1548+
@Override
1549+
public IoBuffer accept(ClassNameMatcher m) {
1550+
return buf.accept(m);
1551+
}
1552+
1553+
/**
1554+
* {@inheritDoc}
1555+
*/
1556+
@Override
1557+
public IoBuffer accept(Pattern pattern) {
1558+
return buf.accept(pattern);
1559+
}
1560+
1561+
/**
1562+
* {@inheritDoc}
1563+
*/
1564+
@Override
1565+
public IoBuffer accept(String... patterns) {
1566+
return buf.accept(patterns);
1567+
}
1568+
1569+
/**
1570+
* {@inheritDoc}
1571+
*/
1572+
public void setMatchers(List<ClassNameMatcher> matchers) {
1573+
buf.setMatchers(matchers);
1574+
}
15381575
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.mina.core.buffer.matcher;
20+
21+
/**
22+
* An object that matches a Class name to a condition.
23+
*/
24+
public interface ClassNameMatcher {
25+
/**
26+
* Returns {@code true} if the supplied class name matches this object's condition.
27+
*
28+
* @param className fully qualified class name
29+
* @return {@code true} if the class name matches this object's condition
30+
*/
31+
boolean matches(String className);
32+
}
33+

0 commit comments

Comments
 (0)