Skip to content

Commit

Permalink
Fix issue #458
Browse files Browse the repository at this point in the history
  • Loading branch information
rygel committed Oct 25, 2018
1 parent cf25f49 commit a82347d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ro.pippo.session;

import java.util.ArrayList;

/**
* @author idealzh
*/
public class ClassFilter {
private ArrayList<String> WhiteList= null;
public ClassFilter() {
WhiteList=new ArrayList<String>();
WhiteList.add("ro.pippo.session.SessionData");
WhiteList.add("java.util.HashMap");
WhiteList.add("ro.pippo.core.Flash");
WhiteList.add("java.util.ArrayList");
}

public boolean isWhiteListed(String className) {
if (className==null) return false;
for(String name:WhiteList) {
if(name.equals(className)) return true;
} return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ro.pippo.session;

import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;

/**
* @author idealzh
*/
public class FilteringObjectInputStream extends ObjectInputStream {
public FilteringObjectInputStream(InputStream in) throws IOException {
super(in);
}

protected Class<?> resolveClass(java.io.ObjectStreamClass descriptor) throws ClassNotFoundException, IOException {
String className = descriptor.getName();
ClassFilter classFilter = new ClassFilter();
if(className != null && className.length() > 0 && !classFilter.isWhiteListed(className)) {
throw new InvalidClassException("Unauthorized deserialization attempt", descriptor.getName());
} else {
return super.resolveClass(descriptor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String encode(SessionData sessionData) {
public SessionData decode(String data) {
byte[] bytes = Base64.getDecoder().decode(data);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
FilteringObjectInputStream objectInputStream = new FilteringObjectInputStream(inputStream)) {
return (SessionData) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new PippoRuntimeException(e, "Cannot deserialize session. A new one will be created.");
Expand Down

0 comments on commit a82347d

Please sign in to comment.