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

Maven Shade support #5967

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions java/ql/src/semmle/code/java/frameworks/SnakeYaml.qll
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import java
import semmle.code.java.dataflow.DataFlow
import semmle.code.java.dataflow.DataFlow2
import semmle.code.java.dataflow.DataFlow3
import semmle.code.xml.MavenPom

/**
* The class `org.yaml.snakeyaml.constructor.SafeConstructor`.
*/
class SnakeYamlSafeConstructor extends RefType {
SnakeYamlSafeConstructor() {
this.hasQualifiedName("org.yaml.snakeyaml.constructor", "SafeConstructor")
this.hasQualifiedName(getAShadedPackage("org.yaml.snakeyaml.constructor"), "SafeConstructor")
}
}

Expand All @@ -27,7 +28,10 @@ class SafeSnakeYamlConstruction extends ClassInstanceExpr {
* The class `org.yaml.snakeyaml.Yaml`.
*/
class Yaml extends RefType {
Yaml() { this.getASupertype*().hasQualifiedName("org.yaml.snakeyaml", "Yaml") }
Yaml() {
this.getASupertype*()
.hasQualifiedName(getAShadedPackage("org.yaml.snakeyaml.constructor"), "Yaml")
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this changed on purpose, shouldn't it be the following? (might require formatting)

Suggested change
.hasQualifiedName(getAShadedPackage("org.yaml.snakeyaml.constructor"), "Yaml")
.hasQualifiedName(getAShadedPackage("org.yaml.snakeyaml"), "Yaml")

}
}

private class SafeYamlConstructionFlowConfig extends DataFlow2::Configuration {
Expand Down
51 changes: 51 additions & 0 deletions java/ql/src/semmle/code/xml/MavenPom.qll
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,54 @@ class MavenRepoJar extends File {
else getVersion().matches(pom.getVersionString() + "%")
}
}

class MavenPlugin extends ProtoPom {
MavenPlugin() { this.hasName("plugin") }
}

class MavenShadePlugin extends MavenPlugin {
MavenShadePlugin() {
this.getGroup().getValue() = "org.apache.maven.plugins" and
Copy link
Contributor

Choose a reason for hiding this comment

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

The groupId is optional, see https://stackoverflow.com/a/65533111.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done

this.getArtifact().getValue() = "maven-shade-plugin"
}

MavenShadeRelocation getARelocation() { result.getPlugin() = this }
}

class MavenShadeRelocation extends XMLElement {
MavenShadePlugin plugin;

MavenShadeRelocation() {
this.getParent().(XMLElement).getParent().(XMLElement).getParent() = plugin and
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to add a comment explaining the hierarchy here, i.e. plugin -> configuration -> relocations -> relocation

It is also possible to specify the configuration per execution, as it was done in the example of the Maven Shade Plugin, see also the POM Reference.

this.hasName("relocation")
}

MavenShadePlugin getPlugin() { result = plugin }

string getPattern() {
exists(XMLElement el | el.getName() = "pattern" and el.getParent() = this |
result = el.getTextValue()
)
}

string getShadedPattern() {
exists(XMLElement el | el.getName() = "shadedPattern" and el.getParent() = this |
result = el.getTextValue()
)
}

predicate relocates(string fromPackage, string toPackage) {
this.getPattern() = fromPackage and this.getShadedPattern() = toPackage
}
}

bindingset[package]
string getAShadedPackage(string package) {
result = package
or
exists(string originalPackage, string shadedPackage, MavenShadeRelocation relocDirective |
relocDirective.relocates(originalPackage, shadedPackage)
|
result = package.regexpReplaceAll("^" + originalPackage, shadedPackage)
)
}