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 all 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
5 changes: 3 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,7 @@ 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"), "Yaml") }
}

private class SafeYamlConstructionFlowConfig extends DataFlow2::Configuration {
Expand Down
77 changes: 77 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,80 @@ class MavenRepoJar extends File {
else getVersion().matches(pom.getVersionString() + "%")
}
}

/**
* A `<plugin>` element in a Maven POM file.
*/
class MavenPlugin extends ProtoPom {
MavenPlugin() { this.hasName("plugin") }
}

/**
* A `maven-shade-plugin` plugin element in a Maven POM file.
*/
class MavenShadePlugin extends MavenPlugin {
MavenShadePlugin() {
(
this.getGroup().getValue() = "org.apache.maven.plugins" or
not exists(this.getGroup()) // org.apache.maven.plugins is the default
) and
this.getArtifact().getValue() = "maven-shade-plugin"
}

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

/**
* A `<relocation>` specification within a `maven-shade-plugin` configuration.
*/
class MavenShadeRelocation extends XMLElement {
MavenShadePlugin plugin;

MavenShadeRelocation() {
this =
[plugin, plugin.getAChild("executions").getAChild("execution")]
.getAChild("configuration")
.getAChild("relocations")
.getAChild("relocation")
}

/**
* Gets the `<plugin>` ancestor of this relocation.
*/
MavenShadePlugin getPlugin() { result = plugin }

/**
* Gets the pattern this relocation replaces (e.g. `org.foo`)
*/
string getPattern() { result = this.getAChild("pattern").getTextValue() }

/**
* Gets the shaded package that replaces `getPattern()` (e.g. `org.bar.shaded.org.foo`).
*/
string getShadedPattern() { result = this.getAChild("shadedPattern").getTextValue() }

/**
* Holds if this `<relocation>` specification relocates `fromPackage` to `toPackage`.
*/
predicate relocates(string fromPackage, string toPackage) {
this.getPattern() = fromPackage and this.getShadedPattern() = toPackage
}
}

/**
* Gets a package name that may be used to refer to `package` after shading, including `package`
* itself.
*
* For example, if `package` is `org.foo.sub` and we have a `<relocation>` specification relocating
* `org.foo` to `org.bar.shaded.org.foo` then the results will be `{"org.foo.sub", "org.bar.shaded.org.foo.sub"}`.
*/
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)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| org.codehaus.plexus.abc | org.shaded.plexus.abc |
| org.codehaus.plexus.def | org.shaded.plexus.def |
| org.codehaus.plexus.util | org.shaded.plexus.util |
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java
import semmle.code.xml.MavenPom

from string pkgFrom, string pkgTo, MavenShadeRelocation reloc
where reloc.relocates(pkgFrom, pkgTo)
select pkgFrom, pkgTo
49 changes: 49 additions & 0 deletions java/ql/test/library-tests/maven/shade-relocations/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>MYGROUP</groupId>
<artifactId>MYARTIFACT</artifactId>
<version>0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<relocations>
<relocation>
<pattern>org.codehaus.plexus.abc</pattern>
<shadedPattern>org.shaded.plexus.abc</shadedPattern>
</relocation>
<relocation>
<pattern>org.codehaus.plexus.def</pattern>
<shadedPattern>org.shaded.plexus.def</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.codehaus.plexus.util</pattern>
<shadedPattern>org.shaded.plexus.util</shadedPattern>
<excludes>
<exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
<exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
</excludes>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package somepackage;

public class Test { } // Nothing to see here; the real test is in pom.xml; this is just to avoid a warning that the extractor didn't find any Java code.