Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Java: Update qhelp: SnakeYaml is safe from version 2.0#20018

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
owen-mc wants to merge2 commits intogithub:main
base:main
Choose a base branch
Loading
fromowen-mc:java/snakeyaml-safe-unsafe-deserialization

Conversation

owen-mc
Copy link
Contributor

It was brought to our attention in#19664 that SnakeYaml is not vulnerable tojava/unsafe-deserialization as of version 2.0 (released in 2023). This is because all constructors now extendSafeConstructor. We already had an exclusion for constructors which extendSafeConstructor, so the query does not need to be updated. This PR updates the qhelp file.

@CopilotCopilotAI review requested due to automatic review settingsJuly 10, 2025 15:57
@owen-mcowen-mc requested a review froma team as acode ownerJuly 10, 2025 15:57
Copy link
Contributor

@CopilotCopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Pull Request Overview

This PR updates the documentation for thejava/unsafe-deserialization query to reflect that SnakeYAML is secure by default starting from version 2.0. The change clarifies that the previous vulnerability recommendations only apply to versions before 2.0, since all constructors now extendSafeConstructor in version 2.0 and later.

  • Updates SnakeYAML security status from "No" to "As of version 2.0"
  • Modifies the recommendation to specify it only applies to versions before 2.0

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedJul 10, 2025
edited
Loading

QHelp previews:

java/ql/src/Security/CWE/CWE-502/UnsafeDeserialization.qhelp

Deserialization of user-controlled data

Deserializing untrusted data using any deserialization framework that allows the construction of arbitrary serializable objects is easily exploitable and in many cases allows an attacker to execute arbitrary code. Even before a deserialized object is returned to the caller of a deserialization method a lot of code may have been executed, including static initializers, constructors, and finalizers. Automatic deserialization of fields means that an attacker may craft a nested combination of objects on which the executed initialization code may have unforeseen effects, such as the execution of arbitrary code.

There are many different serialization frameworks. This query currently supports Kryo, XmlDecoder, XStream, SnakeYaml, JYaml, JsonIO, YAMLBeans, HessianBurlap, Castor, Burlap, Jackson, Jabsorb, Jodd JSON, Flexjson, Gson, JMS, and Java IO serialization throughObjectInputStream/ObjectOutputStream.

Recommendation

Avoid deserialization of untrusted data if at all possible. If the architecture permits it then use other formats instead of serialized objects, for example JSON or XML. However, these formats should not be deserialized into complex objects because this provides further opportunities for attack. For example, XML-based deserialization attacks are possible through libraries such as XStream and XmlDecoder.

Alternatively, a tightly controlled whitelist can limit the vulnerability of code, but be aware of the existence of so-called Bypass Gadgets, which can circumvent such protection measures.

Recommendations specific to particular frameworks supported by this query:

FastJson -com.alibaba:fastjson

  • Secure by Default: Partially
  • Recommendation: Callcom.alibaba.fastjson.parser.ParserConfig#setSafeMode with the argumenttrue before deserializing untrusted data.

FasterXML -com.fasterxml.jackson.core:jackson-databind

  • Secure by Default: Yes
  • Recommendation: Don't callcom.fasterxml.jackson.databind.ObjectMapper#enableDefaultTyping and don't annotate any object fields withcom.fasterxml.jackson.annotation.JsonTypeInfo passing either theCLASS orMINIMAL_CLASS values to the annotation. Readthis guide.

Kryo -com.esotericsoftware:kryo andcom.esotericsoftware:kryo5

  • Secure by Default: Yes forcom.esotericsoftware:kryo5 and forcom.esotericsoftware:kryo >= v5.0.0
  • Recommendation: Don't callcom.esotericsoftware.kryo(5).Kryo#setRegistrationRequired with the argumentfalse on anyKryo instance that may deserialize untrusted data.

ObjectInputStream -Java Standard Library

  • Secure by Default: No
  • Recommendation: Use a validating input stream, such asorg.apache.commons.io.serialization.ValidatingObjectInputStream.

SnakeYAML -org.yaml:snakeyaml

  • Secure by Default: As of version 2.0.
  • Recommendation: For versions before 2.0, pass an instance oforg.yaml.snakeyaml.constructor.SafeConstructor toorg.yaml.snakeyaml.Yaml's constructor before using it to deserialize untrusted data.

XML Decoder -Standard Java Library

  • Secure by Default: No
  • Recommendation: Do not use with untrusted user input.

ObjectMesssage -Java EE/Jakarta EE

  • Secure by Default: Depends on the JMS implementation.
  • Recommendation: Do not use with untrusted user input.

Example

The following example callsreadObject directly on anObjectInputStream that is constructed from untrusted data, and is therefore inherently unsafe.

publicMyObject {publicintfield;MyObject(intfield) {this.field =field;  }}publicMyObjectdeserialize(Socketsock) {try(ObjectInputStreamin =newObjectInputStream(sock.getInputStream())) {return (MyObject)in.readObject();// BAD: in is from untrusted source  }}

Rewriting the communication protocol to only rely on reading primitive types from the input stream removes the vulnerability.

publicMyObjectdeserialize(Socketsock) {try(DataInputStreamin =newDataInputStream(sock.getInputStream())) {returnnewMyObject(in.readInt());// GOOD: read only an int  }}

References

@JLLeitschuh
Copy link
Contributor

@owen-mc
Copy link
ContributorAuthor

Good question. The link still seems to work, and to link to SnakeYaml documentation. But it seems the documentation hasn't been updated for the new behaviour since version 2.0. E.g. it still says:

Note: if you want to limit objects to standard Java objects like List or Long you needto use SafeConstructor.

There is a note at the top of the file that "This documentation is very brief and incomplete. Feel free to fix or improve it." The options are to delete the link because it is confusing, or add a note like "(not updated for new behaviour in version 2.0)". I'm leaning towards the latter. What do you think,@JLLeitschuh ?

@JLLeitschuh
Copy link
Contributor

The latter sounds reasonable

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

Copilot code reviewCopilotCopilot left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@owen-mc@JLLeitschuh

[8]ページ先頭

©2009-2025 Movatter.jp