-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add codemodder framework and replace 1 codemod #24
Changes from 8 commits
77dce55
84f32b9
cf34774
af366db
2ebe359
197bd85
1735091
d2d0176
d5740b3
aed1dbc
26c4830
bbb1edc
4dc300b
fb856d5
7390cb4
51c5fc4
aa13a24
ecf7bfa
dddcb2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@Suppress("DSL_SCOPE_VIOLATION") // https://github.com/gradle/gradle/issues/22797 | ||
plugins { | ||
id("io.openpixee.codetl.base") | ||
id("io.openpixee.codetl.java-library") | ||
id("io.openpixee.codetl.maven-publish") | ||
id("application") | ||
alias(libs.plugins.fileversioning) | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(11)) | ||
} | ||
} | ||
|
||
spotless { | ||
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. This isn't doing anything 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. So I can delete all the 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. right, the formatting is being applied by the plugins at the top of this build script. |
||
java { | ||
// do we need anything here? | ||
nahsra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
publishing { | ||
publications { | ||
register<MavenPublication>("maven") { | ||
from(components["java"]) | ||
artifactId = "codemodder-common" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly(libs.jetbrains.annotations) | ||
implementation(libs.guice) | ||
implementation(libs.contrast.sarif) | ||
implementation(libs.java.security.toolkit) | ||
implementation(libs.slf4j.api) | ||
|
||
testImplementation(testlibs.bundles.junit.jupiter) | ||
testImplementation(testlibs.bundles.hamcrest) | ||
testImplementation(testlibs.assertj) | ||
testImplementation(testlibs.jgit) | ||
testImplementation(testlibs.mockito) | ||
testRuntimeOnly(testlibs.junit.jupiter.engine) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.openpixee.java; | ||
package io.codemodder; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.openpixee.java; | ||
package io.codemodder; | ||
|
||
import java.util.Objects; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package io.openpixee.java; | ||
package io.codemodder; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
@@ -16,9 +15,6 @@ public interface FileWeavingContext { | |
|
||
boolean madeWeaves(); | ||
|
||
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. Took out this tight coupling to JavaParser as this class is intended to be used generally. |
||
/** Intended to be used when dealing with AST objects. */ | ||
boolean isLineIncluded(Node n); | ||
|
||
/** Intended to be used in non-JavaParser situations. */ | ||
boolean isLineIncluded(int line); | ||
|
||
|
@@ -47,14 +43,6 @@ public boolean madeWeaves() { | |
return !weaves.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean isLineIncluded(final Node n) { | ||
if (n.getRange().isEmpty()) { | ||
return true; | ||
} | ||
return includesExcludes.matches(n.getRange().get().begin.line); | ||
} | ||
|
||
@Override | ||
public boolean isLineIncluded(final int line) { | ||
return includesExcludes.matches(line); | ||
|
@@ -65,4 +53,8 @@ static FileWeavingContext createDefault( | |
final File file, final IncludesExcludes includesExcludes) { | ||
return new Default(includesExcludes.getIncludesExcludesForFile(file)); | ||
} | ||
|
||
static FileWeavingContext createDefault(final LineIncludesExcludes includesExcludesForFile) { | ||
return new Default(includesExcludesForFile); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.openpixee.java; | ||
package io.codemodder; | ||
|
||
import java.util.Collections; | ||
import java.util.Objects; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.openpixee.java; | ||
package io.codemodder; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
@Suppress("DSL_SCOPE_VIOLATION") // https://github.com/gradle/gradle/issues/22797 | ||
plugins { | ||
id("io.openpixee.codetl.base") | ||
id("io.openpixee.codetl.java-library") | ||
nahsra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
id("io.openpixee.codetl.maven-publish") | ||
id("application") | ||
alias(libs.plugins.fileversioning) | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(11)) | ||
} | ||
} | ||
|
||
application { | ||
mainClass.set("io.codemodder.codemods.Runner") | ||
} | ||
|
||
spotless { | ||
nahsra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
java { | ||
// do we need anything here? | ||
} | ||
} | ||
|
||
publishing { | ||
publications { | ||
register<MavenPublication>("maven") { | ||
from(components["java"]) | ||
artifactId = "codemodder-default-codemods" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(libs.javax.inject) | ||
implementation(libs.contrast.sarif) | ||
implementation(libs.slf4j.api) | ||
implementation(libs.javaparser.core) | ||
implementation(libs.javaparser.symbolsolver.core) | ||
implementation(libs.javaparser.symbolsolver.logic) | ||
implementation(libs.javaparser.symbolsolver.model) | ||
implementation(project(":languages:codemodder-common")) | ||
implementation(project(":languages:codemodder-framework-java")) | ||
implementation(project(":languages:codemodder-semgrep-provider")) | ||
|
||
testImplementation(testlibs.bundles.junit.jupiter) | ||
testImplementation(testlibs.bundles.hamcrest) | ||
testImplementation(testlibs.assertj) | ||
testImplementation(testlibs.mockito) | ||
|
||
testRuntimeOnly(testlibs.junit.jupiter.engine) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.codemodder.codemods; | ||
|
||
import static io.codemodder.CodemodInvoker.run; | ||
|
||
/** Invokes the codemod from a command line. */ | ||
public final class Runner { | ||
|
||
public static void main(final String[] args) { | ||
run(SecureRandomCodemod.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.codemodder.codemods; | ||
|
||
import com.contrastsecurity.sarif.Result; | ||
import com.contrastsecurity.sarif.SarifSchema210; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.visitor.ModifierVisitor; | ||
import io.codemodder.ChangeConstructorTypeVisitor; | ||
import io.codemodder.CodeDirectory; | ||
import io.codemodder.Codemod; | ||
import io.codemodder.FileWeavingContext; | ||
import io.codemodder.JavaParserChanger; | ||
import io.codemodder.ReviewGuidance; | ||
import io.codemodder.Sarif; | ||
import io.codemodder.providers.sarif.semgrep.SemgrepSarifProvider; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import javax.inject.Inject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Turns {@link java.util.Random} into {@link java.security.SecureRandom}. */ | ||
@Codemod( | ||
value = "pixee:java/secure-random", | ||
author = "[email protected]", | ||
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW) | ||
public final class SecureRandomCodemod implements JavaParserChanger { | ||
|
||
private final SarifSchema210 sarif; | ||
|
||
@Inject | ||
public SecureRandomCodemod( | ||
final CodeDirectory codeDirectory, final SemgrepSarifProvider sarifProvider) | ||
nahsra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throws IOException { | ||
this.sarif = sarifProvider.getSarif(codeDirectory.asPath(), "secure-random.semgrep"); | ||
} | ||
|
||
@Override | ||
public Optional<ModifierVisitor<FileWeavingContext>> createModifierVisitor( | ||
nahsra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final CodeDirectory codeDirectory, final Path path, final CompilationUnit cu) { | ||
List<Result> results = Sarif.getResultsForCompilationUnit(sarif, path); | ||
logger.trace("Found {} results in {} to change", results.size(), path); | ||
if (!results.isEmpty()) { | ||
return Optional.of( | ||
new ChangeConstructorTypeVisitor( | ||
Sarif.findRegions(results), | ||
"java.security.SecureRandom", | ||
"pixee:java/secure-random")); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(SecureRandomCodemod.class); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
rules: | ||
- id: secure-random | ||
nahsra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pattern: new Random() | ||
message: Insecure PRNG | ||
languages: | ||
- java | ||
severity: WARNING |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
@Suppress("DSL_SCOPE_VIOLATION") // https://github.com/gradle/gradle/issues/22797 | ||
plugins { | ||
id("io.openpixee.codetl.base") | ||
id("io.openpixee.codetl.java-library") | ||
id("io.openpixee.codetl.maven-publish") | ||
id("application") | ||
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.
|
||
alias(libs.plugins.fileversioning) | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(11)) | ||
} | ||
} | ||
|
||
spotless { | ||
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. This isn't doing anything |
||
java { | ||
// do we need anything here? | ||
} | ||
} | ||
|
||
publishing { | ||
publications { | ||
register<MavenPublication>("maven") { | ||
from(components["java"]) | ||
artifactId = "codemodder-framework-java" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly(libs.jetbrains.annotations) | ||
|
||
api("io.github.pixee:codetf-java:0.0.2") // TODO bring codetf-java into the monorepo | ||
|
||
implementation(libs.guice) | ||
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. if some of these libraries are part of codemodder-framework's public API, then they should use the |
||
implementation(libs.contrast.sarif) | ||
implementation(libs.javaparser.core) | ||
implementation(libs.javaparser.symbolsolver.core) | ||
implementation(libs.javaparser.symbolsolver.logic) | ||
implementation(libs.javaparser.symbolsolver.model) | ||
implementation(libs.logback.classic) | ||
implementation(libs.maven.model) | ||
implementation(libs.slf4j.api) | ||
implementation(project(":languages:codemodder-common")) | ||
|
||
testImplementation(testlibs.bundles.junit.jupiter) | ||
testImplementation(testlibs.bundles.hamcrest) | ||
testImplementation(testlibs.assertj) | ||
testImplementation(testlibs.mockito) | ||
|
||
testRuntimeOnly(testlibs.junit.jupiter.engine) | ||
} |
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.
It's unusual to have the
application
andjava-library
plugins in a project. Do we need theapplication
plugin? I understand this is a library.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.
I don't understand these so I just copied them all from the root to every new project. 😓
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.
io.openpixee.codetl.base
is our conventions on top of Gradle's base plugin. It defines things constant to all buildscripts like buildscript formatting. It's usually implied by other plugins e.g.java-library
: I can't recall if that's the case here.io.openpixee.codetl.java-library
is our conventions on top of Gradle's java library plugin. It's used to define a library used by other Java projects.application
is Gradle's application plugin. We don't have conventions for this one, I don't think we use it I can't remember.io.openpixee.codetl.maven-publish
is our conventions on top of Gradle's maven publish plugin. It's for publishing artifacts to a Maven repository. We only include it if we need to publish the artifact to a Maven repository.