Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 458 #470

Merged
merged 9 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### [Unreleased][unreleased]

#### Fixed
- [#458]: Java deserialization vulnerability in SerializationSessionDataTranscoder.decode()
rygel marked this conversation as resolved.
Show resolved Hide resolved

#### Changed
- [#466]: Updated `FastJson` to latest version 1.2.51
- [#466]: Updated `FastJSON` to latest version 1.2.51

#### Added

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.pippo.session;
rygel marked this conversation as resolved.
Show resolved Hide resolved

import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.util.ArrayList;

import static ro.pippo.core.util.StringUtils.isNullOrEmpty;

/**
* @author idealzh
*/
public class FilteringObjectInputStream extends ObjectInputStream {
rygel marked this conversation as resolved.
Show resolved Hide resolved
private ArrayList whiteClassNames = new ArrayList<String>();
rygel marked this conversation as resolved.
Show resolved Hide resolved

public FilteringObjectInputStream(InputStream in) throws IOException {
super(in);

whiteClassNames.add("ro.pippo.session.DefaultSessionData");
rygel marked this conversation as resolved.
Show resolved Hide resolved
whiteClassNames.add("java.util.HashMap");
whiteClassNames.add("ro.pippo.core.Flash");
whiteClassNames.add("java.util.ArrayList");
}

protected Class<?> resolveClass(ObjectStreamClass descriptor) throws ClassNotFoundException, IOException {
String className = descriptor.getName();
if (isNullOrEmpty(className) && !isWhiteListed(className)) {
throw new InvalidClassException("Unauthorized deserialization attempt", descriptor.getName());
} else {
return super.resolveClass(descriptor);
}
}

private boolean isWhiteListed(String className) {
if (className == null) return false;
rygel marked this conversation as resolved.
Show resolved Hide resolved
for (Object name : whiteClassNames) {
if (name.equals(className)) return true;
}
return false;
}
}
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