-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Java: CWE-502 Unsafe JSON deserialization with Gson, Flexjson, Jabsorb and JoddJson #5954
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
luchua-bc
wants to merge
9
commits into
github:main
from
luchua-bc:java/unsafe-gson-flexjson-joddjson-deserialization
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5149b91
Rebase on main
luchua-bc bda72f8
Remove non-ASCII characters
luchua-bc d93c96f
Optimize the query
luchua-bc 6a04776
Update the query and add comments
luchua-bc 42948c6
Update the query for Flexjson
luchua-bc 102f95d
Optimize the query and update qhelp file
luchua-bc 662dc22
Change the flow of Android Gson query
luchua-bc cff33a6
Replace final class type check to null check
luchua-bc 2a59ebc
Update the query for Flexjson and qldoc
luchua-bc 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
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,3 @@ | ||
lgtm,codescanning | ||
* The "Deserialization of user-controlled data" (`java/unsafe-deserialization`) query | ||
now recognizes deserialization of `Gson`, `Flexjson`, `JoddJson`, and `Jabsorb`. |
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
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
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,48 @@ | ||
/** | ||
* Provides classes for working with the Flexjson framework. | ||
*/ | ||
|
||
import java | ||
|
||
/** The class `flexjson.JSONDeserializer`. */ | ||
class FlexjsonDeserializer extends RefType { | ||
FlexjsonDeserializer() { this.hasQualifiedName("flexjson", "JSONDeserializer") } | ||
} | ||
|
||
/** The class `flexjson.JSONSerializer`. */ | ||
class FlexjsonSerializer extends RefType { | ||
FlexjsonSerializer() { this.hasQualifiedName("flexjson", "JSONSerializer") } | ||
} | ||
|
||
/** The class `flexjson.ObjectFactory`. */ | ||
class FlexjsonObjectFactory extends RefType { | ||
FlexjsonObjectFactory() { this.hasQualifiedName("flexjson", "ObjectFactory") } | ||
} | ||
|
||
/** The deserialization method `deserialize`. */ | ||
class FlexjsonDeserializeMethod extends Method { | ||
FlexjsonDeserializeMethod() { | ||
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof | ||
FlexjsonDeserializer and | ||
this.getName() = "deserialize" and | ||
not this.getAParameter().getType() instanceof FlexjsonObjectFactory // deserialization method with specified class types in object factory is unlikely to be vulnerable | ||
} | ||
} | ||
|
||
/** The serialization method `serialize`. */ | ||
class FlexjsonSerializeMethod extends Method { | ||
FlexjsonSerializeMethod() { | ||
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof | ||
FlexjsonSerializer and | ||
this.hasName(["serialize", "deepSerialize"]) | ||
} | ||
} | ||
|
||
/** The method `use` to configure allowed class type. */ | ||
class DeserializerUseMethod extends Method { | ||
DeserializerUseMethod() { | ||
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof | ||
FlexjsonDeserializer and | ||
this.hasName("use") | ||
} | ||
} |
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,26 @@ | ||
/** | ||
* Provides classes for working with the Jabsorb JSON-RPC ORB framework. | ||
*/ | ||
|
||
import java | ||
|
||
/** The class `org.jabsorb.JSONSerializer`. */ | ||
class JabsorbSerializer extends RefType { | ||
JabsorbSerializer() { this.hasQualifiedName("org.jabsorb", "JSONSerializer") } | ||
} | ||
|
||
/** The deserialization method `unmarshall`. */ | ||
class JabsorbUnmarshallMethod extends Method { | ||
JabsorbUnmarshallMethod() { | ||
this.getDeclaringType().getASupertype*() instanceof JabsorbSerializer and | ||
this.getName() = "unmarshall" | ||
luchua-bc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
/** The deserialization method `fromJSON`. */ | ||
class JabsorbFromJsonMethod extends Method { | ||
JabsorbFromJsonMethod() { | ||
this.getDeclaringType().getASupertype*() instanceof JabsorbSerializer and | ||
this.getName() = "fromJSON" | ||
} | ||
} |
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,67 @@ | ||
/** | ||
* Provides classes and predicates for working with the JoddJson framework. | ||
*/ | ||
|
||
import java | ||
|
||
/** The class `jodd.json.Parser`. */ | ||
class JoddJsonParser extends RefType { | ||
JoddJsonParser() { this.hasQualifiedName("jodd.json", "JsonParser") } | ||
} | ||
|
||
/** The class `jodd.json.JsonSerializer`. */ | ||
class JoddJsonSerializer extends RefType { | ||
JoddJsonSerializer() { this.hasQualifiedName("jodd.json", "JsonSerializer") } | ||
} | ||
|
||
/** The `parse*` deserialization method. */ | ||
class JoddJsonParseMethod extends Method { | ||
JoddJsonParseMethod() { | ||
this.getDeclaringType() instanceof JoddJsonParser and | ||
this.getName().matches("parse%") | ||
} | ||
} | ||
|
||
/** The serialization method `serialize`. */ | ||
class JoddJsonSerializeMethod extends Method { | ||
JoddJsonSerializeMethod() { | ||
this.getDeclaringType() instanceof JoddJsonSerializer and | ||
this.hasName("serialize") | ||
} | ||
} | ||
|
||
/** The `setClassMetadataName` method. */ | ||
class SetClassMetadataNameMethod extends Method { | ||
SetClassMetadataNameMethod() { | ||
this.getDeclaringType() instanceof JoddJsonParser and | ||
this.hasName("setClassMetadataName") | ||
} | ||
} | ||
|
||
/** A call to `parser.withClassMetadata` method. */ | ||
class WithClassMetadata extends MethodAccess { | ||
WithClassMetadata() { | ||
this.getMethod().getDeclaringType() instanceof JoddJsonParser and | ||
this.getMethod().hasName("withClassMetadata") | ||
} | ||
|
||
/** Gets the constant value passed to this call. */ | ||
boolean getMode() { result = this.getArgument(0).(CompileTimeConstantExpr).getBooleanValue() } | ||
} | ||
|
||
/** | ||
* Holds if there is a call to `parser.withClassMetadata` that explicitly enables | ||
* class metadata. | ||
*/ | ||
predicate enablesClassMetadata(WithClassMetadata wcm) { wcm.getMode() = true } | ||
|
||
/** A call to `parser.allowClass` method. */ | ||
class SetWhitelistClasses extends MethodAccess { | ||
SetWhitelistClasses() { | ||
this.getMethod().getDeclaringType() instanceof JoddJsonParser and | ||
this.getMethod().hasName("allowClass") | ||
} | ||
|
||
/** Gets the configured value. */ | ||
Expr getValue() { result = this.getArgument(0) } | ||
} |
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
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
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,47 @@ | ||
/** | ||
* Provides classes for working with the Gson framework. | ||
*/ | ||
|
||
import java | ||
|
||
/** The class `com.google.gson.Gson`. */ | ||
class Gson extends RefType { | ||
Gson() { this.hasQualifiedName("com.google.gson", "Gson") } | ||
} | ||
|
||
/** The class `com.google.gson.GsonBuilder`. */ | ||
class GsonBuilder extends RefType { | ||
GsonBuilder() { this.hasQualifiedName("com.google.gson", "GsonBuilder") } | ||
} | ||
|
||
/** A method that registers class types in `GsonBuilder`. */ | ||
class RegisterClassTypeMethod extends Method { | ||
RegisterClassTypeMethod() { | ||
this.getDeclaringType() instanceof GsonBuilder and | ||
this.getName().matches("register%") | ||
} | ||
} | ||
|
||
/** The `create` method of `GsonBuilder`. */ | ||
class CreateGsonMethod extends Method { | ||
CreateGsonMethod() { | ||
this.getDeclaringType() instanceof GsonBuilder and | ||
this.hasName("create") | ||
} | ||
} | ||
|
||
/** The `fromJson` deserialization method. */ | ||
class GsonDeserializeMethod extends Method { | ||
GsonDeserializeMethod() { | ||
this.getDeclaringType() instanceof Gson and | ||
this.hasName("fromJson") | ||
} | ||
} | ||
|
||
/** The `toJson` serialization method. */ | ||
class GsonSerializeMethod extends Method { | ||
GsonSerializeMethod() { | ||
this.getDeclaringType() instanceof Gson and | ||
this.hasName("toJson") | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.