-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Added validation on Builder.build() to check if any matcher will #2533
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
Closed
JoeBeeton
wants to merge
1
commit into
FasterXML:2.11
from
JoeBeeton:BasicPolyMorphTypeValidatorBlockUnsafeClasses
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/com/fasterxml/jackson/databind/exc/InsecureTypeMatchException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.fasterxml.jackson.databind.exc; | ||
|
|
||
|
|
||
| /** | ||
| * Exception used when flagging a Matcher that will allow all subtypes of Object.class or Serializable.class | ||
| * As allowing such a wide array of classes to be deserialized will open the application up to security vulnerabilities | ||
| * and so should be avoided. | ||
| */ | ||
| public class InsecureTypeMatchException extends IllegalArgumentException { | ||
|
|
||
| public InsecureTypeMatchException(String msg) { | ||
| super(msg); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| package com.fasterxml.jackson.databind.jsontype; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.*; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import com.fasterxml.jackson.databind.JavaType; | ||
| import com.fasterxml.jackson.databind.JsonMappingException; | ||
| import com.fasterxml.jackson.databind.cfg.MapperConfig; | ||
| import com.fasterxml.jackson.databind.exc.InsecureTypeMatchException; | ||
|
|
||
| /** | ||
| * Standard {@link BasicPolymorphicTypeValidator} implementation that users may want | ||
|
|
@@ -64,6 +66,10 @@ protected abstract static class NameMatcher { | |
| * rules are checked. | ||
| */ | ||
| public static class Builder { | ||
|
|
||
| private static final List<Class<?>> invalidBaseClasses = Arrays.asList(Serializable.class,Object.class); | ||
|
|
||
|
|
||
| protected Set<Class<?>> _invalidBaseTypes; | ||
|
|
||
| /** | ||
|
|
@@ -84,6 +90,8 @@ public static class Builder { | |
|
|
||
| protected Builder() { } | ||
|
|
||
|
|
||
|
|
||
| // // Methods for checking solely by base type (before subtype even considered) | ||
|
|
||
| /** | ||
|
|
@@ -239,14 +247,75 @@ public boolean match(String clazzName) { | |
| }); | ||
| } | ||
|
|
||
| public BasicPolymorphicTypeValidator build() { | ||
| /** | ||
| * Validates and Constructs the BasicPolymorphicTypeValidator. | ||
| * Unlike build() no check is done to ensure the Matchers added to this validator will allow Object.class | ||
| * or Serializable.class and their subtypes to be deserialised. | ||
| * As the name suggests, this is an insecure operation. Please think very carefully before calling this method | ||
| * rather than build(), especially if the caller is outside the trust boundary of this application. As doing | ||
| * so will leave your application open to vulnerabilities including remote code execution. | ||
| * @return the BasicPolymorphicTypeValidator. | ||
| */ | ||
| public BasicPolymorphicTypeValidator build_insecure() { | ||
| return new BasicPolymorphicTypeValidator(_invalidBaseTypes, | ||
| (_baseTypeMatchers == null) ? null : _baseTypeMatchers.toArray(new TypeMatcher[0]), | ||
| (_subTypeNameMatchers == null) ? null : _subTypeNameMatchers.toArray(new NameMatcher[0]), | ||
| (_subTypeClassMatchers == null) ? null : _subTypeClassMatchers.toArray(new TypeMatcher[0]) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Validates and Constructs the BasicPolymorphicTypeValidator. | ||
| * If a Matcher added to this Validator will allow Object.class or Serializable.class and all of their sub types | ||
| * to be deserialised a InsecureTypeMatchException will be thrown. | ||
| * This is due to the inherent security problems of allowing either all classes on the class path to be | ||
| * arbitrarily deserialised in the case of Object.class or large amounts of classes in the case of | ||
| * Serializable.class. | ||
| * If you really need to allow either of these blocked classes. Use build_insecure(). However please be aware | ||
| * of the very real security problems you are introducing into your application by doing so. | ||
| * @return the BasicPolymorphicTypeValidator. | ||
| * @throws InsecureTypeMatchException if any matcher added will allow Object.class, Serializable.class | ||
| */ | ||
| public BasicPolymorphicTypeValidator build() throws InsecureTypeMatchException { | ||
| validateTypeMatch(); | ||
| return build_insecure(); | ||
| } | ||
|
|
||
| /** | ||
| * validates that each of the Matchers will not allow Serializable.class or Object.class. | ||
| * @throws InsecureTypeMatchException if an included Matcher will allow one or both of these classes through. | ||
| */ | ||
| private void validateTypeMatch() throws InsecureTypeMatchException { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Um, no. If user explicitly adds insecure base type, that should not be blocked. |
||
| if(_baseTypeMatchers!=null) { | ||
| for (TypeMatcher tm : _baseTypeMatchers) { | ||
| for (Class<?> klass : invalidBaseClasses) { | ||
| if (tm.match(klass)) { | ||
| throw new InsecureTypeMatchException("Insecure Base class found : " + klass.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if(_subTypeNameMatchers!=null) { | ||
| for (NameMatcher nm : _subTypeNameMatchers) { | ||
| for (Class<?> klass : invalidBaseClasses) { | ||
| if (nm.match(klass.getName())) { | ||
| throw new InsecureTypeMatchException("Insecure Base class found : " + klass.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if(_subTypeClassMatchers!=null) { | ||
| for (TypeMatcher tm : _subTypeClassMatchers) { | ||
| for (Class<?> klass : invalidBaseClasses) { | ||
| if (tm.match(klass)) { | ||
| throw new InsecureTypeMatchException("Insecure Base class found : " + klass.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| protected Builder _appendBaseMatcher(TypeMatcher matcher) { | ||
| if (_baseTypeMatchers == null) { | ||
| _baseTypeMatchers = new ArrayList<>(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build_insecure()is not following any naming convention, but in general I'll add my notes on why I don't think this is the way to go.